I am currently making a code that will buffer the points then extract vertices and add new coordinates. but I also want to change the "max depth"
column to "Height Theo"
and then add another column called "Height actual"
and the values below it should be equal to the negative (-) value of "Height Theo"
before exporting this to CSV file.
This is the output of my current script:
This is the output I want.
This is my code:
from qgis import processing
from datetime import datetime, timedelta
view = iface.layerTreeView()
selected_nodes = view.selectedNodes()
selected_layers = [node.layer() for node in selected_nodes]
print("merging layers....")
merge = processing.runAndLoadResults("native:mergevectorlayers", {
'LAYERS': selected_layers,
'CRS': QgsCoordinateReferenceSystem('USER:100000'),
'OUTPUT': 'TEMPORARY_OUTPUT'
})
print('Creating Buffer...')
buffer = processing.runAndLoadResults("native:buffer", {
'INPUT': merge['OUTPUT'],
'DISTANCE': 0.18,
'SEGMENTS': 2,
'END_CAP_STYLE': 0,
'JOIN_STYLE': 0,
'MITER_LIMIT': 2,
'DISSOLVE': False,
'OUTPUT': 'TEMPORARY_OUTPUT'
})
print('extracting points...')
vertices = processing.runAndLoadResults("native:extractvertices", {
'INPUT': buffer['OUTPUT'],
'OUTPUT': 'TEMPORARY_OUTPUT'
})
print('adding xycoords...')
xycoords = processing.runAndLoadResults("qgis:exportaddgeometrycolumns", {
'INPUT': vertices['OUTPUT'],
'CALC_METHOD': 0,
'OUTPUT': 'TEMPORARY_OUTPUT'
})
dont_delete = ["ID", "X [m]", "Y [m]", "Max. depth [m]"]
all_fields = xycoords.fields().names()
field_ids = [xycoords.fields().indexFromName(f) for f in all_fields if f not in dont_delete]
xycoords.dataProvider().deleteAttributes(field_ids)
xycoords.updateFields()
deleting fields does not work
# 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/OneDrive - Boskalis/Documents/Trial PyQGIS/Creating_Grab.py').read_text()) merging layers.... Creating Buffer... extracting points... adding xycoords... Traceback (most recent call last): File "C:\PROGRA~1\QGIS33~1.0\apps\Python39\lib\code.py", line 90, in runcode exec(code, self.locals) File "<input>", line 1, in <module> File "<string>", line 83, in <module> AttributeError: 'dict' object has no attribute 'fields'
1 Answer 1
I could get the same error:
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 10, in <module> AttributeError: 'dict' object has no attribute 'fields'
AttributeError
arose due to the output type of the "qgis:exportaddgeometrycolumns"
algorithm:
{'OUTPUT': <QgsVectorLayer: 'Added geom info' (memory)>}
As you can see it is a dictionary, however, to achieve field names, one needs to treat the QgsVectorLayer
only. This can be done by adding the ['OUTPUT']
into your code.
So, please edit your code:
xycoords = processing.runAndLoadResults("qgis:exportaddgeometrycolumns", {
'INPUT': vertices['OUTPUT'],
'CALC_METHOD': 0,
'OUTPUT': 'TEMPORARY_OUTPUT'
})
as follows:
xycoords = processing.runAndLoadResults("qgis:exportaddgeometrycolumns", {
'INPUT': vertices['OUTPUT'],
'CALC_METHOD': 0,
'OUTPUT': 'TEMPORARY_OUTPUT'
})['OUTPUT']
Then I can easily get field names of the xycoords
temporary layer with:
all_fields = xycoords.fields().names()
Explore related questions
See similar questions with these tags.