[OSM-dev-fr] un petit coin sur le svn
Nicolas Dumoulin
nicolas_openstreetmap.org at dumoulin63.net
Mar 1 Fév 10:05:32 GMT 2011
Bonjour,
Est-ce que il y aurait un petit coin sur le svn, ou ailleurs pour publier un peu de python ?
C'est quelques fonctions pour mapnik que je me suis concocté pour alléger mes scripts de création de carte.
Pour info, voilà en brut (sans commentaires :-/ ) :
def addPGLayer(m, name, table, symbolizer, dbname="osm", projection="+proj=latlong +datum=WGS84"):
addLayer(m, name, symbolizer,
mapnik.PostGIS(host='localhost',port='5434',user='mapnik',password='mapnik',dbname=dbname,table=table), projection)
def addLayer(m, name, symbolizer, datasource, projection):
if symbolizer:
# création du style
s = mapnik.Style()
r = mapnik.Rule()
r.symbols.extend(symbolizer)
s.rules.append(r)
m.append_style(name,s)
# création de la couche
layer = mapnik.Layer(name, projection)
layer.datasource = datasource
layer.styles.append(name)
m.layers.append(layer)
return layer
def addLinesLayer(m, name, table, color, width,opacity=None):
return addPGLayer(m, name, table, [line(color,width,opacity)])
def addLinesLayerWithContour(m, name, table, color, width, contoursColor, contoursWidth):
addPGLayer(m, name+"-contours", table, [line(contoursColor,width+contoursWidth)])
addPGLayer(m, name, table, [line(color,width)])
def line(color,width,opacity=None):
stk = mapnik.Stroke()
stk.color=mapnik.Color(color)
stk.width=width
stk.line_join = stk.line_join.ROUND_JOIN
stk.line_cap = stk.line_cap.ROUND_CAP
if opacity: stk.opacity = opacity
ls = mapnik.LineSymbolizer(stk)
return ls
def dashedLine(color,width,*dashArray):
# create new stroke, otherwise dashes are not taken in account
stk = mapnik.Stroke()
stk.color=mapnik.Color(color)
stk.width=width
stk.line_join = stk.line_join.BEVEL_JOIN
for dash in dashArray:
stk.add_dash(dash[0],dash[1])
ls = mapnik.LineSymbolizer(stk)
return ls
def polygon(color,opacity=None):
ps = mapnik.PolygonSymbolizer(mapnik.Color(color))
if opacity: ps.fill_opacity = opacity
return ps
def text(size, color, haloFill, haloRadius, placement=mapnik.label_placement.POINT_PLACEMENT, bold=False):
font = 'DejaVu Sans Book'
if bold: font = 'DejaVu Sans Bold'
ts = mapnik.TextSymbolizer('name',font, size, mapnik.Color(color))
ts.halo_fill = mapnik.Color(haloFill)
ts.halo_radius = haloRadius
ts.label_placement = placement
ts.wrap_width=120
ts.allow_overlap=True
ts.force_odd_labels=True
ts.max_char_angle_delta=40.0
ts.minimum_distance=130.0
return ts
def cairoDraw(maps, map_uri, imgx, imgy):
# With cairo
import cairo, math
surface = cairo.ImageSurface (cairo.FORMAT_ARGB32, imgx, imgy)
ctx = cairo.Context(surface)
for m in maps:
mapnik.render(m, ctx)
surface.write_to_png (map_uri)
--
Nicolas Dumoulin
http://wiki.openstreetmap.org/wiki/User:NicolasDumoulin
More information about the dev-fr
mailing list