本文整理汇总了Python中mapnik.render函数的典型用法代码示例。如果您正苦于以下问题:Python render函数的具体用法?Python render怎么用?Python render使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了render函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: render_tile
def render_tile(self, z, x, y):
"""
:param z: Zoom level
:param x: Tile horizontal position
:param y: Tile vertical position
"""
topleft = num2deg(x, y, z)
bottomright = num2deg(x + 1, y + 1, z)
# Bounding box for the tile
bbox = mapnik.Box2d(topleft, bottomright)
bbox = DATA_PROJECTION.forward(bbox)
print("Bouding box: ", bbox)
# self.m.resize(TILE_WIDTH, TILE_WIDTH)
self.m.zoom_to_box(bbox)
MIN_BUFFER = 256
self.m.buffer_size = max(self.m.buffer_size, MIN_BUFFER)
# Render image with default Agg renderer
im = mapnik.Image(TILE_WIDTH, TILE_WIDTH)
mapnik.render(self.m, im)
return im
示例2: test_pre_multiply_status_of_map2
def test_pre_multiply_status_of_map2():
m = mapnik.Map(256,256)
m.background = mapnik.Color(1,1,1,255)
im = mapnik.Image(m.width,m.height)
eq_(validate_pixels_are_not_premultiplied(im),True)
mapnik.render(m,im)
eq_(validate_pixels_are_not_premultiplied(im),True)
示例3: test_background_image_with_alpha_and_background_color
def test_background_image_with_alpha_and_background_color():
m = mapnik.Map(10,10)
m.background = mapnik.Color('rgba(255,255,255,.5)')
m.background_image = '../data/images/yellow_half_trans.png'
im = mapnik.Image(m.width,m.height)
mapnik.render(m,im)
eq_(get_unique_colors(im),['rgba(255,255,85,191)'])
示例4: test_background_image_and_background_color
def test_background_image_and_background_color():
m = mapnik.Map(8,8)
m.background = mapnik.Color('rgba(255,255,255,.5)')
m.background_image = '../data/images/stripes_pattern.png'
im = mapnik.Image(m.width,m.height)
mapnik.render(m,im)
eq_(get_unique_colors(im),['rgba(255,255,255,128)', 'rgba(74,74,74,255)'])
示例5: _render_mapfile
def _render_mapfile(self, mapfile, query):
start_time = time.time()
m = self.map_obj(mapfile)
m.resize(query.size[0], query.size[1])
m.srs = '+init=%s' % str(query.srs.srs_code.lower())
envelope = mapnik.Box2d(*query.bbox)
m.zoom_to_box(envelope)
data = None
try:
if self.layers:
i = 0
for layer in m.layers[:]:
if layer.name != 'Unkown' and layer.name not in self.layers:
del m.layers[i]
else:
i += 1
img = mapnik.Image(query.size[0], query.size[1])
if self.scale_factor:
mapnik.render(m, img, self.scale_factor)
else:
mapnik.render(m, img)
data = img.tostring(str(query.format))
finally:
size = None
if data:
size = len(data)
log_request('%s:%s:%s:%s' % (mapfile, query.bbox, query.srs.srs_code, query.size),
status='200' if data else '500', size=size, method='API', duration=time.time()-start_time)
return ImageSource(BytesIO(data), size=query.size,
image_opts=ImageOptions(format=query.format))
示例6: renderArea
def renderArea(self, width, height, srs, xmin, ymin, xmax, ymax, zoom):
"""
"""
start_time = time()
#
# Mapnik can behave strangely when run in threads, so place a lock on the instance.
#
if global_mapnik_lock.acquire():
try:
if self.mapnik is None:
self.mapnik = get_mapnikMap(self.mapfile)
logging.debug('TileStache.Mapnik.ImageProvider.renderArea() %.3f to load %s', time() - start_time, self.mapfile)
self.mapnik.width = width
self.mapnik.height = height
self.mapnik.zoom_to_box(Box2d(xmin, ymin, xmax, ymax))
img = mapnik.Image(width, height)
mapnik.render(self.mapnik, img)
except:
self.mapnik = None
raise
finally:
# always release the lock
global_mapnik_lock.release()
img = Image.frombytes('RGBA', (width, height), img.tostring())
logging.debug('bbox:%lf,%lf,%lf,%lf', xmin, ymin, xmax, ymax)
logging.debug('TileStache.Mapnik.ImageProvider.renderArea() %dx%d in %.3f from %s', width, height, time() - start_time, self.mapfile)
return img
示例7: test_raster_warping
def test_raster_warping():
lyrSrs = "+init=epsg:32630"
mapSrs = '+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs'
lyr = mapnik.Layer('dataraster', lyrSrs)
if 'gdal' in mapnik.DatasourceCache.instance().plugin_names():
lyr.datasource = mapnik.Gdal(
file = '../data/raster/dataraster.tif',
band = 1,
)
sym = mapnik.RasterSymbolizer()
sym.colorizer = mapnik.RasterColorizer(mapnik.COLORIZER_DISCRETE, mapnik.Color(255,255,0))
rule = mapnik.Rule()
rule.symbols.append(sym)
style = mapnik.Style()
style.rules.append(rule)
_map = mapnik.Map(256,256, mapSrs)
_map.append_style('foo', style)
lyr.styles.append('foo')
_map.layers.append(lyr)
prj_trans = mapnik.ProjTransform(mapnik.Projection(mapSrs),
mapnik.Projection(lyrSrs))
_map.zoom_to_box(prj_trans.backward(lyr.envelope()))
im = mapnik.Image(_map.width,_map.height)
mapnik.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)
示例8: 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(mapnik.Coord(ll0[0], ll0[1]))
c1 = self.map_projection.forward(mapnik.Coord(ll1[0], ll1[1]))
# Create bounding box for the render
bbox = mapnik.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 = mapnik.Image(self.width, self.height)
mapnik.render(self.mapnik_map, image)
image.save(filename, self.filetype)
示例9: renderPage
def renderPage(ll,file):
z = 2
#imgx = 987 * z
#imgy = 801 * z
imgx = int(987 * z )
imgy = int(987 * z *0.77 )
m = mapnik.Map(imgx,imgy)
mapnik.load_map(m,mapfile)
prj = 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")
c0 = prj.forward(mapnik.Coord(ll[0],ll[1]))
c1 = prj.forward(mapnik.Coord(ll[2],ll[3]))
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.zoom_to_box(bbox)
im = mapnik.Image(imgx,imgy)
mapnik.render(m, im)
view = im.view(0,0,imgx,imgy) # x,y,width,height
map_uri=file + '.png'
view.save(map_uri,'png')
file=open(file+'.svg' ,'w')
surface=cairo.SVGSurface(file.name,imgx,imgy)
mapnik.render(m,surface)
#c = cairo.Context(surface)
#c.move_to(50,50)
#c.show_text('testing')
surface.finish();
示例10: test_raster_warping
def test_raster_warping():
lyrSrs = "+init=epsg:32630"
mapSrs = "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"
lyr = mapnik.Layer("dataraster", lyrSrs)
if "gdal" in mapnik.DatasourceCache.plugin_names():
lyr.datasource = mapnik.Gdal(file="../data/raster/dataraster.tif", band=1)
sym = mapnik.RasterSymbolizer()
sym.colorizer = mapnik.RasterColorizer(mapnik.COLORIZER_DISCRETE, mapnik.Color(255, 255, 0))
rule = mapnik.Rule()
rule.symbols.append(sym)
style = mapnik.Style()
style.rules.append(rule)
_map = mapnik.Map(256, 256, mapSrs)
_map.append_style("foo", style)
lyr.styles.append("foo")
_map.layers.append(lyr)
map_proj = mapnik.Projection(mapSrs)
layer_proj = mapnik.Projection(lyrSrs)
prj_trans = mapnik.ProjTransform(map_proj, layer_proj)
_map.zoom_to_box(prj_trans.backward(lyr.envelope()))
im = mapnik.Image(_map.width, _map.height)
mapnik.render(_map, im)
imdata = im.tostring()
assert contains_word("\xff\xff\x00\xff", imdata)
示例11: render_tile
def render_tile(mapfile, tile_uri, x, y, z):
m = mapnik.Map(256, 256)
# Load style XML
mapnik.load_map(m, mapfile, True)
# Obtain <Map> projection
prj = mapnik.Projection(m.srs)
# Projects between tile pixel co-ordinates and LatLong (EPSG:4326)
tileproj = GoogleProjection(z+1)
# 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 = tileproj.fromPixelToLL(p0, z);
l1 = tileproj.fromPixelToLL(p1, z);
# Convert to map projection (e.g. mercator co-ords EPSG:900913)
c0 = prj.forward(mapnik.Coord(l0[0],l0[1]))
c1 = prj.forward(mapnik.Coord(l1[0],l1[1]))
# Bounding box for the tile
bbox = mapnik.Envelope(c0.x,c0.y, c1.x,c1.y)
render_size = 256
m.resize(render_size, render_size)
m.zoom_to_box(bbox)
m.buffer_size = 128
# Render image with default Agg renderer
im = mapnik.Image(render_size, render_size)
mapnik.render(m, im)
im.save(tile_uri, 'png256')
示例12: test_that_coordinates_do_not_overflow_and_polygon_is_rendered
def test_that_coordinates_do_not_overflow_and_polygon_is_rendered():
expected_color = mapnik.Color('white')
ds = mapnik.MemoryDatasource()
context = mapnik.Context()
ds.add_feature(mapnik.Feature.from_geojson(json.dumps(geojson),context))
s = mapnik.Style()
r = mapnik.Rule()
sym = mapnik.PolygonSymbolizer()
sym.fill = expected_color
sym.clip = False
r.symbols.append(sym)
s.rules.append(r)
lyr = mapnik.Layer('Layer')
lyr.datasource = ds
lyr.styles.append('style')
m = mapnik.Map(256,256)
m.background_color = mapnik.Color('black')
m.append_style('style',s)
m.layers.append(lyr)
# 17/20864/45265.png
m.zoom_to_box(mapnik.Box2d(-13658379.710221574,6197514.253362091,-13657768.213995293,6198125.749588372))
# works 15/5216/11316.png
#m.zoom_to_box(mapnik.Box2d(-13658379.710221574,6195679.764683247,-13655933.72531645,6198125.749588372))
im = mapnik.Image(256,256)
mapnik.render(m,im)
eq_(im.get_pixel(128,128),expected_color.packed())
示例13: GetMap
def GetMap(self, params):
if params["bbox"][0] >= params["bbox"][2]:
raise OGCException("BBOX values don't make sense. minx is greater than maxx.")
if params["bbox"][1] >= params["bbox"][3]:
raise OGCException("BBOX values don't make sense. miny is greater than maxy.")
if params.has_key("styles") and len(params["styles"]) != len(params["layers"]):
raise OGCException("STYLES length does not match LAYERS length.")
m = Map(params["width"], params["height"])
if params.has_key("transparent") and params["transparent"] == "FALSE":
m.background = params["bgcolor"]
else:
m.background = Color(0, 0, 0, 0)
maplayers = self.mapfactory.layers
mapstyles = self.mapfactory.styles
for layername in params["layers"]:
try:
layer = maplayers[layername]
except KeyError:
raise OGCException('Layer "%s" not defined.' % layername, "LayerNotDefined")
for stylename in layer.styles:
if stylename in mapstyles.keys():
m.append_style(stylename, mapstyles[stylename])
else:
raise ServerConfigurationError(
'Layer "%s" refers to non-existent style "%s".' % (layername, stylename)
)
m.layers.append(layer)
m.zoom_to_box(Envelope(params["bbox"][0], params["bbox"][1], params["bbox"][2], params["bbox"][3]))
im = Image(params["width"], params["height"])
render(m, im)
im = fromstring("RGBA", (params["width"], params["height"]), rawdata(im))
fh = StringIO()
im.save(fh, PIL_TYPE_MAPPING[params["format"]], quality=100)
fh.seek(0)
return Response(params["format"], fh.read())
示例14: render_tile
def render_tile(self, x, y, z, utf_grid):
# 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
if utf_grid is True:
grid = mapnik.Grid(self.m.width,self.m.height)
mapnik.render_layer(self.m, grid, layer=0, fields=['html_exp'])
utfgrid = grid.encode('utf', resolution=4)
return json.dumps(utfgrid);
else:
im = mapnik.Image(render_size, render_size)
mapnik.render(self.m, im)
return im.tostring('png');
示例15: 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 = mapnik.Layer('dataraster', lyrSrs)
if 'gdal' in mapnik.DatasourceCache.plugin_names():
lyr.datasource = mapnik.Gdal(
file = '../data/raster/dataraster.tif',
band = 1,
)
sym = mapnik.RasterSymbolizer()
sym.colorizer = mapnik.RasterColorizer(mapnik.COLORIZER_DISCRETE, mapnik.Color(255,255,0))
rule = mapnik.Rule()
rule.symbols.append(sym)
style = mapnik.Style()
style.rules.append(rule)
_map = mapnik.Map(256,256, mapSrs)
_map.background=mapnik.Color('white')
_map.append_style('foo', style)
lyr.styles.append('foo')
_map.layers.append(lyr)
_map.zoom_to_box(mapnik.Box2d(3,42,4,43))
im = mapnik.Image(_map.width,_map.height)
mapnik.render(_map, im)
expected_file = './images/support/raster_warping_does_not_overclip_source.png'
actual_file = '/tmp/' + os.path.basename(expected_file)
im.save(actual_file,'png32')
if not os.path.exists(expected_file) or os.environ.get('UPDATE'):
im.save(expected_file,'png32')
actual = mapnik.Image.open(actual_file)
expected = mapnik.Image.open(expected_file)
eq_(actual.tostring('png32'),expected.tostring('png32'), 'failed comparing actual (%s) and expected (%s)' % (actual_file,expected_file))