-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathOSMHelper.py
92 lines (89 loc) · 3.79 KB
/
OSMHelper.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
__version__ = "1.4"
'''
__author__ = "Lukas Bug"
__copyright__ = "Copyright 2018, Esri Deutschland GmbH"
__license__ = "Apache-2.0"
__version__ = "1.4"
__email__ = "lukas.bug@aol.de"
'''
from osm_runner import gen_osm_sdf
from osm_runner_utils import Filters
from threading import Thread
from ExceptionHelper import OSMHelperExceptions as osmh_excps
import os,traceback
threadlist = []
sdflist = []
def requestOSMData(osmconfig, elem, sdflist):
'''
Function to prepare request of an OSM data item using osm-runner
@param osmconfig: A dictionary containing the OSM configuration defined in the file osmconfig.json.
@param elem: OSM-configuration item (category) defined in the file osmconfig.json
@param sdflist: List of spatial dataframes needed for upload to portal.
'''
if elem['isEnabled'] == 'yes':
geom = elem['geometryType']
bbox = osmconfig['boundingBox']
category = elem['categoryName']
excludedattributes = elem['attributeFieldsToExclude']
Filters[elem['categoryName']] = elem['categoryValues']
osmdata = fetchOSMData(geom, bbox, category, excludedattributes)
sdflist.append(osmdata)
def getDataFrameList(osmconfig):
'''
Function to initiate simulatenous (Thread-based) requests to OSM using osm-runner.
@param osmConfig: A dictionary containing the OSM configuration defined in the file osmconfig.json.
'''
for elem in osmconfig['categories']:
t = Thread(target=requestOSMData, args=[osmconfig, elem, sdflist])
threadlist.append(t)
t.start()
for t in threadlist:
t.join()
return sdflist
def fetchOSMData(geom, bbox, category, excludedattributes):
'''
Function to create layer definitions for upload to portal.
@param geom: The geometry type of the requested data.
@param bbox: The extent of the requested data defined by a bounding box.
@param category: The category name of the requested data.
@param excludedattributes: The attributes to be excluded from the current layer.
'''
try:
print('Fetching '+geom+' data from OpenStreetMap on category: '+category+' . . .')
if geom != 'polygon':
sdf = gen_osm_sdf(geom, bbox, excludedattributes, category)
if not sdf.empty:
return sdf
else:
if FileNotFoundError:
raise FileNotFoundError
if ConnectionRefusedError:
raise ConnectionRefusedError
if RuntimeError:
raise RuntimeError
else:
sdf = gen_osm_sdf(geom, bbox, excludedattributes, category, 1)
if not sdf.empty:
return sdf
else:
if FileNotFoundError:
raise FileNotFoundError
if ConnectionRefusedError:
raise ConnectionRefusedError
if RuntimeError:
raise RuntimeError
except FileNotFoundError:
tb = traceback.format_exc()
print('OSM request could not be completed. \n Cause: OSM returned empty result for geometry '+geom+' , \
the scripts exits now. Additional configuration information: Category: '+category+', excluded attributes: \
'+excludedattributes+', \n Disable this configuration and try again. Detailed information: '+tb)
os._exit(-1)
except ConnectionRefusedError:
tb = traceback.format_exc()
print('OSM request could not be completed. \n Cause: OSM refused the connection due to too many requests, \
try again later. Detailed information: '+tb)
os._exit(-1)
except RuntimeError:
tb = traceback.format_exc()
print('OSM request could not be completed. \n Cause: OSM returned an unknown error. Detailed information: '+tb)
os._exit(-1)