Skip to content

Instantly share code, notes, and snippets.

@karussell
Last active July 15, 2019 08:11
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karussell/cc8f141092979ee142fb to your computer and use it in GitHub Desktop.
Save karussell/cc8f141092979ee142fb to your computer and use it in GitHub Desktop.
/*
* Licensed to Peter Karich under one or more contributor license
* agreements. See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*
* Peter Karich licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the
* License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.graphhopper.util;
import com.graphhopper.GraphHopper;
import com.graphhopper.routing.util.EdgeFilter;
import com.graphhopper.storage.index.LocationIndex;
import com.graphhopper.storage.index.QueryResult;
import java.util.ArrayList;
import java.util.List;
/**
* @author Peter Karich
*/
public class MapMatchingExample1
{
public static void main( String[] args )
{
new MapMatchingExample1().start(args);
}
private void start( String[] args )
{
GraphHopper hopper = new GraphHopper().
init(CmdArgs.read(args)).
disableCHShortcuts().
setOSMFile("your-area.pbf").
// we look 20meter around the point, e.g. the device has such an error for GPS measurement
setPreciseIndexResolution(20);
hopper.importOrLoad();
LocationIndex locationIndex = hopper.getLocationIndex();
// User task: get the list somewhere
List<GPXEntry> gpxList = getList();
List<QueryResult> result = new ArrayList<QueryResult>();
for (int i = 0; i < gpxList.size(); i++)
{
GPXEntry gpx = gpxList.get(i);
QueryResult closest = locationIndex.findClosest(gpx.getLat(), gpx.getLon(), EdgeFilter.ALL_EDGES);
if (!closest.isValid())
throw new IllegalStateException("no close edge found for " + gpx);
result.add(closest);
}
System.out.println(result);
}
private List<GPXEntry> getList()
{
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
/*
* Licensed to Peter Karich under one or more contributor license
* agreements. See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*
* Peter Karich licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the
* License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.graphhopper.util;
import com.graphhopper.GraphHopper;
import com.graphhopper.routing.DijkstraBidirectionRef;
import com.graphhopper.routing.Path;
import com.graphhopper.routing.util.EdgeFilter;
import com.graphhopper.routing.util.EncodingManager;
import com.graphhopper.routing.util.FastestWeighting;
import com.graphhopper.routing.util.FlagEncoder;
import com.graphhopper.routing.util.Weighting;
import com.graphhopper.storage.RAMDirectory;
import com.graphhopper.storage.index.LocationIndex;
import com.graphhopper.storage.index.LocationIndexTree;
import com.graphhopper.storage.index.QueryResult;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* @author Peter Karich
*/
public class MapMatchingExample2
{
public static void main( String[] args )
{
new MapMatchingExample2().start(args);
}
private void start( String[] args )
{
GraphHopper hopper = new GraphHopper().
// provide a config ...
// init(CmdArgs.read(args)).
// or do it programmatically:
disableCHShortcuts().
setEncodingManager(new EncodingManager("CAR")).
setGraphHopperLocation("graphhopper-out-folder/").
setOSMFile("/media/SAMSUNG/maps/berlin.pbf");
hopper.importOrLoad();
LocationIndex locationIndex = createLocationIndex(hopper);
List<GPXEntry> gpxList = getList();
List<QueryResult> queryResults = new ArrayList<QueryResult>();
for (int i = 0; i < gpxList.size(); i++)
{
GPXEntry gpx = gpxList.get(i);
QueryResult closest = locationIndex.findClosest(gpx.getLat(), gpx.getLon(), EdgeFilter.ALL_EDGES);
if (!closest.isValid())
throw new IllegalStateException("no close edge found for " + gpx);
queryResults.add(closest);
}
final Map<Integer, Double> edgesMap = convert(queryResults);
final FlagEncoder flagEncoder = hopper.getEncodingManager().getSingle();
Weighting weighting = new FastestWeighting(flagEncoder)
{
@Override
public double calcWeight( EdgeIteratorState edge, boolean reverse )
{
Double value = edgesMap.get(edge.getEdge());
double speed;
if (reverse)
speed = flagEncoder.getReverseSpeed(edge.getFlags());
else
speed = flagEncoder.getSpeed(edge.getFlags());
if (value == null)
// Do not completely exclude edges which are not in our edgesMap
// because this could be just a signal loss or tunnel etc
return 10000 * edge.getDistance() / speed;
// The larger the distance to the edge is the more penalty we put on it
return (edge.getDistance() + value * 5) / speed;
}
};
QueryResult firstQR = queryResults.get(0);
QueryResult lastQR = queryResults.get(queryResults.size() - 1);
DijkstraBidirectionRef dijkstra = new DijkstraBidirectionRef(hopper.getGraph(), flagEncoder, weighting);
Path p = dijkstra.calcPath(firstQR, lastQR);
System.out.println(p);
// Now loop over the edges and associate the GPX points to them with the help of our QueryResults which contains
// the snapped point AND the edge id.
// p.calcEdges();
}
// User task: get the list e.g. from text file
private List<GPXEntry> getList()
{
throw new UnsupportedOperationException("Not supported yet.");
}
// User task: convert the query results in a Map which maps edgeId (obtained via queryResult.getClosestEdge().getEdge())
// to distance (queryResult.getQueryDistance())
private Map<Integer, Double> convert( List<QueryResult> queryResult )
{
// loop and do: map.put(qr.getClosestEdge().getEdge(), qr.getQueryDistance());
throw new UnsupportedOperationException("Not supported yet.");
}
// User task: subclass LocationIndexTree and implement a method which returns the 3 best matching edges
// instead of only one.
private LocationIndex createLocationIndex( GraphHopper hopper )
{
LocationIndex locationIndex = new LocationIndexTree(hopper.getGraph(), new RAMDirectory());
locationIndex.setResolution(20);
locationIndex.prepareIndex();
return locationIndex;
}
}
/*
* Licensed to Peter Karich under one or more contributor license
* agreements. See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*
* Peter Karich licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the
* License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.graphhopper.util;
import com.graphhopper.GraphHopper;
import com.graphhopper.routing.util.EdgeFilter;
import com.graphhopper.storage.index.LocationIndex;
import com.graphhopper.storage.index.QueryResult;
import java.util.ArrayList;
import java.util.List;
/**
* @author Peter Karich
*/
public class MapMatchingExample3
{
public static void main( String[] args )
{
new MapMatchingExample3().start(args);
}
private void start( String[] args )
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment