Correct GPS data using Map Matching API in Mapbox I tried to create a tool.
Map Matching API --Of the Mapbox APIs, we are using the Map Matching API, which is classified as Navigation service. --The Map Matching API is an API that snaps GPS data (latitude, longitude) containing errors onto the road network and corrects it to a probable position.
--This article uses the Map Matching API of Python3 and Mapbox. --Access Tokens are required to use Mapbox's Map Matching API. -When you register an account from Mapbox Site, Access Tokens will be issued automatically. --You can use these Access Tokens to call various APIs. --Mapbox is pay-as-you-go, so please check the Mapbox Pricing Page for detailed pricing. ――In addition, since the free tier is set for up to 50,000 requests every month, you will not be charged unless you make a large number of requests.
――The data used is GPS data acquired by the Garmin watch worn on your wrist when running a marathon event. --Please prepare GPS data as appropriate.
--Enter GPS data (latitude, longitude) in the input CSV file.
--Enter the input CSV file, output CSV file, and api_token (Access Tokens). --Do not change the "Map Matching API URL". --Run the Python script.
MapboxMapMatchingAPI.py
import os
import csv
import json
import datetime
import time
import configparser
import requests
#Input CSV file specification
input_csvfile = "Input Enter the CSV file."
#Output CSV file specification
output_csvfile = "Please enter the output CSV file."
#Map Matching API URL * Do not change.
api_url = "https://api.mapbox.com/matching/v5/mapbox/driving/"
# Mapbox Access token
api_token = "Please enter the Access token."
#Working list
data = []
wklst = []
#Set the input CSV file to the working list
with open(input_csvfile) as f:
#Skip header line
h = next(csv.reader(f))
for v in csv.reader(f):
data.append(v)
for i in range(len(data) -1):
wklst.append([data[i][0],data[i][1],data[i+1][0],data[i+1][1]])
# print(wklst)
#Existence check of output CSV file(Judgment of the presence or absence of a header)
if(os.path.exists(output_csvfile)):
header_flg = 0
else:
header_flg = 1
#Existence check of output CSV file(Judgment of the presence or absence of a header)
if(os.path.exists(output_csvfile)):
header_flg = 0
else:
header_flg = 1
#Request from the working list to Map Matching API and write the result to the output CSV file
with open(output_csvfile, 'a', encoding='shift_jis') as output_csvfile_fp:
fieldnames = ['bef_lat','bef_lng','aft_lat','aft_lng']
csvfile_writer = csv.DictWriter(output_csvfile_fp, fieldnames=fieldnames,lineterminator='\n')
#Output Header output when creating a new CSV file
if header_flg == 1:
csvfile_writer.writeheader()
header_flg == 0
print(len(wklst))
for i in range(len(wklst)):
# print(wklst[i])
#Map Matching API parameter generation
origins = wklst[i][1] + ','+ wklst[i][0]
destinations = wklst[i][3] + ','+ wklst[i][2]
params = {
'radiuses':'50;50',
'access_token': api_token
}
#Request to Map Matching API
raw_response = requests.get(api_url + origins + ';' + destinations, params)
parsed_response = json.loads(raw_response.text)
parsed_response_json = json.dumps(parsed_response, indent=4)
# print(parsed_response_json)
breakpoint
#Extract elements from JSON
if parsed_response['code'] == 'Ok':
lng1 = parsed_response['tracepoints'][0]['location'][0]
lat1 = parsed_response['tracepoints'][0]['location'][1]
lng2 = parsed_response['tracepoints'][1]['location'][0]
lat2 = parsed_response['tracepoints'][1]['location'][1]
else:
lng1 = None
lat1 = None
lng2 = None
lat2 = None
#Write to output CSV file
csvfile_writer.writerow({
'bef_lat': wklst[i][0],
'bef_lng': wklst[i][1],
'aft_lat': lat1,
'aft_lng': lng1
})
print(wklst[i][0],wklst[i][1],lat1,lng1)
print(u'Processing Exit')
--The GPS data (latitude, longitude) before correction is in the first and second columns. --The corrected GPS data (latitude, longitude) will be in the 3rd and 4th columns.
――The execution result is visualized in QGIS as follows. --Blue points are GPS data (latitude, longitude) before correction, and red points are GPS data (latitude, longitude) after correction. ――The blue point that was off the road before the correction is snapped onto the road after the correction, and it is corrected to a certain position (red point).
** * Please note that you will be charged if you use the API beyond the free frame (> _ <) **
Recommended Posts