5

How can I make a script to convert polygon layer to explode lines ? After that I need to get geometry length for conditioned features. I have a part of code ?

import processing
processing.runalg("qgis:polygonstolines","Polygon.shp","Line.shp")
processing.runalg('qgis:explodelines',"Line.shp","Explode_line.shp")
layer = qgis.utils.iface.activeLayer()
vlayer.selectAll() - "QGIS crashes...."

for f in features:
    geom = f.geometry()
    print "Length:", geom.length()

Here need select on a map all features in Explode_line where ID_attribute > 10

  • 1
    Don't know if it's just a type error, but you assign activeLayer() to variable layer, and select from object vlayer. No reason for QGIS to crash, but your script will already fail at this point. – Detlev Dec 4 '15 at 11:38
5

You could use something like the following, I set the paths of the shapefiles which are subsequently loaded into QGIS once they are processed. The code returns the row number and length of the lines which exceed an ID of 10 into the Python Console:

import processing

polygon_path = "C:/Users/You/Desktop/Test//polygon example.shp"
line_path = "C:/Users/You/Desktop/Test//line_example.shp"
explode_path = "C:/Users/You/Desktop/Test//explode example.shp"
expression = '"ID" > 10'

processing.runandload("qgis:polygonstolines",polygon_path,line_path)
processing.runandload('qgis:explodelines',line_path,explode_path)
processing.runandload('qgis:selectbyexpression', explode_path, expression, 0)

layer = qgis.utils.iface.activeLayer()
features = layer.selectedFeatures()
for f in features:
    geom = f.geometry()
    print "Row number:", f.id(), " Length:", geom.length()

If you want the length to be written in the Attribute Table, you could use the Field Calculator algorithm. This creates a new Length field and adds the length of lines with an ID > 10:

import processing

polygon_path = "C:/Users/You/Desktop/Test//polygon example.shp"
line_path = "C:/Users/You/Desktop/Test//line_example.shp"
explode_path = "C:/Users/You/Desktop/Test//explode example.shp"
result_path = "C:/Users/You/Desktop/Test//result.shp"

formula = 'CASE WHEN "ID" > 0 THEN $length ELSE NULL END'

processing.runandload("qgis:polygonstolines",polygon_path,line_path)
processing.runandload('qgis:explodelines',line_path,explode_path)
processing.runandload("qgis:fieldcalculator", explode_path, 'Length', 0, 10, 2, True, formula, result_path)

Edit:

Responding to the comment on saving the printed information into a text file and saving the selected features to a shapefile, you can use the following code in the Python Console:

import processing

polygon_path = "C:/Users/You/Desktop/Test//polygon example.shp"
line_path = "C:/Users/You/Desktop/Test//Lines from polygons.shp"
explode_path = "C:/Users/You/Desktop/Test//Exploded.shp"
save_selected_path = "C:/Users/You/Desktop/Test//Selection.shp"
text_file_path = "C:/Users/You/Desktop//result.txt"

expression = '"ID" > 10'

processing.runandload("qgis:polygonstolines",polygon_path,line_path)
processing.runandload('qgis:explodelines',line_path,explode_path)
processing.runandload('qgis:selectbyexpression', explode_path, expression, 0)
processing.runandload('qgis:saveselectedfeatures', explode_path, save_selected_path)

layer = qgis.utils.iface.activeLayer()
features = layer.getFeatures()
results = []
for f in features:
    geom = f.geometry()
    attrs = "Row number:", f.id(), " Length:", geom.length()
    results.append(attrs)

txt_file = open( text_file_path, 'wt')
txt_file.write('\n'.join( str(r) for r in results))
txt_file.close()
  • That's what I need. Thanks for You ! Maybe do you know: how to dump print "Row number:", f.id(), " Length:", geom.length() to .txt file and how to save new layer from that selection ? This code saves a csv - but not txt QgsVectorFileWriter.writeAsVectorFormat(currentLayer, csvpath, "utf-8",None,"csv"). – Artec Dec 6 '15 at 19:35
  • @Artec - Hi buddy, I've edited the post to include saving the printed information into a text file and saving the selected features into a shapefile :) – Joseph Dec 7 '15 at 12:22
  • 1
    Thanks a lot one more time for Your help. This is exactly what I was looking for. – Artec Dec 7 '15 at 19:29
  • @Artec - Glad I could help :) – Joseph Dec 8 '15 at 10:08

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.