To get field names with fields() method or other field properties (length, type, comment, ...) you can use:
field_names = [field.name() for field in layer.fields()]
# ['id', 'attr1', 'attr2', 'attr3']
Welcome to GIS.SE!
As you thought, there is a way to know if a field exists in a layer not by iterating features but by looking at the layer structure.
In the QGIS Layers Panel, select a layer.
Interesting question! I couldn't find any native signal emitted when an attribute table is opened or closed so I would call this solution a fairly inelegant workaround but it seems to work well enough....
If you are sure there is one layer named LAYER_NAME, the shortest way:
layer = QgsProject.instance().mapLayersByName('LAYER_NAME')
In that way, you can get a layer in a group layer.
You can use this script. First, select a the layer, then run the script. It adds the result as a memory layer. You should save the result as a new file manually or using a code.
input_layer = iface....
The order of when you set the extent, add the map to the layout, and resize the rectangle is important. Steps to add a map to a new layout in QGIS 3.
Instantiate the map object with QgsLayoutItemMap(...
The class has been renamed from QgsVectorJoinInfo to QgsVectorLayerJoinInfo.
You now need to call each join function with their associated parameter:
csvField = 'id'
shpField = 'ID'
joinObject = ...
Here is the solution working for me. The python file must be in the same directory as the QGIS 3 project file :
from qgis.core import (QgsProject, QgsLayoutExporter, ...
You set feature fields to empty QgsFields.
fields = QgsFields() # fields is empty
feature.setFields(fields) # you set the empty fields
Instead, pass layer.fields() to QgsFeature.
feature = QgsFeature(...
To change QgsFeature geometry in QGIS 3 you have to call setGeometry() explicitly.
geom = feat.geometry()
In QGIS 2 geometry() returns a ...
The trick is to use renderer.updateColorRamp(ramp) instead of renderer.setSourceColorRamp(ramp).
Here is a working code snippet to create categories from unique values, pass them to a ...