Wednesday, 8 November 2017

OpenLayers WFS protocol query after vector layer creation


I want to be able to create a layer on init, then load features from a WFS protocol afterward. I'm having trouble doing this.


Using WFS protocol in vector constructor works fine and is not my problem: http://dev.openlayers.org/releases/OpenLayers-2.10/examples/wfs-filter.js


What i want to do is Add a layer like such:


var layer = new OpenLayers.Layer.Vector("Results", {
projection: new OpenLayers.Projection("EPSG:4326")

});

Then Query it using the WFS Protocol.... this is what i can't get to work... what i have so far is:


var protocol = new OpenLayers.Protocol.WFS({
version: "1.1.0",
url: MapBase.Configuration.GeoServer.WFS_Service.EndPointURL,
featurePrefix: MapBase.Configuration.GeoServer.WFS_Service.featurePrefix,
featureType: MapBase.Configuration.GeoServer.WFS_Service.featureType,
featureNS: MapBase.Configuration.GeoServer.WFS_Service.featureNS,
geometryName: MapBase.Configuration.GeoServer.WFS_Service.geometryName

});


var response = protocol.read({
maxFeatures: Search.Properties.maxResults,
callback: function (resp) {
console.log(resp);
layer.addFeatures(resp.features);
}
});


resp.features gets populated fine. My main problem is I can't find where to specify my filter and projection as i can in the Vector constructor. Even though i specified the projection in the vector constructor, when i addFeatures from my WFS response, they come back in a different projection because they end up really small spooning the west coast of Africa. Same thing with the filter. I don't see that WFS protocol alone supports applying a filter? It only seems to work in conjunction with a vector layer constructor.


This seems wrong though because the WFS protocol as described by the OGC support the filter and when openlayers submits it's WFS request via the vector constructor, it does indeed include it just fine. There should be a way to specify projection and filter in the WFS protocol alone?


EDIT #1:


What i have now:


var protocol = new OpenLayers.Protocol.WFS({
version: "1.1.0",
url: MapBase.Configuration.GeoServer.WFS_Service.EndPointURL,
featurePrefix: MapBase.Configuration.GeoServer.WFS_Service.featurePrefix,
featureType: MapBase.Configuration.GeoServer.WFS_Service.featureType,

featureNS: MapBase.Configuration.GeoServer.WFS_Service.featureNS,
geometryName: MapBase.Configuration.GeoServer.WFS_Service.geometryName,
srsName: "EPSG:4326"
// ,
// defaultFilter: myFilter
});

protocol.mergeWithDefaultFilter(Search.GetWFSSpatialFilters());

Answer



You should be able to set a defaultFilter, or once the protocol is created you can use mergeWithDefaultFilter.



var protocol = new OpenLayers.Protocol.WFS({
version: "1.1.0",
url: MapBase.Configuration.GeoServer.WFS_Service.EndPointURL,
featurePrefix: MapBase.Configuration.GeoServer.WFS_Service.featurePrefix,
featureType: MapBase.Configuration.GeoServer.WFS_Service.featureType,
featureNS: MapBase.Configuration.GeoServer.WFS_Service.featureNS,
geometryName: MapBase.Configuration.GeoServer.WFS_Service.geometryName,
srsName: "EPSG:4326",
defaultFilter: myFilter
});


protocol.mergeWithDefaultFilter(myNewFilter);

For the projection you can add in the following for WFS 1.1.0:


srsName: "EPSG:4326"

A few more details here.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...