Skip to main content
added 29 characters in body
Source Link
Taras
  • 34k
  • 4
  • 73
  • 148
import processing
from PyQt5.QtCore import QVariant

overlay_layer = QgsProject.instance().mapLayersByName("route2")[0]
bottom_layer = QgsProject.instance().mapLayersByName("route1")[0]
overlap = r'C:\Users\imper\Documents\python\calculate_overlap_lines\overlap.shp'

# clip overlap - works
processing.run("native:clip", {'INPUT': bottom_layer, \
                               'OVERLAY': overlay_layer, \
                               'OUTPUT': overlap})
iface.addVectorLayer(overlap, '', 'ogr')

# get length - doesn't work
overlap_lyr = QgsProject.instance().mapLayersByName("overlap")[0]
for overlap_lyr in QgsMapLayerRegistry.instance().mapLayers().values():
    features = overlap_lyr.getFeatures()
    for f in features:
        geom = f.geometry()
        leng = geom.length()
        res = overlap_lyr.dataProvider().addAttributes([QgsField("Length", QVariant.Int)])
        overlap_lyr.updateFields()
        fieldIndex = overlap_lyr.dataProvider().fieldNameIndex("Length")
        attrFeatMap = {}
        attrMap = { fieldIndex : leng }
        for feature in overlap_lyr.getFeatures():
            attrFeatMap[ feature.id() ] = attrMap
        overlap_lyr.dataProvider().changeAttributeValues(attrFeatMap)
import processing
from PyQt5.QtCore import QVariant

overlay_layer = QgsProject.instance().mapLayersByName("route2")[0]
bottom_layer = QgsProject.instance().mapLayersByName("route1")[0]
overlap = r'C:\Users\imper\Documents\python\calculate_overlap_lines\overlap.shp'

# clip overlap - works
processing.run("native:clip", {'INPUT': bottom_layer, \
                               'OVERLAY': overlay_layer, \
                               'OUTPUT': overlap})
iface.addVectorLayer(overlap, '', 'ogr')

# get length - doesn't work
overlap_lyr = QgsProject.instance().mapLayersByName("overlap")[0]
for overlap_lyr in QgsMapLayerRegistry.instance().mapLayers().values():
    features = overlap_lyr.getFeatures()
    for f in features:
        geom = f.geometry()
        leng = geom.length()
        res = overlap_lyr.dataProvider().addAttributes([QgsField("Length", QVariant.Int)])
        overlap_lyr.updateFields()
        fieldIndex = overlap_lyr.dataProvider().fieldNameIndex("Length")
        attrFeatMap = {}
        attrMap = { fieldIndex : leng }
        for feature in overlap_lyr.getFeatures():
            attrFeatMap[ feature.id() ] = attrMap
        overlap_lyr.dataProvider().changeAttributeValues(attrFeatMap)
import processing
from PyQt5.QtCore import QVariant

overlay_layer = QgsProject.instance().mapLayersByName("route2")[0]
bottom_layer = QgsProject.instance().mapLayersByName("route1")[0]
overlap = r'C:\Users\imper\Documents\python\calculate_overlap_lines\overlap.shp'

# clip overlap - works
processing.run("native:clip", {'INPUT': bottom_layer, \
                               'OVERLAY': overlay_layer, \
                               'OUTPUT': overlap})
iface.addVectorLayer(overlap, '', 'ogr')

# get length - doesn't work
overlap_lyr = QgsProject.instance().mapLayersByName("overlap")[0]
for overlap_lyr in QgsMapLayerRegistry.instance().mapLayers().values():
    features = overlap_lyr.getFeatures()
    for f in features:
        geom = f.geometry()
        leng = geom.length()
        res = overlap_lyr.dataProvider().addAttributes([QgsField("Length", QVariant.Int)])
        overlap_lyr.updateFields()
        fieldIndex = overlap_lyr.dataProvider().fieldNameIndex("Length")
        attrFeatMap = {}
        attrMap = { fieldIndex : leng }
        for feature in overlap_lyr.getFeatures():
            attrFeatMap[ feature.id() ] = attrMap
        overlap_lyr.dataProvider().changeAttributeValues(attrFeatMap)
import processing
from PyQt5.QtCore import QVariant

overlay_layer = QgsProject.instance().mapLayersByName("route2")[0]
bottom_layer = QgsProject.instance().mapLayersByName("route1")[0]
overlap = r'C:\Users\imper\Documents\python\calculate_overlap_lines\overlap.shp'

# clip overlap - works
processing.run("native:clip", {'INPUT': bottom_layer, \
                               'OVERLAY': overlay_layer, \
                               'OUTPUT': overlap})
iface.addVectorLayer(overlap, '', 'ogr')

# get length - doesn't work
overlap_lyr = QgsProject.instance().mapLayersByName("overlap")[0]
for overlap_lyr in QgsMapLayerRegistry.instance().mapLayers().values():
    features = overlap_lyr.getFeatures()
    for f in features:
        geom = f.geometry()
        leng = geom.length()
        res = overlap_lyr.dataProvider().addAttributes([QgsField("Length", QVariant.Int)])
        overlap_lyr.updateFields()
        fieldIndex = overlap_lyr.dataProvider().fieldNameIndex("Length")
        attrFeatMap = {}
        attrMap = { fieldIndex : leng }
        for feature in overlap_lyr.getFeatures():
            attrFeatMap[ feature.id() ] = attrMap
        overlap_lyr.dataProvider().changeAttributeValues(attrFeatMap)
added 69 characters in body; edited tags
Source Link
Taras
  • 34k
  • 4
  • 73
  • 148

I have two lines that overlap, so I clip the overlap, and I should get its length in kilometers.

enter image description here

The clip part is ok but I can't find any resources on how to get the length. It can be printed in the console or add a field in the clipped shapefile that contains the km length. I used Calculating line lengths using PyQGIS as a guide but it didn't work.

import processing
from PyQt5.QtCore import QVariant

overlay_layer = QgsProject.instance().mapLayersByName("route2")[0]
bottom_layer = QgsProject.instance().mapLayersByName("route1")[0]
overlap = r'C:\Users\imper\Documents\python\calculate_overlap_lines\overlap.shp'

# clip overlap - works
processing.run("native:clip", {'INPUT': bottom_layer, \
                               'OVERLAY': overlay_layer, \
                               'OUTPUT': overlap})
iface.addVectorLayer(overlap, '', 'ogr')

# get length - doesn't work
overlap_lyr = QgsProject.instance().mapLayersByName("overlap")[0]
for overlap_lyr in QgsMapLayerRegistry.instance().mapLayers().values():
    features = overlap_lyr.getFeatures()
    for f in features:
        geom = f.geometry()
        leng = geom.length()
        res = overlap_lyr.dataProvider().addAttributes([QgsField("Length", QVariant.Int)])
        overlap_lyr.updateFields()
        fieldIndex = overlap_lyr.dataProvider().fieldNameIndex("Length")
        attrFeatMap = {}
        attrMap = { fieldIndex : leng }
        for feature in overlap_lyr.getFeatures():
            attrFeatMap[ feature.id() ] = attrMap
        overlap_lyr.dataProvider().changeAttributeValues(attrFeatMap)

I'm getting this error raise QgsProcessingException(msg) _core.QgsProcessingException: There were errors executing the algorithm.:

raise QgsProcessingException(msg) _core.QgsProcessingException: There were errors executing the algorithm.

I have two lines that overlap, so I clip the overlap, and I should get its length in kilometers.

enter image description here

The clip part is ok but I can't find any resources on how to get the length. It can be printed in the console or add a field in the clipped shapefile that contains the km length. I used Calculating line lengths using PyQGIS as a guide but it didn't work.

import processing
from PyQt5.QtCore import QVariant

overlay_layer = QgsProject.instance().mapLayersByName("route2")[0]
bottom_layer = QgsProject.instance().mapLayersByName("route1")[0]
overlap = r'C:\Users\imper\Documents\python\calculate_overlap_lines\overlap.shp'

# clip overlap - works
processing.run("native:clip", {'INPUT': bottom_layer, \
'OVERLAY': overlay_layer, \
'OUTPUT': overlap})
iface.addVectorLayer(overlap, '', 'ogr')

# get length - doesn't work
overlap_lyr = QgsProject.instance().mapLayersByName("overlap")[0]
for overlap_lyr in QgsMapLayerRegistry.instance().mapLayers().values():
    features = overlap_lyr.getFeatures()
    for f in features:
        geom = f.geometry()
        leng = geom.length()
        res = overlap_lyr.dataProvider().addAttributes([QgsField("Length", QVariant.Int)])
        overlap_lyr.updateFields()
        fieldIndex = overlap_lyr.dataProvider().fieldNameIndex("Length")
        attrFeatMap = {}
        attrMap = { fieldIndex : leng }
        for feature in overlap_lyr.getFeatures():
            attrFeatMap[ feature.id() ] = attrMap
        overlap_lyr.dataProvider().changeAttributeValues(attrFeatMap)

I'm getting this error raise QgsProcessingException(msg) _core.QgsProcessingException: There were errors executing the algorithm.

I have two lines that overlap, so I clip the overlap, and I should get its length in kilometers.

enter image description here

The clip part is ok but I can't find any resources on how to get the length. It can be printed in the console or add a field in the clipped shapefile that contains the km length. I used Calculating line lengths using PyQGIS as a guide but it didn't work.

import processing
from PyQt5.QtCore import QVariant

overlay_layer = QgsProject.instance().mapLayersByName("route2")[0]
bottom_layer = QgsProject.instance().mapLayersByName("route1")[0]
overlap = r'C:\Users\imper\Documents\python\calculate_overlap_lines\overlap.shp'

# clip overlap - works
processing.run("native:clip", {'INPUT': bottom_layer, \
                               'OVERLAY': overlay_layer, \
                               'OUTPUT': overlap})
iface.addVectorLayer(overlap, '', 'ogr')

# get length - doesn't work
overlap_lyr = QgsProject.instance().mapLayersByName("overlap")[0]
for overlap_lyr in QgsMapLayerRegistry.instance().mapLayers().values():
    features = overlap_lyr.getFeatures()
    for f in features:
        geom = f.geometry()
        leng = geom.length()
        res = overlap_lyr.dataProvider().addAttributes([QgsField("Length", QVariant.Int)])
        overlap_lyr.updateFields()
        fieldIndex = overlap_lyr.dataProvider().fieldNameIndex("Length")
        attrFeatMap = {}
        attrMap = { fieldIndex : leng }
        for feature in overlap_lyr.getFeatures():
            attrFeatMap[ feature.id() ] = attrMap
        overlap_lyr.dataProvider().changeAttributeValues(attrFeatMap)

I'm getting this error:

raise QgsProcessingException(msg) _core.QgsProcessingException: There were errors executing the algorithm.

edited tags
Link
Bera
  • 77.8k
  • 14
  • 78
  • 188
deleted 5 characters in body
Source Link
BallpenMan
  • 1.2k
  • 11
  • 23
Loading
added 23 characters in body
Source Link
BallpenMan
  • 1.2k
  • 11
  • 23
Loading
added 45 characters in body; edited tags; edited title
Source Link
PolyGeo
  • 65.4k
  • 29
  • 114
  • 345
Loading
Source Link
BallpenMan
  • 1.2k
  • 11
  • 23
Loading