forked from andrewjdyck/csvToGeoJSON
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsvToGeoJSON.py
52 lines (41 loc) · 1.04 KB
/
csvToGeoJSON.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
import csv
# Read in raw data from csv
rawData = csv.reader(open('sample.csv', 'rb'), dialect='excel')
# the template. where data from the csv will be formatted to geojson
template = \
''' \
{ "type" : "Feature",
"id" : %s,
"geometry" : {
"type" : "Point",
"coordinates" : ["%s","%s"]},
"properties" : { "name" : "%s", "value" : "%s"}
},
'''
# the head of the geojson file
output = \
''' \
{ "type" : "Feature Collection",
{"features" : [
'''
# loop through the csv by row skipping the first
iter = 0
for row in rawData:
iter += 1
if iter >= 2:
id = row[0]
lat = row[1]
lon = row[2]
name = row[3]
pop = row[4]
output += template % (row[0], row[1], row[2], row[3], row[4])
# the tail of the geojson file
output += \
''' \
]
}
'''
# opens an geoJSON file to write the output to
outFileHandle = open("output.geojson", "w")
outFileHandle.write(output)
outFileHandle.close()