-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssr2_osm.py
67 lines (55 loc) · 3.03 KB
/
ssr2_osm.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
import os
import logging
logger = logging.getLogger('utility_to_osm.ssr2.osm')
import utility_to_osm.kommunenummer as kommunenummer
import utility_to_osm.argparse_util as argparse_util
import utility_to_osm.overpass_helper as overpass_helper
global kommuneNr_2_relationId
kommuneNr_2_relationId = dict()
def overpass_stedsnr_in_relation(relation_id, cache_filename,
old_age_days = 14,
root_template = '',
query_template_filename = 'query_template.xml',):
"""Queries OSM for all ssr:stedsnr in the given relation-id (say a single Kommune, or the entire country)"""
query_template_filename = os.path.join(root_template, query_template_filename)
query = overpass_helper.get_xml_query(query_template_filename,
relation_id=relation_id, use='relation')
osm = overpass_helper.overpass_xml(xml=query, old_age_days=old_age_days,
cache_filename=cache_filename)
return osm
def overpass_stedsnr_in_kommunenr(n, cache_filename, cache_dir, **kwargs):
"""Calls overpass_stedsnr_in_relation after converting from kommune-nr to osm-relation-id"""
global kommuneNr_2_relationId
if len(kommuneNr_2_relationId) == 0:
print('getting cache')
kommuneNr_2_relationId = kommunenummer.get_osm_kommune_ids(cache_dir=cache_dir)
relation_id = kommuneNr_2_relationId[int(n)]
return overpass_stedsnr_in_relation(relation_id, cache_filename, **kwargs)
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(description='')
argparse_util.add_verbosity(parser, default=logging.INFO)
parser.add_argument('--output', default='output',
help='Output root directory.')
args = parser.parse_args()
logging.basicConfig(level=args.loglevel)
kommuneNr_2_name, _ = kommunenummer.kommunenummer(cache_dir=args.output)
kommuneNr_2_relationId = kommunenummer.get_osm_kommune_ids(cache_dir=args.output)
for key in kommuneNr_2_name:
try:
kommuneNr_2_name[key], kommuneNr_2_relationId[key]
logger.info('kommune_nr = %s, kommune_name = %s, osm_relation_id = %s',
key, kommuneNr_2_name[key], kommuneNr_2_relationId[key])
except:
logger.error('Error key=%s', key)
# for key, value in kommuneNr_2_relationId.items():
# print(key, value)
for key in sorted(kommuneNr_2_relationId.keys()):
# relation_id = kommuneNr_2_relationId[key]
kommune_nr_str = '%04d' % key
cache_filename = os.path.join(args.output,
kommune_nr_str,
'%s-osmStedsnr.osm' % kommune_nr_str)
# overpass_stedsnr = overpass_stedsnr_in_relation(relation_id, cache_filename)
overpass_stedsnr = overpass_stedsnr_in_kommunenr(kommune_nr_str, cache_filename, cache_dir=args.output)
print('%s items in osm = %s' % (kommune_nr_str, len(overpass_stedsnr)))