Reverse geocoding in Bash using GPS position from exiftool

I am writing a bash script that renames JPG files based on their EXIF ​​tags. My source files are named like this:

IMG_2110.JPG
IMG_2112.JPG
IMG_2113.JPG
IMG_2114.JPG

I need to rename them as follows:

2015-06-07_11-21-38_iPhone6Plus_USA-CA-Los_Angeles_IMG_2110.JPG
2015-06-07_11-22-41_iPhone6Plus_USA-CA-Los_Angeles_IMG_2112.JPG
2015-06-13_19-05-10_iPhone6Plus_Morocco-Fez_IMG_2113.JPG
2015-06-13_19-12-55_iPhone6Plus_Morocco-Fez_IMG_2114.JPG

My bash script uses exiftool to parse the EXIF ​​header and rename the files. For those files that do not contain the EXIF ​​creation date, I use the file modification time.

#!/bin/bash
IFS=$'\n'

for i in *.*; do
    MOD=`stat -f %Sm -t %Y-%m-%d_%H-%m-%S $i`
    model=$( exiftool -f -s3 -"Model" "${i}" )
    datetime=$( exiftool -f -s3 -"DateTimeOriginal" "${i}" )
    stamp=${datetime//:/-}"_"${model// /}
    echo ${stamp// /_}$i
done

I'm stuck in place. I need to identify the country and city using the GPS information from the EXIF ​​tag. exiftool provides a field called GPS Position. Of all the fields, this seems the most useful for locating.

GPS Position : 40 deg 44' 49.36" N, 73 deg 56' 28.18" W

Google provides a public API for geolocation, but this requires latitude / longitude coordinates in this format:

40.7470444°, -073.9411611°

API ( , ):

https://maps.googleapis.com/maps/api/geocode/json?latlng=40.7470444,-073.9411611

:

  • GPS /, , Google?

  • JSON, , , ? , ? Id , - . - , COUNTRY-City.

bash script. pygeocoder gpsbabel, , , . -, API (http://www.earthpoint.us/Convert.aspx).

+4
3

# 1 awk :

awk '/GPS Position/{
  lat=$4; lat+=strtonum($6)/60; lat+=strtonum($7)/3600; if($8!="N,")lat=-lat;
  lon=$9; lon+=strtonum($11)/60; lon+=strtonum($12)/3600; if($13!="E")lon=-lon;
  printf "%.7f %.7f\n",lat,lon
  }'
+2

, , .

, , , EXIFTool. :

exiftool -n -p '$GPSLatitude,$GPSLongitude' image_name.jpg

, Google, API .

.

+2

PHP, Marco, !

function get_gps($gps_pos) {

$parts = explode(" ",str_replace(array("deg ",",","'","\""),"",$gps_pos));

$lat_deg = $parts[0];
$lat_min = $parts[1];
$lat_sec = $parts[2];
$lat_dir = $parts[3];

$lon_deg = $parts[4];
$lon_min = $parts[5];
$lon_sec = $parts[6];
$lon_dir = $parts[7];

if ($lat_dir == "N") {
    $lat_sin = "+";
    } else {
    $lat_sin = "-";
    }

if ($lon_dir == "E") {
    $lon_sin = "+";
    } else {
    $lon_sin = "-";
    }

$latitiude = $lat_sin.($lat_deg+($lat_min/60)+($lat_sec/3600));
$longitude = $lon_sin.($lon_deg+($lon_min/60)+($lon_sec/3600));

return $latitiude.",".$longitude;

}
0

Source: https://habr.com/ru/post/1609702/


All Articles