Skip to content

Commit

Permalink
reformatting and correctly routing xml responseformat
Browse files Browse the repository at this point in the history
  • Loading branch information
mvexel committed May 25, 2018
1 parent 547d2bf commit c8f96ff
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 79 deletions.
10 changes: 6 additions & 4 deletions examples/overpassify_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@
def response():
Settings(timeout=1400)
search = Area(3600134503) + Area(3600134502)
ways = Way(search,
maxspeed=None,
highway=NotRegex("cycleway|footway|path|service"),
access=NotRegex("private"))
ways = Way(
search,
maxspeed=None,
highway=NotRegex("cycleway|footway|path|service"),
access=NotRegex("private"),
)
out(ways, body=True, geom=True, qt=True)
noop()

Expand Down
7 changes: 3 additions & 4 deletions examples/readme_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

api = overpass.API()
response = api.get('node["name"="Salt Lake City"]')
print([(
feature['id'],
feature['properties']['name'])
for feature in response['features']])
print(
[(feature["id"], feature["properties"]["name"]) for feature in response["features"]]
)
3 changes: 2 additions & 1 deletion examples/turn_restriction_relations_as_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
overpass_response = api.get(
turn_restrictions_query,
responseformat='csv(::"id",::"user",::"timestamp",restriction,"restriction:conditional")',
verbosity='meta')
verbosity="meta",
)

print(overpass_response)
8 changes: 2 additions & 6 deletions examples/unique_users_for_area.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@
users = {"ids": [], "usernames": []}
message_urls = []
api = overpass.API(debug=False)
result = api.Get(
query,
responseformat="csv(::uid,::user)",
verbosity="meta")
result = api.Get(query, responseformat="csv(::uid,::user)", verbosity="meta")
del result[0] # header
for row in result:
uid = int(row[0])
Expand All @@ -29,7 +26,6 @@
continue
users["ids"].append(uid)
users["usernames"].append(username)
message_urls.append("https://www.openstreetmap.org/message/new/{}".format(
username))
message_urls.append("https://www.openstreetmap.org/message/new/{}".format(username))
print(users)
print(message_urls)
13 changes: 9 additions & 4 deletions overpass/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@

"""Thin wrapper around the OpenStreetMap Overpass API."""

__title__ = 'overpass'
__version__ = '0.6.0'
__license__ = 'Apache 2.0'
__title__ = "overpass"
__version__ = "0.6.0"
__license__ = "Apache 2.0"

from .api import API
from .queries import MapQuery, WayQuery
from .errors import (
OverpassError, OverpassSyntaxError, TimeoutError, MultipleRequestsError, ServerLoadError, UnknownOverpassError
OverpassError,
OverpassSyntaxError,
TimeoutError,
MultipleRequestsError,
ServerLoadError,
UnknownOverpassError,
)
from .utils import *
54 changes: 25 additions & 29 deletions overpass/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@
import geojson
import logging
from io import StringIO
from .errors import (OverpassSyntaxError, TimeoutError, MultipleRequestsError,
ServerLoadError, UnknownOverpassError, ServerRuntimeError)
from .errors import (
OverpassSyntaxError,
TimeoutError,
MultipleRequestsError,
ServerLoadError,
UnknownOverpassError,
ServerRuntimeError,
)


class API(object):
Expand Down Expand Up @@ -51,18 +57,13 @@ def __init__(self, *args, **kwargs):
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True

def get(self,
query,
responseformat="geojson",
verbosity="body",
build=True):
def get(self, query, responseformat="geojson", verbosity="body", build=True):
"""Pass in an Overpass query in Overpass QL."""
# Construct full Overpass query
if build:
full_query = self._construct_ql_query(
query,
responseformat=responseformat,
verbosity=verbosity)
query, responseformat=responseformat, verbosity=verbosity
)
else:
full_query = query

Expand All @@ -71,13 +72,13 @@ def get(self,

# Get the response from Overpass
r = self._get_from_overpass(full_query)
content_type = r.headers.get('content-type')
content_type = r.headers.get("content-type")

if self.debug:
print(content_type)
if content_type == "text/csv":
result = []
reader = csv.reader(StringIO(r.text), delimiter='\t')
reader = csv.reader(StringIO(r.text), delimiter="\t")
for row in reader:
result.append(row)
return result
Expand All @@ -92,12 +93,11 @@ def get(self,
# Check for valid answer from Overpass.
# A valid answer contains an 'elements' key at the root level.
if "elements" not in response:
raise UnknownOverpassError(
"Received an invalid answer from Overpass.")
raise UnknownOverpassError("Received an invalid answer from Overpass.")

# If there is a 'remark' key, it spells trouble.
overpass_remark = response.get('remark', None)
if overpass_remark and overpass_remark.startswith('runtime error'):
overpass_remark = response.get("remark", None)
if overpass_remark and overpass_remark.startswith("runtime error"):
raise ServerRuntimeError(overpass_remark)

if responseformat is not "geojson":
Expand All @@ -121,15 +121,12 @@ def _construct_ql_query(self, userquery, responseformat, verbosity):

if responseformat == "geojson":
template = self._GEOJSON_QUERY_TEMPLATE
complete_query = template.format(
query=raw_query,
verbosity=verbosity)
complete_query = template.format(query=raw_query, verbosity=verbosity)
else:
template = self._QUERY_TEMPLATE
complete_query = template.format(
query=raw_query,
out=responseformat,
verbosity=verbosity)
query=raw_query, out=responseformat, verbosity=verbosity
)

if self.debug:
print(complete_query)
Expand All @@ -144,7 +141,7 @@ def _get_from_overpass(self, query):
data=payload,
timeout=self.timeout,
proxies=self.proxies,
headers={'Accept-Charset': 'utf-8;q=0.7,*;q=0.7'}
headers={"Accept-Charset": "utf-8;q=0.7,*;q=0.7"},
)

except requests.exceptions.Timeout:
Expand All @@ -160,10 +157,10 @@ def _get_from_overpass(self, query):
elif self._status == 504:
raise ServerLoadError(self._timeout)
raise UnknownOverpassError(
"The request returned status code {code}".format(
code=self._status))
"The request returned status code {code}".format(code=self._status)
)
else:
r.encoding = 'utf-8'
r.encoding = "utf-8"
return r

def _as_geojson(self, elements):
Expand All @@ -185,9 +182,8 @@ def _as_geojson(self, elements):
continue

feature = geojson.Feature(
id=elem["id"],
geometry=geometry,
properties=elem.get("tags"))
id=elem["id"], geometry=geometry, properties=elem.get("tags")
)
features.append(feature)

return geojson.FeatureCollection(features)
9 changes: 2 additions & 7 deletions overpass/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ def __init__(self, south, west, north, east):

def __str__(self):
return self._QUERY_TEMPLATE.format(
west=self.west,
south=self.south,
east=self.east,
north=self.north
west=self.west, south=self.south, east=self.east, north=self.north
)


Expand All @@ -45,6 +42,4 @@ def __init__(self, query_parameters):
self.query_parameters = query_parameters

def __str__(self):
return self._QUERY_TEMPLATE.format(
query_parameters=self.query_parameters
)
return self._QUERY_TEMPLATE.format(query_parameters=self.query_parameters)
40 changes: 19 additions & 21 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
from setuptools import setup

setup(
name='overpass',
packages=['overpass'],
version='0.6.0',
description='Python wrapper for the OpenStreetMap Overpass API',
long_description='See README.md',
author='Martijn van Exel',
author_email='m@rtijn.org',
url='https://github.com/mvexel/overpass-api-python-wrapper',
license='Apache',
keywords=['openstreetmap', 'overpass', 'wrapper'],
name="overpass",
packages=["overpass"],
version="0.6.0",
description="Python wrapper for the OpenStreetMap Overpass API",
long_description="See README.md",
author="Martijn van Exel",
author_email="m@rtijn.org",
url="https://github.com/mvexel/overpass-api-python-wrapper",
license="Apache",
keywords=["openstreetmap", "overpass", "wrapper"],
classifiers=[
'License :: OSI Approved :: Apache Software License',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Topic :: Scientific/Engineering :: GIS',
'Topic :: Utilities',
"License :: OSI Approved :: Apache Software License",
"Programming Language :: Python :: 2.7",
"Programming Language :: Python :: 3.3",
"Programming Language :: Python :: 3.4",
"Programming Language :: Python :: 3.5",
"Topic :: Scientific/Engineering :: GIS",
"Topic :: Utilities",
],
install_requires=['requests>=2.3.0', 'geojson>=1.0.9'],
extras_require={
'test': ['pytest'],
}
install_requires=["requests>=2.3.0", "geojson>=1.0.9"],
extras_require={"test": ["pytest"]},
)
6 changes: 3 additions & 3 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def test_geojson():

map_query = overpass.MapQuery(37.86517, -122.31851, 37.86687, -122.31635)
osm_geo = api.get(map_query)
assert len(osm_geo['features']) > 1
assert len(osm_geo["features"]) > 1

osm_geo = api.get('node(area:3602758138)[amenity=cafe]')
assert len(osm_geo['features']) > 1
osm_geo = api.get("node(area:3602758138)[amenity=cafe]")
assert len(osm_geo["features"]) > 1

0 comments on commit c8f96ff

Please sign in to comment.