2

I'm new to QGIS, but familiar with Python and PyQt, and am trying to make sense of the terminology of "layer", "feature", "attribute", "field", and "data provider". Every time I think I understand what they represent and how they relate, the PyQgis API throws me for a loop.

Here's how I understand it. A layer is a component of a map visualization, the 'view', so to speak, corresponding to the 'model' data given by a data provider. A feature is a thing of interest on the map. Information about a feature is stored in the data provider. Data is categorized as either GIS or non-GIS. Non-GIS data is accessed by users as though it were stored in two dimensions via the attribute table. The table consists of records (rows) and fields (columns). Features are provided by the provider and managed by a layer which is in turn handled by the canvas. We say that a record is an attribute and this corresponds to a feature.

professional illustration of the relationships

This mental model feels cohesive until I do something like try to define several features in a vector layer. In that case, I need to do things like get the provider from the layer and specify feature fields through an API call named "setAttribute".

layer = self.iface.addVectorLayer("point", "points", "memory")

provider = layer.dataProvider()
provider.addAttributes([qgis.core.QgsField('uuid', QtCore.QVariant.String, 'str', 32)])
layer.updateFields()

for point in self.point_list:
    feature = qgis.core.QgsFeature()
    feature.setFields(layer.fields(), True)  # needed to reference by name
    feature.setAttribute('uuid', point['uuid'])
    geom = qgis.core.QgsGeometry.fromWkt(point['geom'])
    feature.setGeometry(geom)
    provider.addFeature(feature)

layer.renderer().symbol().setColor(QtGui.QColor("red"))
layer.updateExtents()

I would instead expect to be able to define a provider up-front and assign it to a layer.

I also would expect to put a value in a column with something like "setColumn" or "setField" since a feature is an attribute, right?

I also notice that many objects have similar methods. For example, layers can delete attributes. I thought attributes were just for data providers and features.

Are those just for convenience or is data stored there outside the data provider?

2
  • 1
    usually, features are rows and attributes are columns or fields on a feature
    – Ian Turton
    Nov 4, 2021 at 9:00
  • A layer has more than just pointing to the features (rows of geometries+attributes), as it can also contain styling information, labeling instruction, metadata, projection information etc. It is however a very convenient abstraction of the underlying data provider functions: feature.setAttribute('field_name', value) can be used regardless of the data storage, could it be in a database, in a file or even just in memory.
    – JGH
    Nov 4, 2021 at 13:16

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.