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.

I need to know how to display a WFS layers, but not the whole content, only the part within a rectangle.

I have this code:

var SMap = new OpenLayers.Layer.Vector("Status map", {
    strategies: [new OpenLayers.Strategy.BBOX()],
    protocol: new OpenLayers.Protocol.WFS({
        url: "http://demo.opengeo.org/geoserver/wfs",
        featureType: "states"
    }),
});
SMap.setVisibility(true);
map.addLayers([SMap]);

And I also have two coordinates from this code:

controlS = new OpenLayers.Control();
OpenLayers.Util.extend(controlS, {
    draw: function () {
        this.box = new OpenLayers.Handler.Box(controlS,
            {"done": this.notice},
            {keyMask: OpenLayers.Handler.MOD_SHIFT});
            this.box.activate();
    },
    notice: function (bounds) {
        var ll = map.getLonLatFromPixel(new OpenLayers.Pixel(bounds.left, bounds.bottom)); 
        var ur = map.getLonLatFromPixel(new OpenLayers.Pixel(bounds.right, bounds.top)); 
        alert(ll.lon.toFixed(4) + ", " + 
              ll.lat.toFixed(4) + ", " + 
              ur.lon.toFixed(4) + ", " + 
              ur.lat.toFixed(4));
    }
});
map.addControl(controlS);

What can I do to display only the WFS layer's content within the imaginary box formed by 2 pairs of coordinates?

Thanks and regards, Rafael

share|improve this question
add comment

1 Answer 1

I found the solution. I put the code here because I think it can be very useful.

controlS = new OpenLayers.Control();
OpenLayers.Util.extend(controlS, {
    draw: function () {
          this.box = new OpenLayers.Handler.Box(controlS,
              {"done": this.notice},
              {keyMask: OpenLayers.Handler.MOD_SHIFT});
              this.box.activate();
    },
    notice: function (bounds) {
    var ll = map.getLonLatFromPixel(new OpenLayers.Pixel(bounds.left, bounds.bottom)); 
    var ur = map.getLonLatFromPixel(new OpenLayers.Pixel(bounds.right, bounds.top)); 
    var SMap = new OpenLayers.Layer.Vector("Status map", {
        strategies: [new OpenLayers.Strategy.BBOX()],
        filter: new OpenLayers.Filter.Spatial({ 
            type: OpenLayers.Filter.Spatial.BBOX, 
        value: new OpenLayers.Bounds(ll.lon,ll.lat,ur.lon,ur.lat),
        }),
        protocol: new OpenLayers.Protocol.WFS({
            url: "http://demo.opengeo.org/geoserver/wfs",
        featureType: "states"
        }),
    });
    SMap.setVisibility(true);
    map.addLayers([SMap]);
    }
});
map.addControl(controlS);
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.