簡體   English   中英

如何給出開始,結束坐標

[英]How to give start,end coordinates

我是qgis的新手,在這里我想在地圖上(道路矢量層)的兩個選定點之間找到一條路徑。 用戶使用鼠標單擊來選擇這些點。 因此,這里我使用astar算法來查找兩點之間的路徑。

*********************************** astar.py **************** ******************

import heapq

class AStar(object):  
    def __init__(self, graphAstar):  
        self.graphAstar = graphAstar

    def heuristic(self, node, start, end):
        raise NotImplementedError

    def search(self, start, end):
    openset = set()
        closedset = set()
        current = start
        openHeap = []
        openset.add(current)
        openHeap.append((0,current))
        while openset:
        temp = heapq.heappop(openHeap)
        current = temp[1]
            if current == end:
                path = []
                while current.parent:
                    path.append(current)
                    current = current.parent
                path.append(current)
                return path[::-1]
            openset.remove(current)
            closedset.add(current)
            for node in self.graphAstar[current]:
                if node in closedset:
                    continue
                if node in openset:
                    new_g = current.gg + current.move_cost(node)
                    if node.gg > new_g:
                        node.gg = new_g
                        node.parent = current
                else:
                    node.gg = current.gg + current.move_cost(node)
                    node.H = self.heuristic(node, start, end)
                    node.parent = current
                    openset.add(node)
            heapq.heappush(openHeap, (node.H,node))
        return None

    class AStarNode(object):
        def __init__(self):
            self.gg = 0
            self.H = 0
            self.parent = None

        def move_cost(self, other):
            raise NotImplementedError

*****************************astar_grid.py*******************************

    from astar import AStar, AStarNode
    from math import sqrt

    class AStarGrid(AStar):
        def heuristic(self, node, start, end):
            return sqrt((end.x - node.x)**2 + (end.y - node.y)**2)

    class AStarGridNode(AStarNode):
        def __init__(self, x, y):
            self.x, self.y = x, y
            super(AStarGridNode, self).__init__()

        def move_cost(self, other):
            diagonal = abs(self.x - other.x) == 1 and abs(self.y - other.y) == 1
            return 14 if diagonal else 10

在主代碼中,以下方法用於從矢量層創建圖形。

************************** plugin.py ********************* *************

def make_graph(self, mapinfo):
    nodes = [[AStarGridNode(x, y) for y in range(mapinfo['height'])] for x in range(mapinfo['width'])]
    graphAstar = {}
    for x, y in product(range(mapinfo['width']), range(mapinfo['height'])):
        node = nodes[x][y]
        graphAstar[node] = []
        for i, j in product([-1, 0, 1], [-1, 0, 1]):
                if not (0 <= x + i < mapinfo['width']): continue
                if not (0 <= y + j < mapinfo['height']): continue
                graphAstar[nodes[x][y]].append(nodes[x+i][y+j])
    return graphAstar, nodes

我在FindRoutes方法中調用了該方法。

def findRoutes(self):
vl=self.canvas.currentLayer()
director = QgsLineVectorLayerDirector( vl, -1, '', '', '', 3 )
properter = QgsDistanceArcProperter()
director.addProperter( properter )
crs = self.canvas.mapRenderer().destinationCrs()
builder = QgsGraphBuilder( crs )

global x1
global y1
global x2
global y2
pStart = QgsPoint( x1, y1 )
pStop = QgsPoint( x2, y2 )
graphAstar, nodes = self.make_graph({ "width": 8, "height": 8 })
paths = AStarGrid(graphAstar)

start, end = ??
path = paths.search(start, end)

我的問題是,如何將起點和終點坐標傳遞給上面的函數? 因為將它們作為坐標傳遞(起點,終點= pStart,pStop)不起作用。 如何將它們添加到作為節點創建的圖形中? 還是有任何簡單的方法嗎?

請幫助我找到解決此問題的方法。 謝謝

當我做astar時,我使用的節點是astar的實習生,並且包含參考點和原始點對象(您的位置元組)。

也許與您的AStarGridNode

在您的情況下:

start = AStarGridNode(x1, y1)
stop = AStarGridNode(x2, y2)

這部分可能在您的搜索功能中,以向用戶隱藏。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM