1
1
import sys
2
2
import requests
3
3
import json
4
- from shapely .geometry import Point
4
+ import geojson
5
+
5
6
6
7
class API (object ):
7
8
"""A simple Python wrapper for the OpenStreetMap Overpass API"""
@@ -69,10 +70,15 @@ def _ConstructQLQuery(self, userquery, asGeoJSON=False):
69
70
if not raw_query .endswith (";" ):
70
71
raw_query += ";"
71
72
72
- complete_query = self ._QUERY_TEMPLATE .format (
73
+ if asGeoJSON :
74
+ template = self ._GEOJSON_QUERY_TEMPLATE
75
+ else :
76
+ template = self ._QUERY_TEMPLATE
77
+
78
+ complete_query = template .format (
73
79
responseformat = self .responseformat ,
74
- query = raw_query
75
- )
80
+ query = raw_query )
81
+
76
82
if self .debug :
77
83
print (complete_query )
78
84
return complete_query
@@ -116,9 +122,9 @@ def _asGeoJSON(self, elements):
116
122
features = []
117
123
for elem in elements :
118
124
elem_type = elem ["type" ]
119
- if elem [ "type" ] == "node" :
120
- geometry = geojson .Point ((elem ["lon" ], elem ["lat" ]))
121
- elif elem [ "type" ] == "way" :
125
+ if elem_type == "node" :
126
+ geometry = geojson .Point ((elem ["lon" ], elem ["lat" ]))
127
+ elif elem_type == "way" :
122
128
points = []
123
129
for coords in elem ["geometry" ]:
124
130
points .append ((coords ["lon" ], coords ["lat" ]))
@@ -127,13 +133,14 @@ def _asGeoJSON(self, elements):
127
133
continue
128
134
129
135
feature = geojson .Feature (
130
- id = elem ["id" ],
131
- geometry = geometry ,
132
- properties = elem .get ("tags" ))
136
+ id = elem ["id" ],
137
+ geometry = geometry ,
138
+ properties = elem .get ("tags" ))
133
139
features .append (feature )
134
140
135
141
return geojson .FeatureCollection (features )
136
142
143
+
137
144
class OverpassException (Exception ):
138
145
def __init__ (self , status_code , message ):
139
146
self .status_code = status_code
0 commit comments