1

I wrote a little Python script script which, given a GPS position and a radius, draw an image with the help of mapnik, with all the roads contained in the bounding box centered in the GPS position with the given radius. Now, all the street have the same stroke-width, meaning that are rendered without caring of the road type. I need to do render the roads in a more realistical way, respecting the real size (a little residential road is different from an highway!). Do you have any advice to do this automatically (without defining a stroke width for each road type)? Thank you all!

Here's the code which deals with mapnik:

map_output = 'mymap.png'    
m = mapnik.Map(picResolution, picResolution, "+init=epsg:3857")

m.background = mapnik.Color("#000000")
highway = mapnik.LineSymbolizer()
highway.stroke = mapnik.Color("#ffffff")
#stroke_rate = 0.013
highway.stroke_width = stroke_rate*picResolution

f = mapnik.Expression("[highway]")

rules = mapnik.Rule()
rules.symbols.append(highway)

style = mapnik.Style()
style.rules.append(rules)
m.append_style("highways", style)

layer = mapnik.Layer("highways", "+init=epsg:4326")
ds = mapnik.Shapefile(file=shapein)
layer.datasource = ds
layer.styles.append("highways")

m.layers.append(layer)

#left bottom right top
bbox=bbox_helper.boundingBox(gps[0], gps[1], radius * 1e-3)
bbox=(mapnik.Box2d(bbox[1],bbox[0],bbox[3],bbox[2]))

# Calculate projected boundaries
prj = mapnik.Projection("+init=epsg:3857")
wgs84 = mapnik.Projection('+init=epsg:4326')
tr = mapnik.ProjTransform(wgs84, prj)
bbox = tr.forward(bbox)

print "bbox transformed: ", bbox.__str__()

m.zoom_to_box(bbox)

#print "Scale = ", m.scale(), " Scale denominator = ", m.scale_denominator()
mapnik.render_to_file(m, map_output)
  • Why 'without defining a stroke width for each road type'? That's what maps like OSM do all the time. If by 'in a more realistical way' yous meant 'as wide as the real road is', you're in entering a land of trouble for two reasons: * Data usually don't include this. * Depending on your projection, you will have to compensate any stretching that might be happening; for instance, with web mercator, a 6m road in the equator will look thinner than the same road close to the poles. – Marcos Dione Sep 23 '21 at 07:46

0 Answers0