Commit 419b9e62 authored by Branko Kokanovic's avatar Branko Kokanovic

Adding mapping for https://regio-osm.de

parent 11a67771
# -*- coding: utf-8 -*-
from typing import List
import os
import csv
......@@ -56,3 +58,25 @@ def read_output_csv(input_file: str, filter_district: str = None, filter_municip
sz['building_is_apartments'] = sz['building_is_apartments'] or False
stambene_zajednice.sort(key=itemgetter('district', 'municipality', 'street', 'number'))
return stambene_zajednice
def osm_to_dataset_municipality(municipality: str) -> List[str]:
"""
Try to convert name from OSM municipality name to name from catastre
:param municipality: Name of municipality, as from OSM
:return: Name from municipality, as if from catastre
"""
if municipality.startswith('градска општина '):
trimmed = municipality.replace('градска општина ', '')
if trimmed == 'палилула':
return [trimmed, 'палилула (београд)']
else:
return [trimmed]
if municipality.startswith('општина '):
trimmed = municipality.replace('општина ', '')
if trimmed == 'палилула':
return [trimmed, 'палилула (ниш)']
return [trimmed]
if municipality.startswith('град '):
return [municipality.replace('град ', '')]
return [municipality, ]
\ No newline at end of file
......@@ -18,7 +18,7 @@ from geopy.exc import GeocoderServiceError
from geopy.geocoders import Nominatim
from transliteration import cyr2lat
from common import read_input_csv, read_output_csv
from common import read_input_csv, read_output_csv, osm_to_dataset_municipality
api = overpy.Overpass(url='http://overpass.openstreetmap.fr/api/interpreter')
geolocator = Nominatim(user_agent="https://gitlab.com/stalker314314/stambene-zajednice-analysis")
......@@ -111,20 +111,6 @@ def osm_to_dataset_district(district: str) -> List[str]:
return [district, ]
def osm_to_dataset_municipality(municipality: str) -> List[str]:
if municipality.startswith('градска општина '):
trimmed = municipality.replace('градска општина ', '')
if trimmed == 'палилула':
return [trimmed, 'палилула (београд)']
else:
return [trimmed]
if municipality.startswith('општина '):
return [municipality.replace('општина ', '')]
if municipality.startswith('град '):
return [municipality.replace('град ', '')]
return [municipality, ]
@retry_on_error()
def find_district(entity_id: str, entity_type: str):
sleep(0.5)
......
# -*- coding: utf-8 -*-
"""
This file is only needed to generate OSM<->municipality mapping, needed for https://regio-osm.de/ project.
"""
import overpy
import csv
from common import read_output_csv, osm_to_dataset_municipality
api = overpy.Overpass(url='http://overpass.openstreetmap.fr/api/interpreter')
def write_csv(output_file, output):
with open(output_file, 'w') as h:
writer = csv.DictWriter(h, fieldnames=['municipalityosm', 'municipalitynamedataset',
'wikidata', 'osmadminlevel', 'hierarchy'])
writer.writeheader()
for data in output:
writer.writerow(data)
def find_municipalities_ovepass():
result = api.query("""
area["name"="Србија"]["admin_level"=2]->.a;
(
relation(area.a)["admin_level"="7"];
relation(area.a)["admin_level"="8"];
);
out;
// &contact=branko@kokanovic.org
""")
return [
{'name': r.tags['name'],
'admin_level': r.tags['admin_level'],
'wikidata': r.tags['wikidata'] if 'wikidata' in r.tags else ''
}
for r in result.relations]
def main():
output = []
stambene_zajednice = read_output_csv('result.csv')
# extract distinct municipalities from catastre
for sz in stambene_zajednice:
found = False
for data in output:
if sz['municipality'] == data['municipalitynamedataset']:
found = True
break
if not found:
output_row = {}
output_row['municipalitynamedataset'] = sz['municipality']
output_row['municipalityosm'] = ''
output_row['wikidata'] = ''
output_row['osmadminlevel'] = ''
output_row['hierarchy'] = 'Србија,' + sz['district']
output.append(output_row)
# Now try to match with OSM data
osm_municipalities = find_municipalities_ovepass()
for data in output:
found = -1
for index, osm_municipality in enumerate(osm_municipalities):
possible_matches = osm_to_dataset_municipality(osm_municipality['name'].lower())
for possible_match in possible_matches:
if possible_match == data['municipalitynamedataset'].lower():
data['municipalityosm'] = osm_municipality['name']
data['wikidata'] = osm_municipality['wikidata']
data['osmadminlevel'] = osm_municipality['admin_level']
found = index
break
if found > -1:
break
if found > -1:
del osm_municipalities[found]
print('Remaining municipalities:', osm_municipalities)
write_csv('regio-osm.csv', output)
if __name__ == '__main__':
main()
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment