-
Notifications
You must be signed in to change notification settings - Fork 2
/
csv2geojson.py
110 lines (75 loc) · 2.25 KB
/
csv2geojson.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import csv
# Read in raw data from csv
rawData = csv.reader(open('list.csv', 'rb'), dialect='excel')
# the template. where data from the csv will be formatted to geojson
# you will most likely have to modify the properities to match your csv file
template = \
''' \
{ "type":"Feature", "properties":{"city": "%s", "income": "%s"}, "geometry": {"type":"Polygon", "coordinates": %s } }%s
'''
# the head of the geojson file
output = '''var statesData = {"type" : "FeatureCollection", "features" : [
'''
polygonTemplate = '[[%s]]'
polygon = ''
indexNow = 0
list = list(rawData)
def isPolygonBegin( indexNow ):
boo = False
indexBefore = indexNow - 1
idBefore = ''
idNow = list[indexNow][0]
#set the BEFORE id if its not the first row
if indexNow >= 1 :
idBefore = list[indexBefore][0]
if idBefore != idNow :
boo = True
elif indexNow == 0:
boo = True
return boo
def isPolygonEnd(indexNow):
boo = False
indexNext = indexNow + 1
idNext = ''
idNow = list[indexNow][0]
#set the AFTER id if its not the last row
if indexNext <= len(list)-2 :
idNext = list[indexNext][0]
if idNext != idNow:
boo = True
elif indexNow == len(list)-1:
boo = True
return boo
def polygonCreate(indexNow):
indexBefore = indexNow-1
indexNext = indexNow+1
idBefore = ''
idNext = ''
idNow = list[indexNow][0]
row = list[indexNow]
return '[' + row[1] + ', ' + row[2] + ']'
for row in list:
#this is the first row of the polygon
if isPolygonBegin(indexNow):
polygon = ''
polygon += polygonCreate(indexNow)
#this is the last row of the polygon
if isPolygonEnd(indexNow):
polygonList = polygonTemplate % (polygon)
comma = ''
if indexNow<len(list)-1:
comma = ','
output += template % (list[indexNow][0], list[indexNow][1], polygonList, comma)
else:
polygon += ','
indexNow += 1
# the tail of the geojson file
output += \
''' \
]
}
'''
# opens an geoJSON file to write the output to
outFileHandle = open("list.geojson", "w")
outFileHandle.write(output)
outFileHandle.close()