I'm trying to use Python to create a TextSymbolizer to display a trail name above the trail LineSymbolizer. Below this layer is the OSM base layer which contains its own data for that trail.

I can display the line but I cannot display any text. I've run out of ideas, Google is proving fruitless so I'm looking for new ideas, if I may be so bold.

I've compiled Mapnik with debug turned on only to find that there's no logging near the text symbolizer code. I had a go with gdb but I haven't spent enough time to workout how to debug from python into the Mapnik shared library. If anybody has any guidance for how to do this then I'd be grateful. I'm happy enough to debug but I'm not familiar with gdb.

I also edited the osm.xml and took away the trails and trail names which might be interfering with the TextSymbolizer but I didn't see any difference.

Relevant code:

    imgx = 400
    imgy = 400

    m = mapnik.Map(imgx,imgy)
    m.buffer_size = 1000

    workoutParams = dict(host='localhost',port='5432',user='postgres',password='password',dbname='postgis')
    workoutParams['estimate_extent'] = False
    workoutTable = "(select id, geometry, 'really long trail name' as trail_name from pub_trail_segment where pub_trail_id = %s) as route" % idNumber
    workoutParams['table']= workoutTable
    workoutDatasource = PostGIS(**workoutParams)
    workoutLayer = Layer('route','+proj=latlong +datum=WGS84')
    workoutLayer.datasource = workoutDatasource
    workoutStyle = mapnik.Style()
    segmentStyleRule = mapnik.Rule()
    segmentStyleRule.symbols.append(mapnik.LineSymbolizer(mapnik.Color('blue'),1)) #50 for print
    segmentTextSymbolizer = mapnik.TextSymbolizer('trail_name', 'book-fonts', 20, mapnik.Color('blue'))
    segmentTextSymbolizer.halo_radius = 1
    segmentTextSymbolizer.label_placement = mapnik.label_placement.LINE_PLACEMENT
    segmentTextSymbolizer.allow_overlap = True
    segmentTextSymbolizer.avoid_edges = False

    im = mapnik.Image(imgx,imgy)
    mapnik.render(m, im)

What am I doing wrong?

Many thanks for any ideas

Cros-posted to StackOverflow

2 Answers 2


Two options: Create a second style object and attach the text symbolizer to that style, then append that second style to the layer. Layers in Mapnik can accept any number of styles and will render them in the order attached, so if you attach the a second style with text rules, then they will be rendered second. However in the case of polygons with text most people go further and create a second layer with its own style containing text rules. This will ensure that all text is rendered after all other geometries are rendered.

  • Thanks for the response, Dane. One question: as part of my previous experiments I commented out the LineSymbolizer and still got nothing. Do you feel this changes anything in your answer?
    – Sarge
    Jun 1, 2011 at 15:59
  • Sarge, I may not have read your question correctly the first time. If you text labels are not showing up at all (vs. just being covered partially by the lines) then your problem is more subtle. Ideally you could call mapnik.save_map(m,'map.xml') and post that somewhere for me to look at, or ideally, post and email the mapnik-users list. Jun 7, 2011 at 5:14

The problem was this part in the PostGIS element

'really long trail name' as trail_name

Mapnik appeared to have a problem with it. When I replaced it with the proper join the name displayed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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