Skip to content

Commit b8c6a8e

Browse files
author
Evan Derickson
committed
Efficiency refactors
1 parent cd544c5 commit b8c6a8e

File tree

2 files changed

+25
-34
lines changed

2 files changed

+25
-34
lines changed

overpass/api.py

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,19 @@
33
# which is licensed under Apache 2.0.
44
# See LICENSE.txt for the full license text.
55

6-
import requests
7-
import json
86
import csv
9-
import geojson
7+
import json
108
import logging
119
from datetime import datetime
12-
from shapely.geometry import Polygon, Point
1310
from io import StringIO
14-
from .errors import (
15-
OverpassSyntaxError,
16-
TimeoutError,
17-
MultipleRequestsError,
18-
ServerLoadError,
19-
UnknownOverpassError,
20-
ServerRuntimeError,
21-
)
11+
12+
import geojson
13+
import requests
14+
from shapely.geometry import Point, Polygon
15+
16+
from .errors import (MultipleRequestsError, OverpassSyntaxError,
17+
ServerLoadError, ServerRuntimeError, TimeoutError,
18+
UnknownOverpassError)
2219

2320

2421
class API(object):
@@ -109,14 +106,11 @@ def get(self, query, responseformat="geojson", verbosity="body", build=True, dat
109106
if self.debug:
110107
print(content_type)
111108
if content_type == "text/csv":
112-
result = []
113109
reader = csv.reader(StringIO(r.text), delimiter="\t")
114-
for row in reader:
115-
result.append(row)
116-
return result
110+
return [row for row in reader]
117111
elif content_type in ("text/xml", "application/xml", "application/osm3s+xml"):
118112
return r.text
119-
elif content_type == "application/json":
113+
else:
120114
response = json.loads(r.text)
121115

122116
if not build:
@@ -132,7 +126,7 @@ def get(self, query, responseformat="geojson", verbosity="body", build=True, dat
132126
if overpass_remark and overpass_remark.startswith("runtime error"):
133127
raise ServerRuntimeError(overpass_remark)
134128

135-
if responseformat is not "geojson":
129+
if responseformat != "geojson":
136130
return response
137131

138132
# construct geojson
@@ -236,21 +230,18 @@ def _as_geojson(self, elements):
236230
if member["role"] == "inner":
237231
points = [(coords["lon"], coords["lat"]) for coords in member.get("geometry", [])]
238232
# Check that the inner polygon is complete
239-
if points and points[-1] == points[0]:
240-
# We need to check to which outer polygon the inner polygon belongs
241-
point = Point(points[0])
242-
check = False
243-
for poly in polygons:
244-
polygon = Polygon(poly[0])
245-
if polygon.contains(point):
246-
poly.append(points)
247-
check = True
248-
break
249-
if not check:
250-
raise UnknownOverpassError("Received corrupt data from Overpass (inner polygon cannot "
251-
"be matched to outer polygon).")
252-
else:
233+
if not points or points[-1] != points[0]:
253234
raise UnknownOverpassError("Received corrupt data from Overpass (incomplete polygon).")
235+
# We need to check to which outer polygon the inner polygon belongs
236+
point = Point(points[0])
237+
for poly in polygons:
238+
polygon = Polygon(poly[0])
239+
if polygon.contains(point):
240+
poly.append(points)
241+
break
242+
else:
243+
raise UnknownOverpassError("Received corrupt data from Overpass (inner polygon cannot "
244+
"be matched to outer polygon).")
254245
# Finally create MultiPolygon geometry
255246
if polygons:
256247
geometry = geojson.MultiPolygon(polygons)

overpass/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ class Utils(object):
88

99
@classmethod
1010
def to_overpass_id(cls, osmid, area=False):
11-
area_base = 2400000000
12-
relation_base = 3600000000
1311
if area:
12+
area_base = 2400000000
1413
return int(osmid) + area_base
14+
relation_base = 3600000000
1515
return int(osmid) + relation_base

0 commit comments

Comments
 (0)