前言
在阅读本篇文章之前,可以回顾一下上一篇文章。【GIS】——使用Python bindings操作mapnik
参考
https://github.com/mapnik/mapnik/wiki/GettingStartedInXML
创建Python脚本
在存放shapefile(ne_110m_admin_0_countries.shp)的文件夹下建立名为world_map.py
import mapnik
stylesheet = 'world_style.xml'
image = 'world_style.png'
m = mapnik.Map(600,300)
mapnik.load_map(m,stylesheet)
m.zoom_all()
mapnik.render_to_file(m,image)
print "rendered image to '%s'" % image
编写Style样式
在存放shapefile的文件夹下建立名为world_style.xml
@H_403_38@<Map background-color="steelblue" srs="+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs">
@H_403_38@<Style name="My Style"> <@H_403_38@Rule> <@H_403_38@PolygonSymbolizer @H_403_38@fill="#f2eff9" /> <@H_403_38@LineSymbolizer @H_403_38@stroke="@H_403_38@rgb(50%,50%,50%)" @H_403_38@stroke-width="0.1" /> </@H_403_38@Rule> @H_403_38@</Style>
@H_403_38@<Layer name="world" srs="+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs">
@H_403_38@<StyleName>My Style@H_403_38@</StyleName>
@H_403_38@<Datasource>
@H_403_38@<Parameter name="type">shape@H_403_38@</Parameter>
@H_403_38@<Parameter name="file">ne_110m_admin_0_countries.shp@H_403_38@</Parameter>
@H_403_38@</Datasource>
@H_403_38@</Layer>
@H_403_38@</Map>
运行
在此文件夹下打开命令窗口,输入python world_map.py