Created
November 8, 2016 19:50
-
-
Save natebeaty/c36ba7438a5c3d8771a29683028b703e to your computer and use it in GitHub Desktop.
ruby script to convert json output from mashape/boundaries-io to google maps friendly json
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
#!/usr/bin/ruby | |
require 'json' | |
output = '' | |
file = File.read('niagarazips.json') | |
city = 'NiagaraFalls' | |
# file = File.read('nczips.json') | |
# city = 'NashEdgecombe' | |
# file = File.read('nyzips.json') | |
# city = 'NewYork' | |
zipdata = JSON.parse(file) | |
zipdata['features'].each do |feature| | |
output << "{\n" | |
output << "\"name\": \"#{feature['properties']['zipCode']}\",\n" | |
output << "\"url\": \"/Map/#{city}&community=#{feature['properties']['zipCode']}\",\n" | |
output << "\"path\": [" | |
# print "#{feature['geometry']['coordinates'][0].count}\n" | |
if feature['geometry']['coordinates'][0].count > 1 | |
feature['geometry']['coordinates'][0].each do |coords| | |
output << "{ \"lng\": #{coords[0]}, \"lat\": #{coords[1]} }," | |
end | |
else | |
feature['geometry']['coordinates'][0][0].each do |coords| | |
output << "{ \"lng\": #{coords[0]}, \"lat\": #{coords[1]} }," | |
end | |
end | |
output = output.chomp(",") | |
output << "]\n},\n" | |
end | |
print output.chomp(",\n") | |
# niagara.json: | |
# {"type":"FeatureCollection", | |
# "features":[{"type":"Feature", | |
# "properties":{"zipCode":"14301", | |
# "city":"Niagara falls", | |
# "county":"Niagara", | |
# "state":"NY"}, | |
# "geometry":{"type":"MultiPolygon", | |
# "coordinates":[[[[-79.013848,43.097141], | |
# [-79.014109,43.094514], | |
# [-79.012735,43.094266], | |
# [-79.01012,43.095689], | |
# output: | |
# { | |
# "name": "11715", | |
# "url": "/Map/NiagaraFalls&community=11715", | |
# "path": [{ "lng": -73.045439, "lat": 40.76267 },{ "lng": -73.043262, "lat": 40.757258 },{ "lng": -73.042097, "lat": 40.757259 },{ "lng": -73.042052, "lat": 40.749109 },{ "lng": -73.044883, "lat": 40.747724 },{ "lng": -73.044764, "lat": 40.746905 },{ "lng": -73.042965, "lat": 40.747026 },{ "lng": -73.042077, "lat": 40.747159 },{ "lng": -73.041859, "lat": 40.735888 },{ "lng": -73.03941, "lat": 40.734114 },{ "lng": -73.038974, "lat": 40.7337 },{ "lng": -73.038052, "lat": 40.729618 },{ "lng": -73.034977, "lat": 40.729412 },{ "lng": -73.033588, "lat": 40.734881 },{ "lng": -73.033283, "lat": 40.737681 },{ "lng": -73.031727, "lat": 40.738768 },{ "lng": -73.027409, "lat": 40.743812 },{ "lng": -73.027295, "lat": 40.745124 },{ "lng": -73.023737, "lat": 40.747045 },{ "lng": -73.021948, "lat": 40.747528 },{ "lng": -73.022324, "lat": 40.749102 },{ "lng": -73.021948, "lat": 40.748405 },{ "lng": -73.021694, "lat": 40.748881 },{ "lng": -73.024478, "lat": 40.752066 },{ "lng": -73.024037, "lat": 40.753688 },{ "lng": -73.024959, "lat": 40.758578 },{ "lng": -73.028787, "lat": 40.756208 },{ "lng": -73.030043, "lat": 40.758798 },{ "lng": -73.031184, "lat": 40.758563 },{ "lng": -73.035137, "lat": 40.766112 },{ "lng": -73.037151, "lat": 40.767138 },{ "lng": -73.040752, "lat": 40.767604 },{ "lng": -73.042164, "lat": 40.768546 },{ "lng": -73.042161, "lat": 40.767034 },{ "lng": -73.043304, "lat": 40.765948 },{ "lng": -73.042427, "lat": 40.762683 },{ "lng": -73.045439, "lat": 40.76267 }] | |
# }, | |
# { | |
# "name": "12866", | |
# "url": "/Map/NiagaraFalls&community=12866", | |
# "path": [{ "lng": -73.751375, "lat": 42.986077 },{ "lng": -73.752142, "lat": 42.98472 },{ "lng": -73.751105, "lat": 42.984607 },{ "lng": -73.749647, "lat": 42.984562 },{ "lng": -73.749626, "lat": 42.984893 },{ "lng": -73.749364, "lat": 42.985685 },{ "lng": -73.749517, "lat": 42.985997 },{ "lng": -73.75061, "lat": 42.986024 },{ "lng": -73.751375, "lat": 42.986077 }] | |
# }, | |
# ... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment