Skip to content

Instantly share code, notes, and snippets.

@abruzzi

abruzzi/exifr2geojson.rb

Last active Aug 29, 2015
Embed
What would you like to do?
places I have been in Xi'an
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
{ :type => "Feature",
:geometry => geometry,
:properties => {
:altitude => altitude
}
}
end
def extract_image_info(path)
image_types = "*.jpg"
info = []
Dir.glob("#{path}/#{image_types}") do |jpg|
begin
info << format_attachment_gps(jpg)
print "."
rescue Exception => e
print "E"
end
end
puts "\n"
{
:type => "FeatureCollection",
:features => info
}.to_json
end
info = extract_image_info(ARGV[0])
File.open('places-local.geojson', "w") do |f|
f.write(info)
end
require 'plist'
require 'json'
def extract_info(lon, lat, datetime)
geometry = {
:type => "Point",
:coordinates => [lon, lat]
}
{ :type => "Feature",
:geometry => geometry,
:properties => {
:datetime => datetime
}
}
end
def main
xmlfile = File.open("AlbumData.xml", "r")
iphoto = Plist::parse_xml(xmlfile.read)
masters = iphoto["Master Image List"]
info = []
masters.each_pair do |id, photo|
if photo['longitude'] && photo['latitude']
info << extract_info(photo['longitude'], photo['latitude'], photo['DateAsTimerIntervalGMT'])
print "."
else
print "E"
end
end
puts "\n"
{
:type => "FeatureCollection",
:features => info
}.to_json
end
File.open('places-local.geojson', "w") do |f|
info = main
f.write(info)
end
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@abruzzi

This comment has been minimized.

Copy link
Owner Author

@abruzzi abruzzi commented May 5, 2014

seems github can understand geojson, and it adds some markers automatically!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment