-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathosmjson2geojson.py
More file actions
76 lines (55 loc) · 2.27 KB
/
Copy pathosmjson2geojson.py
File metadata and controls
76 lines (55 loc) · 2.27 KB
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
__VERSION__ = '0.0.1.dev1'
def convert(osmjson):
result_dict = {'type': 'FeatureCollection'}
if "generator" in osmjson:
result_dict["generator"] = "overpass-ide" # I want to be identical to turbopass geojson export
if "osm3s" in osmjson:
if "copyright" in osmjson["osm3s"]:
result_dict["copyright"] = osmjson["osm3s"]["copyright"]
if "timestamp_osm_base" in osmjson["osm3s"]:
result_dict["timestamp"] = osmjson["osm3s"]["timestamp_osm_base"]
if "elements" in osmjson:
features = convert_elements(osmjson["elements"])
result_dict['features'] = features
return result_dict
def convert_elements(elements):
relations = convert_relations_with_center(elements)
ways = convert_ways_with_center(elements)
nodes = convert_nodes(elements)
return relations + ways + nodes
def convert_nodes(elements):
features = []
for element in elements:
if element['type'] == 'node':
coordinates = [element['lon'], element['lat']]
feature = create_point_feature(element, coordinates)
features.append(feature)
return features
def convert_ways_with_center(elements):
features = []
for element in elements:
if element['type'] == 'way' and 'center' in element:
coordinates = [element['center']['lon'], element['center']['lat']]
element['tags']['@geometry'] = 'center'
feature = create_point_feature(element, coordinates)
features.append(feature)
return features
def convert_relations_with_center(elements):
features = []
for element in elements:
if element['type'] == 'relation' and 'center' in element:
coordinates = [element['center']['lon'], element['center']['lat']]
element['tags']['@geometry'] = 'center'
feature = create_point_feature(element, coordinates)
features.append(feature)
return features
def create_point_feature(element, coordinates):
return {
'type': 'Feature',
'id': f"{element['type']}/{element['id']}",
'properties': {'@id': f"{element['type']}/{element['id']}", **element['tags']},
'geometry': {
'coordinates': coordinates,
'type': 'Point',
}
}