1

I've tried to render a few maps in Mapnik and I have a little problem. I want to render a map that contains two layers: point and polygon. I've used this script:

import mapnik
rule = mapnik.Rule()
rule.symbols.append(mapnik.PolygonSymbolizer(mapnik.Color("darkgreen")))
rule.symbols.append(mapnik.PointSymbolizer())
style = mapnik.Style()
style.rules.append(rule)
layer = mapnik.Layer("mapLayer")
layer.datasource = mapnik.Shapefile(file="polska.shp")
layer.styles.append("mapStyle")
map = mapnik.Map(640, 480)
map.background = mapnik.Color("white")
map.append_style("mapStyle", style)
map.layers.append(layer)
map.zoom_all()
mapnik.render_to_file(map, "map.png", "png")

There is only one shapefile to render, but that is my problem. When I'm adding the 4th line rule.symbols.append(mapnik.PointSymbolizer()), I always get rendered polygons with additional points (centroids of each object I guess): enter image description here

Why are there black points? Is there any possibility to render two layers (polygons and points) at the same time, without these centroids? What am I doing wrong?

  • Does your single shape file contain both points and polygons? – Ilja Zverev Aug 29 '14 at 17:22
1

I realise this is a hella-old question, but maybe someone else faces a similar problem and is scratching their heads over it.

The issue appears to be that your shapefile contains 2 layers, but you only add one mapnik layer; mapnik isnot going to be able to style two independent sets of data if it only has one layer.

Then you feed it the whole shapefile as the layer's datasource - I think mapnik.Shapefile() assumes that the shapefile only has one layer (so maybe it just yanks out the first layer?).

If there are two layers in your shapefile, and you can't access them independently using mapnik.Shapefile(), then each could be loaded into a separate layer by using mapnik.Ogr().

Assume polska.shp has two layers:

  • a polygon layer called "polylayername" and
  • a points layer called "ptlayername".

The following will give two mapnik layers from the data...

poly_lyr = mapnik.layer('polygons')
poly_lyr.datasource= mapnik.Ogr(file="polska.shp",layer="*polylayername*") 
pt_lyr = mapnik.layer('points')
pt_lyr.datasource= mapnik.Ogr(file="polska.shp",layer="*ptlayername*") 

Then go on with the styling as usual: each layer can be styled separately.

If the shapefile doesn't have two layers, you're kinda boned. But it should be possible to split out the points data (in QGIS, or using mapnik.Ogr() with layer_by_sql, 'querying' the points out by their aspatial characteristics).

| improve this answer | |
0

You should create a separate style for the points.

s = mapnik.Style() 
r = mapnik.Rule() 
point_sym = mapnik.PointSymbolizer()
point_sym.filename = './symbols/airport.png' 
r.symbols.append(point_sym) # add the PointSymbolizer to the rule object
s.rules.append(r) 
m.append_style('airport point', s)

Now instead of black dots the points will be the image found at './symbols/airport.png'. In order to move the points you would have to add a different layer with a data source containing the Point geometries. If you want to add points dynamically in your script check out mapnik.MemoryDatasource

I have to assume that the reason your points are centered is because your shape file contains both polygons and points. You could add the proper points to your shape file so that mapnik would render them automatically. If I didn't answer your question please post your shape file and we can get to the bottom of it.

| improve this answer | |
  • I think the problem is that the shapefile has more than one layer, and he's only creating one mapnik layer... so where dmh126 is expecting the points layer to be styled, mapnik is using the polygon data and trying to style it as points (and therefore using the centroid). If he creates a new layer and gives it a datasource that is the points layer from the shapefile, it should remove the 'wrong' points and put in the 'right' points. – GT. Jun 1 '15 at 10:40

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.