mirror of
https://github.com/Brandon-Rozek/website.git
synced 2025-05-24 12:26:43 +00:00
New posts
This commit is contained in:
parent
980ac04f51
commit
9db8d7b105
2 changed files with 150 additions and 0 deletions
111
content/blog/GPS-EXIF-data-from-photos-decimal-format.md
Normal file
111
content/blog/GPS-EXIF-data-from-photos-decimal-format.md
Normal file
|
@ -0,0 +1,111 @@
|
||||||
|
---
|
||||||
|
title: "Retreiving GPS data in decimal format from EXIF data in photos"
|
||||||
|
date: 2022-06-19T19:01:35-04:00
|
||||||
|
draft: false
|
||||||
|
tags: []
|
||||||
|
math: false
|
||||||
|
---
|
||||||
|
|
||||||
|
For a new feature that I'm cooking up for my website, I need to grab the GPS information from the EXIF data stored in my images. Luckily, `imagemagick`
|
||||||
|
comes to our rescue.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
identify -verbose $IMAGE_FILE | grep GPS
|
||||||
|
```
|
||||||
|
|
||||||
|
This will return something like the following:
|
||||||
|
```
|
||||||
|
exif:GPSAltitude: 1250/100
|
||||||
|
exif:GPSAltitudeRef: .
|
||||||
|
exif:GPSDateStamp: 2022:06:12
|
||||||
|
exif:GPSImgDirection: 137/1
|
||||||
|
exif:GPSImgDirectionRef: M
|
||||||
|
exif:GPSInfo: 1004
|
||||||
|
exif:GPSLatitude: 40/1, 50/1, 1815/100
|
||||||
|
exif:GPSLatitudeRef: N
|
||||||
|
exif:GPSLongitude: 73/1, 53/1, 3625/100
|
||||||
|
exif:GPSLongitudeRef: W
|
||||||
|
exif:GPSTimeStamp: 17/1, 32/1, 30/1
|
||||||
|
exif:GPSVersionID: ....
|
||||||
|
```
|
||||||
|
|
||||||
|
To request a specific field, for example Latitude:
|
||||||
|
```bash
|
||||||
|
identify -format "%[EXIF:GPSLatitude]\n" "$IMAGE_FILE"
|
||||||
|
```
|
||||||
|
|
||||||
|
As with the verbose flag, it will return the information in degrees format
|
||||||
|
```
|
||||||
|
40/1, 50/1, 1815/100
|
||||||
|
```
|
||||||
|
|
||||||
|
The following bash function will take the degrees format and convert
|
||||||
|
it to the more common decmial format:
|
||||||
|
```bash
|
||||||
|
DegreesToDecimal() {
|
||||||
|
L0=$(echo "$1" | cut -d "," -f 1)
|
||||||
|
L1=$(echo "$1" | cut -d "," -f 2)
|
||||||
|
L2=$(echo "$1" | cut -d "," -f 3)
|
||||||
|
echo "scale=6;$L0 + $L1/60 + $L2/3600" | bc
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
For example:
|
||||||
|
```bash
|
||||||
|
LAT=$(identify -format "%[EXIF:GPSLatitude]\n" "$IMAGE_FILE")
|
||||||
|
LAT_DEC=$(DegreesToDecimal "$LAT")
|
||||||
|
echo "$LAT_DEC"
|
||||||
|
```
|
||||||
|
will output:
|
||||||
|
```
|
||||||
|
40.838374
|
||||||
|
```
|
||||||
|
|
||||||
|
We can then package this into a script which will output the latitude, longitude, and altitude (m) of an image in decmial format.
|
||||||
|
```bash
|
||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
set -o errexit
|
||||||
|
set -o nounset
|
||||||
|
set -o pipefail
|
||||||
|
|
||||||
|
show_usage() {
|
||||||
|
echo "Usage: getLLA.sh [imagefile]"
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
|
||||||
|
# Check argument count
|
||||||
|
if [ "$#" -ne 1 ]; then
|
||||||
|
show_usage
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Check that relevant command exist
|
||||||
|
if ! command -v identify > /dev/null; then
|
||||||
|
echo "Command identify from imagemagick not found. Exiting..."
|
||||||
|
fi
|
||||||
|
|
||||||
|
IMAGE_FILE="$1"
|
||||||
|
LAT=$(identify -format "%[EXIF:GPSLatitude]\n" "$IMAGE_FILE")
|
||||||
|
LAT_DIR=$(identify -format "%[EXIF:GPSLatitudeRef]\n" "$IMAGE_FILE")
|
||||||
|
LON=$(identify -format "%[EXIF:GPSLongitude]\n" "$IMAGE_FILE")
|
||||||
|
LON_DIR=$(identify -format "%[EXIF:GPSLongitudeRef]\n" "$IMAGE_FILE")
|
||||||
|
ALT=$(identify -format "%[EXIF:GPSAltitude]\n" "$IMAGE_FILE")
|
||||||
|
|
||||||
|
DegreesToDecimal() {
|
||||||
|
L0=$(echo "$1" | cut -d "," -f 1)
|
||||||
|
L1=$(echo "$1" | cut -d "," -f 2)
|
||||||
|
L2=$(echo "$1" | cut -d "," -f 3)
|
||||||
|
echo "scale=6;$L0 + $L1/60 + $L2/3600" | bc
|
||||||
|
}
|
||||||
|
|
||||||
|
LAT_DEC=$(DegreesToDecimal "$LAT")
|
||||||
|
LON_DEC=$(DegreesToDecimal "$LON")
|
||||||
|
ALT_DEC=$(echo "scale=6;$ALT" | bc)
|
||||||
|
|
||||||
|
LAT_PREFIX=$([ $LAT_DIR == "S" ] && echo "-" || echo "")
|
||||||
|
LON_PREFIX=$([ $LON_DIR == "W" ] && echo "-" || echo "")
|
||||||
|
|
||||||
|
echo "$LAT_PREFIX$LAT_DEC"
|
||||||
|
echo "$LON_PREFIX$LON_DEC"
|
||||||
|
echo "$ALT_DEC"
|
||||||
|
```
|
39
content/blog/conditional-assignment-bash.md
Normal file
39
content/blog/conditional-assignment-bash.md
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
---
|
||||||
|
title: "Conditional Assignment in Bash"
|
||||||
|
date: 2022-06-19T18:49:47-04:00
|
||||||
|
draft: false
|
||||||
|
tags: ["Bash"]
|
||||||
|
math: false
|
||||||
|
---
|
||||||
|
|
||||||
|
Many programming languages include an quick way to perform a
|
||||||
|
conditional assignment. That is, assigning a variable with a value
|
||||||
|
based on some condition. Normally this is done through a ternary
|
||||||
|
operator. For example, here is how to write it in Javascript
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
age = 16;
|
||||||
|
ageType = (age > 18) "Adult": "Child";
|
||||||
|
```
|
||||||
|
The variable `ageType` is dependent upon the value of `age`. If it is above 18 then `ageType = "Adult"` otherwise `ageType = "Child"`.
|
||||||
|
|
||||||
|
A more verbose way of accomplishing the same thing is the following:
|
||||||
|
```javascript
|
||||||
|
if (age > 18) {
|
||||||
|
ageType = "Adult"
|
||||||
|
} else {
|
||||||
|
ageType = "Child"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
How do we do conditional assignment in Bash? One way is to make use of subshells and echoing out the values.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
AGE_TYPE=$([ $AGE -gt 18 ] && echo "Adult" || echo "Child")
|
||||||
|
```
|
||||||
|
A common programming feature called *short-circuiting* makes it
|
||||||
|
so that if the first condition (`[ $AGE -gt 18 ]`) is false, then it
|
||||||
|
will skip the right side of the AND (`&&`) expression. This is because
|
||||||
|
`False && True` is always `False`. However, `False || True` is equal
|
||||||
|
to `True`, so the language needs to evaluate the right part of an
|
||||||
|
OR (`||`) expression.
|
Loading…
Add table
Reference in a new issue