overpass-0.5.6/0000755€ž¸S€GlÄ 0000000000012756636250014263 5ustar martijnv00000000000000overpass-0.5.6/overpass/0000755€ž¸S€GlÄ 0000000000012756636250016125 5ustar martijnv00000000000000overpass-0.5.6/overpass/__init__.py0000644€ž¸S€GlÄ 0000000054512756424337020243 0ustar martijnv00000000000000# -*- coding: utf-8 -*- """Thin wrapper around the OpenStreetMap Overpass API.""" __title__ = 'overpass' __version__ = '0.1.0' __license__ = 'Apache 2.0' from .api import API from .queries import MapQuery, WayQuery from .errors import ( OverpassError, OverpassSyntaxError, TimeoutError, MultipleRequestsError, ServerLoadError, UnknownOverpassError ) overpass-0.5.6/overpass/api.py0000644€ž¸S€GlÄ 0000001171212756424337017253 0ustar martijnv00000000000000import requests import json import geojson from .errors import (OverpassSyntaxError, TimeoutError, MultipleRequestsError, ServerLoadError, UnknownOverpassError, ServerRuntimeError) class API(object): """A simple Python wrapper for the OpenStreetMap Overpass API""" SUPPORTED_FORMATS = ["geojson", "json", "xml"] # defaults for the API class _timeout = 25 # seconds _endpoint = "https://overpass-api.de/api/interpreter" _debug = False _QUERY_TEMPLATE = "[out:{out}];{query}out {verbosity};" _GEOJSON_QUERY_TEMPLATE = "[out:json];{query}out body geom;" def __init__(self, *args, **kwargs): self.endpoint = kwargs.get("endpoint", self._endpoint) self.timeout = kwargs.get("timeout", self._timeout) self.debug = kwargs.get("debug", self._debug) self._status = None if self.debug: import httplib import logging httplib.HTTPConnection.debuglevel = 1 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""" # Construct full Overpass query if build: full_query = self._ConstructQLQuery(query, responseformat=responseformat, verbosity=verbosity) else: full_query = 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. if "elements" not in response: 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'): raise ServerRuntimeError(overpass_remark) if responseformat is not "geojson": return response # construct geojson return self._asGeoJSON(response["elements"]) def Search(self, feature_type, regex=False): """Search for something.""" raise NotImplementedError() def _ConstructQLQuery(self, userquery, responseformat, verbosity): raw_query = str(userquery) if not raw_query.endswith(";"): raw_query += ";" if responseformat == "geojson": template = self._GEOJSON_QUERY_TEMPLATE 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) if self.debug: print(complete_query) return complete_query def _GetFromOverpass(self, query): """This sends the API request to the Overpass instance and returns the raw result, or an error.""" payload = {"data": query} try: r = requests.post( self.endpoint, data=payload, timeout=self.timeout, headers={'Accept-Charset': 'utf-8;q=0.7,*;q=0.7'} ) except requests.exceptions.Timeout: raise TimeoutError(self._timeout) self._status = r.status_code if self._status != 200: if self._status == 400: raise OverpassSyntaxError(query) elif self._status == 429: raise MultipleRequestsError() elif self._status == 504: raise ServerLoadError(self._timeout) raise UnknownOverpassError( "The request returned status code {code}".format( code=self._status ) ) else: r.encoding = 'utf-8' return r.text def _asGeoJSON(self, elements): features = [] for elem in elements: elem_type = elem["type"] if elem_type == "node": geometry = geojson.Point((elem["lon"], elem["lat"])) elif elem_type == "way": points = [] for coords in elem["geometry"]: points.append((coords["lon"], coords["lat"])) geometry = geojson.LineString(points) else: continue feature = geojson.Feature( id=elem["id"], geometry=geometry, properties=elem.get("tags")) features.append(feature) return geojson.FeatureCollection(features) overpass-0.5.6/overpass/errors.py0000644€ž¸S€GlÄ 0000000217112756424337020015 0ustar martijnv00000000000000class OverpassError(Exception): """An error during your request occurred. Super class for all Overpass api errors.""" pass class OverpassSyntaxError(OverpassError, ValueError): """The request contains a syntax error.""" def __init__(self, request): self.request = request class TimeoutError(OverpassError): """A request timeout occurred.""" def __init__(self, timeout): self.timeout = timeout class MultipleRequestsError(OverpassError): """You are trying to run multiple requests at the same time.""" pass class ServerLoadError(OverpassError): """The Overpass server is currently under load and declined the request. Try again later or retry with reduced timeout value.""" def __init__(self, timeout): self.timeout = timeout class UnknownOverpassError(OverpassError): """An unknown kind of error happened during the request.""" def __init__(self, message): self.message = message class ServerRuntimeError(OverpassError): """The Overpass server returned a runtime error""" def __init__(self, message): self.message = message overpass-0.5.6/overpass/queries.py0000644€ž¸S€GlÄ 0000000235112756424337020156 0ustar martijnv00000000000000# -*- coding: utf-8 -*- class MapQuery(object): """Query to retrieve complete ways and relations in an area.""" _QUERY_TEMPLATE = "(node({south},{west},{north},{east});<;);" def __init__(self, south, west, north, east): """ Initialize query with given bounding box. :param bbox Bounding box with limit values in format west, south, east, north. """ self.west = west self.south = south self.east = east self.north = north def __str__(self): return self._QUERY_TEMPLATE.format( west=self.west, south=self.south, east=self.east, north=self.north ) class WayQuery(object): """Query to retrieve a set of ways and their dependent nodes satisfying the input parameters""" _QUERY_TEMPLATE = "(way{query_parameters});(._;>;);" def __init__(self, query_parameters): """Initialize a query for a set of ways satisfying the given parameters. :param query_parameters Overpass QL query parameters""" self.query_parameters = query_parameters def __str__(self): return self._QUERY_TEMPLATE.format( query_parameters=self.query_parameters ) overpass-0.5.6/overpass.egg-info/0000755€ž¸S€GlÄ 0000000000012756636250017617 5ustar martijnv00000000000000overpass-0.5.6/overpass.egg-info/dependency_links.txt0000644€ž¸S€GlÄ 0000000000112756636250023665 0ustar martijnv00000000000000 overpass-0.5.6/overpass.egg-info/PKG-INFO0000644€ž¸S€GlÄ 0000000132412756636250020714 0ustar martijnv00000000000000Metadata-Version: 1.1 Name: overpass Version: 0.5.6 Summary: Python wrapper for the OpenStreetMap Overpass API Home-page: https://github.com/mvexel/overpass-api-python-wrapper Author: Martijn van Exel Author-email: m@rtijn.org License: Apache Description: See README.md Keywords: openstreetmap,overpass,wrapper Platform: UNKNOWN Classifier: License :: OSI Approved :: Apache Software License Classifier: Programming Language :: Python :: 2.6 Classifier: Programming Language :: Python :: 2.7 Classifier: Programming Language :: Python :: 3.3 Classifier: Programming Language :: Python :: 3.4 Classifier: Programming Language :: Python :: 3.5 Classifier: Topic :: Scientific/Engineering :: GIS Classifier: Topic :: Utilities overpass-0.5.6/overpass.egg-info/requires.txt0000644€ž¸S€GlÄ 0000000005612756636250022220 0ustar martijnv00000000000000requests>=2.3.0 geojson>=1.0.9 [test] pytest overpass-0.5.6/overpass.egg-info/SOURCES.txt0000644€ž¸S€GlÄ 0000000037512756636250021510 0ustar martijnv00000000000000setup.cfg setup.py overpass/__init__.py overpass/api.py overpass/errors.py overpass/queries.py overpass.egg-info/PKG-INFO overpass.egg-info/SOURCES.txt overpass.egg-info/dependency_links.txt overpass.egg-info/requires.txt overpass.egg-info/top_level.txtoverpass-0.5.6/overpass.egg-info/top_level.txt0000644€ž¸S€GlÄ 0000000001112756636250022341 0ustar martijnv00000000000000overpass overpass-0.5.6/PKG-INFO0000644€ž¸S€GlÄ 0000000132412756636250015360 0ustar martijnv00000000000000Metadata-Version: 1.1 Name: overpass Version: 0.5.6 Summary: Python wrapper for the OpenStreetMap Overpass API Home-page: https://github.com/mvexel/overpass-api-python-wrapper Author: Martijn van Exel Author-email: m@rtijn.org License: Apache Description: See README.md Keywords: openstreetmap,overpass,wrapper Platform: UNKNOWN Classifier: License :: OSI Approved :: Apache Software License Classifier: Programming Language :: Python :: 2.6 Classifier: Programming Language :: Python :: 2.7 Classifier: Programming Language :: Python :: 3.3 Classifier: Programming Language :: Python :: 3.4 Classifier: Programming Language :: Python :: 3.5 Classifier: Topic :: Scientific/Engineering :: GIS Classifier: Topic :: Utilities overpass-0.5.6/setup.cfg0000644€ž¸S€GlÄ 0000000014412756636250016103 0ustar martijnv00000000000000[metadata] description-file = README.md [egg_info] tag_build = tag_date = 0 tag_svn_revision = 0 overpass-0.5.6/setup.py0000644€ž¸S€GlÄ 0000000171112756635764016006 0ustar martijnv00000000000000from codecs import open as codecs_open from setuptools import setup, find_packages setup( name='overpass', packages=['overpass'], version='0.5.6', 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.6', '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'], } )