Search code examples
geolocationopenstreetmaplatitude-longitude

Find if a point (latitude, longitude) is within OpenStreetMaps "way" (area)


I have a list of longitude and latitude points for various houses, offices, etc. I am trying to split them up to determine if they are inside a certain Way or not. I don't want to use the old "centre point" of an area and then radius value as that is not accurate enough.

So for example if I had 4 locations in an Way like "Richmond Upon Thames" that looks like this: enter image description here

It should return just point B and C. Is this possible using Open Street Maps API?


Solution

  • If you like Java, you could load the way as a Polygon and use the JTS (Java Topology Suite) library, or the AWT library to compute whether your points are inside or not.

    Here is an example of how the Atlas library uses a combination of both in that specific case. For you it would look like this:

    1. Convert each latitude/longitude pair of the Way to a Location object
    2. Add each Location to a List and create a new Polygon with it
    3. Call the Polygon.fullyGeometricallyEncloses(Location) method on that Polygon with each of the points of interest you have

    The Atlas library is available in Maven Central for you to download.