slevin
slevin

Reputation: 3888

Setting maxFeatures on vector openlayer layer

So I use Openlayers 3.9.0 and I use a loader to get a vector layer from Geoserver. Here is the code that currently works.

    var sourceVector = new ol.source.Vector({
        format: new ol.format.GeoJSON(),
        loader: function (extent) {
            $.ajax('http://localhost:5550/geoserver/mymap/wfsservice=WFS&version=1.0.0&request=GetFeature&typeName=mymap:mylayer&outputFormat=application/json', 
            {type: 'GET'})
            .done(      
             function(response) {
                        var geojsonFormat = new ol.format.GeoJSON({});
                        sourceVector.addFeatures(geojsonFormat.readFeatures(response,{dataProjection :projection,featureProjection : projection}));
                    })
            .fail(function () {alert("BAD");});
        },
        strategy: new ol.loadingstrategy.tile(ol.tilegrid.createXYZ({maxZoom: 20}))
    });

By setting a maxFeatures (...&maxFeatures=50&...) to my url I dont get all the features. Does this means that if I zoom in I will see more features and if I zoom out I will see less? Is maxFeatures related to the bbox and renders features according to the current map view and zoom levels? Or this isnt the concept? Because in my case , I always see a fixed number of features.

Thanks

Upvotes: 0

Views: 604

Answers (1)

Alvin Lindstam
Alvin Lindstam

Reputation: 3142

The loader function of an ol.source.Vector is called with an extent, a resolution and a projection. The semantic expectation is that the loader function is responsible for loading all features within that extent.

ol.source.Vector maintains a history of all loaded extents, and will not try to load an extent that is within the already loaded extents.

So if you use the tile loading strategy at a low zoom level, and your maxFeatures causes some features to be ignored, zooming in will not make them appear (because loading that extent should already have been done be the parent tile load).

Because of this, using the WFS parameter maxFeatures with ol.source.Vector is generally a bad idea. If you really need to limit the number of features per request, consider limiting your layer to higher zoom levels or making several request for each loader call.

Additionally, your loader code does not use the extent parameter, making each request identical. Your loaders only responsibility is to load the features within the given extent. If you want to load all features independently of their location, use the all loading strategy.

Upvotes: 3

Related Questions