geotools的最短路径实现

Geotools提供了一个Graph的扩展包,使用它可以实现最短路径的查找,提供的算法有Dijkstra和AStarApi的功能非常强大,只需要提供linefeatures对象,即可创建graph,然后调用算法即可实现最短路径查找,权重可以自由设置,对于不懂算法的人用起来也毫不费力。

Dijkstra的使用

String filePath = "E:\\gis资料\\测试数据\\道路中心线.shp";
//读取shp数据
DataStore dataStore = readShapeFile(filePath);
SimpleFeatureSource featureSource = dataStore.getFeatureSource(dataStore.getTypeNames()[0]);
SimpleFeatureCollection simFeatureCollect =featureSource.getFeatures();

final Integer num = new Integer(0);
System.out.println("shp文件原始线的个数:" + simFeatureCollect.size());

 

//创建graph数据结构
Graph graph = buildGraph(simFeatureCollect);

 

//这里是定义权重

DijkstraIterator.EdgeWeighter weighter = new DijkstraIterator.EdgeWeighter(){
    @Override
    public double getWeight(Edge edge) {

//这个方法返回的值就是权重,这里使用的最简单的线的长度

//如果有路况、限速等信息,可以做的更复杂一些
        SimpleFeature feature = (SimpleFeature)edge.getObject();
        Geometry geometry = (Geometry)feature.getDefaultGeometry();

        return geometry.getLength();
    }
};
Date startT = new Date();

//初始化查找器
DijkstraShortestPathFinder pf = new DijkstraShortestPathFinder(graph,start,weighter);
pf.calculate();

//传入终点,得到最短路径
Path path = pf.getPath(destination);
Date end = new Date();
System.out.println("迪杰斯特拉算法耗时:" +(end.getTime() - startT.getTime()));
System.out.println("迪杰斯特拉算法距离:"+getPathLength(path));
System.out.println(destination.getID()+""+start.equals(destination));

 

 

//AStar算法

public static void AStarShortestPath(Graph graph,Node startNode,Node endNode){
    AStarIterator.AStarFunctions aStarFunction = new  AStarIterator.AStarFunctions(endNode){
        @Override
        public double cost(AStarIterator.AStarNode aStarNode, AStarIterator.AStarNode aStarNode1) {
           Edge edge = aStarNode.getNode().getEdge(aStarNode1.getNode());
            SimpleFeature feature = (SimpleFeature)edge.getObject();
            Geometry geometry = (Geometry)feature.getDefaultGeometry();
            //System.out.println(aStarNode.getH());
            return geometry.getLength();
        }

        @Override
        public double h(Node node) {
            return -10;
        }
    };
    Date start = new Date();
    AStarShortestPathFinder aStarPf = new AStarShortestPathFinder(graph,startNode,endNode,aStarFunction);
    try {
        aStarPf.calculate();
        Date end = new Date();
        System.out.println("AStar算法耗时:" +(end.getTime() - start.getTime()));
        System.out.println("AStar算法距离:" + getPathLength(aStarPf.getPath()));
    } catch (Exception e) {
        e.printStackTrace();
    }

}

 

 

 

AStar算法使用也很简单,可参考api使用文档。两个算法效率比较下来,AStar算法效率更好。算法验证和效率比较:

使用同样的起点和终点,分别调用上面两个算法,计算结果如下:

shp文件原始线段的个数:67749

AStar算法耗时:84ms

AStar距离:0.2307215100346536

迪杰斯特拉耗时:188ms

迪杰斯特拉距离:0.2307215100346536

转载于:https://www.cnblogs.com/yinchuanqi/p/5607650.html

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值