I need to be able to merge features on the condition that the selected features are adjacent to each other.

How can I achieve this using Open Layers version 5?

  • 1
    What exactly do you mean by 'adjecent'? Whate have you tried till now? – TomazicM Oct 14 '19 at 17:10
  • @TomazicM I've just done research haven't tried anything yet. And by adjacent I mean as "having a common vertex and a common side." – ohryan Oct 14 '19 at 17:22

You can select the geometries on front end using openlayers and then write a postgres query to check if they intersect with each other by using ST_Intersects and apply ST_UNION if the intersects is true. This works if the backend is postgres. If you use any other DB, use appropriate spatial queries and return the result to front-end for providing appropriate user information

| improve this answer | |
  • Ok cool, I think that's the right direction. For adjacent features you would probably use ST_Touches instead. (I previously didn't know about these methods so thank you). I'm still wondering how you'd achieve this completely client side – ohryan Oct 14 '19 at 17:21
  • 3
    You might be able to use turf.js client side. Touching features should return a result for intersect but the area of the result would be zero. – Mike Oct 14 '19 at 17:48

Using turf.js#intersect method you can find out if two features share a border.

from their docs:

Takes two polygons and finds their intersection. If they share a border, returns the border; if they don't intersect, returns undefined.

Using this, you can convert open layer features to turf.js#polygonand feed them to the intersect method. If the return value is not null, the items are adjacent (share a border) or intersect.

However this will not distinguish between whether they intersect or just share a border.

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.