-
Notifications
You must be signed in to change notification settings - Fork 84
/
getdatafromosm.py
114 lines (91 loc) · 3.59 KB
/
getdatafromosm.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import sys
import overpy
import imagestoosm.config as cfg
import os
import shapely.geometry
import shapely.wkt
import shapely.ops
import geojson
api = overpy.Overpass()
summary = {}
def saveOsmData(query) :
result = api.query(query)
for way in result.ways:
# "leisure=pitch,sport=" , don't use "-" char" in featureDirectoryName
featureDirectoryName = way.tags.get("sport")
outputDirectoryName = os.path.join(cfg.rootOsmDir,featureDirectoryName)
if ( os.path.exists(outputDirectoryName) == False):
os.makedirs(outputDirectoryName)
if ( (featureDirectoryName in summary) == False) :
summary[featureDirectoryName] = 1
else:
summary[featureDirectoryName] += 1
filenameBase= os.path.join(cfg.rootOsmDir,featureDirectoryName,str(way.id))
#print("Name: %d %s %s" % ( way.id ,way.tags.get("name", ""),filenameBase))
# leave the csv file for now, will delete when the script for the next
# stage is rewritten.
with open("%s.csv" % (filenameBase), "wt") as text_file:
for node in way.nodes:
text_file.write("%0.7f\t%0.7f\n" % (node.lat, node.lon))
with open("%s.GeoJSON" % (filenameBase), "wt") as text_file:
rawNodes = []
for node in way.nodes:
rawNodes.append( (node.lon, node.lat) )
try:
geom = shapely.geometry.Polygon(rawNodes)
tags = way.tags
tags['wayOSMId'] = way.id
features =[]
features.append( geojson.Feature(geometry=geom, properties=tags))
featureC = geojson.FeatureCollection(features)
text_file.write(geojson.dumps(featureC))
except Exception as e:
print(e)
queryFull = """[timeout:125];
(
area[admin_level=4][boundary=administrative][name="Massachusetts"];
area[admin_level=4][boundary=administrative][name="New York"];
area[admin_level=4][boundary=administrative][name="Connecticut"];
area[admin_level=4][boundary=administrative][name="Rhode Island"];
area[admin_level=4][boundary=administrative][name="Pennsylvania"];
)->.searchArea;
(
way["sport"="baseball"]["leisure"="pitch"](area.searchArea);
way["sport"="tennis"]["leisure"="pitch"](area.searchArea);
way["sport"="soccer"]["leisure"="pitch"](area.searchArea);
way["sport"="american_football"]["leisure"="pitch"](area.searchArea);
way["sport"="basketball"]["leisure"="pitch"](area.searchArea);
);
(._;>;);
out body;
"""
# fetch all ways and nodes
queryMA = """[timeout:125];
(
area[admin_level=4][boundary=administrative][name="Massachusetts"];
)->.searchArea;
(
way["sport"="baseball"]["leisure"="pitch"](area.searchArea);
way["sport"="tennis"]["leisure"="pitch"](area.searchArea);
way["sport"="soccer"]["leisure"="pitch"](area.searchArea);
way["sport"="american_football"]["leisure"="pitch"](area.searchArea);
way["sport"="basketball"]["leisure"="pitch"](area.searchArea);
);
(._;>;);
out body;
"""
saveOsmData(queryFull)
# Other possible data to query
# - bridges
# - solar panels farms
# - wind turbines
# - railroad crossings.
# - active rail roads
# - water tanks
# - wafer/lakes/rivers
# - parking lots
# - driveways
# - gas stations
# - building (Microsoft has already done this)
# - Running track
print(summary)