python——基于gmplot实现调用谷歌地图进行GPS轨迹可视化

因为需要做一个GPS数据的地图轨迹绘制,基于gmplot可以很好的绘制出,所以我们开始吧!

安装gmplot模块

pip install gmplot

获取GPS开源数据集

Microsoft Research Asia的GeoLife GPS Trajectories Dataset。 该数据集包含在3年内收集的182个用户的GPS轨迹。
在这里插入图片描述

下载解压即可。

调用gmplot模块读取GPS数据并实现地图轨迹绘制

可以在gmplot的github网站汇总查看API的使用用法。

import numpy as np  
import matplotlib.pyplot as plt   
import pandas as pd  
import os  
import gmplot

#定义数据文件的路径
user = "005"
userdata = './class/ITS/Geolife_Trajectories/Data/{}/Trajectory/'.format(user)

filelist = os.listdir(userdata)  #返回指定路径下所有文件和文件夹的名字,并存放于一个列表中
names = ['lat','lng','zero','alt','days','date','time']
df_list = [pd.read_csv(userdata + f,header=6,names=names,index_col=False) for f in filelist]  
# f为文件索引号,header为列数,names为列表列名,index_col为行索引的列编号或列名

df = pd.concat(df_list, ignore_index=True) #表格列字段不同的表合并

# 删除未使用的列
df.drop(['zero', 'days'], axis=1, inplace=True) #drop函数默认删除行,列需要加axis = 1

# 每隔1~5秒记录一次数据,这种情况太频繁了。 将它减少到每分钟
df_min = df.iloc[::12, :] #每隔12行取一次
df_min.head(10)  #查看前5行
print ('Total GPS points: ' + str(df_min.shape[0]))  #df.shape():查看行数和列数

# 声明地图的中心,以及我们希望地图放大多少倍
gmap = gmplot.GoogleMapPlotter(df_min.lat[0], df_min.lng[0], 11)
gmap.plot(df_min.lat, df_min.lng)  #描绘轨迹点
gmap.draw("./class/ITS/user.html")   #显示图
print("over")

打开生成的user.html,就是绘好轨迹的地图啦。

在这里插入图片描述

参考文章:

  • 3
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
使用 Python 绘制 Google 地图上的轨迹需要使用 Google Maps API。您需要首先获取一个 Google 地图 API 密钥,然后使用 Python 的 requests 库向 Google Maps API 发送请求,以获取地图数据。然后,您可以使用 matplotlib 库或其他绘图库将数据可视化轨迹。 以下是一个示例代码,演示如何在 Google 地图上绘制一个简单的轨迹: ```python import requests import matplotlib.pyplot as plt # Replace YOUR_API_KEY with your actual API key API_KEY = "YOUR_API_KEY" # Define the starting and ending points of the track start = "40.748817,-73.985428" end = "37.7749,-122.419416" # Send a request to Google Maps API to get the directions url = f"https://maps.googleapis.com/maps/api/directions/json?origin={start}&destination={end}&key={API_KEY}" response = requests.get(url) data = response.json() # Extract the coordinates from the directions data points = [] for step in data["routes"][0]["legs"][0]["steps"]: points.extend(step["polyline"]["points"]) # Decode the polyline data into latitude and longitude pairs def decode_polyline(polyline_str): index, lat, lng = 0, 0, 0 coordinates = [] while index < len(polyline_str): b, shift = 0, 0 while True: b = ord(polyline_str[index]) - 63 index += 1 lat += (b & 0x1f) << shift shift += 5 if not b & 0x20: break shift, b = 0, 0 while True: b = ord(polyline_str[index]) - 63 index += 1 lng += (b & 0x1f) << shift shift += 5 if not b & 0x20: break coordinates.append((lat / 100000.0, lng / 100000.0)) return coordinates coordinates = decode_polyline("".join(points)) # Plot the track on a Google map fig, ax = plt.subplots(figsize=(8,8)) ax.plot([lng for lat, lng in coordinates], [lat for lat, lng in coordinates], color='blue', alpha=0.7, linewidth=3, solid_capstyle='round', zorder=2) ax.set_title('Track from New York to San Francisco') ax.set_xlim(min([lng for lat, lng in coordinates])-1, max([lng for lat, lng in coordinates])+1) ax.set_ylim(min([lat for lat, lng in coordinates])-1, max([lat for lat, lng in coordinates])+1) ax.axis('off') plt.show() ``` 在这个例子中,我们使用 Google Maps API 获取从纽约到旧金山的方向,并使用 matplotlib 绘制了轨迹。当然,您可以使用其他库和数据来绘制更复杂的轨迹

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值