Skip to content

Instantly share code, notes, and snippets.

@max-mapper
Created June 20, 2011 21:06
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save max-mapper/1036586 to your computer and use it in GitHub Desktop.
Save max-mapper/1036586 to your computer and use it in GitHub Desktop.
grab exif data from jpgs and emit geojson coordinates using ruby
require 'json'
require 'base64'
require 'exifr'
def to_decimal(dms)
dms[0].to_f + dms[1].to_f / 60 + dms[2].to_f / 3600
end
def to_geojson(exif)
lat_exif = exif.gps_latitude
lon_exif = exif.gps_longitude
return "" unless lon_exif && lat_exif
lon = to_decimal(lon_exif.map(&:to_f))
lon = -lon if exif.gps_longitude_ref == "W"
lat = to_decimal(lat_exif.map(&:to_f))
lat = -lat if exif.gps_latitude_ref == "S"
{
:type => "Point",
:coordinates => [lon, lat]
}
end
def format_attachment_gps(file)
geometry, altitude = nil, nil
body = File.read file
r, w = IO.pipe
w.write_nonblock(body)
exif = EXIFR::JPEG.new(r)
geometry = to_geojson(exif)
altitude = exif.gps_altitude.to_f
altitude = -altitude if exif.gps_altitude_ref.to_i < 0
{geometry: geometry, altitude: altitude}
end
Dir.glob("*.jpg").each {|file| p format_attachment_gps(file)}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment