Created
June 20, 2011 21:06
-
-
Save max-mapper/1036586 to your computer and use it in GitHub Desktop.
grab exif data from jpgs and emit geojson coordinates using ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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