[GIS] OpenLayers pan to CQL filter result

cql-filtergeoserverhtmlopenlayers-2

I'm new to OpenLayers and looking for some help. I would like to filter my shapefile with CQL and display the result. (that's actually working 🙂

The Problem is that I don't know how to pan the map to the result.
So here is my code. I would be overgrateful for your help!

suche = new OpenLayers.Layer.WMS(
    "nkGIS:HauptAdressen - Tiled", "http://localhost:8080/geoserver/nkGIS/wms",
    {
        LAYERS: 'nkGIS:HauptAdressen',
        STYLES: '',
        transparent: "true",
        format: 'image/png',
    },
    {
        buffer: 0,
        displayOutsideMaxExtent: true,
        isBaseLayer: false,
        yx : {'EPSG:900913' : false}
    } 
);

map.addLayers([suche]);

osm = new OpenLayers.Layer.OSM();
map.addLayers([poi]);

map.addLayers([osm]);

poi.setVisibility(false);
suche.setVisibility(false);

// support GetFeatureInfo
map.events.register('click', map, function (e) {
    document.getElementById('nodelist').innerHTML = "Loading... please wait...";
    var params = {
        REQUEST: "GetFeatureInfo",
        EXCEPTIONS: "application/vnd.ogc.se_xml",
        BBOX: map.getExtent().toBBOX(),
        SERVICE: "WMS",
        INFO_FORMAT: 'text/html',
        QUERY_LAYERS: map.layers[0].params.LAYERS,
        FEATURE_COUNT: 50,
        Layers: 'nkGIS:HauptAdressen',
        WIDTH: map.size.w,
        HEIGHT: map.size.h,
        format: format,
        styles: map.layers[0].params.STYLES,
        srs: map.layers[0].params.SRS};

        // handle the wms 1.3 vs wms 1.1 madness
        if(map.layers[0].params.VERSION == "1.3.0") {
            params.version = "1.3.0";
            params.j = parseInt(e.xy.x);
                    params.i = parseInt(e.xy.y);
                } else {
                    params.version = "1.1.1";
                    params.x = parseInt(e.xy.x);
                    params.y = parseInt(e.xy.y);
                }

                // merge filters
                if(map.layers[0].params.CQL_FILTER != null) {
                    params.cql_filter = map.layers[0].params.CQL_FILTER;
                } 

                OpenLayers.loadURL("http://localhost:8080/geoserver/nkGIS/wms", params, this, setHTML, setHTML);
                OpenLayers.Event.stop(e);

            });
        }

    function updateFilter(){
        if(pureCoverage)
            return;

        var filterType = "cql";
        var str = "SName LIKE'" + document.getElementById('str').value +"'" + "AND " + "HNR LIKE'" + document.getElementById('nr').value +"'";

        // by default, reset all filters
        var filterParams = {
            str: null,
            cql_filter: null,
            featureId: null
        };
        if (OpenLayers.String.trim(str) != "") {
            if (filterType == "cql") 
                filterParams["cql_filter"] = str;
        }
        // merge the new filter definitions
        mergeNewParams(filterParams);
        suche.setVisibility(true)
    }


    function resetFilter() {
        if(pureCoverage)
          return;

        document.getElementById('str').value = "";
        updateFilter();
    }

    function mergeNewParams(params){
        suche.mergeNewParams(params);
    } 

Best Answer

As far as I know, it doesn't work with WMS.

I've also recently started working the OpenLayers, with GeoServer. I work for a County in n'rn Wisconsin, and wanted a map that would filter by parcel number and zoom to the filtered parcels. I searched Google a couple days, and came to the conclusion it didn't work with WMS. Might be because WMS just requests an image... The request sends the bounding box. According to the OpenLayers API documentation, WMS doesn't have a getDataExtent() function.

So, I switched to WFS. Here is a sample map I've been working on. It takes the pin= URL parameter and filters to a LIKE comparison. (View the page source to see more.) On the GeoServer side, I set a WFS request limit of 100 features on the parcels layer so that somebody couldn't download the vector data for the almost 18,000 parcels.

The zoom happens in the last line of the script...

wfs.events.register('loadend', wfs, function(evt){map.zoomToExtent(wfs.getDataExtent())});

It waits for the event of the layer to finish loading, then zooms the map to the layer extent.

Related Question