Show street address for geotagged photos

26 views
Skip to first unread message

Adrian London

unread,
Mar 10, 2024, 4:57:16 PM3/10/24
to Gallery 3 Users
Do you use the exif_gps module?  Would you like the street address to show underneath the map in the sidebar?  've had a play and found a way.

Earlier today a thought hit me - I want to get the city from each geotagged photo and use that for searching capabilities.  I've not done that yet, but I now know how to get the data I need.  Anyway ...

Add these lines to views/exif_gps_dynamic_sidebar.html.php at the end, right before the final </div>:

  <? /* Lookup address of the photo if API key is set */
    if ($item->is_photo() && module::get_var("exif_gps", "googlemap_api_key", "") != "") {
    $addressJSON = file_get_contents(
"https://maps.googleapis.com/maps/api/geocode/json?latlng="
.$latitude.",".$longitude
."&key=".module::get_var("exif_gps", "googlemap_api_key"));
    $address = json_decode($addressJSON,true);
    echo "<br><b>Address: </b>" . $address['results']['0']['formatted_address'];
    }
  ?>




J.R.

unread,
Mar 10, 2024, 7:14:25 PM3/10/24
to gallery...@googlegroups.com
Adrian.

That's quite a trick. None of my current Gallery installations have any need for geolocation tagging info, but it does sound cool.

-- J.R.
--
WHEN USING AN EMAIL PROGRAM to reply to this message, click REPLY TO LIST or REPLY TO ALL so your reply goes out to everyone in the group. If you click REPLY or REPLY TO SENDER Google will *only* send your reply to the original author (not recommended).
 
To post a NEW MESSAGE to the group, send an new email to:
gallery...@googlegroups.com
 
To view or sign in to this group on the web, use this URL:
https://groups.google.com/forum/#!forum/gallery-3-users
---
You received this message because you are subscribed to the Google Groups "Gallery 3 Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gallery-3-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gallery-3-users/fe6b630a-ff25-453b-9a85-529054d0e255n%40googlegroups.com.

Adrian London

unread,
Mar 14, 2024, 6:06:41 AM3/14/24
to Gallery 3 Users
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", "") != "") {

          $addressJSON = file_get_contents(
  "https://maps.googleapis.com/maps/api/geocode/json?latlng="
  .$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++;



Reply all
Reply to author
Forward
0 new messages