Skip to content

Extracting Latitude Longitude from Images

Thomas Schranz edited this page Jul 26, 2017 · 10 revisions

Extracting Latitude/Longitude from Images

Nowadays images from photo cameras including those smart phones often come with a lot of useful metadata.

Fortunately there already exists an extensive and widely used Java library called metadata-extractor and a Clojure wrapper for it called exif-processor that we can use to extract this information.

Add [io.joshmiller/exif-processor "0.2.0"] to your :dependencies in project.clj.

(ns exifdemo.core
  (:require [haversine.core      :as haversine]
            [exif-processor.core :as exif]
            [clojure.edn         :as edn]))

; a regular expression pattern that we can use to extract numbers from a string
(def numbers-pattern #"[0-9]+[.]?[0-9]*")

; turns coordinate information represented as string "50° 49' 8.59" into numbers for degrees, minutes and seconds (50 49 8.59)
(defn parse-coordinate [s]
  (map edn/read-string (re-seq numbers-pattern s)))

; here we access the metadata of the image file, fetch its latitude and longitude and parse the coordinates
; you can use exif/exif-for-url to fetch an image from a URL instead of from the file system
(let [metadata (exif/exif-for-filename "/Users/tosh/Downloads/harbor-gps.jpg")
      latitude  (parse-coordinate (get metadata "GPS Latitude"))
      longitude (parse-coordinate (get metadata "GPS Longitude"))]
  (def latlng [latitude longitude]))

; helper function to convert degrees, minutes and seconds to decimal coordinates
(defn dms-to-decimal [degrees minutes seconds]
  (+ degrees
    (/ minutes 60)
    (/ seconds 3600)))

{:latitude  (apply dms-to-decimal (first  latlng))
 :longitude (apply dms-to-decimal (second latlng))}

Further reading

https://en.wikipedia.org/wiki/Exif https://github.com/drewnoakes/metadata-extractor-images/wiki (sample images) https://github.com/drewnoakes/metadata-extractor/wiki/SampleOutput (sample output)

Clone this wiki locally