admin管理员组文章数量:1400028
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!
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!
Share Improve this question asked Apr 9, 2014 at 15:37 IrfanClemsonIrfanClemson 1,7796 gold badges35 silver badges56 bronze badges1 Answer
Reset to default 6I 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/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./azgs/azgs-leaflet (I have not used it myself).
本文标签: javascriptLeafletjs WMS Layer StylesStack Overflow
版权声明:本文标题:javascript - Leaflet.js: WMS Layer Styles - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744107151a2591118.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论