2

I have used Openlayers 3 to give me a bounding box extent and then make a WFS call to Geoserver using those extents and returning a geoJSON and it has worked perfectly, giving me the features within the bounding box from the complete dataset.

Now I have a geoJSON that already is a complete dataset and a local file and not coming back as WFS call. Is it possible to get select features from that geoJSON based off the OL3 bounding box function?

2

You can use the function getFeaturesInExtent(extent), after you read your local geojson file.

So this code should work for you:

vectorLayer = new ol.layer.Vector({
              title: 'Layer',
              source: new ol.source.GeoJSON({
                  projection : 'EPSG:3857',
                 url: 'local_geojson_data.json'
})}) ; //layer viewing the whole data

var selected_features = vectorLayer.getSource.getFeaturesInExtent([minx, miny, maxx, maxy]);
//select only features in extent

selected_vectorLayer = new ol.layer.Vector({
              title: 'selected_Layer',
              source: new ol.source.Vector({
                  features : selected_features
})}) ; //draw those features in a new layer
  • I am using v3.3.0 of OL3 and when I call geoJSONobject.features.getFeaturesInExtent('bbox extents') it says "getFeaturesInExtent is not a function" could this be because it is still experimental? – Tim May 29 '15 at 15:52
  • 1
    The GeoJSON object is not an OL3 class, just a simple, native JavaScript object. You have to load the features first in a vector source, and call getFeaturesInExtent() on the source object. – Gabor Farkas May 29 '15 at 16:23
  • i've explained more my answer to give a better understanding – geogeek May 29 '15 at 17:39

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.