nospor
nospor

Reputation: 4220

Openlayers bbox strategy

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

Upvotes: 1

Views: 1552

Answers (2)

oshistory
oshistory

Reputation: 11

Following up on @bennos comment: you can expose the fid in Mapserver using the FORMATOPTION USE_FEATUREID=true as described in: https://mapserver.org/output/ogr_output.html

USE_FEATUREID=true/false

Starting from MapServer v7.0.2. Defaults to false. Include feature ids in the generated output, if the ows_featureid metadata key is set at the layer level. The featureid column to use should be an integer column. Useful if you need to include an “id” attribute to your geojson output. Use with caution as some OGR output drivers may behave strangely when fed with random FIDs.

    OUTPUTFORMAT
      NAME "application/json"
      DRIVER "OGR/GEOJSON"
      MIMETYPE "application/json"
      FORMATOPTION "FORM=SIMPLE"
      FORMATOPTION "FILENAME=ol-query.json"
      FORMATOPTION "STORAGE=memory"
      FORMATOPTION "USE_FEATUREID=true"
    END  

Upvotes: 1

bennos
bennos

Reputation: 323

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.

Upvotes: 1

Related Questions