2

This code is meant to automatically go through two sets of points, and determine distances from each point in one set to each point in the other set. However,the code does not appear to actually cycle through the stations. (When I test this, I should get 9 different distances, but instead each loop of three repeats the same set of numbers. Also, the pStop point doesn't change when I test it.)

The code does work when i just link 1 station to 1 address.

stations =QgsMapLayerRegistry.instance().mapLayersByName('CTA_Stations_crs')[0]
addresses = QgsMapLayerRegistry.instance().mapLayersByName('Chicago Test Layer.shp')[0]

for train in range(0, 3):

    stationID = QgsFeatureRequest().setFilterFid(train)
    stationStation = next(stations.getFeatures(stationID))

    addressData=addresses.dataProvider()
    stationName = stationStation.attribute('LONGNAME')
    addressData.addAttributes([QgsField(stationName,QVariant.Double)])
    addresses.updateFields()

    for house in range(0,3):

        addressID = QgsFeatureRequest().setFilterFid(house)
        addressAddress = next(addresses.getFeatures(addressID))

        graphLayer = mmqgis_find_layer('Link.shp')
        director = QgsLineVectorLayerDirector(graphLayer, -1, '', '', '', 3)
        properter = QgsDistanceArcProperter()
        director.addProperter(properter)
        builder = QgsGraphBuilder(graphLayer.crs())

        pStart = QgsPoint(addressAddress.attribute('Longitude'), addressAddress.attribute('Latitude'))
        pStop = QgsPoint(stationStation.attribute('POINT_X'), stationStation.attribute('POINT_Y'))
        tiedPoints = director.makeGraph(builder, [pStart, pStop])
        graph = builder.graph()

        tStart = tiedPoints[0]
        tStop = tiedPoints[1]

        idStart = graph.findVertex(tStart)
        idStop = graph.findVertex(tStop)

        (tree, cost) = QgsGraphAnalyzer.dijkstra(graph, idStart, 0)
        Distance = cost[graph.findVertex(pStop)]/1840.18514097
        print(Distance)
        addresses.startEditing()
        attrIndex = addressData.fieldNameIndex(stationName)
        addresses.changeAttributeValue(addressAddress.id(), attrIndex, Distance)
        addresses.commitChanges()
        iface.vectorLayerTools().stopEditing(addresses) 
1

More fooling around and I have my answer. In

pStop = QgsPoint(stationStation.attribute('POINT_X'), stationStation.attribute('POINT_Y'))

point_x and point_y are different coordinates than everything else, and instead of not working, the graph function turned them all into the same point, which somehow connected properly. Changing it to

pStop = QgsPoint(stationStation.geometry().asPoint().x(), stationStation.geometry().asPoint().y())

fixed this problem, and the loop part works fine. (A different issue was created, but that's not part of this question.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.