Mapnik ShieldSymbolizer

978 views
Skip to first unread message

Jared

unread,
Aug 21, 2008, 7:18:07 PM8/21/08
to Cascadia Users of Geospatial Open Source
The presentation on Mapnik really got me interested so I started
playing around. Could anyone point out some good/any documentation or
better yet an example on how to use the ShieldSymbolizer? Also, where
are the Python API docs? I am primarily a Java programmer and I can't
find anything resembling JavaDocs for the python bindings.

Thanks!
Jared

Dane Springmeyer

unread,
Aug 22, 2008, 2:02:37 PM8/22/08
to Jared, Cascadia Users of Geospatial Open Source
Hi Jared,

Good questions.

I will answer inline...

On Aug 21, 2008, at 4:18 PM, Jared wrote:

>
> The presentation on Mapnik really got me interested so I started
> playing around.


Great, so you were able to install mapnik without a problem? It would
be great to know your system and method to get a sense of how you
faired.

> Could anyone point out some good/any documentation or
> better yet an example on how to use the ShieldSymbolizer?

Right, the ShieldSymbolizer sounds more tricky that is actually is.
All is it is kind of a combination of a PointSymbolizer with both text
and a graphic. A good example of the SheildSymbolizer is the
OpenStreetMap usage:

http://openstreetmap.org/?lat=47.6611&lon=-122.3346&zoom=14&layers=B00FTF

For highways they use a specific color and size rectangle

motorway_shield4.png

Jared

unread,
Aug 26, 2008, 6:12:00 PM8/26/08
to Cascadia Users of Geospatial Open Source
Thanks Dane!

I installed Mapnik on a windows machine using the win32 binaries. I
did discover that you can only install these binaries at C:
\mapnik-0_5_1 otherwise it complains that it can't find the font
files. I first tried to install on my D:\ drive. It appears that the
font directory is hard coded somewhere.

I got the ShieldSymbolizer working in the XML format using your code
and then figured out how its done in Python by looking at the C++ code
in the Mapnik Trac subversion repository.

shield = ShieldSymbolizer('NAME','DejaVu Sans Bold',
6,Color('#000000'),'images/ushighway_shield_20.png','png',20,20)

The parameters are:
name, font name, font size, font color, image file, image type, width,
height

To avoid having a lot of shield symbols close together, you can
specify the minimum_distance attribute like this:

shield.minimum_distance = 50

I am not sure what units that is in, but 50 worked well for me.

Again thank you for the help!

Jared

On Aug 22, 11:02 am, Dane Springmeyer <dane.springme...@gmail.com>
wrote:
> http://openstreetmap.org/?lat=47.6611&lon=-122.3346&zoom=14&layers=B0...
>
> For highways they use a specific color and size rectangle
>
>  motorway_shield4.png
> < 1KViewDownload
>
> as a png graphic, and then add text on top of it. You can get more  
> fancy with the graphic, but you will have to create it yourself.  
> Mapnik does the rendering combination but DOES NOT have any prebuilt  
> graphics or symbols.
>
> So, assuming your are rendering data from PostGIS in WGS 84  
> projection, to set this up you would download a sample 'shield'  
> rectangle png graphic from here:
>
> http://svn.openstreetmap.org/applications/rendering/mapnik/symbols/mo...
>
> then create a your XML mapfile like this while filling in the ALL_CAPS  
> values:
>
> <?xml version="1.0" encoding="utf-8"?>
> <!DOCTYPE Map>
> <Map bgcolor="white" srs="+proj=latlong +datum=WGS84">
>
>    <Style name="roads-text">
>      <Rule>
>        <ShieldSymbolizer name="FIELD_NAME_TO_LABEL" face_name="DejaVu  
> Sans Bold" size="11" fill="#809bc0" placement="line" file="/PATH/TO/
> YOUR/symbols/motorway_shield4.png" type="png" width="31" height="17"  
> min_distance="100"/>
>      </Rule>
>    </Style>
>
>    </Layer>
>     <Layer name="roads text labels" status="on" srs="+proj=latlong  
> +datum=WGS84">
>       <StyleName>roads-text</StyleName>
>       <Datasource>
>        <Parameter name="type">postgis</Parameter>
>        <Parameter name="host">localhost</Parameter>
>        <Parameter name="port">5432</Parameter>
>        <Parameter name="user">postgres</Parameter>
>        <Parameter name="dbname">YOUR_POSTGIS_DATABASE</Parameter>
>        <Parameter name="table">YOUR_POSTGIS_ROADS_TABLE </Parameter>
>      </Datasource>
>    </Layer>
>
> </Map>
>
> Then read that into your python script and you should be able to  
> render your field value (a road name or id) on top of/within the  
> shield symbol.
>
> >  Also, where
> > are the Python API docs?  I am primarily a Java programmer and I can't
> > find anything resembling JavaDocs for the python bindings.
>
> There are not currently API docs, infortunately. Luckily the mapnik  
> python bindings are fairly simple and straightforward names, so  
> experiementing with sample code and doing:
> $ python
>  >>>import mapnik
>  >>>dir(mapnik)
>  >>>help(mapnik)
>
> will get you basic api help.
>
> Beyond that what I like to do is in a shell run:
>
> $ pydoc -p 8080
>
> and then open a browser and go to :
>
> http://localhost:8080/mapnik.html
>
> which will give you python-style api docs. Then click on the '_mapnik'  
> link to see the C++ api that is exposed within python. Docstrings and  
> code comments are wanting, so consider contributing patches when you  
> figure out what a function does!
>
> Cheers,
>
> Dane
>
> > Thanks!
> > Jared
> >
Reply all
Reply to author
Forward
0 new messages