Search code examples
openlayersopenlayers-5

Check if coordinates are on the boundary of a feature in OL


I am using OpenLayers 5 https://openlayers.org/ and cannot find a solution how to check if given coordinates are on the boundary of a feature.

I played with map.getFeaturesAtPixel and geometry.intersectsCoordinate, but there is always the problem that the functions also return true if the coordinates are IN the feature. I only want the result to be true if the coordinates are ON the boundary of the feature.

Background: The user can draw lines on a map and connect them with other features (therefore I use ol.interaction.Snap). When saving I want to have the features which were 'snapped' bei the user. I cannot find an event or something else what tells me what features where snapped. Therefore I was trying to find a solution to self extract the features which are connected to the new line, but nothing works.

I hope the picture makes it clear. The user draws the new line (blue), the line snaps in the two polygones. After drawing the line, I want to read the two features.

Any help is welcome!

enter image description here


Solution

  • In the absence of any more obvious built-in method checking if the closest point on the geometry is the same as the coordinate, this should work:

    var closest = geometry.getClosestPoint(coordinate);
    if (closest[0] == coordinate[0] && closest[1] == coordinate[1]) {
       ...
    }