Skip to main content
added 36 characters in body
Source Link
Taras
  • 34.1k
  • 4
  • 73
  • 148

Your code is working for me without a problem. I've trytried it in the QGIS python consolePython Console.

When iI run it with a first start and end point, iI get the red line in the cable layer then iI run the script with othersother start and end pointpoints and iI get the yellow line added in the same layer.   

enter image description here

and what iI get in the attribute table (length and geom calculatecalculated with the field calculator) enter image description here

I just removeremoved the part of the code using QgsRubberBand as it was useless. The

The code I used:

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
from qgis.gui import *
from qgis.networkanalysis import *

#building the graph**
vl = qgis.utils.iface.mapCanvas().currentLayer()
director = QgsLineVectorLayerDirector(vl, -1, '', '', '', 3)
properter = QgsDistanceArcProperter()
director.addProperter(properter)
crs = qgis.utils.iface.mapCanvas().mapRenderer().destinationCrs()
builder = QgsGraphBuilder(crs)

#coordinates of the Start point and endPoint** 
pStart = QgsPoint(-1.041,0.916)
pStop = QgsPoint(-0.813,0.152)
tiedPoints = director.makeGraph(builder, [pStart, pStop])
graph = builder.graph()

#Calculation of the shortest path**    
tStart = tiedPoints[0]
tStop = tiedPoints[1]

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

(tree, cost) = QgsGraphAnalyzer.dijkstra(graph, idStart, 0)

if tree[idStop] == -1:
  print "Path not found"
else:
  p = []
  curPos = idStop
  while curPos != idStart:
    pnt = graph.vertex(graph.arc(tree[curPos]).inVertex()).point()

    p.append(pnt)

    curPos = graph.arc(tree[curPos]).outVertex()

  p.append(tStart)

 #I build here a polyline based on the points in the p[] array
geom = QgsGeometry.fromPolyline(p)


 # I add here my result in a memory layer called cable and i want to do it for every feature i get after transforming the qgsrubberband object     

v_layer = None
for layer in QgsMapLayerRegistry.instance().mapLayers().values():
   if layer.name() == 'cable':
        v_layer = layer
if v_layer is None: 
    v_layer = QgsVectorLayer("LineString", "cable", "memory")
    v_layer.addAttribute(QgsField("id", QVariant.String))
    v_layer.addAttribute(QgsField("type", QVariant.String))
    QgsMapLayerRegistry.instance().addMapLayers([v_layer])

pr = v_layer.dataProvider()
v_layer.startEditing()


seg = QgsFeature()
seg.setGeometry(geom)
pr.addFeatures( [seg] )


v_layer.commitChanges()
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
from qgis.gui import *
from qgis.networkanalysis import *

#building the graph**
vl = qgis.utils.iface.mapCanvas().currentLayer()
director = QgsLineVectorLayerDirector(vl, -1, '', '', '', 3)
properter = QgsDistanceArcProperter()
director.addProperter(properter)
crs = qgis.utils.iface.mapCanvas().mapRenderer().destinationCrs()
builder = QgsGraphBuilder(crs)

#coordinates of the Start point and endPoint** 
pStart = QgsPoint(-1.041,0.916)
pStop = QgsPoint(-0.813,0.152)
tiedPoints = director.makeGraph(builder, [pStart, pStop])
graph = builder.graph()

#Calculation of the shortest path**    
tStart = tiedPoints[0]
tStop = tiedPoints[1]

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

(tree, cost) = QgsGraphAnalyzer.dijkstra(graph, idStart, 0)

if tree[idStop] == -1:
  print "Path not found"
else:
  p = []
  curPos = idStop
  while curPos != idStart:
    pnt = graph.vertex(graph.arc(tree[curPos]).inVertex()).point()

    p.append(pnt)

    curPos = graph.arc(tree[curPos]).outVertex()

  p.append(tStart)

 #I build here a polyline based on the points in the p[] array
geom = QgsGeometry.fromPolyline(p)


 # I add here my result in a memory layer called cable and i want to do it for every feature i get after transforming the qgsrubberband object     

v_layer = None
for layer in QgsMapLayerRegistry.instance().mapLayers().values():
   if layer.name() == 'cable':
        v_layer = layer
if v_layer is None: 
    v_layer = QgsVectorLayer("LineString", "cable", "memory")
    v_layer.addAttribute(QgsField("id", QVariant.String))
    v_layer.addAttribute(QgsField("type", QVariant.String))
    QgsMapLayerRegistry.instance().addMapLayers([v_layer])

pr = v_layer.dataProvider()
v_layer.startEditing()


seg = QgsFeature()
seg.setGeometry(geom)
pr.addFeatures( [seg] )


v_layer.commitChanges()

Your code is working for me without problem. I've try it in the QGIS python console.

When i run it with a first start and end point, i get the red line in the cable layer then i run the script with others start and end point and i get the yellow line added in the same layer.  enter image description here

and what i get in the attribute table (length and geom calculate with the field calculator) enter image description here

I just remove the part of the code using QgsRubberBand as it was useless. The code I used:

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
from qgis.gui import *
from qgis.networkanalysis import *

#building the graph**
vl = qgis.utils.iface.mapCanvas().currentLayer()
director = QgsLineVectorLayerDirector(vl, -1, '', '', '', 3)
properter = QgsDistanceArcProperter()
director.addProperter(properter)
crs = qgis.utils.iface.mapCanvas().mapRenderer().destinationCrs()
builder = QgsGraphBuilder(crs)

#coordinates of the Start point and endPoint** 
pStart = QgsPoint(-1.041,0.916)
pStop = QgsPoint(-0.813,0.152)
tiedPoints = director.makeGraph(builder, [pStart, pStop])
graph = builder.graph()

#Calculation of the shortest path**    
tStart = tiedPoints[0]
tStop = tiedPoints[1]

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

(tree, cost) = QgsGraphAnalyzer.dijkstra(graph, idStart, 0)

if tree[idStop] == -1:
  print "Path not found"
else:
  p = []
  curPos = idStop
  while curPos != idStart:
    pnt = graph.vertex(graph.arc(tree[curPos]).inVertex()).point()

    p.append(pnt)

    curPos = graph.arc(tree[curPos]).outVertex()

  p.append(tStart)

 #I build here a polyline based on the points in the p[] array
geom = QgsGeometry.fromPolyline(p)


 # I add here my result in a memory layer called cable and i want to do it for every feature i get after transforming the qgsrubberband object     

v_layer = None
for layer in QgsMapLayerRegistry.instance().mapLayers().values():
   if layer.name() == 'cable':
        v_layer = layer
if v_layer is None: 
    v_layer = QgsVectorLayer("LineString", "cable", "memory")
    v_layer.addAttribute(QgsField("id", QVariant.String))
    v_layer.addAttribute(QgsField("type", QVariant.String))
    QgsMapLayerRegistry.instance().addMapLayers([v_layer])

pr = v_layer.dataProvider()
v_layer.startEditing()


seg = QgsFeature()
seg.setGeometry(geom)
pr.addFeatures( [seg] )


v_layer.commitChanges()

Your code is working for me without a problem. I've tried it in the QGIS Python Console.

When I run it with a first start and end point, I get the red line in the cable layer then I run the script with other start and end points and I get the yellow line added in the same layer. 

enter image description here

and what I get in the attribute table (length and geom calculated with the field calculator) enter image description here

I just removed the part of the code using QgsRubberBand as it was useless.

The code I used:

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
from qgis.gui import *
from qgis.networkanalysis import *

#building the graph**
vl = qgis.utils.iface.mapCanvas().currentLayer()
director = QgsLineVectorLayerDirector(vl, -1, '', '', '', 3)
properter = QgsDistanceArcProperter()
director.addProperter(properter)
crs = qgis.utils.iface.mapCanvas().mapRenderer().destinationCrs()
builder = QgsGraphBuilder(crs)

#coordinates of the Start point and endPoint** 
pStart = QgsPoint(-1.041,0.916)
pStop = QgsPoint(-0.813,0.152)
tiedPoints = director.makeGraph(builder, [pStart, pStop])
graph = builder.graph()

#Calculation of the shortest path**    
tStart = tiedPoints[0]
tStop = tiedPoints[1]

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

(tree, cost) = QgsGraphAnalyzer.dijkstra(graph, idStart, 0)

if tree[idStop] == -1:
  print "Path not found"
else:
  p = []
  curPos = idStop
  while curPos != idStart:
    pnt = graph.vertex(graph.arc(tree[curPos]).inVertex()).point()

    p.append(pnt)

    curPos = graph.arc(tree[curPos]).outVertex()

  p.append(tStart)

 #I build here a polyline based on the points in the p[] array
geom = QgsGeometry.fromPolyline(p)


 # I add here my result in a memory layer called cable and i want to do it for every feature i get after transforming the qgsrubberband object     

v_layer = None
for layer in QgsMapLayerRegistry.instance().mapLayers().values():
   if layer.name() == 'cable':
        v_layer = layer
if v_layer is None: 
    v_layer = QgsVectorLayer("LineString", "cable", "memory")
    v_layer.addAttribute(QgsField("id", QVariant.String))
    v_layer.addAttribute(QgsField("type", QVariant.String))
    QgsMapLayerRegistry.instance().addMapLayers([v_layer])

pr = v_layer.dataProvider()
v_layer.startEditing()


seg = QgsFeature()
seg.setGeometry(geom)
pr.addFeatures( [seg] )


v_layer.commitChanges()
remove pictures
Source Link
YoLecomte
  • 3.3k
  • 16
  • 30

enter image description hereenter image description hereenter image description hereYour Your code is working for me without problem. I've try it in the QGIS python console.

enter image description hereenter image description hereenter image description hereYour code is working for me without problem. I've try it in the QGIS python console.

Your code is working for me without problem. I've try it in the QGIS python console.

These are the screen i get when executing the code.
Source Link

Yourenter image description hereenter image description hereenter image description hereYour code is working for me without problem. I've try it in the QGIS python console.

Your code is working for me without problem. I've try it in the QGIS python console.

enter image description hereenter image description hereenter image description hereYour code is working for me without problem. I've try it in the QGIS python console.

added 9 characters in body
Source Link
YoLecomte
  • 3.3k
  • 16
  • 30
Loading
Source Link
YoLecomte
  • 3.3k
  • 16
  • 30
Loading