[GIS] OpenLayers – Get features inside a polygon

intersectionopenlayers-2vector

In my OpenLayers map I allow users to draw an ad-hoc polygon vector. Then I need to get all the features from a WFS vector layer that fall within the ad-hoc polygon.

Here is the code I have that allows the user to draw the ad-hoc polygon in the map. This code works fine:

function buildIt() {
//CREATE THE NEW VECTOR 
var polygonLayer = new OpenLayers.Layer.Vector("Poly Layer");
//ADD IT TO THE MAP
map.addLayer(polygonLayer);

//CREATE A DRAWFEATURE CONTROL
var polyedit = new OpenLayers.Control.DrawFeature(
polygonLayer,//POINT IT AT THE USER DEFINED POLYGON LAYER
OpenLayers.Handler.Polygon
);

// 'point' is one of the named callbacks specified by OpenLayers.Handler.Polygon
// and is called every time a point is added to the feature being drawn
polyedit.handler.callbacks.point = function (pt) { 
    console.log(pt)
}
map.addControl(polyedit);//ADD THE DRAWFEATURE CONTROL TO THE MAP
polyedit.activate();//ACTIVATE THE DRAWFEATURE CONTROL

}

After this I am clueless. How can I get all the features that fall within the user defined polygon? Specifically, I need to get the attributes from each WFS feature (sometimes points, sometimes polygons) that fall within the user drawn ad-hoc polygon.

Any suggestions would be appreciated! Thanks.

Best Answer

After some research I was able to find a solution. This function allows a user to draw an ad-hoc polygon and then interates through another WFS layer to locate any polygons that intersect the ad-hoc polygon. In my case I used this with WFS layers served from my GeoServer.

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("nameOfTheVectorLayerYouWantToTestForIntersection");

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