Last active
August 29, 2015 14:01
-
-
Save abruzzi/44550f2b4cb812b19701 to your computer and use it in GitHub Desktop.
places I have been in Xi'an
This file contains 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 | |
{ :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 |
This file contains 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 '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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
seems github can understand geojson, and it adds some markers automatically!