0

i have a VectorSource built from GeoJSON, and i know how to get the extent of all the features inside the vectorSource, but i don't know how, or what the best method is, to get the extent only of certain features. Basically instead of

const featExtent = featEntrySource.getExtent();

I'm looking for something like (in pseudo code)

const featExtent = featEntrySource.getExtent(<where each feature.get('referenceid') === certainValue>);

whats the best way to do this?

2

Option 1: Use a second VectorSource

(Option 2 below is probably better. This option would likely be slow, and consume network resources, etc. But is still included as a matter of interest.)

Depending on how your GeoJSON is generated, you may be able to generate a second GeoJSON string that only includes the features of interest. Eg, if your GeoJSON comes from a query, you could modify that query to specifically include only your features of interest.

Then create a second VectorSource from this GeoJSON and simply:

featExtent = featOfInterestSource.getExtent()

(You would still retain your original GeoJSON and your original VectorSource for all other purposes and only use the new GeoJSON and new VectorSource for calculating the extent of the features of interest.)

Option 2: Combine the extents of each feature of interest

Alternatively, you could loop through each of your features and get the extent of each feature.

First, set the extent to that of the first feature of interest's geometry: featuresOfInterest[0].getGeometry().getExtent()

Then use a loop to expand this extent by the geometry extent of each of the remaining features of interest: ol.extent.extend(featExtent, featuresOfInterest[i].getGeometry().getExtent())

Putting it all together (assuming your features of interest are already in an array):

featExtent = featuresOfInterest[0].getGeometry().getExtent();
var i;
for (i = 0; i < featuresOfInterest.length; i++) { 
    ol.extent.extend(featExtent, featuresOfInterest[i].getGeometry().getExtent());
}
  • I like your option 2 more, it seems more performant in my mind than option 1? but how do you combine multiple extents into 1 overall extent? – Phil Sep 19 '18 at 23:48
  • That's what the ol.extent.extend() line does, and if you run it for each feature, it will extend the extent to include each of those features (answer is updated now to include the actual for loop). See: openlayers.org/en/latest/apidoc/module-ol_extent.html where it says that this function is to "Modify and extent to include another extent". – Son of a Beach Sep 19 '18 at 23:54
0

If I want to keep a copy of original features with no new http call(s),

I would simply filter the source features and assign filtered fatures to a variable

var filteredFeatures = vectorLayer.getSource().getFeatures()
  .filter(feature => {
    return ['CAN', 'USA', 'MEX'].includes(feature.getId());
    // could be based on feature property e.g
    // return feature.get('name') === 'Japan';
})

then I would create a second source and insert filtered features

var new_source = new ol.source.Vector();
new_source.addFeatures(filteredFeatures);

and then get the extent from this second source

console.log(new_source.getExtent());

I prefer this solution to option 2 already provided as I prefer avoiding getting manually extent via a loop whereas a source has already a getExtent method.

PS: you can play by copying/pasting the above code in the console of http://openlayers.org/en/v4.6.5/examples/vector-layer.html. I also use the syntax of standalone OpenLayers lib whereas you may use ES6 import

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.