mapnik / mapnik

Mapnik is an open source toolkit for developing mapping applications

Home Page:http://mapnik.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Use of advanced functions/math in XML expressions (e.g. transform/PointSymbolizer and format/TextSymbolizer)

Sieboldianus opened this issue · comments

I have a stylesheet that uses expression language to symbolize Points and Text based on values in a field.

For exmaple:

<Style name="sample">
  <Rule>
    <PointSymbolizer 
        allow-overlap="yes" 
        file="../data-test/circle.svg" 
        transform="scale(0.05+([Join_Count]/1000))"/>
  </Rule>
</Style>

The circle size is mapped to the field [Join_Count].

Similar can be done for TextSymbolizer using the <Format /> tag:

<Style name="sample2">
    <Rule>
        <TextSymbolizer placement="point">
            <Format size="6+[Weights]*0.18">[ImpTag]</Format>
        </TextSymbolizer>
    </Rule>
</Style>

Here, the text size is calculated from the field [Weights], based on the expression 6+[Weights]*0.18">[ImpTag].

Issue:

I would like to use some more advanced expressions, such as the square root. I tried math.Sqrt(), sqrt(), ^0.5, &radic;, &srt;, **0.5, SQRT() etc. and all return errors on stylesheet load:

Traceback (most recent call last):
File "xml.py", line 15, in
mapnik.load_map(m, stylesheet)
RuntimeError: boost::spirit::qi::expectation_failure

Since the stylesheet seems to be evaluated on mapnik.load_map(m, stylesheet), I explicitly added an import to math:

import mapnik
import math 

stylesheet = 'tyle.xml'
image = 'tyle.png'
m = mapnik.Map(200, 100)
mapnik.load_map(m, stylesheet)
m.zoom_all() 
mapnik.render_to_file(m, image)

.. but I get the same error.

One exception is the pow() function (Python), where I get no error but the map does not show results/the resultinmg values do not seem to be taken into account (e.g. pow([Field], 0.5)).

My alternative is to rewrite the shapefile, or move to PostGis/DB and calculate the field in SQL. I would prefer, however, to do this at map renderering time.