mapnik / python-mapnik

Python bindings for mapnik
GNU Lesser General Public License v2.1
157 stars 91 forks source link

Cannot set dasharray via Python bindings #209

Open roelandschoukens opened 5 years ago

roelandschoukens commented 5 years ago

Consider a simple map:

<?xml version="1.0" encoding="utf-8"?>
<Map background-color="rgb(255,255,255)">
  <Style name="dashed">
    <Rule>
      <LineSymbolizer stroke="rgb(0,0,0)" stroke-width="3" stroke-dasharray="3,3"/>
    </Rule>
  </Style>
  <Layer name="Test">
    <StyleName>dashed</StyleName>
    <Datasource>
      <Parameter name="encoding">utf8</Parameter>
            <Parameter name="inline">
id|name|wkt
1|line|LINESTRING (30 10, 10 30, 40 40)
            </Parameter>
            <Parameter name="type">csv</Parameter>
    </Datasource>
  </Layer>
</Map>

Now consider a Python script to recreate that map

import mapnik

csv = '''id|name|wkt
1|line|LINESTRING (30 10, 10 30, 40 40)'''

mmap = mapnik.Map(300,300)
mmap.background_color = mapnik.Color("white")
style = mapnik.Style()
rule = mapnik.Rule()
ls = mapnik.LineSymbolizer()
ls.stroke=mapnik.Color("blue")
ls.stroke_width=3
# ls.stroke_dasharray="3,3"
rule.symbols.append(ls)
style.rules.append(rule)
mmap.append_style("dashed",style)

layer = mapnik.Layer("Test")
layer.styles.append("dashed")
layer.datasource = mapnik.CSV(inline=csv)
mmap.layers.append(layer)
mmap.zoom_to_box(mapnik.Box2d(9, 9, 41, 41))

mapnik.render_to_file(mmap,'dashed.png', 'png')

This is all reasonably straightforward, except for the stroke array. If you try to set it the line will not be rendered.

(in general I found that using any invalid value to a property of the symbolizer will cause that symbolizer to be silently dropped. So it might be a case of not knowing what to assign to stroke_dasharray)