0

I'm using a combination of OpenGeo Server components and hand written GeoExt code to create a map interface. I would like to have a ComboBox that queries the OpenGeo PostGIS database in order to create a list of counties in a state. As the user begins typing the ComboBox searches the store of counties to narrow down to the one they are looking for. Upon selecting the county I would like to change the style of the layer in order to show the selected county.

Using the GeoExplorer I am able to create various styles in a single layer that relate to certain rules. For example:

I have a layer that references 100 counties in a state. The layer is made up of 100 different shape geometries, each of which defines a single county. I have one layer style that outlines the counties like this:

<sld:NamedLayer>
<sld:Name>opengeo_CountiesNC_OSM</sld:Name>
<sld:UserStyle>
  <sld:Name>opengeo_CountiesNC_OSM</sld:Name>
  <sld:Title>A transparent stroked polygon style</sld:Title>
  <sld:IsDefault>1</sld:IsDefault>

I have another layer style that refers to only a specific county and outlines it. This style uses a rule "Match: Name = Durham" in order to determine which county to outline.

<sld:NamedLayer>
<sld:Name>County Lines_5f024dcb</sld:Name>
<sld:UserStyle>
  <sld:Name>County Lines_5f024dcb</sld:Name>
  <sld:Title>Durham</sld:Title>
  <sld:Abstract>County lines for only Durham County</sld:Abstract>
  <sld:FeatureTypeStyle>
    <sld:Name>name</sld:Name>

I would like to have a style that represents each of the counties in the layer so that they could be individually selected and shown on the map. Rather than have 100 different layers I believe it is easier to have 100 styles.

Is there some way to change which style of a layer is being displayed via GeoExt or OpenGeo JavaScript code? Having a listener on the ComboBox to read the selected record and display the corresponding layer seems ideal. I'm not sure what access I have to the layer styles that are kept in the GeoServer from the web application side of things. What calls can I make in JS to appropriately retrieve and display data?

Edit When creating a WMS Layer there is an optional param "styles" that allows for an array of String style names:

new OpenLayers.Layer.WMS("County Lines",
                "geoserver/wms",
                {layers: "opengeo:County Lines",
                    transparent: true,
                    styles:[
                        "opengeo_CountiesNC_OSM",
                        "County Lines_Durham"
                    ]},
                {
                    visibility: true,
                    displayInLayerSwitcher: false,
                    opacity: .7
                }
            )

With multiple styles imported it should be possible to switch between them?

EDIT 2

Ended up doing something similar to the WMS Store. Because I already have a Google Geocoder I discovered that I could hook into the results of that ComboBox to determine when a county had been search for. Once a search had been performed the results can be trimmed and inserted into a merge request.

if(str.match(/(\D\s)+County, NC, USA/))
                    {
                        console.log("Match! It's a county");
                        console.log("County Lines_" + str.substring(0, str.indexOf('C')));
                        mapPanel.map.layers[2].mergeNewParams({styles: "County Lines_" + str.substring(0, str.indexOf('C'))});}
0

Yes the styles parameter is the way to switch between named styles, you can use mergeNewParams for this on the layer itself. To get a list of all available styles, you can parse them from the WMS GetCapabilities response. GeoExt should have this info available for you in the WMSCapabilitiesStore.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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