-
Notifications
You must be signed in to change notification settings - Fork 391
/
Copy pathmap.py
78 lines (58 loc) · 2.41 KB
/
map.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
"""
Data Visualization Project
Parse data from an ugly CSV or Excel file, and render it in
JSON-like form, visualize in graphs, and plot as a map.
Part III: Take the data we parsed earlier and create a different format
for rendering a map. Here, we parse through each line item of the
CSV file and create a geojson object, to be collected into one geojson
file for uploading to gist.github.com.
"""
import geojson as g
import parse as p
def create_map(data_file):
"""Creates a GeoJSON file.
Returns a GeoJSON file that can be rendered in a GitHub
Gist at gist.github.com. Just copy the output file and
paste into a new Gist, then create either a public or
private gist. GitHub will automatically render the GeoJSON
file as a map.
"""
# Define type of GeoJSON we're creating
geo_map = {"type": "FeatureCollection"}
# Define empty list to collect each point to graph
item_list = []
# Iterate over our data to create GeoJSOn document.
# We're using enumerate() so we get the line, as well
# the index, which is the line number.
for index, line in enumerate(data_file):
# Skip any zero coordinates as this will throw off
# our map.
if line['X'] == "0" or line['Y'] == "0":
continue
# Setup a new dictionary for each iteration.
data = {}
# Assign line items to appropriate GeoJSON fields.
data['type'] = 'Feature'
data['id'] = index
data['properties'] = {'title': line['Category'],
'description': line['Descript'],
'date': line['Date']}
data['geometry'] = {'type': 'Point',
'coordinates': (line['X'], line['Y'])}
# Add data dictionary to our item_list
item_list.append(data)
# For each point in our item_list, we add the point to our
# dictionary. setdefault creates a key called 'features' that
# has a value type of an empty list. With each iteration, we
# are appending our point to that list.
for point in item_list:
geo_map.setdefault('features', []).append(point)
# Now that all data is parsed in GeoJSON write to a file so we
# can upload it to gist.github.com
with open('file_sf.geojson', 'w') as f:
f.write(g.dumps(geo_map))
def main():
data = p.parse(p.MY_FILE, ",")
return create_map(data)
if __name__ == '__main__':
main()