本文整理汇总了Python中mapnik.save_map函数的典型用法代码示例。如果您正苦于以下问题:Python save_map函数的具体用法?Python save_map怎么用?Python save_map使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了save_map函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
def main(file, **kwargs):
""" Given an input layers file and a directory, print the compiled
XML file to stdout and save any encountered external image files
to the named directory.
"""
mmap = mapnik.Map(1, 1)
# allow [zoom] filters to work
mmap.srs = '+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]'
cascadenik.load_map(mmap, file, **kwargs)
(handle, filename) = tempfile.mkstemp(suffix='.xml', prefix='cascadenik-mapnik-')
os.close(handle)
mapnik.save_map(mmap, filename)
if kwargs.get('pretty'):
doc = ElementTree.fromstring(open(filename, 'rb').read())
cascadenik._compile.indent(doc)
f = open(filename, 'wb')
doc.write(f)
f.close()
if kwargs.get('compiled'):
os.rename(filename, kwargs['compiled'])
else:
print open(filename, 'r').read()
os.unlink(filename)
return 0
示例2: import_style_mml
def import_style_mml(url):
"""
"""
# Create a local style.xml file by way of a dummy mapnik.Map instance.
mmap = mapnik.Map(1, 1)
mmap.srs = epsg3857
cascadenik.load_map(mmap, url, 'gunicorn', verbose=False)
mapnik.save_map(mmap, 'gunicorn/style.xml')
# Build a new TileStache configuration file.
config = json.load(open('gunicorn/tilestache.cfg'))
config['layers'] = {'tiles': {'provider': {}}}
layer = config['layers']['tiles']
layer['provider']['name'] = 'mapnik'
layer['provider']['mapfile'] = 'style.xml'
layer['bounds'] = dict(zip('south west north east'.split(), options.bbox))
layer['bounds'].update(dict(low=0, high=18))
layer['preview'] = dict(zoom=15, lat=(options.bbox[0]/2 + options.bbox[2]/2), lon=(options.bbox[1]/2 + options.bbox[3]/2))
# Done.
json.dump(config, open('gunicorn/tilestache.cfg', 'w'), indent=2)
示例3: main
def main(src_file, dest_file, **kwargs):
""" Given an input layers file and a directory, print the compiled
XML file to stdout and save any encountered external image files
to the named directory.
"""
mmap = mapnik.Map(1, 1)
# allow [zoom] filters to work
mmap.srs = '+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]'
load_kwargs = dict([(k, v) for (k, v) in kwargs.items() if k in ('cache_dir', 'verbose', 'datasources_cfg')])
cascadenik.load_map(mmap, src_file, dirname(realpath(dest_file)), **load_kwargs)
(handle, tmp_file) = tempfile.mkstemp(suffix='.xml', prefix='cascadenik-mapnik-')
os.close(handle)
mapnik.save_map(mmap, tmp_file)
if kwargs.get('pretty'):
doc = ElementTree.fromstring(open(tmp_file, 'rb').read())
cascadenik._compile.indent(doc)
f = open(tmp_file, 'wb')
ElementTree.ElementTree(doc).write(f)
f.close()
# manually unlinking seems to be required on windows
if os.path.exists(dest_file):
os.unlink(dest_file)
shutil.move(tmp_file, dest_file)
return 0
示例4: test_gen_map
def test_gen_map():
mapxmlfile = '../data/good_maps/raster_colorizer.xml'
mapxmloutputfile = 'raster_colorizer_test_save.xml'
outputfile = 'raster_colorizer_test.png'
m = mapnik.Map(800, 600)
mapnik.load_map(m, mapxmlfile)
mapnik.save_map(m, mapxmloutputfile)
m.zoom_all()
mapnik.render_to_file(m, outputfile)
示例5: test_gen_map
def test_gen_map():
mapxmlfile = '../data/good_maps/raster_colorizer.xml'
mapxmloutputfile = 'raster_colorizer_test_save.xml'
outputfile = 'raster_colorizer_test.png'
m = mapnik.Map(800, 600)
try:
mapnik.load_map(m, mapxmlfile)
mapnik.save_map(m, mapxmloutputfile)
m.zoom_all()
mapnik.render_to_file(m, outputfile)
except RuntimeError,e:
# only test datasources that we have installed
if not 'Could not create datasource' in str(e):
raise RuntimeError(str(e))
示例6: get_paired_images
def get_paired_images(w,h,mapfile):
tmp_map = 'tmp_map.xml'
m = mapnik.Map(w,h)
mapnik.load_map(m,mapfile)
i = mapnik.Image(w,h)
m.zoom_all()
mapnik.render(m,i)
mapnik.save_map(m,tmp_map)
m2 = mapnik.Map(w,h)
mapnik.load_map(m2,tmp_map)
i2 = mapnik.Image(w,h)
m2.zoom_all()
mapnik.render(m2,i2)
os.remove(tmp_map)
return i,i2
示例7: main
def main(file, dir):
""" Given an input layers file and a directory, print the compiled
XML file to stdout and save any encountered external image files
to the named directory.
"""
mmap = mapnik.Map(1, 1)
cascadenik.load_map(mmap, file, dir)
(handle, filename) = tempfile.mkstemp(suffix='.xml', prefix='cascadenik-mapnik-')
os.close(handle)
mapnik.save_map(mmap, filename)
print open(filename, 'r').read()
os.unlink(filename)
return 0
示例8: serialize
def serialize(xml,options):
try:
import mapnik
except:
sys.exit(color_text(1,'Error: saving xml requires Mapnik python bindings to be installed'))
m = mapnik.Map(1,1)
if options.from_string:
mapnik.load_map_from_string(m,xml,True)
else:
mapnik.load_map(m,xml,True)
if options.output:
mapnik.save_map(m,options.output)
else:
if hasattr(mapnik,'mapnik_version') and mapnik.mapnik_version() >= 700:
print mapnik.save_map_to_string(m)
else:
sys.exit(color_text(1,'Minor error: printing XML to stdout requires Mapnik >=0.7.0, please provide a second argument to save the output to a file'))
示例9: import_style_tdcfg
def import_style_tdcfg(url):
""" Load a Cascadenik style and its constituent pieces from a URL.
"""
style = json.loads(urlopen(url).read())
mapfile = urljoin(options.style, style['mapfile'])
# Create a local style.xml file by way of a dummy mapnik.Map instance.
mmap = mapnik.Map(1, 1)
mmap.srs = epsg3857
cascadenik.load_map(mmap, mapfile, 'gunicorn', verbose=False)
mapnik.save_map(mmap, 'gunicorn/style.xml')
# Build a new TileStache configuration file.
config = json.load(open('gunicorn/tilestache.cfg'))
config['layers'] = {'tiles': {'provider': {}}}
layer = config['layers']['tiles']
layer['provider']['name'] = 'mapnik'
layer['provider']['mapfile'] = 'style.xml'
layer['bounds'] = dict(zip('south west north east'.split(), options.bbox))
layer['bounds'].update(dict(low=0, high=18))
layer['preview'] = dict(zoom=15, lat=(options.bbox[0]/2 + options.bbox[2]/2), lon=(options.bbox[1]/2 + options.bbox[3]/2))
# Apply various layer options.
for (parameter, value) in style['layer'].items():
if parameter == 'png options' and 'palette' in value:
palette_url = urljoin(url, value['palette'])
palette_data = urlopen(palette_url).read()
palette_file = 'gunicorn/palette.act'
print >> stderr, ' ', palette_file, '<--', palette_url
open(palette_file, 'w').write(palette_data)
value['palette'] = 'palette.act'
layer[parameter] = value
# Done.
json.dump(config, open('gunicorn/tilestache.cfg', 'w'), indent=2)
示例10: compare_map
def compare_map(xml):
m = mapnik.Map(256, 256)
absolute_base = os.path.abspath(os.path.dirname(xml))
mapnik.load_map(m, xml, False, absolute_base)
(handle, test_map) = tempfile.mkstemp(suffix='.xml', prefix='mapnik-temp-map1-')
os.close(handle)
(handle, test_map2) = tempfile.mkstemp(suffix='.xml', prefix='mapnik-temp-map2-')
os.close(handle)
if os.path.exists(test_map):
os.remove(test_map)
mapnik.save_map(m, test_map)
new_map = mapnik.Map(256, 256)
mapnik.load_map(new_map, test_map,False,absolute_base)
open(test_map2,'w').write(mapnik.save_map_to_string(new_map))
diff = ' diff %s %s' % (os.path.abspath(test_map),os.path.abspath(test_map2))
try:
eq_(open(test_map).read(),open(test_map2).read())
except AssertionError, e:
raise AssertionError('serialized map "%s" not the same after being reloaded, \ncompare with command:\n\n$%s' % (xml,diff))
示例11: compare_map
def compare_map(xml):
m = mapnik.Map(256, 256)
absolute_base = os.path.abspath(os.path.dirname(xml))
try:
mapnik.load_map(m, xml, False, absolute_base)
except RuntimeError as e:
# only test datasources that we have installed
if not 'Could not create datasource' in str(e) \
and not 'could not connect' in str(e):
raise RuntimeError(str(e))
return
(handle, test_map) = tempfile.mkstemp(
suffix='.xml', prefix='mapnik-temp-map1-')
os.close(handle)
(handle, test_map2) = tempfile.mkstemp(
suffix='.xml', prefix='mapnik-temp-map2-')
os.close(handle)
if os.path.exists(test_map):
os.remove(test_map)
mapnik.save_map(m, test_map)
new_map = mapnik.Map(256, 256)
mapnik.load_map(new_map, test_map, False, absolute_base)
with open(test_map2, 'w') as f:
f.write(mapnik.save_map_to_string(new_map))
diff = ' diff -u %s %s' % (os.path.abspath(test_map),
os.path.abspath(test_map2))
try:
with open(test_map) as f1:
with open(test_map2) as f2:
eq_(f1.read(), f2.read())
except AssertionError as e:
raise AssertionError(
'serialized map "%s" not the same after being reloaded, \ncompare with command:\n\n$%s' %
(xml, diff))
if os.path.exists(test_map):
os.remove(test_map)
else:
# Fail, the map wasn't written
return False
示例12: render_tile
def render_tile(layer, z, x, y):
"""
Render the tile using mapnik.
"""
folder = os.path.join("baselayers",layer)
if not os.path.exists(folder):
raise ValueError("files not found")
# Create map
m = mapnik.Map(TILE_WIDTH, TILE_HEIGHT)
# Load mapnik xml stylesheet
stylesheet = os.path.join("baselayers",str(layer),"style.xml")
mapnik.load_map(m, stylesheet)
# Zoom to all features
m.zoom_all()
# Store artifacts
stylesheet = os.path.join("stylesheets",layer + ".xml")
mapnik.save_map(m, str(stylesheet))
# Render Map Tile
renderer = TiledMapRenderer(m)
im = renderer.render_tile(z, x, y)
# Return image
return im.tostring('png'), 200, {'Content-type': 'image/png'}
示例13: buildMapnikXML
def buildMapnikXML(extractName, **kwargs):
mapnik_args = dict()
mapnik_args['host'] = kwargs.get('host','localhost')
mapnik_args['port'] = kwargs.get('port','5432')
mapnik_args['user'] = kwargs.get('user','postgres')
mapnik_args['password'] = kwargs.get('password','')
mapnik_args['dbname'] = kwargs.get('database','osm')
#FIXME: We can probably calculate this based on the extract
mapnik_args['estimate_extent'] = 'false'
mapnik_args['extent'] = ''
mapnik_args['prefix'] = extractName
mapnik_args['world_boundaries'] = kwargs.get('world_boundaries','./world_boundaries/')
#FIXME: Can we determine the import projection from the DB?
mapnik_args['epsg'] = kwargs.get('epsg','900913;')
mapnik_args['symbols'] = kwargs.get('symbols','./symbols/')
inc_files = ['inc/settings.xml.inc', 'inc/datasource-settings.xml.inc']
for inc_file in inc_files:
infile = open(inc_file + ".template")
outfile = open(inc_file, "w")
s = infile.read()
s = s % mapnik_args
outfile.truncate(0)
outfile.write(s)
infile.close()
outfile.close()
import mapnik
m = mapnik.Map(1,1)
mapnik.load_map(m,"osm_template.xml",True)
mapnik.save_map(m,extractName + ".xml")
示例14: render_raster_tile
def render_raster_tile(datasource, z, x, y):
file = os.path.join("baselayers", datasource + ".tif")
if not os.path.exists(file):
raise ValueError("file not found")
raster = mapnik.Gdal(file=os.path.abspath(file))
lyr = mapnik.Layer("TIFF")
lyr.datasource = raster
lyr.srs = "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0.0 +k=1.0 +units=m [email protected] +wktext +no_defs +over"
# Create map
m = mapnik.Map(TILE_WIDTH, TILE_HEIGHT)
# Create Style and Rules
s = mapnik.Style()
r = mapnik.Rule()
# Create style for lyr
r.symbols.append(mapnik.RasterSymbolizer())
s.rules.append(r)
m.append_style('TIFF',s)
lyr.styles.append('TIFF')
# Set map srs to google
# 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')
# m.srs = merc.params()
m.srs = "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0.0 +k=1.0 +units=m [email protected] +wktext +no_defs +over"
# Add layer to map
m.layers.append(lyr)
# Zoom to all features
#m.maximum_extent = mapnik.Box2d(-180,-90,180,90)
m.maximum_extent = mapnik.Box2d(-20037508.34,-20037508.34,20037508.34,20037508.34) # merc_bounds
m.zoom_all()
# Store artifacts
stylesheet = os.path.join("stylesheets", datasource + ".xml")
mapnik.save_map(m, str(stylesheet))
# Render Map Tile
renderer = TiledMapRenderer(m)
im = renderer.render_tile(z, x, y)
# Return image
return im.tostring('png'), 200, {'Content-type': 'image/png'}
示例15: test
def test():
# TODO: Write a better test
# 1. Construct map in memory
# 2. Save map as XML
# 3. Load map to a second object
# 4. Compare both map objects
map = mapnik.Map(256, 256)
in_map = "../data/good_maps/osm-styles.xml"
mapnik.load_map(map, in_map)
test_map = "test_map.xml"
mapnik.save_map(map, test_map)
new_map = mapnik.Map(256, 256)
mapnik.load_map(new_map, test_map)
eq_(open(test_map).read(),mapnik.save_map_to_string(new_map))
if os.path.exists(test_map):
os.remove(test_map)
else:
# Fail, the map wasn't written
return False