问题
My App uses openstreetmap files to show waypoint (or coordinates) on a map.
Based on an Openstreet map file: how can I determine the shortest distance from a GPS coordinate to the (walking) path.
Can I read an OSM (map) or .osm file ... to access the paths?
回答1:
There are some misconceptions. First, the OSM file is not the map file. The OSM file can be used to create a format to make map rendering fast e.g. mapsforge format does this.
With e.g. GraphHopper you import the OSM file like .pbf or .osm which then creates a graph which can be used for problems you described. Not exactly sure what problem you have though ;)
回答2:
The following solution works:
- Get the graphhopper project in your IDE.
- Get the raw Openstreetmap data (.pbf) via this very nice provider.
- Run this command in your (git) bash shell: ./graphhopper.sh -a import -i gelderland-latest.osm.pbf
Make a new (maven) project with this pom.xml file:
nl.xyz graphhopper 1.0 system ${project.basedir}/libs/graphhopper-web-1.0-SNAPSHOT.jar
Copy generated graphhopper-web-1.0-SNAPSHOT.jar from the graphhopper project to the libs folder of your new project so it will be included in your path.
Copy the generated graphhopper data to a folder within your small project.
Create this simple demo:
public static void main(String[] args) { private static String mapsFolder = "graphhopper-data/yourlocation-latest.osm-gh"; GraphHopper graphHopper = new GraphHopper().forMobile(); graphHopper.load(mapsFolder); System.out.println("Found graph " + graphHopper.getGraphHopperStorage().toString() + ", nodes:" + graphHopper.getGraphHopperStorage().getNodes()); QueryResult queryResult = graphHopper.getLocationIndex().findClosest( 52.11111, 6.111111, EdgeFilter.ALL_EDGES); double distance = queryResult.getQueryDistance(); System.out.println( "Shortest distance is: " + distance); }
UPDATE: Using the JAR on Android may give a lot of compiler errors due to library version mismatchers. A better solution for getting this example run at Android, is using this dependency in your build.gradle:
implementation 'com.graphhopper:graphhopper-core:1.0-pre33'
Consequence is that you have to set the default routing profile. You can achieve via this change:
graphHopper = new GraphHopper().forMobile();
// Next line:
graphHopper.setProfiles( Collections.singletonList(new ProfileConfig("my_car").setVehicle("car").setWeighting("fastest")));
graphHopper.load(routingDataFolder);
I hope you enjoy this wonderful 'graphhopper' software!
来源:https://stackoverflow.com/questions/26688063/reading-osm-map-or-other-format-files-to-determine-shortest-distance-from-coor