Search code examples
javascriptreactjsleafletopenstreetmap

Checking if object in Open Street Maps is a building


I'm using Leaflet library in my ReactJS app and I wonder if there is a simple way to recognize if object clicked by user is a building. Idea that came up to my mind is to check map colour under clicked position. Does it make sense? I appreciate your help.

Colleagues in comments advised to give some use-case:

App I'm working on is meant to mark antique buildings with elevation in bad shape so city architecture management had simpler job of searching for them. Every user of this App can mark such building. To prevent hooligans from corrupting data with senseless points on map I wanted to validate as a first step if clicked point is a building.

I hope it will clarify problem a little bit.


Solution

  • I wonder if there is a simple way to recognize if object clicked by user is a building.

    No.

    You basically want to run arbitrary point-in-polygon queries against OSM's building dataset, and I will presume that you don't want to host that dataset yourself.

    The simplest way to do this is to perform queries to an Overpass API server, passing a is_in query and filtering by the building tag key. The OSM website's query feature functionality uses such a technique.

    With this technique you won't have to worry about hosting the data, just about creating the right Overpass API query. Please bear in mind that the Overpass API servers are run by volunteers and their resources are limited.

    The second simplest way would be to download a OSM extract of you area of interest, and run the point-in-polygon queries yourself, by whatever means you like (PostGIS' ST_Intersect, turf.js, etc etc).

    If you will be using Leaflet, another approach would be to use vector tiles, and set it up in such a way that the buildings thematic layer is interactive. This will require you to be aware of the limitations of the vector tile servers.

    Idea that came up to my mind is to check map colour under clicked position.

    That is unreliable. Think about labels on top of buildings, or the colour of the edge of the building area, or buildings that don't render with the standard colour (e.g. places of worship, monuments).