Skip to content

Commit c8f96ff

Browse files
committed
reformatting and correctly routing xml responseformat
1 parent 547d2bf commit c8f96ff

9 files changed

+71
-79
lines changed

Diff for: examples/overpassify_example.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@
1818
def response():
1919
Settings(timeout=1400)
2020
search = Area(3600134503) + Area(3600134502)
21-
ways = Way(search,
22-
maxspeed=None,
23-
highway=NotRegex("cycleway|footway|path|service"),
24-
access=NotRegex("private"))
21+
ways = Way(
22+
search,
23+
maxspeed=None,
24+
highway=NotRegex("cycleway|footway|path|service"),
25+
access=NotRegex("private"),
26+
)
2527
out(ways, body=True, geom=True, qt=True)
2628
noop()
2729

Diff for: examples/readme_example.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
api = overpass.API()
1111
response = api.get('node["name"="Salt Lake City"]')
12-
print([(
13-
feature['id'],
14-
feature['properties']['name'])
15-
for feature in response['features']])
12+
print(
13+
[(feature["id"], feature["properties"]["name"]) for feature in response["features"]]
14+
)

Diff for: examples/turn_restriction_relations_as_list.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
overpass_response = api.get(
1919
turn_restrictions_query,
2020
responseformat='csv(::"id",::"user",::"timestamp",restriction,"restriction:conditional")',
21-
verbosity='meta')
21+
verbosity="meta",
22+
)
2223

2324
print(overpass_response)

Diff for: examples/unique_users_for_area.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@
1717
users = {"ids": [], "usernames": []}
1818
message_urls = []
1919
api = overpass.API(debug=False)
20-
result = api.Get(
21-
query,
22-
responseformat="csv(::uid,::user)",
23-
verbosity="meta")
20+
result = api.Get(query, responseformat="csv(::uid,::user)", verbosity="meta")
2421
del result[0] # header
2522
for row in result:
2623
uid = int(row[0])
@@ -29,7 +26,6 @@
2926
continue
3027
users["ids"].append(uid)
3128
users["usernames"].append(username)
32-
message_urls.append("https://www.openstreetmap.org/message/new/{}".format(
33-
username))
29+
message_urls.append("https://www.openstreetmap.org/message/new/{}".format(username))
3430
print(users)
3531
print(message_urls)

Diff for: overpass/__init__.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,18 @@
77

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

10-
__title__ = 'overpass'
11-
__version__ = '0.6.0'
12-
__license__ = 'Apache 2.0'
10+
__title__ = "overpass"
11+
__version__ = "0.6.0"
12+
__license__ = "Apache 2.0"
1313

1414
from .api import API
1515
from .queries import MapQuery, WayQuery
1616
from .errors import (
17-
OverpassError, OverpassSyntaxError, TimeoutError, MultipleRequestsError, ServerLoadError, UnknownOverpassError
17+
OverpassError,
18+
OverpassSyntaxError,
19+
TimeoutError,
20+
MultipleRequestsError,
21+
ServerLoadError,
22+
UnknownOverpassError,
1823
)
1924
from .utils import *

Diff for: overpass/api.py

+25-29
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,14 @@
99
import geojson
1010
import logging
1111
from io import StringIO
12-
from .errors import (OverpassSyntaxError, TimeoutError, MultipleRequestsError,
13-
ServerLoadError, UnknownOverpassError, ServerRuntimeError)
12+
from .errors import (
13+
OverpassSyntaxError,
14+
TimeoutError,
15+
MultipleRequestsError,
16+
ServerLoadError,
17+
UnknownOverpassError,
18+
ServerRuntimeError,
19+
)
1420

1521

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

54-
def get(self,
55-
query,
56-
responseformat="geojson",
57-
verbosity="body",
58-
build=True):
60+
def get(self, query, responseformat="geojson", verbosity="body", build=True):
5961
"""Pass in an Overpass query in Overpass QL."""
6062
# Construct full Overpass query
6163
if build:
6264
full_query = self._construct_ql_query(
63-
query,
64-
responseformat=responseformat,
65-
verbosity=verbosity)
65+
query, responseformat=responseformat, verbosity=verbosity
66+
)
6667
else:
6768
full_query = query
6869

@@ -71,13 +72,13 @@ def get(self,
7172

7273
# Get the response from Overpass
7374
r = self._get_from_overpass(full_query)
74-
content_type = r.headers.get('content-type')
75+
content_type = r.headers.get("content-type")
7576

7677
if self.debug:
7778
print(content_type)
7879
if content_type == "text/csv":
7980
result = []
80-
reader = csv.reader(StringIO(r.text), delimiter='\t')
81+
reader = csv.reader(StringIO(r.text), delimiter="\t")
8182
for row in reader:
8283
result.append(row)
8384
return result
@@ -92,12 +93,11 @@ def get(self,
9293
# Check for valid answer from Overpass.
9394
# A valid answer contains an 'elements' key at the root level.
9495
if "elements" not in response:
95-
raise UnknownOverpassError(
96-
"Received an invalid answer from Overpass.")
96+
raise UnknownOverpassError("Received an invalid answer from Overpass.")
9797

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

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

122122
if responseformat == "geojson":
123123
template = self._GEOJSON_QUERY_TEMPLATE
124-
complete_query = template.format(
125-
query=raw_query,
126-
verbosity=verbosity)
124+
complete_query = template.format(query=raw_query, verbosity=verbosity)
127125
else:
128126
template = self._QUERY_TEMPLATE
129127
complete_query = template.format(
130-
query=raw_query,
131-
out=responseformat,
132-
verbosity=verbosity)
128+
query=raw_query, out=responseformat, verbosity=verbosity
129+
)
133130

134131
if self.debug:
135132
print(complete_query)
@@ -144,7 +141,7 @@ def _get_from_overpass(self, query):
144141
data=payload,
145142
timeout=self.timeout,
146143
proxies=self.proxies,
147-
headers={'Accept-Charset': 'utf-8;q=0.7,*;q=0.7'}
144+
headers={"Accept-Charset": "utf-8;q=0.7,*;q=0.7"},
148145
)
149146

150147
except requests.exceptions.Timeout:
@@ -160,10 +157,10 @@ def _get_from_overpass(self, query):
160157
elif self._status == 504:
161158
raise ServerLoadError(self._timeout)
162159
raise UnknownOverpassError(
163-
"The request returned status code {code}".format(
164-
code=self._status))
160+
"The request returned status code {code}".format(code=self._status)
161+
)
165162
else:
166-
r.encoding = 'utf-8'
163+
r.encoding = "utf-8"
167164
return r
168165

169166
def _as_geojson(self, elements):
@@ -185,9 +182,8 @@ def _as_geojson(self, elements):
185182
continue
186183

187184
feature = geojson.Feature(
188-
id=elem["id"],
189-
geometry=geometry,
190-
properties=elem.get("tags"))
185+
id=elem["id"], geometry=geometry, properties=elem.get("tags")
186+
)
191187
features.append(feature)
192188

193189
return geojson.FeatureCollection(features)

Diff for: overpass/queries.py

+2-7
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@ def __init__(self, south, west, north, east):
2525

2626
def __str__(self):
2727
return self._QUERY_TEMPLATE.format(
28-
west=self.west,
29-
south=self.south,
30-
east=self.east,
31-
north=self.north
28+
west=self.west, south=self.south, east=self.east, north=self.north
3229
)
3330

3431

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

4744
def __str__(self):
48-
return self._QUERY_TEMPLATE.format(
49-
query_parameters=self.query_parameters
50-
)
45+
return self._QUERY_TEMPLATE.format(query_parameters=self.query_parameters)

Diff for: setup.py

+19-21
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,25 @@
11
from setuptools import setup
22

33
setup(
4-
name='overpass',
5-
packages=['overpass'],
6-
version='0.6.0',
7-
description='Python wrapper for the OpenStreetMap Overpass API',
8-
long_description='See README.md',
9-
author='Martijn van Exel',
10-
author_email='[email protected]',
11-
url='https://github.com/mvexel/overpass-api-python-wrapper',
12-
license='Apache',
13-
keywords=['openstreetmap', 'overpass', 'wrapper'],
4+
name="overpass",
5+
packages=["overpass"],
6+
version="0.6.0",
7+
description="Python wrapper for the OpenStreetMap Overpass API",
8+
long_description="See README.md",
9+
author="Martijn van Exel",
10+
author_email="[email protected]",
11+
url="https://github.com/mvexel/overpass-api-python-wrapper",
12+
license="Apache",
13+
keywords=["openstreetmap", "overpass", "wrapper"],
1414
classifiers=[
15-
'License :: OSI Approved :: Apache Software License',
16-
'Programming Language :: Python :: 2.7',
17-
'Programming Language :: Python :: 3.3',
18-
'Programming Language :: Python :: 3.4',
19-
'Programming Language :: Python :: 3.5',
20-
'Topic :: Scientific/Engineering :: GIS',
21-
'Topic :: Utilities',
15+
"License :: OSI Approved :: Apache Software License",
16+
"Programming Language :: Python :: 2.7",
17+
"Programming Language :: Python :: 3.3",
18+
"Programming Language :: Python :: 3.4",
19+
"Programming Language :: Python :: 3.5",
20+
"Topic :: Scientific/Engineering :: GIS",
21+
"Topic :: Utilities",
2222
],
23-
install_requires=['requests>=2.3.0', 'geojson>=1.0.9'],
24-
extras_require={
25-
'test': ['pytest'],
26-
}
23+
install_requires=["requests>=2.3.0", "geojson>=1.0.9"],
24+
extras_require={"test": ["pytest"]},
2725
)

Diff for: tests/test_api.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def test_geojson():
1717

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

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

0 commit comments

Comments
 (0)