-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.py
94 lines (75 loc) · 2.44 KB
/
parser.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
import googlemaps
import csv
from geojson import Feature, Point, FeatureCollection
import json
from os import listdir
def write_file(filename, text):
with open(filename, 'w') as outfile:
outfile.write(text)
def read_file(filename):
with open(filename, 'r') as file:
return file.read()
gmaps = googlemaps.Client(key='AIzaSyBapNiIlZ8ZZ-j7rA9Z59S3BIzbZ2opob4')
geocoding = {}
def geocode(city):
if city not in geocoding:
result = gmaps.geocode(city + ', Germany')
lng = result[0]['geometry']['location']['lng']
lat = result[0]['geometry']['location']['lat']
geocoding[city] = Point((lng, lat))
return geocoding[city]
DESCRIPTION = 0
START = 1
END = 2
LENGTH = 3
PLZ = 4
CITY = 5
try:
geocoding = json.loads(read_file('data/geocode.json'))
except:
geocoding = {}
cities = {}
i = 0
data_dir = 'data/'
for filename in listdir(data_dir):
with open(data_dir + filename) as csvfile:
print('Lese ' + filename)
reader = csv.reader(csvfile, delimiter=',', )
row = reader.__next__() # skip first row
for row in reader:
# Geocode city (only once per city)
city = row[CITY]
# Create event
event = {
'description': row[0],
'start': row[1],
'end': row[2],
'length': row[3]
}
# Append event to city
if city not in cities:
cities[city] = Feature(geometry=geocode(city), properties=
{
'title': city,
'plz': row[4],
'events': [event],
'description': '',
"icon": {
"className": "marker",
"html": "",
"iconSize": 'null'
}
})
else:
cities[city].properties['events'].append(event)
# Extend marker description
cities[city].properties['description'] += '<b>{}</b><br>Zeitraum: {} - {}<br><br>' \
.format(event['description'], event['start'], event['end'])
# Create feature list
features = []
for key, feature in cities.items():
features.append(feature)
feature_collection = FeatureCollection(features)
# Write to file
write_file('data/features.geojson', json.dumps(feature_collection))
write_file('data/geocode.json', json.dumps(geocoding))