How to plot the map matching result using edgeID

Hi. I am new to graphhopper and java. I am trying to plot the paths and snapped points by using the returned edgeID and snapped coordinates. Here are the codes I run the map matching function in MapMatching class.

package com.graphhopper.matching;
import com.graphhopper.GraphHopper;
import com.graphhopper.config.LMProfile;
import com.graphhopper.config.Profile;
import com.graphhopper.util.Helper;
import com.graphhopper.util.PMap;
import com.graphhopper.util.shapes.GHPoint;

import java.util.*;
import java.util.ArrayList;
import java.util.List;

public class testMapMatching2 {

    // declare attributes
    private static final String GH_LOCATION = "../target/mapmatchingtest-ch";
    private static GraphHopper graphHopper;

    public static void main(String args[]) throws Exception {

        // import gps data from the csv file
        String line = "";
        String splitBy = ",";

        List<Observation> coordinates = new ArrayList<>();

            //parsing a CSV file into BufferedReader class constructor
            BufferedReader br = new BufferedReader(new FileReader(

            // create the index
            ListIterator<Observation> iter = coordinates.listIterator();

            // consume the first line and ignore

            // read the data line by line
            while ((line = br.readLine()) != null)   //returns a Boolean value
                String[] coordinate = line.split(splitBy);    // use comma as separator
                coordinates.add(iter.nextIndex(),new Observation(new GHPoint( Double.valueOf(coordinate[1]),
                       Double.valueOf(coordinate[2])))); // add each line into the arraylist
//            System.out.println(coordinates); // check the output
        catch (IOException e)

      // set up graphHopper
        Helper.removeDir(new File(GH_LOCATION));
        graphHopper = new GraphHopper();
        graphHopper.setProfiles(new Profile("my_profile").setVehicle("bike").setWeighting("fastest"));
        graphHopper.getLMPreparationHandler().setLMProfiles(new LMProfile("my_profile"));

        // set up hints - set the profile
        PMap hintsObj = new PMap();
        PMap hints = hintsObj.putObject("profile", "my_profile");

        // conduct map matching - call match()
        MapMatching2 obj = new MapMatching2(graphHopper, hints);
        MatchResult2 mr = obj.match(coordinates);

        // store the output into String
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PrintStream ps = new PrintStream(baos);
        PrintStream old = System.out;

        System.out.println("edge_ID," + "snap1," + "snap2," + "snap3," + "snap4," + "\n" + mr);


        String baosString = baos.toString();

        // store String into a text file
            FileWriter fw=new FileWriter("C:\\distanceCalculation\\output\\distanceAMK.txt");
        }catch(Exception e){System.out.println(e);}



For the result, I can get the edgeID and the coordinates of snapped points. Is there a way to plot the paths by using the edgeID? Can the edgeID link to OSM wayID? Thank you in advance.

You can get the coordinates (PointList) of each edge like this:

List<EdgeMatch> edgeMatches = mr.getEdgeMatches();
PointList pointList = edgeMatches.get(0).fetchWayGeometry(FetchMode.ALL);

The OSM way ID is not included by default. But searching the forum you should be able to figure out how to do this.

Powered by Discourse