This is a static archive of our old OpenStreetMap Help Site. Please post any new questions and answers at community.osm.org.

How to detect if point is inside a building polygon with overpass ?

3

I am trying to figure out how to detect if lat/lng pair is inside a building polygon. So far I have something like this: http://overpass-turbo.eu/s/ocv

This code gets me the nearest but it also gets me the closest buildings, which I don't want - to avoid that I have made the around parameter as small as possible. Is there a better solution ?

asked 07 Apr '17, 22:38

krizzz's gravatar image

krizzz
56113
accept rate: 0%


One Answer:

0

There is a solution using Python and gis_geometrics [1]. Then create your Python file in the same directory as gis_geometrics.py:

from gis_geometrics import OSM_Polygon, Overpass

First you need to get your building polygon in gis_geometrics. There are several methods for this: Either you use an Overpass answer. Execute the Overpass query in Python using overpy. Then create a buildings dict with Overpass.getBuildingNodes() and pass each element of its items to the constructor of OSM_Polygon.

Or you prefer to get the building using it's way id (e.g. WAY_ID=265258282):

import overpy
api = overpy.Overpass()
building = Overpass.getPolygonByWayId(api, WAY_ID)

See also alternative methods to find a building (Overpass.getBuildingByRelationId, Overpass.getBuildingsByBB, OSM_Polygon.getPolygonByCoords).

Then you can do awesome operations on the building:

if building.isInPolygon(LAT,LON): print("%f,%f is inside the building."%(LAT,LON))

[1] https://github.com/timojuez/home/blob/master/mylib/gis_geometrics.py

answered 17 Jul '17, 11:02

timojuez's gravatar image

timojuez
1
accept rate: 0%

edited 17 Jul '17, 11:05

Source code available on GitHub .