作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试根据记录的 GPS 轨迹使用 mapnik 和 python 渲染 map 。我从数据库中获取 gps 数据,所以它只是一个数组(纬度、经度)。
有谁知道这样做的例子吗?我知道我需要先创建一个形状文件,但我是 mapnik 的新手,到目前为止我还不太了解。也许有一个很好的例子我会得到它:-)
谢谢
最佳答案
实际上,最简单的方法是使用 KML 文件。安装simplekml模块,然后遍历您的数组以创建 KML 文件。
import simplekml
kml = simplekml.Kml()
for i, coord in enumerate(coords):
# assuming coord is a lat, lon tuple
kml.newpoint(name="Point %s" % i, coords=[coord])
kml.save("GPS_tracking_data.kml")
现在您可以将其作为数据源加载到 mapnik 中并绘制它;
import mapnik
# Setup the map
map_canvas = mapnik.Map(width_in_px, height_in_px)
map_canvas.background = mapnik.Color('rgb(0,0,0,0)') # transparent
# Create a symbolizer to draw the points
style = mapnik.Style()
rule = mapnik.Rule()
point_symbolizer = mapnik.MarkersSymbolizer()
point_symbolizer.allow_overlap = True
point_symbolizer.opacity = 0.5 # semi-transparent
rule.symbols.append(point_symbolizer)
style.rules.append(rule)
map_canvas.append_style('GPS_tracking_points', style)
# Create a layer to hold the ponts
layer = mapnik.Layer('GPS_tracking_points')
layer.datasource = mapnik.Ogr(file="GPS_tracking_data.kml", layer_by_index=0)
layer.styles.append('GPS_tracking_points')
map_canvas.layers.append(layer)
# Save the map
map_canvas.zoom_all()
mapnik.render_to_file(map_canvas, 'GPS_tracking_points.png', 'png')
这应该就可以了。 python+mapnik 的文档有点薄弱,但如果你引用,你应该能够在此基础上构建;
关于Python + Mapnik : Example on how to render a map with a gps track on it,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15691525/
我是一名优秀的程序员,十分优秀!