Skip to main content
added qgis tag
Source Link
Kadir Şahbaz
  • 78k
  • 57
  • 257
  • 404

I need to use a plugin that's designed for use within the QGIS interface in PyQGIS.

For example, https://github.com/dgoedkoop/joinmultiplelines works great but I want to run it within a script, so that I can pass each group of lines to it to create the entire network.

So something like

layer = iface.activeLayer()
selection = layer.getFeatures() #all features
#selection = layer.selectedFeatures() #only selected features
dpr = layer.dataProvider()

completed_list=[]
for f in selection:
    print (f.id())
    text=f['RoadMntnc']
    if text not in completed_list:
        completed_list.append(text)
        line_selection=f.getFeatures( QgsFeatureRequest().setFilterExpression ( u'"RoadMntnc" = text' ) )
        joined_line=MultipleLinesPlugin(line_selection)
        #merge layers based on https://gis.stackexchange.com/questions/169523/how-to-merge-layers-into-a-single-qgsvectorlayer/169543
        vl = QgsVectorLayer("LineString", "temporary_lines", "memory")
        pr = vl.dataProvider()
        feats1 = joined_line.getFeatures()
        for feature in feats1:
          pr.addFeatures([feature])
    else:
        pass
        
vl.updateExtents()
newfile=r'c:\scratch\roads_joined.shp'
QgsVectorFileWriter.writeAsVectorFormat(v1,newfile,"utf-8",v.sourceCrs(),"ESRI Shapefile")

How could I structure the joined_line=MultipleLinesPlugin(line_selection) to use the plugin?

I need to use a plugin that's designed for use within the QGIS interface in PyQGIS.

For example https://github.com/dgoedkoop/joinmultiplelines works great but I want to run it within a script, so that I can pass each group of lines to it to create the entire network.

So something like

layer = iface.activeLayer()
selection = layer.getFeatures() #all features
#selection = layer.selectedFeatures() #only selected features
dpr = layer.dataProvider()

completed_list=[]
for f in selection:
    print (f.id())
    text=f['RoadMntnc']
    if text not in completed_list:
        completed_list.append(text)
        line_selection=f.getFeatures( QgsFeatureRequest().setFilterExpression ( u'"RoadMntnc" = text' ) )
        joined_line=MultipleLinesPlugin(line_selection)
        #merge layers based on https://gis.stackexchange.com/questions/169523/how-to-merge-layers-into-a-single-qgsvectorlayer/169543
        vl = QgsVectorLayer("LineString", "temporary_lines", "memory")
        pr = vl.dataProvider()
        feats1 = joined_line.getFeatures()
        for feature in feats1:
          pr.addFeatures([feature])
    else:
        pass
        
vl.updateExtents()
newfile=r'c:\scratch\roads_joined.shp'
QgsVectorFileWriter.writeAsVectorFormat(v1,newfile,"utf-8",v.sourceCrs(),"ESRI Shapefile")

How could I structure the joined_line=MultipleLinesPlugin(line_selection) to use the plugin?

I need to use a plugin that's designed for use within the QGIS interface in PyQGIS.

For example, https://github.com/dgoedkoop/joinmultiplelines works great but I want to run it within a script, so that I can pass each group of lines to it to create the entire network.

So something like

layer = iface.activeLayer()
selection = layer.getFeatures() #all features
#selection = layer.selectedFeatures() #only selected features
dpr = layer.dataProvider()

completed_list=[]
for f in selection:
    print (f.id())
    text=f['RoadMntnc']
    if text not in completed_list:
        completed_list.append(text)
        line_selection=f.getFeatures( QgsFeatureRequest().setFilterExpression ( u'"RoadMntnc" = text' ) )
        joined_line=MultipleLinesPlugin(line_selection)
        #merge layers based on https://gis.stackexchange.com/questions/169523/how-to-merge-layers-into-a-single-qgsvectorlayer/169543
        vl = QgsVectorLayer("LineString", "temporary_lines", "memory")
        pr = vl.dataProvider()
        feats1 = joined_line.getFeatures()
        for feature in feats1:
          pr.addFeatures([feature])
    else:
        pass
        
vl.updateExtents()
newfile=r'c:\scratch\roads_joined.shp'
QgsVectorFileWriter.writeAsVectorFormat(v1,newfile,"utf-8",v.sourceCrs(),"ESRI Shapefile")

How could I structure the joined_line=MultipleLinesPlugin(line_selection) to use the plugin?

edited tags; edited title
Link
PolyGeo
  • 65.4k
  • 29
  • 114
  • 345

Is there an easy way to use a Using QGIS plugin that's designed for use in the interface within PyQGIS?

Source Link
GeorgeC
  • 8.3k
  • 7
  • 56
  • 143
Loading