Skip to content

Commit

Permalink
http > https and some minor formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
mvexel committed Sep 18, 2017
1 parent b35ac6e commit c7b5bf8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 23 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ The wrapper will, well, wrap those.

You will get your result as a dictionary, which represents the
JSON output you would get [from the Overpass API
directly](http://overpass-api.de/output_formats.html#json>). So you
directly](https://overpass-api.de/output_formats.html#json>). So you
could do this for example:

```python
Expand All @@ -47,11 +47,11 @@ The API object takes a few parameters:

#### endpoint

The default endpoint is `http://overpass-api.de/api/interpreter` but
The default endpoint is `https://overpass-api.de/api/interpreter` but
you can pass in another instance:

```python
api = overpass.API(endpoint=http://overpass.myserver/interpreter)
api = overpass.API(endpoint=https://overpass.myserver/interpreter)
```

#### timeout
Expand All @@ -76,7 +76,7 @@ them like to normal query to the API.
#### MapQuery

This is a shorthand for a [complete ways and
relations](http://wiki.openstreetmap.org/wiki/Overpass_API/Language_Guide#Recursing_up_and_down:_Completed_ways_and_relations)
relations](https://wiki.openstreetmap.org/wiki/Overpass_API/Language_Guide#Recursing_up_and_down:_Completed_ways_and_relations)
query in a bounding box (the 'map call'). You just pass the bounding box
to the constructor:

Expand Down
49 changes: 30 additions & 19 deletions overpass/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
from .errors import (OverpassSyntaxError, TimeoutError, MultipleRequestsError,
ServerLoadError, UnknownOverpassError, ServerRuntimeError)


class API(object):
"""A simple Python wrapper for the OpenStreetMap Overpass API"""
"""A simple Python wrapper for the OpenStreetMap Overpass API."""

SUPPORTED_FORMATS = ["geojson", "json", "xml"]

# defaults for the API class
_timeout = 25 # second
_endpoint = "http://overpass-api.de/api/interpreter"
_endpoint = "https://overpass-api.de/api/interpreter"
_debug = False

_QUERY_TEMPLATE = "[out:{out}];{query}out {verbosity};"
Expand All @@ -26,45 +27,52 @@ def __init__(self, *args, **kwargs):
self._status = None

if self.debug:
# http://stackoverflow.com/a/16630836
# https://stackoverflow.com/a/16630836
try:
import http.client as http_client
except ImportError:
# Python 2
import httplib as http_client
http_client.HTTPConnection.debuglevel = 1

# You must initialize logging, otherwise you'll not see debug output.
# You must initialize logging,
# otherwise you'll not see debug output.
logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
requests_log = logging.getLogger("requests.packages.urllib3")
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True


def Get(self, query, responseformat="geojson", verbosity="body", build=True):
"""Pass in an Overpass query in Overpass QL"""

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._ConstructQLQuery(query, responseformat=responseformat, verbosity=verbosity)
full_query = self._ConstructQLQuery(query,
responseformat=responseformat,
verbosity=verbosity)
else:
full_query = query

if self.debug:
logging.getLogger().info(query)

# Get the response from Overpass
raw_response = self._GetFromOverpass(full_query)

if responseformat == "xml" or responseformat.startswith("csv"):
return raw_response

response = json.loads(raw_response)

# Check for valid answer from Overpass. A valid answer contains an 'elements' key at the root level.
# 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)
Expand All @@ -88,10 +96,15 @@ def _ConstructQLQuery(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)
complete_query = template.format(
query=raw_query,
out=responseformat,
verbosity=verbosity)

if self.debug:
print(complete_query)
Expand All @@ -110,7 +123,7 @@ def _GetFromOverpass(self, query):
timeout=self.timeout,
headers={'Accept-Charset': 'utf-8;q=0.7,*;q=0.7'}
)

except requests.exceptions.Timeout:
raise TimeoutError(self._timeout)

Expand All @@ -125,9 +138,7 @@ def _GetFromOverpass(self, query):
raise ServerLoadError(self._timeout)
raise UnknownOverpassError(
"The request returned status code {code}".format(
code=self._status
)
)
code=self._status))
else:
r.encoding = 'utf-8'
return r.text
Expand Down

0 comments on commit c7b5bf8

Please sign in to comment.