Last active
November 8, 2016 22:18
-
-
Save natebeaty/cd9d7c2fd1fea2c92679323b1191dfcd to your computer and use it in GitHub Desktop.
quick ruby script to convert 2 CSV files of community areas + community lat/lng boundary paths
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
#!/usr/bin/ruby | |
require 'CSV' | |
output = '' | |
# get areas | |
areas = {} | |
CSV.foreach("community-areas.csv", headers:true) do |row| | |
areas[row['shapeid']] = row['community'] | |
end | |
# output google maps-friendly json | |
shapeAt = -1; | |
CSV.foreach("community-paths.csv", headers:true) do |row| | |
if row['shapeid'] != shapeAt | |
if shapeAt != -1 | |
output = output.chomp(',') | |
output << "]\n},\n" | |
end | |
output << "{\n" | |
output << "\"name\": \"#{areas[row['shapeid']]}\",\n" | |
output << "\"url\": \"/Map/Chicago&community=#{areas[row['shapeid']]}\",\n" | |
output << "\"path\": [" | |
shapeAt = row['shapeid'] | |
end | |
output << "{ \"lng\": #{row['X']}, \"lat\": #{row['Y']} }," | |
end | |
output << "]\n}\n" | |
File.open("communities.json", 'w') {|f| f.write(output) } | |
print 'done!' | |
# community-areas.csv: | |
# shapeid,area_numbe,comarea_,shape_area,perimeter,community,comarea_id,shape_len,area_num_1,area,status | |
# 0,35,0,46004621.16,0,DOUGLAS,0,31027.05451,35,0,Current | |
# 1,36,0,16913961.04,0,OAKLAND,0,19565.50615,36,0,Current | |
# 2,37,0,19916704.87,0,FULLER PARK,0,25339.08975,37,0,Current | |
# 3,38,0,48492503.16,0,GRAND BOULEVARD,0,28196.83716,38,0,Current | |
# 4,39,0,29071741.93,0,KENWOOD,0,23325.16791,39,0,Current | |
# 5,4,0,71352328.24,0,LINCOLN SQUARE,0,36624.60308,4,0,Not being mapped | |
# 6,40,0,42373881.48,0,WASHINGTON PARK,0,28175.31609,40,0,Current | |
# 7,41,0,45105380.17,0,HYDE PARK,0,29746.7082,41,0,Current | |
# 8,42,0,57815179.51,0,WOODLAWN,0,46936.95924,42,0,Current | |
# 9,1,0,51259902.45,0,ROGERS PARK,0,34052.39758,1,0,Not being mapped | |
# 10,11,0,64868161.68,0,JEFFERSON PARK,0,44011.95717,11,0,Not being mapped | |
# ... | |
# community-paths.csv: | |
# shapeid,X,Y,Path,Type,Address,City,State,ZIP Code,Name | |
# 0,-87.60914088,41.8446925,1,Polygon,,,,, | |
# 0,-87.60914875,41.8446616,2,Polygon,,,,, | |
# 0,-87.60916112,41.84458961,3,Polygon,,,,, | |
# 0,-87.60916766,41.84451718,4,Polygon,,,,, | |
# 0,-87.60916861,41.84445626,5,Polygon,,,,, | |
# 0,-87.60915012,41.84423872,6,Polygon,,,,, | |
# 0,-87.60907241,41.84419474,7,Polygon,,,,, | |
# 0,-87.60900627,41.84410647,8,Polygon,,,,, | |
# 0,-87.60896502,41.84404346,9,Polygon,,,,, | |
# 0,-87.60891566,41.84395529,10,Polygon,,,,, | |
# 0,-87.6088998,41.84387362,11,Polygon,,,,, | |
# 0,-87.60886701,41.84380438,12,Polygon,,,,, | |
# 0,-87.60885143,41.84369761,13,Polygon,,,,, | |
# ... | |
# desired output: | |
# { | |
# "name": "Neighborhood 1", | |
# "url": "#", | |
# "path": [ | |
# {"lng": -87.60914088, "lat": 41.8446925}, | |
# ... | |
# ] | |
# } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment