[GIS] display multiple vector layers from single PostGis table in GeoServer using OpenLayers — REWORDED

geoserveropenlayers-2postgis

Prompting suggestions I’m going to attempt to reword my question

I have a PostGis table that contains overlapping vector polygons. I need to display a selection in Geoserver and provide functionality for the user to toggle on & off unwanted polygons.

I need to add multiple layers from a PostGis Table.

I’ve made a start with the code below but I’m stuck with the following:

  • Why are all the layers Base Layers? Land should be the only base
    layer the rest overlays?

  • Can all overlay layers be on at once?

  • How do I move the layerswitcher control outside the map area? When I
    click inside the map how do I show the attribution?

  • How do I create a new layer by script either with a polygon given a
    co-ordinate string, or a point and radius?

  • How do I create a list of all the selected layers?

    body {
    margin: 1em;
    }
    #map {
    width: 800px;
    height: 475px;
    border: 1px solid black;
    }

    OpenLayers.IMAGE_RELOAD_ATTEMPTS = 3;

        var map;
        function init(){
            map = new OpenLayers.Map('map');
            var Base = new OpenLayers.Layer.WMS(
                "Land",
                "http://localhost:8080/geoserver/cite/wms",
                {LAYERS: 'cite:land',
                 format: 'image/png',
                 isBaseLayer: true
    
                }
            );
    
            var L10299 = new OpenLayers.Layer.WMS(
                "10299",
                "http://localhost:8080/geoserver/cite/wms",
                {
                VIEWPARAMS: 'kathy:10299',
                LAYERS: 'cite:Q_Results',
                isBaseLayer: false
                }
            );
    
    
            var L43352 = new OpenLayers.Layer.WMS(
                "43352",
                "http://localhost:8080/geoserver/cite/wms",
                {
                VIEWPARAMS: 'kathy:43352',
                LAYERS: 'cite:Q_Results',
                isBaseLayer: false
                }
            );
    
    
            var L25177 = new OpenLayers.Layer.WMS(
                "25177",
                "http://localhost:8080/geoserver/cite/wms",
                {
                VIEWPARAMS: 'kathy:25177',
                LAYERS: 'cite:Q_Results',
                isBaseLayer: false
                }
            );
    
    
            map.addLayers([Base, L10299, L43352, L25177]);
    
    
    
            map.addControl(new OpenLayers.Control.LayerSwitcher());
    
            map.zoomToExtent(
                new OpenLayers.Bounds(12.996, 52.172,13.931, 52.958)
            );
        }
    </script>
    

    OpenLayers PostGis Layers

Best Answer

Yes it's possible.

Starting with GeoServer 2.1.0 the user can also create a new layer by specifying a raw SQL query

You can create SQL Views which reference the same table using SQL Statements.

Instructions and Documentation

Update Per OpenLayers question in comment:

It really depends on how you want to serve out your data which depends on what kind and how much data you have.

If the data is vector data and not more than +-300 features you can use WFS. Add that layer like so: OpenLayers GeoServer WFS Layer Example

If the data is Raster data or you just want your layer served out as an image rather than vector features, then add that layer using WMS. Like so: OpenLayers WMS Layer Example

Update #2:

Based on your latest comment, what you can do is: add multiple wfs layers to your open layers map which point to ONE layer on your GeoServer but have a filter defined so you only get ONE feature back based on ID or some other attribute in your filter.

Take a look at this example.

Basically it will be something like:

var wfs = new OpenLayers.Layer.Vector("WFS", {
    strategies: [new OpenLayers.Strategy.BBOX()],
    protocol: new OpenLayers.Protocol.WFS({
        url: "http://demo.opengeo.org/geoserver/wfs",
        featureType: "tasmania_roads",
        featureNS: "http://www.openplans.org/topp"
    }),
    filter: new OpenLayers.Filter.Logical({
        type: OpenLayers.Filter.Logical.And,
        filters: [
            new OpenLayers.Filter.Comparison({
                type: OpenLayers.Filter.Comparison.EQUAL_TO,
                property: "id",
                value: "POLYGONID"
            })
        ]
    })
});
Related Question