1

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

K.E.
  • 818
  • 2
  • 12
  • 28
  • You could use an analysis library like turfjs ([link](https://turfjs.org)) to know if the line intersects a polygon. For example, using `lineItersect` ([link](https://turfjs.org/docs/#lineIntersect)) will return the points of intersection. If you want to use only openlayers, you could get the snapped points by comparing each coordinate of the line with each coordinate of the polygons. You can get the coordinates from the geometries using `getCoordinates`. – cabesuon Nov 07 '19 at 17:01
  • 1
    Try using `var closest = geometry.getClosestPoint(coordinate); if (closest[0] == coordinate[0] && closest[1] == coordinate[1]) { ....` – Mike Nov 07 '19 at 17:59
  • 1
    @Mike great hint, thank you. If you want to write this as answer, I would accept it. – K.E. Nov 07 '19 at 20:48

1 Answers1

2

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]) {
   ...
}
Ben
  • 17
  • 6
Mike
  • 16,042
  • 2
  • 14
  • 30