Monday, 29 October 2018

Zooming to WMS layer after CQL filter in OpenLayers?


I created a layer that contains points and I added it to a map as WMS layer in OpenLayers. I did a CQL filter and it works but I'd like to zoom to the selected point when I worked with getextent or getdataextent it doesn't zoom to the right place. It missed with a few Km. Here is my the code of filter:


function filterNom(){
var filterType ="cql";
var filter = document.getElementById('batima').value;
// by default, reset all filters
var filterParams = {cql_filter: null};
if (OpenLayers.String.trim(filter) != "") {

if (filterType == "cql")
filterParams["cql_filter"] = "batiment='"+filter+"'";

}
// merge the new filter definitions
wfs_layer.mergeNewParams(filterParams);
var bounds = new OpenLayers.Bounds
bounds = wfs_layer.getExtent()
map.setCenter(wfs_layer.getExtent(),18);
var center = map.getCenter();

center.transform(new OpenLayers.Projection("EPSG:4326"), new OpenLayers.Projection("EPSG:900913"));
alert(center.toString());
}

and here is the code to add the WMS layer:


  wfs_layer = new OpenLayers.Layer.WMS("Patrimoines",
"http:// localhost/geoserver/cite/wms",
{layers: 'cite:patrimoine' , transparent: true },
{isBaseLayer: false, opacity: 1, singleTile: true, visibility: true,
projection: 'EPSG:900913'});


Answer



Your wms layer is a image, so the zooming to it will not work. I set up a vector layer with a protocol and filter and then am able to zoom to the particular feature. I did some cutting and pasting but I think I got the important parts. Let me know if this is sufficient


filter = new OpenLayers.Filter.Comparison({
type: OpenLayers.Filter.Comparison.EQUAL_TO,
property: "field name",
value: 123
});

protocol = {
version: "1.1.0",

srsName: "EPSG:900913",
url: your url,
featurePrefix: "prefix",
featureNS: namespace,
featureType: featuretype,
geometryName: "geom"
};

vector_LAYER = new OpenLayers.Layer.Vector("vector", {
strategies: [new OpenLayers.Strategy.Fixed()],

styleMap: styleMap,
filter: filter,
protocol: protocol
});

vector_LAYER.events.on({
featureadded: function() {
map.zoomToExtent(vector_LAYER.getDataExtent());
}
});


Then somewhere down down in my code I set the filter and refresh the layer.


filter.value = filter;
vector_LAYER.refresh();

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...