Skip to content

OverpassAPIProvider

MKer edited this page Dec 16, 2016 · 7 revisions

Introduction

Overpass API is a super-powerful search API directly accessing to OpenSteetMap data.

OSMBonusPack OverpassAPIProvider gives you access to this API. It comes with 2 flavors: either using the simple "POI" approach, or using the KML flavor to get full geometry and tags.

OverpassAPIProvider is available from OSMBonusPack v5.0.

POI approach

OverpassAPIProvider#urlForPOISearch is a helper method to build a request for a simple tag search. Have a look at OSM Map Features to discover usual OSM tags (cinemas, fuel stations, speed cameras, shops,...).

OverpassAPIProvider#getPOIsFromUrl performs the query, and convert the features found as POI objects.

For each feature, only 1 position is retrieved, not the whole geometry. This approach is simple (homogeneous), and quite efficient (small amount of data).

You can safely get up to 500 features in one request.

Example when querying "amenity=cinema":

KML flavor

It combines the power of OverpassAPI query language, with the power of KML representation.

OverpassAPIProvider#urlForTagSearchKml(String tag, BoundingBoxE6 bb, int limit, int timeout) is a helper method to build a request for a simple tag search.

OverpassAPIProvider#addInKmlFolder(KmlFolder kmlFolder, String url) performs the query, and convert the result to KML content, that is added in the kmlFolder:

Each OSM element becomes a KML Placemark, with a KML Geometry depending on the OSM type of the element:

  • OSM node becomes a KML Point
  • OSM open way becomes a KML LineString
  • OSM closed way becomes a KML Polygon
  • OSM relation becomes a KML MultiGeometry

For each element:

  • The OSM id is copied as the KML Placemark id.
  • All OSM tags (key=value) are copied as KML Extended Data.
  • The "name" tag is copied as the KML Placemark name.

Some use cases:

  • Get cinemas => urlForTagSearchKml("amenity=cinema", bb, limit, timeout);
  • Get all shops => urlForTagSearchKml("shop", bb, limit, timeout);
  • Get buildings with their shape => urlForTagSearchKml("building", bb, limit, timeout);
  • Get all features having "website" tag => urlForTagSearchKml("website", bb, limit, timeout);

Code sample (I let you guess what we are retrieving here):

OverpassAPIProvider overpassProvider = new OverpassAPIProvider();
String url = overpassProvider.urlForTagSearchKml("highway=speed_camera", mapView.getBoundingBox(), 500, 30);
KmlDocument kmlDocument = new KmlDocument();
boolean ok = overpassProvider.addInKmlFolder(mKmlDocument.mKmlRoot, url);
FolderOverlay kmlOverlay = (FolderOverlay) kmlDocument.mKmlRoot.buildOverlay(mapView, null, null, kmlDocument);
map.getOverlays().add(kmlOverlay);
mapView.getOverlays().add(kmlOverlay);

Example when querying "building":

For more complex searches, have a look at #urlForTagSearchKml, and then you can build your own Overpass API request. You should be familiar with the OverpassQL query syntax.

To use #addInKmlFolder, your url request must ensure that:

  • Content is in JSON format => obtained with the "[out:json]" setting
  • ways and relations have the "geometry" element => obtained with the "out geom" setting.