So this is my code :
from qgis import processing
inlayDir = iface.activeLayer()
Sp_Offset = 'E:/QGIS/General/20230117_Boundaries R8/PVD Hectares/PH_RPSOffset8-0_AEB.gpkg'
Sp ='E:/QGIS/General/20230117_Boundaries R8/PVD Hectares/PH_RPS8-0_AEB.gpkg'
dissolved=processing.runAndLoadResults("native:dissolve",
{'INPUT': inlayDir,
'FIELD':[],
'OUTPUT': 'TEMPORARY_OUTPUT'})
UN_Sp_Offset = processing.runAndLoadResults( "qgis:union",{
'INPUT' : Sp_Offset,
'OUTPUT' : 'TEMPORARY_OUTPUT',
'OVERLAY' : dissolved['OUTPUT'],
'OVERLAY_FIELDS_PREFIX' : '' })
Clip = processing.runAndLoadResults("native:clip",
{ 'INPUT':UN_Sp_Offset['OUTPUT'],
'OVERLAY' : dissolved['OUTPUT'],
'OUTPUT' : 'TEMPORARY_OUTPUT',
})
# create an instance of QgsDistanceArea
distance_area = QgsDistanceArea()
# create the fields list
fields = QgsFields()
fields.append(QgsField('area', QVariant.Double))
inlayDir = iface.activeLayer()
fields.append(QgsField('perimeter', QVariant.Double))
# start editing the vector layer
with edit(clip):
# add the fields
for field in fields:
vl.addAttribute(field)
# loop into the features
for feature in vl.getFeatures():
geometry = feature.geometry()
# update the feature with teh QgsDistanceArea calculation
feature['area'] = distance_area.measureArea(geometry)
feature['perimeter'] = distance_area.measurePerimeter(geometry)
Clip.updateFeature(feature)
# add the layer to the map
project.addMapLayer(Clip)
and I want to add "Area and Perimeter" in another column in my attribute table next to my columns:
Error Message:
# Python Console
# Use iface to access QGIS API interface or type help(iface) for more info
# Security warning: typing commands from an untrusted source can harm your computer
exec(Path('C:/Users/CMCA/AppData/Local/Temp/tmp8h99ttbs.py').read_text())
Traceback (most recent call last):
File "C:\PROGRA~1\QGIS32~1.0\apps\Python39\lib\code.py", line 90, in runcode
exec(code, self.locals)
File "<input>", line 1, in <module>
File "<string>", line 15, in <module>
File "C:\PROGRA~1/QGIS32~1.0/apps/qgis/./python\qgis\core\additions\edit.py", line 38, in __enter__
assert self.layer.startEditing()
AssertionError
exec(Path('C:/Users/CMCA/AppData/Local/Temp/tmpd0rbj1lv.py').read_text())
Traceback (most recent call last):
File "C:\PROGRA~1\QGIS32~1.0\apps\Python39\lib\code.py", line 90, in runcode
exec(code, self.locals)
File "<input>", line 1, in <module>
File "<string>", line 42, in <module>
File "C:\PROGRA~1/QGIS32~1.0/apps/qgis/./python\qgis\core\additions\edit.py", line 38, in __enter__
assert self.layer.startEditing()
AssertionError
exec(Path('C:/Users/CMCA/AppData/Local/Temp/tmplsle_e88.py').read_text())
Traceback (most recent call last):
File "C:\PROGRA~1\QGIS32~1.0\apps\Python39\lib\code.py", line 90, in runcode
exec(code, self.locals)
File "<input>", line 1, in <module>
File "<string>", line 42, in <module>
File "C:\PROGRA~1/QGIS32~1.0/apps/qgis/./python\qgis\core\additions\edit.py", line 38, in __enter__
assert self.layer.startEditing()
AssertionError
exec(Path('C:/Users/CMCA/AppData/Local/Temp/tmp2ubmh9vo.py').read_text())
Traceback (most recent call last):
File "C:\PROGRA~1\QGIS32~1.0\apps\Python39\lib\code.py", line 90, in runcode
exec(code, self.locals)
File "<input>", line 1, in <module>
File "<string>", line 42, in <module>
File "C:\PROGRA~1/QGIS32~1.0/apps/qgis/./python\qgis\core\additions\edit.py", line 38, in __enter__
assert self.layer.startEditing()
AssertionError
Can someone help me with the coding?
Clip
with capitalC
while inwith edit(clip)
you enteredclip
without the capital letter.