Let's try the HERE Route Matching API QGIS Plugin
Tags: qgis, gis, opensource, showdev
Published: 2022-11-17 07:02:41

Original article (Japanese): https://qiita.com/Kanahiro/items/7d588f238df9be1e8eea

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/knr030o54bzspriqampa.gif)

## Introduction
In the world of GIS, there is a technique called “map matching” or “route matching”, which fits GPS or other location data to roads.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6malj1f2tmuhx90vip3n.png)

The simplest way to explain the technique of “route matching”, is that it is the operation used to move a point to the closest geographic feature. However, as shown below, it is not possible to create a suitable path simply by moving points. Matching based on the order of the points, and the deviation of each point is required.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ixoh58n4vknao6l0phsw.png)

Route searching technique is necessary to solve this problem. Through some Googling, you may find that some people would suggest creating your own route searching server. However, it is a difficult task for not only ordinary users of GIS, but also GIS technicians and engineers. However, this operation can also be interpreted as “inputting a series of points, and receiving the most likely line”, and there are multiple services that provides WebAPI for such an operation. 

- [HERE Route Matching](https://developer.here.com/documentation/route-matching/dev_guide/index.html)
- [Mapbox Map Matching] (https://docs.mapbox.com/api/navigation/map-matching/)
- [Its-mo Navi map matching] (https://support.e-map.ne.jp/manuals/v3/?q=road_path_drive_api_man)

MIERUNE, which is great at developing QGIS plugins developed a plugin that uses such services to implement route matching in QGIS. In developing the plugin, we decided to use the HERE API, which is thought to be the strongest in route searching technology. This article discusses the technical details and how to use the HERE API plugin. 

## About HERE’s API
HERE publishes a wide range of APIs on location information and services, including APIs for maps, geocoding and route searching.  HERE even publishes 5 APIs for route searching, which are shown below. (excluding the 2 deprecated APIs, as of July 2022)

- HERE Routing API v8 (For route searching)
- HERE Isoline Routing API v8 (For calculating service areas)
- HERE Matrix Routing API v8 (For calculating routes when there are multiple points of departure and arrival)
- **HERE Route Matching v8**
- HERE Waypoints Sequence v8 (For solving the traveling salesman problem)

When only the types of APIs are considered, Mapbox also provides the same types of API. However, HERE’s route searching service also considers the realtime state of the traffic lights, search for routes that trucks can pass through, and even take in mind the range of electric vehicles (EV). It can be said that HERE’s API has a wide range of functionalities that are focused for logistics and transportation companies. 

The plugin that we developed uses the **HERE Route matching v8** API, and if there is demand for other plugins, we are also considering the development of plugins for the other APIs as well.

## HERE Route API plugin
{% embed https://github.com/MIERUNE/qgis-here-api-plugin %}
QGIS has to be install prior to using the plugin. Although the plugin works for any version of QGIS that starts with 3, we suggest using the latest LTR of QGIS, as the plugin may not work in versions that are too old.

## Manual for the plugin

### Install
1. Open “manage and install plugins”
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oqgxiy12y46erbm6816v.png)

2. Search for HERE in the list of QGIS plugins and install the HERE Route API Plugin
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2d7ewnjis4y3vf0he8tf.png)

3. Confirm that the HERE Route API Plugin is installed in the plugin menu
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3wfvgw8nbwmip9t53ng2.png)


### Setting up the API key
1. Get an API key at [HERE Platform] (https://platform.here.com/)
2. From the menu bar, open "Plugin", "HERE Route API Plugin", "Config", insert your API key and click on "OK"
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8rdh6aob4xelik378oql.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0gc6q3ui23s2mfis6wow.png)

### Route matching
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0jmxgh61sixilaedas4y.png)

Route matching for this plugin conducts operations in which “The plugin takes any point layer as input, and outputs the likeliest line layer”. Because of the operation conducted by the plugin, this manual considers that a point layer exists in the QGIS project. (However, the number of features in the point layer is less than 10,000) The following explanation takes the point layer in the above image as the input. Moreover, as the API may only take 400 features at once, the request will be divided when the input layer has more than 400 features. More specifically, the plugin sends 400 features in each request, and then combine the results given by the APIs. From the menu bar open “plugin”, “HERE Route API Plugin”, and then “Route Matching”.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oykksbpaql5zoe6bodtz.png)

1. Select the target point layer in the Layer pulldown list
2. From the Sort Field pulldown list select the field that contains the order in which the points are lined up in (Optional, if it is empty, the fid field will automatically be used
3. From the Mode pulldown list, select car, bus, bicycle or pedestrian
4. Click on OK to start the operation
5. When the operation is finished, the results will be shown

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r8u7upf1gyg9qxr87b0j.png)

With some acceptable deviations, the likeliest route that the points took will be shown.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a7lod599i7nwp6o1udz8.png)

### Conclusion
Actually, the Route Matching API accepts a large amount of parameters. The input parameters used in the plugin is only the most general few of them. It is very difficult to utilize all of the input parameters, which also shows that the API supports a variety of use cases. As the QGIS plugin introduced in this article only covers a part of the functionalities, please also try the API yourself!