Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Openlayer 3 - check if a feature is within bounds of extent

Tags:

openlayers-3

I have a list of features and a vector layer and I need to know, whether each feature is within the bounds of the view of the map or not.

I'm using openlayers v3.9.0 and in the corresponding documentation there is a function containsExtent() (link) which takes an extent and returns a boolean. Seems to be exactly the function I'm looking for. But an error is thrown saying that containsExtent is not a function.

Uncaught TypeError: extent.containsExtent is not a function

code snippet:

// someVectorSource is of type ol.source.Vector
// allMyFeatures is an array of features of type ol.Feature

var extent = someVectorSource.getExtent();
_.each(allMyFeatures, function(feature) {
  if (extent.containsExtent(feature.getGeometry().getExtent())) {
    // do something
  }
});

What is the problem here?

If the is a better way, to get only those features which are within the extent, in a single call without iterating through the list, would be even better.

like image 234
schub Avatar asked Dec 24 '22 11:12

schub


1 Answers

The correct syntax is:

ol.extent.containsExtent(extent, feature.getGeometry().getExtent())

If you look closer at the doc page, you'll see that the method is a static one, not part of a ol.Extent object. FYI, there's no actual ol.Extent object in ol3. It's just an array of 4 numbers. I think ol.Extent is just a reference for the compiler.

HTH

like image 194
Alexandre Dubé Avatar answered Mar 18 '23 07:03

Alexandre Dubé