-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrape.py
32 lines (26 loc) · 1.42 KB
/
scrape.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
# Julian Lemmerich
# getting all cities with their location from an osm file
import osmium
import csv
class CityHandler(osmium.SimpleHandler):
def __init__(self):
super(CityHandler, self).__init__()
self.cities = []
# reading borders
with open("./data/borders.txt", 'r') as file:
borderstr = file.read().strip('\n').split(",")
self.borders = [float(borderstr[0]), float(borderstr[1]), float(borderstr[2]), float(borderstr[3])]
def node(self, n):
if 'place' in n.tags:
if n.tags.get('place') == 'city' or n.tags.get('place') == 'town' or n.tags.get('place') == 'village' or n.tags.get('place') == 'suburb':
if float(self.borders[1]) < n.location.lat < float(self.borders[3]) and float(self.borders[0]) < n.location.lon < float(self.borders[2]):
try: print("matched! " + n.tags.get('name') + ", " + str(n.location.lat) + ", " + str(n.location.lon))
except: print("matched! -, " + str(n.location.lat) + ", " + str(n.location.lon)) #if location has no name
self.cities.append([n.location.lat, n.location.lon])
c = CityHandler()
c.apply_file("./data/osm.osm.pbf", locations=True)
# write cities to csv for plotting with R
with open('data\origins.csv', 'w', newline='') as file:
writer = csv.writer(file)
for l in c.cities:
writer.writerow(l)