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

1

5

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.

Cowbind answered 10/12, 2015 at 13:9 Comment(0)
T
12

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

Tintinnabulum answered 10/12, 2015 at 13:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.