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.

Powered by Discourse