1
votes

I have bbox strategy for one source of data. Code looks like this:

bbox: function newBboxFeatureSource(url, typename) {
    return new ol.source.Vector({
        loader: function (extent) {
            let u = `${url}&TYPENAME=${typename}&bbox=${extent.join(",")}`;

            $.ajax(u).then((response) => {
                this.addFeatures(
                    geoJsonFormat.readFeatures(response)
                );
            });
        },
        strategy: ol.loadingstrategy.bbox
    });
},

I works fine but... When I pan/move the map then this loader is calling again and add another features which fit to new box. But there is a lot of duplicates then because some of new features are just the same as old. So I wanted first clear all features using this.clear() before add new features but when I add this command then loader is running all the time and I have "infinitive loop". Do you know why? How can I disable loading new features after calling this.clear()?

edit:

my response with features looks like this:

{ "type": "FeatureCollection", "crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:EPSG::3857" } }, "features": [ { "type": "Feature", "properties": { "ogc_fid": "2", "name": "AL" }, "geometry": { "type": "MultiPolygon" , "coordinates": [ [ [ ... ] ] ] } }, { "type": "Feature", "properties": { "ogc_fid": "3", "name": "B" }, "geometry": { "type": "MultiPolygon" , "coordinates": [ [ [ ...] ] ] } } ..... and so on

I've removed coordinates because there was too many of them.

My features are generated by mapserver and are configured in .map file which looks like this:

LAYER
    NAME "postcode_area_boundaries"
    METADATA
        "wfs_title"                 "Postcode area boundaries"
        "wfs_srs"                   "EPSG:3857"
        "wfs_enable_request"        "*"
        "wfs_getfeature_formatlist" "json"
        "wfs_geomtype"              "multipolygon"
        "wfs_typename"              "postcode_area_boundaries"
        "wms_context_fid"           "id"
        "wfs_featureid"             "id"
        "gml_featureid"             "id"
        "gml_include_items"         "id,postarea,wkb_geometry"
        "gml_postarea_alias"        "name"
        "ows_featureid"             "id"
        "tinyows_table"             "postcode_area_boundaries"
        "tinyows_retrievable"       "1"
        "tinyows_include_items"     "id,postarea,wkb_geometry"
    END
    TYPE POLYGON
    STATUS ON
    CONNECTIONTYPE POSTGIS
    CONNECTION "..."
    DATA "wkb_geometry FROM postcode_area_boundaries USING UNIQUE id"
    DUMP TRUE
END
1
Do you have unique ids on your features? If you get your features from a geoserver or any server check if they have unique ids, if not create the unique ids on your server and the problem should dissapear. Also, cosider using a url function instead of a loader. - pavlos
@pavlos I've inherited this code by previous developer. Changing loader into url function doesn't resolve problem. About unique ids: I've updated my questions and added example of feature response. I have unique column in db which is called ogc_fid. - nospor
Are you able to switch ogc_fid to just fid???? - pavlos
@pavlos to do this I will have to change a lot of js code... there is also hidden logic. But ok, I will try. Thank you for your answers. I will let you know when I do this :) - nospor
Just add a new column called fid within your DB, populate it with ogc_fid and do a reload on your geoserver. Good luck - pavlos

1 Answers

1
votes

To summarize the discussion and answer the initial question:

The features sent by the server need an attribute called id, which must be unique and the same for the feature on every request.

{type: "Feature", id: "some-wfs.1234", properties: { "ogc_fid": 2, ...

See this GitHub Issue for the original comment of ahocevar.

In GeoServer this can be achieved if you set an identifier in your layer. I guess there is something similar to set in MapServer.