Article Directory
Because you need to draw a GPS data map trajectory, it can be drawn very well based on gmplot, so let's get started!
Install the gmplot module
pip install gmplot
Obtain GPS open source dataset
GeoLife GPS Trajectories Dataset from Microsoft Research Asia . This data set contains GPS trajectories of 182 users collected in 3 years.
Just download and unzip.
Call gmplot module to read GPS data and realize map trajectory drawing
You can summarize the usage of the API on the github website of gmplot .
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")
Open the generated user.html
map and draw the trajectory map.
Reference article: