1

I used a WFS layer from GeoServer in OpenLayers it's shown on the map, but when I want to use a filter it doesn't work; I don't know where the error is.

This is the code:

<script type="text/javascript">
OpenLayers.ProxyHost = "/cgi-bin/proxy.cgi?url=";
function init() {
//set up a save strategy
var saveStrategy = new OpenLayers.Strategy.Save();

//set up projections
// World Geodetic System 1984 projection
var WGS84 = new OpenLayers.Projection("EPSG:4326");   
// WGS84 Google Mercator projection
var WGS84_google_mercator = new OpenLayers.Projection("EPSG:900913");   

//Initialize the map
//creates a new openlayers map in the <div> html element with id="map"
var map = new OpenLayers.Map ("map", {   
controls:[
//allows the user pan ability
new OpenLayers.Control.Navigation(),
//displays the pan/zoom tools                   
new OpenLayers.Control.PanZoom(),
//displays a layer switcher
new OpenLayers.Control.LayerSwitcher()   
],
//projection: WGS84_google_mercator,
displayProjection: WGS84
} );
// map extent
var mapextent =
new OpenLayers.Bounds(-7.70912,33.65164,-7.39876,33.52465).transform(WGS84, WGS84_google_mercator);

//base layers
var openstreetmap = new OpenLayers.Layer.OSM();
var google_maps = new OpenLayers.Layer.Google(
"Google Maps", {
numZoomLevels: 20
}
);
var google_satellite = new OpenLayers.Layer.Google(
"Google Satellite", {
type: google.maps.MapTypeId.SATELLITE,
numZoomLevels: 20
}
);

//wfs-t editable overlay
var wfs_layer = new OpenLayers.Layer.Vector("Editable Features", {
strategies: [new OpenLayers.Strategy.BBOX(), saveStrategy],
protocol: new OpenLayers.Protocol.WFS({
version: "1.1.0",
// loading data through localhost url path
url: "http:// localhost/geoserver/wfs",
featureNS :  "http://www.opengeospatial.net/cite",
maxExtent: mapextent,
// layer name
featureType: "patrimoine",
// geometry column name
geometryName: "geometry",
schema: "http:// localhost/geoserver/wfs/DescribeFeatureType?version=1.1.0&;typename=cite:patrimoine"
})
});    
map.addLayers([openstreetmap, google_maps, google_satellite, wfs_layer]);
map.zoomToExtent(mapextent);
}

function filter(){
            var filterType ="cql";
            //var filter = document.getElementById('zone').value;
            // by default, reset all filters
            var filterParams = {cql_filter: null};
            if (OpenLayers.String.trim(filter) != "") {
                if (filterType == "cql") 
                    filterParams["cql_filter"] = "batiment='Bastion de la sqala'";

// In this line my parametre it' batiment I made a test on the batiment wich has a value "Bastion de la sqala"               

            }
            // merge the new filter definitions
            mergeNewParams(filterParams);

        }

        function mergeNewParams(params){
            Region.mergeNewParams(params);

        }

and when I type in console map.layers[2].features it show me an error:

Cannot read property '2' of undefined
  • Not sure if its related, by why is the bottom of your <a href="dev.openlayers.org/docs/files/OpenLayers/BaseTypes/…> (33.65164) greater than the top (33.52465)? – BradHards Apr 22 '14 at 22:04
  • Probably the reason for the error is that OpenLayers generates key value pair requests with both BBOX and CQL_FILTER parameters. According to WFS standard only one of BBOX, FILTER and FEATUREID parameters is acceptable. In your case CQL_FILTER behaves like FILTER. You must remove the BBOX parameter and add the corresponding spatial filter inside FILTER / CQL_FILTER. Unfortunately I do not know how to do it with OpenLayers. – user30184 May 28 '14 at 11:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.