Skip to content
This repository was archived by the owner on Jun 10, 2025. It is now read-only.

List comprehension, literal comparison, import ordering, staticmethod #133

Merged
merged 5 commits into from
Mar 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 23 additions & 28 deletions overpass/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,23 @@
# which is licensed under Apache 2.0.
# See LICENSE.txt for the full license text.

import requests
import json
import csv
import geojson
import json
import logging
from datetime import datetime
from shapely.geometry import Polygon, Point
from io import StringIO

import geojson
import requests
from shapely.geometry import Point, Polygon

from .errors import (
OverpassSyntaxError,
TimeoutError,
MultipleRequestsError,
OverpassSyntaxError,
ServerLoadError,
UnknownOverpassError,
ServerRuntimeError,
TimeoutError,
UnknownOverpassError,
)


Expand Down Expand Up @@ -109,14 +111,10 @@ def get(self, query, responseformat="geojson", verbosity="body", build=True, dat
if self.debug:
print(content_type)
if content_type == "text/csv":
result = []
reader = csv.reader(StringIO(r.text), delimiter="\t")
for row in reader:
result.append(row)
return result
return list(csv.reader(StringIO(r.text), delimiter="\t"))
elif content_type in ("text/xml", "application/xml", "application/osm3s+xml"):
return r.text
elif content_type == "application/json":
else:
response = json.loads(r.text)

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

if responseformat is not "geojson":
if responseformat != "geojson":
return response

# construct geojson
Expand Down Expand Up @@ -236,21 +234,18 @@ def _as_geojson(self, elements):
if member["role"] == "inner":
points = [(coords["lon"], coords["lat"]) for coords in member.get("geometry", [])]
# Check that the inner polygon is complete
if points and points[-1] == points[0]:
# We need to check to which outer polygon the inner polygon belongs
point = Point(points[0])
check = False
for poly in polygons:
polygon = Polygon(poly[0])
if polygon.contains(point):
poly.append(points)
check = True
break
if not check:
raise UnknownOverpassError("Received corrupt data from Overpass (inner polygon cannot "
"be matched to outer polygon).")
else:
if not points or points[-1] != points[0]:
raise UnknownOverpassError("Received corrupt data from Overpass (incomplete polygon).")
# We need to check to which outer polygon the inner polygon belongs
point = Point(points[0])
for poly in polygons:
polygon = Polygon(poly[0])
if polygon.contains(point):
poly.append(points)
break
else:
raise UnknownOverpassError("Received corrupt data from Overpass (inner polygon cannot "
"be matched to outer polygon).")
# Finally create MultiPolygon geometry
if polygons:
geometry = geojson.MultiPolygon(polygons)
Expand Down
4 changes: 2 additions & 2 deletions overpass/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

class Utils(object):

@classmethod
def to_overpass_id(cls, osmid, area=False):
@staticmethod
def to_overpass_id(osmid, area=False):
area_base = 2400000000
relation_base = 3600000000
if area:
Expand Down