Created
February 12, 2014 01:59
-
-
Save szeitlin/8948575 to your computer and use it in GitHub Desktop.
example from Lynn Root's workshop.
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
import geojson | |
import parse as p #shorter name for our parse program | |
def create_map(data_file): | |
"""csv file -> parse -> creates a geojson - javascript object notation - file """ | |
# define the type of geoJSON that we're creating | |
geo_map = {"type": "FeatureCollection"} #GeoJSON specification | |
# define an empty list to collect each latitude and longitude point | |
item_list = [] | |
# iterate over our data to create the geoJSON document (list of points_) | |
# index is row number, line is row | |
# using built-in function enumerate gives us those (index, line) for each row | |
for index, line in enumerate(data_file): | |
if line['X'] == "0" or line ['Y'] == "0": | |
continue #skip those blank values because it will think they're the equator | |
data = {} | |
data['type'] = 'Feature' #this is a geoJSON thing | |
data['id'] = index #row number | |
data['properties'] = {'title': line['Category'], 'description':line['Descript'], | |
'data':line['Date'], 'location':line['Location']} | |
data['geometry'] = {'type': 'Point', 'coordinates': (line["X"], line["Y"])} #geoJSON option for plotting | |
item_list.append(data) | |
#note that value of the properties key is another dict | |
#same for geometry | |
#coordinates are set to a tuple | |
# iterate over each point and add it to the geoJSON object | |
for point in item_list: | |
geo_map.setdefault('features', []).append(point) | |
#setdefault gives features all the points in the form of filling that empty list | |
# write to a new GeoJSON file | |
with open("file_sf.geojson", "w") as f: | |
f.write(geojson.dumps(geo_map)) | |
#with will automatically close the file for you when it's done | |
#w is for writing to a new file, wb is for appending to an existing file | |
def main(): | |
data = p.parse(p.MY_FILE, ",") | |
return create_map(data) | |
if __name__== "__main__": | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment