使用pygmaps包在谷歌地图上绘制数据?
Python pygmaps库为谷歌地图javascript api提供了一个包装器。使用这个库,Python用户可以创建一个类似matplotlib的界面来生成html和javascript,然后可以描绘用户想要添加到谷歌地图顶部的所有附加信息。
所需库
我们只使用pygmaps库/包。您可以使用pip安装pygmaps库,例如:
$pip install pygmaps (windows os) $sudo pip3 install pygmaps (linux os)
我们将编写一个程序来显示:
通过提供经度、纬度和缩放级别来使用pygmaps创建地图。
通过为网格提供起始和结束经度和纬度点以及网格大小来在地图上设置网格。
在地图上添加一个点并通过设置颜色来显示它。
在地图上绘制一个以米为半径的点周围的圆。
通过提供经度和纬度点的列表来在地图上绘制路径。
以下是通过pygmaps获取不同功能的实现。
import pygmaps ########## CONSTRUCTOR: pygmaps.maps(latitude, longitude, zoom) ############################## # DESC:initialize a map with latitude and longitude of center point #and map zoom level "15" # PARAMETER1:latitude (float) latittude of map center point # PARAMETER2:longitude (float) latittude of map center point # PARAMETER3:zoom (int) map zoom level 0~20 # RETURN:the instant of pygmaps #======================================================================================== mymap = pygmaps.pygmaps(17.45,78.29, 15) ########## FUNCTION: setgrids(start-Lat, end-Lat, Lat-interval, start-Lng, end-Lng, Lng-interval) ###### # DESC:set grids on map # PARAMETER1:start-Lat (float), start (minimum) latittude of the grids # PARAMETER2:end-Lat (float), end (maximum) latittude of the grids # PARAMETER3:Lat-interval (float) grid size in latitude # PARAMETER4:start-Lng (float), start (minimum) longitude of the grids # PARAMETER5:end-Lng (float), end (maximum) longitude of the grids # PARAMETER6:Lng-interval (float) grid size in longitude # RETURN:no returns #======================================================================================== mymap.setgrids(17.45, 17.46, 0.001, 78.29,78.30, 0.001) ########## FUNCTION: addpoint(latitude, longitude, [color])############################# # DESC:add a point into a map and dispaly it, color is optional default is red # PARAMETER1:latitude (float) latitude of the point # PARAMETER2:longitude (float) longitude of the point # PARAMETER3:color (string) color of the point showed in map, using HTML color code #HTML COLOR CODE: http://www.computerhope.com/htmcolor.htm #e.g. red "#FF0000", Blue "#0000FF", Green "#00FF00" # RETURN:no return #======================================================================================== mymap.addpoint(17.45,78.29, "#FF0000","Hello") ########## FUNCTION: addradpoint(latitude, longitude, radius, [color])################## # DESC: add a point with a radius (Meter) - Draw cycle # PARAMETER1:latitude (float) latitude of the point # PARAMETER2:longitude (float) longitude of the point # PARAMETER3:radius (float), radius in meter # PARAMETER4:color (string) color of the point showed in map, using HTML color code #HTML COLOR CODE: http://www.computerhope.com/htmcolor.htm #e.g. red "#FF0000", Blue "#0000FF", Green "#00FF00" # RETURN:no return #======================================================================================== mymap.addradpoint(17.45,78.29, 150, "#0000FF") ########## FUNCTION: addpath(path,[color])############################################## # DESC:add a path into map, the data struceture of Path is a list of points # PARAMETER1:path (list of coordinates) e.g. [(lat1,lng1),(lat2,lng2),...] # PARAMETER2:color (string) color of the point showed in map, using HTML color code #HTML COLOR CODE: http://www.computerhope.com/htmcolor.htm #e.g. red "#FF0000", Blue "#0000FF", Green "#00FF00" # RETURN:no return #======================================================================================== path = [(17.45,78.29), (17.55, 78.39), (17.65,78.49), ] mymap.addpath(path,"#00FF00") ########## FUNCTION: draw(file)###################################################### # DESC:create the html map file (.html) # PARAMETER1:file (string) the map path and file # RETURN:no return, generate html file in specified directory #======================================================================================== mymap.draw('./mymap.html') print('OK')
注意:运行mymap.addpoint函数时,您可能会遇到TypeError错误,例如:
TypeError: addpoint() takes from 3 to 4 positional arguments but 5 were given
为了避免这种情况,我在pygmaps.py包的addpoint函数中添加了标题参数。
输出
从上面的地图可以看出,我们能够在一个地图中可视化多个功能。
广告