2

I have a Leaflet javascript web application which uses WMS to make calls to GeoServer. The returned object is Geometry plus attributes. While the Geometry (polygons) render fine as Counties in the US, I need to make the Counties layer to show different colors, based on County populations.

Here is code to load the WMS data:

     var wmscounty = L.tileLayer.wms("<?php echo  $geoserverwms_url; ?>", {
        layers: '<?php echo  $geoserverwms_layer_countypop; ?>',
        format: 'image/png',
        transparent: true,
        version: '1.1.1',
        attribution: "countypopulation"
    }
    wmscounty.addTo(map);

The code I could possibly use set the layer's styles are:

  function getColorCounties(d) {
        return d > 1000000 ? '#800026' :
        d > 50000   ? '#FED976' :
                          '#FFEDA0';
    }
   function styleCounties(feature) {
        return {
            weight: 2,

            fillColor: getColorCounties(feature.properties.COUNTY_POP)
        };
    }

I don't know how to pass the 'feature' object to the styleCounties() function? Should it be in some onAdd() function? Or some 'forEach'. There are some examples available but I can't find any for Leaflet/WMS.

Thanks!

1 Answer 1

5

I must say I'm a bit confused. WMS is a service to get raster images from the server. You don't get geometries or attributes from WMS.

Since WMS gives raster images, you can't style it with Javascript. So you can't just set weight, fill color, etc. from the Javascript, since the data has already been rendered on the server when the Javascript sees it.

However, you can set the styles parameter in the WMS request, which instructs the WMS server (GeoServer in your case) to apply a certain style to the returned images; see the GetMap reference: http://docs.geoserver.org/stable/en/user/services/wms/reference.html#getmap.

If you really want to control styling on the client, you should probably use WFS instead, which will give you the geometries in vector format, and the attributes. GeoServer can return them in GeoJSON format, which makes it easy to work with them in Leaflet. There's at least one plugin you could use for this: https://github.com/azgs/azgs-leaflet (I have not used it myself).

1
  • Liedman, yes, after I posted my Question, I did some searches and found out that WMS won't do. So I created some 'styles' in GeoServer with some 'expressions' and that did the work. I don't think I can use WFS though: The GeoJSON would be too large because it will have polygons for all the American Counties. A shape-to-GeoJSON text output generated a file of 284 mb. My guess is that WFS generated GeoJSON would be too large as well. But your answer is correct. Thanks! Apr 11, 2014 at 16:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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