I created a tool to correct GPS data with Mapbox Map Matching API (Mapbox Map Matching API)

Purpose

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.

Execution environment

--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.

Prerequisites

Usage data

――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.

Execution method

Input settings

--Enter GPS data (latitude, longitude) in the input CSV file.

image.png

Run

--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')

Execution result

--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.

image.png

――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).

image.png

Points to note

** * Please note that you will be charged if you use the API beyond the free frame (> _ <) **

Recommended Posts

I created a tool to correct GPS data with Mapbox Map Matching API (Mapbox Map Matching API)
I tried to draw a route map with Python
I created a Python library to call the LINE WORKS API
[3rd] I tried to make a certain authenticator-like tool with python
[4th] I tried to make a certain authenticator-like tool with python
[1st] I tried to make a certain authenticator-like tool with python
I tried to introduce a serverless chatbot linked with Rakuten API to Teams
I made a tool to automatically browse multiple sites with Selenium (Python)
I tried to make "Sakurai-san" a LINE BOT with API Gateway + Lambda
Send data to DRF API with Vue.js
I made a Hex map with Python
I tried to make a Web API
I made a tool to convert Jupyter py to ipynb with VS Code
I created a password tool in Python.
[Patent analysis] I tried to make a patent map with Python without spending money
I made a tool to easily display data as a graph by GUI operation.
I tried to make a castle search API with Elasticsearch + Sudachi + Go + echo
I tried to make a suspicious person MAP quickly using Geolonia address data
I tried to make a simple image recognition API with Fast API and Tensorflow
I want to analyze songs with Spotify API 2
I tried to save the data with discord
I want to knock 100 data sciences with Colaboratory
I want to make a game with Python
I tried to get CloudWatch data with Python
I want to analyze songs with Spotify API 1
Folium: Visualize data on a map with Python
I made a tool to compile Hy natively
I played with Diamond, a metrics collection tool
I made a tool to get new articles
I want to write to a file with Python
I created a stacked bar graph with matplotlib in Python and added a data label
Here is a mechanism to reduce fraudulent papers. Data material created with iPython Notebook.
I wrote a CLI tool in Go language to view Qiita's tag feed with CLI
I tried scraping food recall information with Python to create a pandas data frame
I tried to create a table only with Django
I want to give a group_id to a pandas data frame
I want to transition with a button in flask
Try to make a command standby tool with python
I want to climb a mountain with reinforcement learning
I want to work with a robot in python.
I want to split a character string with hiragana
I tried to uncover our darkness with Chatwork API
I tried to automatically generate a password with Python3
I want to manually create a legend with matplotlib
I tried collecting data from a website with Scrapy
I tried to analyze J League data with Python
I made a repeating text data generation tool "rpttxt"
Upload to a shared drive with Google Drive API V3
I want to run a quantum computer with Python
Deploy a web app created with Streamlit to Heroku
I want to bind a local variable with lambda
I created an environment for Masonite, a Python web framework similar to Laravel, with Docker!
I tried to build a service that sells machine-learned data at explosive speed with Docker
I tried to implement a volume moving average with Quantx
I want to be able to analyze data with Python (Part 3)
I tried to search videos using Youtube Data API (beginner)
A network diagram was created with the data of COVID-19.
I tried to make various "dummy data" with Python faker
I tried to automatically create a report with Markov chain
[Linux] Copy data from Linux to Windows with a shell script
I want to be able to analyze data with Python (Part 1)