Python Mapnik:关于如何使用gps轨道渲染地图的示例
作者:互联网
我正在尝试使用mapnik和python从记录的GPS轨迹渲染地图.我从数据库中获取gps数据,因此它只是一个数组(lat,long).
有谁知道这样做的例子?我知道我需要先创建一个形状文件,但我是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的文档有点弱,但如果你引用的话,你应该可以在此基础上构建;
> The mapnik wiki
> The python mapnik package docs
标签:python,render,openstreetmap,mapnik 来源: https://codeday.me/bug/20190703/1370639.html