2

When I am adding a point to my web map I want to give it a letter that represents the area where the point is, so I need to know what area the point is in. In OpenLayers 2 I did it like this: areas[i].geometry.containsPoint(point) ---> returns true if it does.

Is there anything like this in OL3?

I searched in the OL3 API but didn't find it.

1
  • It would be reasonably straightforward to write, following the example from OL2. Point in polygon calcs are about 10 lines of code. Commented Jan 22, 2015 at 8:41

3 Answers 3

4

You can use yourVectorLayer.getSource().getFeaturesAtCoordinate(point). The result is an array, if its length > 0 then the point is inside, otherwise it's not.

1
  • Does this really work? Which version of ol3 are you using? I think the getFeaturesCoordinate() method is knackered (ol3.11). Commented Jan 7, 2016 at 14:48
2

These kind of operations aren't supported out of the box in ol3. (maybe in the future).

But you can use a library like http://turfjs.org , in this case you'll need https://github.com/Turfjs/turf-within or https://github.com/Turfjs/turf-inside

You can use ol.format.GeoJSON to convert from a ol3-geometry and geojson for turf and back.

2
  • And, jsts. Commented Jan 22, 2015 at 8:40
  • yes that is an other option, in that case you'll need ol.format.WKT for conversion.
    – warrieka
    Commented Jan 22, 2015 at 8:47
2

The latest version as of writing is 3.16.0, has the API: ol.extent.containsXY(extent, x, y)

See more info in the latest API docs

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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