Go program finds photos with nearby GPS coordinates

Two Libraries for One, Hallelujah!

On GitHub, several libraries for reading Exif metadata from JPG photos vie for the favor of Go programmers. The most handy package I found was goexif2, which doesn't freak programmers out with thousands of options, but uses Decode() to analyze an image and returns the longitude and latitude as floating points with LatLong(). The project by GitHub user xor-gate is no longer under development according to the readme, but it still worked perfectly for the given task.

If you are looking for enhancements, you will find about a dozen forks, but deciding on a specific one is like tossing a coin. I found the original is good enough, and it can be installed in the usual style for Go:

go get github.com/xor-gate/goexif2/exif

Following which, programs can utilize its functions (e.g., by lines 5 and 83 in Listing 1).

The GeoPos() function from line 76 expects an image path as a string and returns two floats and an error value to the caller. First it opens the image file for reading, interprets the JPEG data with Decode(), then uses LatLong() to read the GPS Latitude and Longitude Exif tags, analyzes their angular minutes and seconds, and converts them into floating-point values.

golang-geo was selected as the library featuring geometry functions for determining the distance between two locations. Line 4 adds it to the program under the geo nick; the installation again needs to be completed up front – as shown above – with go get.

Distance on a Great Circle

The programming logic as of line 63 determines the distance between the currently examined image and the reference shot. Using longitude and latitude pairs, it calls NewPoint() to fill a Go structure for golang-geo and then calls the GreatCircleDistance() function with the start point p1 and the end point p2. The distance between the two points in kilometers is returned as a floating-point number. If it is above the maximum distance of 100 meters (0.1 kilometers, or about 100 yards) specified in line 12, the return statement in line 68 ignores the image; otherwise, lines 71 and 72 output the name of the photo file and its offset to the reference image.

While searching for photos of the now closed restaurant mentioned at the beginning, the algorithm actually discovered something interesting. I had passed in a reference photo of the restaurant's menu, shot recently (Figure 3), as a salt. The search came back with a photo of a wooden table, showing empty beer and wine glasses, no doubt at the same restaurant, after the waiter had brought the bill in a clean water glass, as was the custom at this place (Figure 4). Eureka, what a historic moment!

Figure 3: The reference photo for the image search shows the menu of what was once Mike Schilli's favorite restaurant.
Figure 4: Found: Another snapshot, taken after receiving the check in the same restaurant on another day.

Big AI Brother

Imagine the possibilities if the GPS data of a photo collection are available. An AI program using the nearest neighbor method could, for example, form clusters of the snapshot locations – as already explained in a previous issue [5] – and thus determine the most often frequented locations of the mobile phone owner. If you think about it, it's downright scary! But social media companies, as you know, live off it to keep the lights on.


  1. Mitnick, Kevin. The Art of Invisibility. Little, Brown and Company, 2017: https://www.amazon.com/Art-Invisibility-Worlds-Teaches-Brother/dp/0316380504
  2. Orthodrome: https://en.wikipedia.org/wiki/Great-circle_distance
  3. golang-geo, a library for calculating distances between geographical points: https://www.github.com/kellydunn/golang-geo
  4. Listings for this article: ftp://ftp.linux-magazine.com/pub/listings/linux-magazine.com/225/
  5. "Calculating Clusters with AI Methods" by Mike Schilli, Linux Magazine, issue 145, December 2012, p. 62

The Author

Mike Schilli works as a software engineer in the San Francisco Bay area, California. Each month in his column, which has been running since 1997, he researches practical applications of various programming languages. If you email him at mailto:mschilli@perlmeister.com he will gladly answer any questions.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Perl: Elasticsearch

    The Elasticsearch full-text search engine quickly finds expressions even in huge text collections. With a few tricks, you can even locate photos that have been shot in the vicinity of a reference image.

  • Treasure Hunt

    A geolocation guessing game based on the popular Wordle evaluates a player's guesses based on the distance from and direction to the target location. Mike Schilli turns this concept into a desktop game in Go using the photos from his private collection.

  • Obfuscation Filter

    Mike Schilli loves his privacy. That's why he's created a Go program that adds a geo-obfuscation layer to cellphone photos before they are published on online platforms to prevent inquisitive minds from inferring the location.

  • Cave Painter

    While searching for a method to draw geodata right into the terminal, Mike Schilli discovers the wondrous world of map projections.

  • Perl: Plotting GPS Data

    Perl hackers take to the hills with a navigation system that provides a graphical rendering of a hiking tour.

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More