0

Using OpenLayers 5.3.3

  1. Draw a circle
  2. Get the extent of the circle in the drawend event:
    const extent = event.feature.getGeometry().getExtent();

  3. The try to determine the features in the extent using forEachFeatureInExtent

  4. The collection of features returned include features that are outside the boundaries of the circle.

The same behavior occurs for an irregularly shaped polygon. However, for a drawn box it appears to work correctly.

Why is the forEachFeatureInExtent not correctly returning the features in the specified extent? What has to be done differently for a circle or irregularly shaped polygon?

  • 1
    Definition of geometry extent could be stated also as: The smallest rectangle aligned with coordinate axes that contains the whole geometry. This explains behavior you are getting. I don't have a lot of experience with OpenLayers, but as far as I know, there is no built in method for checking if one geometry contains the other one. For this you can for example use Turf.js library. – TomazicM Jan 3 at 18:59
1

Even a box would not work correctly if the view was rotated (see the box selection example which has been updated for version 6 https://openlayers.org/en/latest/examples/box-selection.html)

If you are attempting to select Point features with a circle you could consider the initial result a shortlist and check each for intersection with the circle geometry:

const circle = event.feature.getGeometry();
const selected = [];
source.forEachFeatureInExtent(circle.getExtent(), function(feature) {
  if (circle.intersectsExtent(feature.getGeometry().getExtent()) {
    selected.push(feature);
  }
});
  • 1
    According to the OpenLayers docs feature.getExtent() is not a method. Do you mean feature.getGeometry().getExtent() instead? – ChrisP Jan 8 at 17:51
  • Yes, it must be accessed via the geometry – Mike Jan 8 at 19:00
  • So this works for points but not for testing if a polygon is w/in a polygon/circle. Comparing the extent of a polygon provides a rectangular representation which does not accurately reflect the borders of the polygon. Would one have to compare all the coordinates of the polygon to determine if they are in the circle/polygon? – ChrisP Jan 10 at 17:43
  • OpenLayers doesn't have a method to test if geometries intersect unless one of them is a point. Edges might intersect a circle even if the vertices don't so you would need to use a third party tool such as turf. – Mike Jan 10 at 17:50
  • Any insights as to what methods in Turf would be applicable to this problem? I'm a relative newcomer to the geospatial area. – ChrisP Jan 10 at 18:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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