本文整理汇总了Python中mapnik2.render函数的典型用法代码示例。如果您正苦于以下问题:Python render函数的具体用法?Python render怎么用?Python render使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了render函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: index
def index(self):
x = cherrypy.request.headers['x']
y = cherrypy.request.headers['y']
z = cherrypy.request.headers['z']
img = mapnik.Image()
img.background = self._mapnik_map.background
if not x or not y or not z:
error_msg = "Failed request:\n\tx: %s\n\ty: %s\n\tz: %s\n" % (x, y, z)
cherrypy.reponse.headers["Status"] = "400"
return error_msg
else:
# NOTE: Process request correctly
x = int(x)
y = int(y)
z = int(z)
envelope = self.merc.xyz_to_envelope(x,y,zoom)
if self.envelope.intersects(envelope):
self._mapnik_map.zoom_to_box(envelope)
self._mapnik_map.buffer_size = self.buffer_size
mapnik.render(self._mapnik_map, img)
cherrypy.reponse.headers["Status"] = "200"
return img.tostring('png256')
示例2: rpc_renderArea
def rpc_renderArea(self, minlat, minlon, maxlat, maxlon):
"""
Renders a map for the given coordin
"""
Map = None
LandmassShapefile = 'cape/Interface/ne_110m_admin_0_countries.shp'
merc = mapnik.Projection(
'+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m [email protected] '
'+no_defs +over')
# long/lat in degrees, aka ESPG:4326 and "WGS 84"
longlat = mapnik.Projection('+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs')
# can also be constructed as:
#longlat = mapnik.Projection('+init=epsg:4326')
im = mapnik.Image(self.mapsize)
m = self.rendermap
m.srs = merc.params()
bbox = mapnik.Box2d(minlat, minlon, maxlat, maxlon)
transform = mapnik.ProjTransform(longlat, merc)
merc_bbox = transform.forward(bbox)
m.zoom_to_box(merc_bbox)
mapnik.render(m, im)
Map = im.tostring('png')
return (True, Map)
示例3: render_tile
def render_tile(self, x, y, z):
# Calculate pixel positions of bottom-left & top-right
p0 = (x * TILE_SIZE, (y + 1) * TILE_SIZE)
p1 = ((x + 1) * TILE_SIZE, y * TILE_SIZE)
# Convert to LatLong (EPSG:4326)
l0 = self.tileproj.fromPixelToLL(p0, z)
l1 = self.tileproj.fromPixelToLL(p1, z)
# Convert to map projection (e.g. mercator co-ords EPSG:900913)
c0 = self.prj.forward(mapnik.Coord(l0[0], l0[1]))
c1 = self.prj.forward(mapnik.Coord(l1[0], l1[1]))
# Bounding box for the tile
if hasattr(mapnik, "mapnik_version") and mapnik.mapnik_version() >= 800:
bbox = mapnik.Box2d(c0.x, c0.y, c1.x, c1.y)
else:
bbox = mapnik.Envelope(c0.x, c0.y, c1.x, c1.y)
render_size = TILE_SIZE
self.m.resize(render_size, render_size)
self.m.zoom_to_box(bbox)
self.m.buffer_size = 128
# Render image with default Agg renderer
im = mapnik.Image(render_size, render_size)
mapnik.render(self.m, im)
self.writer.write(x, y, z, im)
示例4: render
def render(self, filename, tile_x, tile_y, zoom):
"""
Render a single tile to a given filename.
"""
print 'Rendering %s' % (filename)
# Calculate pixel positions of bottom-left & top-right
half_width = self.width / 2
half_height = self.height / 2
px0 = (tile_x * self.width, (tile_y + 1) * self.height)
px1 = ((tile_x + 1) * self.width, tile_y * self.height)
# Convert tile coords to LatLng
ll0 = self.tile_projection.fromPixelToLL(px0, zoom);
ll1 = self.tile_projection.fromPixelToLL(px1, zoom);
# Convert LatLng to map coords
c0 = self.map_projection.forward(mapnik2.Coord(ll0[0], ll0[1]))
c1 = self.map_projection.forward(mapnik2.Coord(ll1[0], ll1[1]))
# Create bounding box for the render
bbox = mapnik2.Box2d(c0.x, c0.y, c1.x, c1.y)
self.mapnik_map.zoom_to_box(bbox)
self.mapnik_map.buffer_size = self.buffer_size
# Render image with default renderer
image = mapnik2.Image(self.width, self.height)
mapnik2.render(self.mapnik_map, image)
image.save(filename, self.filetype)
示例5: get_one
def get_one(self, tile):
bbox = self.tilegrid.extent(tile.tilecoord, self.buffer)
bbox2d = mapnik.Box2d(bbox[0], bbox[1], bbox[2], bbox[3])
size = tile.tilecoord.n * self.tilegrid.tile_size + 2 * self.buffer
self.mapnik.resize(size, size)
self.mapnik.zoom_to_box(bbox2d)
if self.output_format == "grid":
grid = mapnik.Grid(self.tilegrid.tile_size, self.tilegrid.tile_size)
for n, l in enumerate(self.mapnik.layers):
if l.name in self.layers_fields:
mapnik.render_layer(self.mapnik, grid, layer=n, fields=self.layers_fields[l.name])
encode = grid.encode("utf", resolution=self.resolution)
if self.drop_empty_utfgrid and len(encode["data"].keys()) == 0:
return None
tile.data = dumps(encode)
else:
# Render image with default Agg renderer
im = mapnik.Image(size, size)
mapnik.render(self.mapnik, im)
tile.data = im.tostring(self.output_format)
return tile
示例6: drawTile_bbox
def drawTile_bbox(self,bbox,imgx,imgy,fname):
"""Call Mapnik to draw a map image for data in
bounding box bbox. The image size is imgx by imgy pixels, and
the output filename is fname.
29sep2009 GJ ORIGINAL VERSION, based on generate_image.py
06jan2010 GJ Renamed to drawTile_bbox from drawTile
"""
mapfile = str(self.preferences_list['mapfile'])
map_uri = str(fname)
print ("mapfile=%s" % mapfile)
# png image
m = mapnik.Map(imgx,imgy)
mapnik.load_map(m,mapfile)
m.zoom_to_box(bbox)
im = mapnik.Image(imgx,imgy)
if HAS_MAPNIK2:
# We apply the scale factor to the map
print 'using scale factors from mapnik2'
scale_factor = float( self.preferences_list['dpi'] ) / 72
print "Scale factor is: ", scale_factor, "================="
mapnik.render(m, im, scale_factor)
else:
print 'NOT using scale_factors'
mapnik.render(m, im)
view = im.view(0,0,imgx,imgy) # x,y,width,height
view.save(map_uri,'png')
示例7: render_tile
def render_tile(self, tile_uri, x, y, z):
# Calculate pixel positions of bottom-left & top-right
p0 = (x * 256, (y + 1) * 256)
p1 = ((x + 1) * 256, y * 256)
# Convert to LatLong (EPSG:4326)
l0 = self.tileproj.fromPixelToLL(p0, z)
l1 = self.tileproj.fromPixelToLL(p1, z)
# Convert to map projection (e.g. mercator co-ords EPSG:900913)
c0 = self.prj.forward(mapnik.Coord(l0[0], l0[1]))
c1 = self.prj.forward(mapnik.Coord(l1[0], l1[1]))
# Bounding box for the tile
if hasattr(mapnik, "mapnik_version") and mapnik.mapnik_version() >= 800:
bbox = mapnik.Box2d(c0.x, c0.y, c1.x, c1.y)
else:
bbox = mapnik.Envelope(c0.x, c0.y, c1.x, c1.y)
render_size = 256
self.m.resize(render_size, render_size)
self.m.zoom_to_box(bbox)
self.m.buffer_size = 128
# Render image with default Agg renderer
im = mapnik.Image(render_size, render_size)
mapnik.render(self.m, im)
# Ensure the tile isn't blank before saving
if not im.tostring("png") == self.blankTileStr:
im.save(tile_uri, "png256")
# Return True if the tile was saved
return True
else:
return False
示例8: get
def get(self):
mapfile = os.path.join(DATA_PATH, 'hazard', 'flood.shp')
#mapfile = os.path.join(DATA_PATH, 'impact', 'impact.json')
filename = os.path.join(DATA_PATH, 'pdf', 'report_map.pdf')
filename2 = os.path.join(DATA_PATH, 'pdf', 'report_map.png')
map = mapnik2.Map(600, 400)
map.background = mapnik2.Color('steelblue')
s = mapnik2.Style()
r = mapnik2.Rule()
polygon_symbolizer = mapnik2.PolygonSymbolizer(mapnik2.Color('#f2eff9'))
r.symbols.append(polygon_symbolizer)
line_symbolizer = mapnik2.LineSymbolizer(mapnik2.Color('rgb(50%,50%,50%)'),0.1)
r.symbols.append(line_symbolizer)
s.rules.append(r)
map.append_style('My Style',s)
ds = mapnik2.Shapefile(file=mapfile)
layer = mapnik2.Layer('world')
layer.datasource = ds
layer.styles.append('My Style')
map.layers.append(layer)
map.zoom_all()
mapnik2.render_to_file(map, filename2, 'png')
#bbox = mapnik.Envelope(-180.0,-90.0,180.0,90.0)
#map.zoom_to_box(bbox)
surface = cairo.PDFSurface(filename, map.width, map.height)
mapnik2.render(map, surface)
surface.finish()
with open(filename) as data:
pdf = data.read()
data.close()
self.set_header("Content-Type", "application/pdf")
self.write(pdf)
示例9: test_raster_warping_does_not_overclip_source
def test_raster_warping_does_not_overclip_source():
lyrSrs = "+init=epsg:32630"
mapSrs = '+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs'
lyr = mapnik2.Layer('dataraster', lyrSrs)
if 'gdal' in mapnik2.DatasourceCache.instance().plugin_names():
lyr.datasource = mapnik2.Gdal(
file = '../data/raster/dataraster.tif',
band = 1,
)
sym = mapnik2.RasterSymbolizer()
sym.colorizer = mapnik2.RasterColorizer(mapnik2.COLORIZER_DISCRETE, mapnik2.Color(255,255,0))
rule = mapnik2.Rule()
rule.symbols.append(sym)
style = mapnik2.Style()
style.rules.append(rule)
_map = mapnik2.Map(256,256, mapSrs)
_map.background=mapnik2.Color('white')
_map.append_style('foo', style)
lyr.styles.append('foo')
_map.layers.append(lyr)
_map.zoom_to_box(mapnik2.Box2d(3,42,4,43))
im = mapnik2.Image(_map.width,_map.height)
mapnik2.render(_map, im)
# save a png somewhere so we can see it
save_data('test_raster_warping_does_not_overclip_source.png',
im.tostring('png'))
assert im.view(0,200,1,1).tostring()=='\xff\xff\x00\xff'
示例10: test_raster_warping
def test_raster_warping():
lyrSrs = "+init=epsg:32630"
mapSrs = '+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs'
lyr = mapnik2.Layer('dataraster', lyrSrs)
if 'gdal' in mapnik2.DatasourceCache.instance().plugin_names():
lyr.datasource = mapnik2.Gdal(
file = '../data/raster/dataraster.tif',
band = 1,
)
sym = mapnik2.RasterSymbolizer()
sym.colorizer = mapnik2.RasterColorizer(mapnik2.COLORIZER_DISCRETE, mapnik2.Color(255,255,0))
rule = mapnik2.Rule()
rule.symbols.append(sym)
style = mapnik2.Style()
style.rules.append(rule)
_map = mapnik2.Map(256,256, mapSrs)
_map.append_style('foo', style)
lyr.styles.append('foo')
_map.layers.append(lyr)
prj_trans = mapnik2.ProjTransform(mapnik2.Projection(mapSrs),
mapnik2.Projection(lyrSrs))
_map.zoom_to_box(prj_trans.backward(lyr.envelope()))
im = mapnik2.Image(_map.width,_map.height)
mapnik2.render(_map, im)
# save a png somewhere so we can see it
save_data('test_raster_warping.png', im.tostring('png'))
imdata = im.tostring()
assert contains_word('\xff\xff\x00\xff', imdata)
示例11: renderToPdf
def renderToPdf(envLL, filename, sizex, sizey):
"""Renders the specified Box2d and zoom level as a PDF"""
basefilename = os.path.splitext(filename)[0]
mergedpdf = None
for mapname in MAPNIK_LAYERS:
print 'Rendering', mapname
# Render layer PDF.
localfilename = basefilename + '_' + mapname + '.pdf';
file = open(localfilename, 'wb')
surface = cairo.PDFSurface(file.name, sizex, sizey)
envMerc = LLToMerc(envLL)
map = mapnik.Map(sizex, sizey)
mapnik.load_map(map, mapname + ".xml")
map.zoom_to_box(envMerc)
mapnik.render(map, surface)
surface.finish()
file.close()
# Merge with master.
if not mergedpdf:
mergedpdf = PdfFileWriter()
localpdf = PdfFileReader(open(localfilename, "rb"))
page = localpdf.getPage(0)
mergedpdf.addPage(page)
else:
localpdf = PdfFileReader(open(localfilename, "rb"))
page.mergePage(localpdf.getPage(0))
output = open(filename, 'wb')
mergedpdf.write(output)
output.close()
示例12: generate_map_tile
def generate_map_tile(self, m, filename, z, x, y):
# Code taken from OSM generate_tiles.py
proj = GoogleProjection()
mprj = mapnik.Projection(m.srs)
p0 = (x * 256, (y + 1) * 256)
p1 = ((x + 1) * 256, y * 256)
l0 = proj.fromPixelToLL(p0, z);
l1 = proj.fromPixelToLL(p1, z);
c0 = mprj.forward(mapnik.Coord(l0[0], l0[1]))
c1 = mprj.forward(mapnik.Coord(l1[0], l1[1]))
if hasattr(mapnik,'mapnik_version') and mapnik.mapnik_version() >= 800:
bbox = mapnik.Box2d(c0.x, c0.y, c1.x, c1.y)
else:
bbox = mapnik.Envelope(c0.x, c0.y, c1.x, c1.y)
m.resize(256, 256)
m.zoom_to_box(bbox)
im = mapnik.Image(256, 256)
mapnik.render(m, im)
# See https://github.com/mapnik/mapnik/wiki/OutputFormats for output
# formats and special parameters. The default here is 32 bit PNG with 8
# bit per component and alpha channel.
if mapnik_version == 2:
im.save(str(filename), "png32")
else:
im.save(str(filename), "png")
示例13: renderArea
def renderArea(self, width, height, srs, xmin, ymin, xmax, ymax, zoom):
"""
"""
if self.mapnik is None:
self.mapnik = mapnik.Map(0, 0)
if exists(self.mapfile):
mapnik.load_map(self.mapnik, str(self.mapfile))
else:
handle, filename = mkstemp()
os.write(handle, urlopen(self.mapfile).read())
os.close(handle)
mapnik.load_map(self.mapnik, filename)
os.unlink(filename)
self.mapnik.width = width
self.mapnik.height = height
self.mapnik.zoom_to_box(mapnik.Envelope(xmin, ymin, xmax, ymax))
img = mapnik.Image(width, height)
mapnik.render(self.mapnik, img)
img = Image.fromstring("RGBA", (width, height), img.tostring())
return img
示例14: render_tile
def render_tile(self, tile_uri, x, y, z):
# Calculate pixel positions of bottom-left & top-right
p0 = (x * 256, (y + 1) * 256)
p1 = ((x + 1) * 256, y * 256)
# Convert to LatLong (EPSG:4326)
l0 = self.tileproj.fromPixelToLL(p0, z);
l1 = self.tileproj.fromPixelToLL(p1, z);
# Convert to map projection (e.g. mercator co-ords EPSG:900913)
c0 = self.prj.forward(mapnik.Coord(l0[0],l0[1]))
c1 = self.prj.forward(mapnik.Coord(l1[0],l1[1]))
# Bounding box for the tile
if hasattr(mapnik,'mapnik_version') and mapnik.mapnik_version() >= 800:
bbox = mapnik.Box2d(c0.x,c0.y, c1.x,c1.y)
else:
bbox = mapnik.Envelope(c0.x,c0.y, c1.x,c1.y)
render_size = 256
self.m.resize(render_size, render_size)
self.m.zoom_to_box(bbox)
if(self.m.buffer_size < 128):
self.m.buffer_size = 128
# Render image with default Agg renderer
im = mapnik.Image(render_size, render_size)
mapnik.render(self.m, im)
im.save(tile_uri, 'png256')
示例15: render_dynamic
def render_dynamic(self, painter):
if self.mapnik_map:
w = painter.device().width()
h = painter.device().height()
# using canvas dims leads to shift in QGIS < 1.3...
#w = self.canvas.width()
#h = self.canvas.height()
try:
self.mapnik_map.resize(w,h)
except:
self.mapnik_map.width = w
self.mapnik_map.height = h
if self.qCanvas:
can = self.qCanvas
else:
can = self.canvas
try:
e = can.extent()
except:
can = self.canvas
e = can.extent()
bbox = mapnik.Envelope(e.xMinimum(),e.yMinimum(),
e.xMaximum(),e.yMaximum())
self.mapnik_map.zoom_to_box(bbox)
im = mapnik.Image(w,h)
mapnik.render(self.mapnik_map,im)
if os.name == 'nt':
qim = QImage()
qim.loadFromData(QByteArray(im.tostring('png')))
painter.drawImage(0,0,qim)
else:
qim = QImage(im.tostring(),w,h,QImage.Format_ARGB32)
painter.drawImage(0,0,qim.rgbSwapped())
can.refresh()