I wanted to write a module or enhance the search module to allow me to find all photos taken in a country. However, the Google Maps API isn't consistent in providing a country name in the same place in its json response. I also thought it would be cool to grab the city name too, and that was even harder to extract.
Sure, I could send the address to some AI thing and ask it for the country and address, but that's beyond my coding abilities.
Luckily, I found a solution. Sending the latitude and longitude to
https://www.bigdatacloud.com, with its free Reverse Geocoding API, gives me exactly what I want. You need to register and get the API key (and add it as a variable in the database - or you can amend the admin routine within the exif_gps module).
I then customised my (already very heavily customised) exif_gps module and now have "address", "country" and "city" fields added to the exif_coordinates database table. I edited the module's task and grabbed that data for all of my geotagged photos and wrote the info to the database, so that it's not having to use the API each time the photo is looked at, in case in future the APIs die or start being charged for.
I also have "altitude" in the above table - a customisation I made a few years ago when I realised my iPhone (my only camera nowadays) is capturing that info and I wanted a record of the mountains/hills I was cycling around on a cycling holiday.
An edit made to exif_gps_task.php showing the APIs is as follows:
// Check each photo in the array to see if it already has exif gps data associated with it.
// If it doesn't, attempt to extract gps coordinates.
foreach (ORM::factory("item")
->where("id", ">", $last_id)
->where("type", "=", "photo")
->order_by("id")
->find_all(1000) as $item) {
$record = ORM::factory("exif_coordinate")->where("item_id", "=", $item->id)->find();
if (!$record->loaded()) {
exif_gps::extract($item);
}
// Add address (new feature) if empty but exif gps data exists
if ($record->latitude != "" && $record->longitude != "" && $record->address == "") {
if (module::get_var("exif_gps", "googlemap_api_key2", "") != "") {
.$record->latitude.",".$record->longitude
."&key=".module::get_var("exif_gps", "googlemap_api_key2"));
$address = json_decode($addressJSON,true);
$record->address = $address['results']['0']['formatted_address'];
$record->save();
}
}
// Add city and country if BigDataCloud API exists
if ($record->latitude != "" && $record->longitude != "" && $record->city == "" && $record->country == "") {
if (module::get_var("exif_gps", "BigDataCloud_api_key", "") != "") {
$addressJSON = file_get_contents(
"https://api-bdc.net/data/reverse-geocode?"
."latitude=".$record->latitude."&longitude=".$record->longitude
."&key=".module::get_var("exif_gps", "BigDataCloud_api_key"));
$address = json_decode($addressJSON,true);
$record->country = $address['countryName'];
$record->city = $address['city'];
$record->save();
}
}
$last_id = $item->id;
$completed++;