GIS 通过osm2po离线最短路径和路径规划后台计算

最近做了一个离线的最短计算和代码,原来打算拿PG通过数据库计算,但是数据不好构造,也达不到商用价值,最后终于参考别人的,用OSM2PO来计算最短路径,买了一份POI数据,做了下兴趣点查询,一下是代码

public static double getDistanceByMap(List<String> wkts) throws Exception {
        ClassPathResource resource = new ClassPathResource("data/shanxi-latest.osm.gph");    // static/pattern下的 test.txt文件
        //File file = ResourceUtils.getFile("classpath:data/shanxi-latest.osm.gph");
        Graph graph = new Graph(resource.getInputStream());
        Properties params = new Properties();
        params.setProperty("findShortestPath", "true");
        params.setProperty("ignoreRestrictions", "false");
        params.setProperty("ignoreOneWays", "false");
        params.setProperty("heuristicFactor", "0.0"); // 0.0 Dijkstra, 1.0 good A*
        double distance = 0;
        for (int i = 0; i < wkts.size() - 1; i++) {
            double tempdistance = 0;
            WKTReader reader = new WKTReader(JTSFactoryFinder.getGeometryFactory());
            Point beginpoint = (Point) reader.read(wkts.get(i));
            Point endpoint = (Point) reader.read(wkts.get(i + 1));
            LatLon source = new LatLon(beginpoint.getY(), beginpoint.getX());
            LatLon target = new LatLon(endpoint.getY(), endpoint.getX());
            int[] vertexIds = findClosestVertexIds(graph, source, target);

            TspDefaultMatrix matrix = new TspDefaultMatrix(graph, vertexIds, Float.MAX_VALUE, null, params);
            float[][] distances = matrix.getCosts();
            for (int indexI = 0; indexI < distances.length; indexI++) {
                for (int indexJ = 0; indexJ < distances.length; indexJ++) {
                    System.out.println(distances[indexI][indexJ]);
                    if(distances[indexI][indexJ] > 0.5) {
                        tempdistance = distances[indexI][indexJ];
                    }
                }
            }
            distance+=tempdistance;
        }
        graph.close();
        return distance;
    }

POM文件的信息

  <dependency>
            <groupId>de.cm.osm2po</groupId>
            <artifactId>osm2po-core</artifactId>
            <version>5.2.99</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/lib/osm2po-core-5.2.99.jar</systemPath>
        </dependency>

数据的下载可以去OSM官网查询下,有关PDF数据生成gph,
参考:https://blog.csdn.net/zhu1361/article/details/51691456
osm2po地址:http://osm2po.de/

你可能感兴趣的:(osM)