[GIS] Get features inside a polygon from a WMS Layer in OpenLayers2

feature-layerfeaturesintersectionopenlayers-2wms

i have found some answers for this and that and the most promising approach seems to be this one here:

OpenLayers – Get features inside a polygon

But i have no clue how to rewrite this to suite a WMS Layer
So i ended up with two quite different approaches where one is able to highlight Poligons from a WMS Layer and the other would read out Informations of a WFS Layer. And i need to combine the two somehow. Well here is what i have so far:

  <script>

    var controls = [
        new OpenLayers.Control.Navigation(),
        new OpenLayers.Control.PanZoomBar(),
        new OpenLayers.Control.LayerSwitcher({ 'ascending': false }),
        new OpenLayers.Control.WMSGetFeatureInfo({ 'infoFormat': 'application/vnd.ogc.wms' })
    ];



    var map = new OpenLayers.Map('map',
    {
        controls: controls,
        numZoomLevels: 24,
        projection: new OpenLayers.Projection("EPSG:900913"),
        displayProjection: new OpenLayers.Projection("EPSG:4326")
    });

    //OpenStreetMaps Base-Layer
    var wms1 = new OpenLayers.Layer.OSM("OpenStreetMaps");
    wms1.projection = new OpenLayers.Projection("EPSG:900913");
    wms1.isBaseLayer = true;

    //Another Layer
    var wms4 = new OpenLayers.Layer.WMS(
                "BOS",
                "http://localhost:8081/geoserver/test/wms",
                { layers: 'test:m_v_leitstellen', visibility: true, transparent: true, opacity: 1 },
                { isBaseLayer: false, singleTile: true, tileOptions: { maxGetUrlLength: 2048 } }
    );
    map.addLayers([wms1, wms4]);
    map.zoomToMaxExtent();


    //New Stuff
    var select = new OpenLayers.Layer.Vector("Selection", {
        styleMap: new OpenLayers.Style(OpenLayers.Feature.Vector.style["select"])
    });
    var hover = new OpenLayers.Layer.Vector("Hover");

    map.addLayers([select, hover]);

    //Another Control
    control = new OpenLayers.Control.GetFeature({
        protocol: OpenLayers.Protocol.WFS.fromWMSLayer(wms4),
        box: true,
        hover: true,
        multipleKey: "shiftKey",
        toggleKey: "ctrlKey"
    })

    control.events.register("featureselected", this, function (e) {
        select.addFeatures([e.feature]);
    });
    control.events.register("featureunselected", this, function (e) {
        select.removeFeatures([e.feature]);
    });
    control.events.register("hoverfeature", this, function (e) {
        hover.addFeatures([e.feature]);
    });
    control.events.register("outfeature", this, function (e) {
        hover.removeFeatures([e.feature]);
    });
    map.addControl(control);
    control.activate();

    var vector_style_01 = new OpenLayers.Style({
        'fillColor': '#ff0000',
        'strokeColor': '#000000',
        'strokeWidth': 3
    });

    var vector_style_02 = new OpenLayers.Style({
        'fillColor': '#ff00ff',
        'strokeColor': '#000000',
        'strokeWidth': 3
    });

    var vector_style_map_1 = new OpenLayers.StyleMap({
        'default': vector_style_01
    });

    var vector_style_map_2 = new OpenLayers.StyleMap({
        'default': vector_style_02
    });

    //set the style for both the Hover & Select
    hover.styleMap = vector_style_map_1;
    select.styleMap = vector_style_map_2;




    function buildIt() {//START FUNCTION buildIt

        //CREATE A NEW EMPTY VECTOR LAYER 
        var polygonAdHoc = new OpenLayers.Layer.Vector("Poly Layer");
        //ADD THE NEW VECTOR LAYER TO THE OPENLAYERS MAP
        map.addLayer(polygonAdHoc);
        //SET A VARIABLE TO THE NAME OF THE EXISTING LAYER THAT WILL BE TESTED FOR INTERSECTION WITH THE USER CREATED POLYGON
        //I CHOSE TO GET THE LAYER BY NAME BUT YOU MIGHT CHOOSE TO DO IT ANOTHER WAY
        var standLyr = map.getLayersByName("BOS");
        alert("Name: " + standLyr[0]);
        //CREATE A DRAW FEATURE CONTROL FOR THE USER CREATED VECTOR LAYER
        var draw = new OpenLayers.Control.DrawFeature(polygonAdHoc, OpenLayers.Handler.Polygon);
        //ADD THE DRAW FEATURE CONTROL TO THE MAP
        map.addControl(draw);
        //ACTIVATE THE DRAW FEATURE CONTROL
        draw.activate();

        //WHEN THE USER FINISHES DRAWING THE AD-HOC POLYGON THE beforefeatureadded EVENT WILL FIRE
        polygonAdHoc.events.on({
            beforefeatureadded: function (event) {
                poly = event.feature.geometry;//SET A VARIABLE TO THE USERDRAWN POLYGONS GEOMETRY
                //alert("polygonAdHoc.features[0].geometry: " + poly);//IF YOU WANT TO SEE THE GEOMETRY COORDINATES UNCOMMENT THIS LINE
                for (var a = 0; a < standLyr[0].features.length; a++) {//LOOP THRU THE STANDS FEATURES OF THE LAYER YOU WANT TO TEST FOR INTERSECTION WITH THE USER DRAWN POLYGON
                    if (poly.intersects(standLyr[0].features[a].geometry)) {//IF THE USER DRAWN POLYGON INTERSECTS THE TARGET LAYERS FEATURE REPRESENTED BY THE VARIABLE "a" THEN
                        //IDENTIFY THE FEATURE THAT INTERSECTS 
                        //FOR SIMPLICITIES SAKE I CHOSE TO JUST FIRE AN ALERT
                        //MY ACTUAL APP ADDS EACH SELECTED FEATURE TO A SELECT FEATURE CONTROL AND HIGHLIGHTS EACH POLYGON ON THE MAP                
                        alert("stands feature intersection: " + standLyr[0].features[a].attributes['nameOfAttribute']);
                    }//END OF IF STATEMENT
                }//END OF FOR STATEMENT
                draw.deactivate();//I ONLY WANT THE USER TO BE ABLE TO DRAW ONE AD-HOC POLYGON
                //SO I DEACTIVATE THE DRAW FEATURE CONTROL AFTER THEY CREATE THE FIRST POLYGON
                return false;
            }//END OF beforefeatureadded FUNCTION
        });//END OF polygonAdHoc.events.on
    }//END OF buildIt FUNCTION
  </script>

I know that standLyr[0].features… isnt working, because its not dealing with a WFS Layer. Is there any way of simple conversion of parts of a WMS Layer to a WFS Layer?

Best Answer

Is there any way of simple conversion of parts of a WMS Layer to a WFS Layer?

The answer to this question is NO

There are no features in a WMS layer, a WMS layer is an image. The WMS image may have been created from one or more feature datasets (but these only exist on the server not in the client).

At any one point location in a WMS image you can get information on the data that makes up that layer at that location (and indeed any other layers in the WMS service at that point location), this is done using a WMS GetFeatureInfo request. Such a request may or may not (depending on how the WMS server has been configured) return the geometry of the data at that location.