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, 22:38

krizzz's gravatar image

krizzz
41113
accept rate: 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

permanent link

answered 17 Jul, 11:02

timojuez's gravatar image

timojuez
1
accept rate: 0%

edited 17 Jul, 11:05

Your answer
toggle preview

Follow this question

By Email:

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

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "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:

×238
×155

question asked: 07 Apr, 22:38

question was seen: 312 times

last updated: 17 Jul, 11:05

powered by OSQA