Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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.

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

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
share|improve this answer
add comment

Do you need the custom polygon, or is a box like in the WFS GetFeature Example good enough?

Another way would be to use the WFS - GetFeature option with a Bounding Box of your polygon. You should get the parameters of your Bounding Box with something like:

polygonLayer.features[0].geometry.bounds.bottom
polygonLayer.features[0].geometry.bounds.left
polygonLayer.features[0].geometry.bounds.right
polygonLayer.features[0].geometry.bounds.top

I am not sure, what the output will be, but I guess it would contain also the attributes of the contained features.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.