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') addresses = QgsMapLayerRegistry.instance().mapLayersByName('Chicago Test Layer.shp') 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 tStop = tiedPoints 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)