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.io.*;
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<>();

        try
        {
            //parsing a CSV file into BufferedReader class constructor
            BufferedReader br = new BufferedReader(new FileReader(
                    "C:\\output_gps\\allgps.csv"));

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

            // consume the first line and ignore
            br.readLine();

            // 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)
        {
            e.printStackTrace();
        }

      // set up graphHopper
        Helper.removeDir(new File(GH_LOCATION));
        graphHopper = new GraphHopper();
        graphHopper.setOSMFile("C:\\map-matching\\files\\singapore-latest.osm.pbf");
        graphHopper.setGraphHopperLocation(GH_LOCATION);
        graphHopper.setProfiles(new Profile("my_profile").setVehicle("bike").setWeighting("fastest"));
        graphHopper.getLMPreparationHandler().setLMProfiles(new LMProfile("my_profile"));
        graphHopper.importOrLoad();


        // 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.setOut(ps);

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

        System.out.flush();
        System.setOut(old);

        String baosString = baos.toString();
        System.out.println(baosString);

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


}


}

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.

Hi, I´m also trying to plot paths and snapped points.

@Ms_CHEN_Xinyu What is the MapMatching2 class you are using? Is it different from the standard MapMatching? The MapMatching class has a obj.doWork method, but not the obj.match(coordinates) that you are using. Where do you get it from?

@easbar The same happens with fetchWayGeometry. I don’t find it in EdgeMatch.

Any help on what am I doing wrong?

How can I get a one to one list of snapped points from a list of observations?

You can do edgeMatches.get(0).getEdgeState().fetchWayGeometry(FetchMode.ALL);. I think this was a typo in my previous post (missing the getEdgeState() call).

Just in case it is useful for someone, I solved my issue with the following code:

Profile profile=new Profile("car");
profile.getHints().putObject("profile", "car");
MapHandler.getMapHandler().getHopper().setProfiles(profile);

mapMatching=new MapMatching(MapHandler.getMapHandler().getHopper(),profile.getHints());

matchResult=mapMatching.doWork(observations);
        
Path path = matchResult.getMergedPath();
PointList pointList= path.calcPoints(); 

I’m not using the previously mentioned MapMatching.match , but it can be found in:
https://github.com/graphhopper/graphhopper/blob/master/map-matching/src/main/java/com/graphhopper/matching/MapMatching.java