1

Whenever i think I understand encodings, I don't.. How do I correctly send a layername to the getmap function?

running python 2.7

from owslib.wms import WebMapService
wms_url = "http://ext-geodata.lansstyrelsen.se/arcgis/services/WMS/LST_WMS_riksintressen_3_5_st/mapserver/wmsserver?"

wms = WebMapService(wms_url)
my_layer = list(wms.contents)[1]

#output = u'ST Riksintresse K\xe4rnomr\xe5de  MB3kap5'

img = wms.getmap(layers=[my_layer],
                 styles=['default'],
                 srs='EPSG:3006',
                 bbox=(7000000,577000,7113000,690000),
                 size=(500, 500),
                 format='image/png',
                 transparent=True
                 )

out = open('testarea.png', 'wb')
out.write(img.read())
out.close()

When I run this, I get a UnicodeEncodeError. I understand I need to encode the layername correctly, but I fail to understand how.

l = my_layer.encode('utf-8')
img = wms.getmap(layers=[l], #etc etc...

yields

ServiceException: Parameter 'layers' contains unacceptable layer names.

How do I parse values from the contents list to layername correctly?

1

1 Answer 1

1

Try adding the following declaration at the top of your file:

# -*- coding: utf-8 -*-

Then instead of l = my_layer.encode('utf-8') use:

l = u'my_layer.encode("utf-8")'
1
  • That worked, thank you! Is there any chance you can elaborate on why this works? It is the first time I have seen the .encode('utf-8') nestled into another string, and I am not really grasping why it works. May 30, 2017 at 14:13

Your Answer

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

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