How to style transparent overlayed WMS layer

可紊 提交于 2019-12-04 09:59:20

At first glance, it looks like this is a Mapserver 5.x implementation, and getCapabilities notes that UserDefinedSymbolization SupportSLD="1" is enabled at the Map level, which IIRC means that all Layers should inherit it.

So in theory you should be able to supply SLD either in the GET request or in a file somewhere and supply the URL of your SLD to the GET request, and be all set.

Writing the SLD will be the annoying bit, but if you think you have some SLD that should work but doesn't, paste it here.

Jpsy

Use a wrapper script that reads the image from the WMS and recolorizes it:

Wrapper

Here is an example written in PHP:

<?php
$url = $_GET['url'];
$im = imagecreatefrompng($url);

if($im && imagefilter($im, IMG_FILTER_COLORIZE, 255, 0, 0, 0)){
    // this line is only needed if original image has transparency (32bit/pixel)
    // and you want to preserve that transparency
    imagesavealpha($im, true);

    header('Content-type: image/png');
    imagepng($im);
}else{
    echo 'Conversion failed.';
}

imagedestroy($im);
exit;
?>

Then instead of calling your WMS link, you call the wrapper and pass the WMS link as a parameter (recolor_png.php?url=...). The wrapper reads the original image and returns a new PNG with a colorized version. Don't forget that the link that you pass as a parameter must be urlencoded to work correctly (all special characters replaced by their %XX hex notation). In JavaScript you can do that using the encodeURIComponent() method.

Here is a working example using your link and the above wrapper on my server:

http://www.digilog.de/pub/stackoverflow/recolor_png2.php?url=http%3A%2F%2Fgeoportal2.uhul.cz%2Fwms_oprl%2F%3FSERVICE%3DWMS%26REQUEST%3DGetMap%26SERVICE%3DWMS%26VERSION%3D1.1.1%26LAYERS%3DHMLCR%26FORMAT%3Dimage%2Fpng%3B%2520mode%3D24bit%26FGCOLOR%3D0xFF0000%26TRANSPARENT%3DTRUE%26SRS%3DEPSG%3A4326%26BBOX%3D16.58935546875%2C49.37522008143603%2C16.600341796875%2C49.38237278700955%26WIDTH%3D256%26HEIGHT%3D256%26STYLES%3D

This is the resulting image (semi-transparent version):


(source: digilog.de)

The same wrapper without semi-transparent output (imagesavealpha deleted from code):

http://www.digilog.de/pub/stackoverflow/recolor_png.php?url=http%3A%2F%2Fgeoportal2.uhul.cz%2Fwms_oprl%2F%3FSERVICE%3DWMS%26REQUEST%3DGetMap%26SERVICE%3DWMS%26VERSION%3D1.1.1%26LAYERS%3DHMLCR%26FORMAT%3Dimage%2Fpng%3B%2520mode%3D24bit%26FGCOLOR%3D0xFF0000%26TRANSPARENT%3DTRUE%26SRS%3DEPSG%3A4326%26BBOX%3D16.58935546875%2C49.37522008143603%2C16.600341796875%2C49.38237278700955%26WIDTH%3D256%26HEIGHT%3D256%26STYLES%3D

And the resulting non-transparent image:


(source: digilog.de)

I will leave that wrappers online for some days for you to test.

Caching

As this conversion is processor intensive, it might be wise to add some caching code to the wrapper:

  • create a hash code from the give URL, e.g.: $hash=md5($url)
  • check whether an image named $hash.png exists in a storage subfolder
  • if so: read the image from file and return it
  • otherwise: create image, save it as $hash.png in subfolder and also return it immediately

If you expect your WMS content to change over time: Also check the creation date of cached images and purge them if they are too old (like a month or so). So any changes to the WMS maps will ripple into your system after a maximum time of one month.

The WMS link already has some custom params that allow you to do exactly what you ask for. The link is the following:

  http://geoportal2.uhul.cz/mapserv/php/mapserv3.php?project=oprl_2011&mode=map&mapsize=256%20256&layers=HMLCR%20&x=1322616184548&map_SMO_class_0_color=0%200%200&map_HMLCR_class_0_color=255%200%200&mapext=-679915.1258015268%20-1062651.2224427482%20-679660.3694656485%20-1062461.062442748

If you inspect it you will notice that between all the url encoded parameters there is a parameter interestingly called: map_HMLCR_class_0_color

If you change its value to, say green (whose RGB code is 0,255,0), the layers are rendered in green. The color is expressed as an RGB code. There is also another parameter map_SMO_class_0_color but I don't understand what that does. Perhaps it styles some feature which is not visible in that request?

Example for green color:

 http://geoportal2.uhul.cz/mapserv/php/mapserv3.php?project=oprl_2011&mode=map&mapsize=256%20256&layers=HMLCR%20&x=1322616184548&map_SMO_class_0_color=0%20100%20200&map_HMLCR_class_0_color=0%20255%200&mapext=-679915.1258015268%20-1062651.2224427482%20-679660.3694656485%20-1062461.062442748

which produces the following:

A note con rgb color representation in Mapserver

the RGB triplet must be written as follows in the request:

R G B

(note that the blanks are required). Whose URl encoded representation is:

R%20G%20B

because %20 is how space is encoded in URLs.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!