NOTICE: is no longer in use from 1st March 2024. Please use the OpenStreetMap Community Forum

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:

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

accept rate: 0%

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

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))


permanent link

answered 17 Jul '17, 11:02

timojuez's gravatar image

accept rate: 0%

edited 17 Jul '17, 11:05

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text]( "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:


question asked: 07 Apr '17, 22:38

question was seen: 5,377 times

last updated: 26 Apr '20, 13:07

NOTICE: is no longer in use from 1st March 2024. Please use the OpenStreetMap Community Forum