2

I have got a few layers, in which the data attribute table looks exactly the same including:

  • Type (string)
  • Address (string)
  • Postcode (string)
  • X (East) (decimal)
  • Y (north (decimal)
  • Latitude (decimal)
  • Longitude (decimal)
  • ObjectID (string)
  • length (meters) (decimal)

I have to create them every time for every single layer. Is there any way to make them loaded instantly as a blank data attribute table for the layer I want?

1
  • Are you creating the features later on, or do you just fill in the "blanks"?
    – Erik
    Commented Sep 25, 2020 at 11:36

2 Answers 2

4

You can execute the following code in the QGIS Python console to create a table and add it to the QGIS project.

For the example, this is a temporary layer with a Polygon geometry which will be called layer_name.

your_layer = QgsVectorLayer("Polygon", "layer_name", "memory")
prov = your_layer.dataProvider()

prov.addAttributes([QgsField("Type", QVariant.String),
QgsField("Address",  QVariant.String),
QgsField("Postcode", QVariant.String),
QgsField("X", QVariant.Double),
QgsField("Y", QVariant.Double),
QgsField("Latitude", QVariant.Double),
QgsField("Longitude", QVariant.Double),
QgsField("ObjectID", QVariant.String),
QgsField("Length", QVariant.Double)])


your_layer.updateFields()
QgsProject.instance().addMapLayer(your_layer)
1
  • It's fine, but this code creates a separate layer for me. I have got existing layers already.
    – Geographos
    Commented Sep 28, 2020 at 13:42
3

If you are open to a small python script, here you go:

from qgis.PyQt.QtCore import QVariant
from qgis.core import QgsField
from qgis.utils import iface

x = 20
y = 2

attributes = [QgsField("Type", QVariant.String, len=x),
              QgsField("Address", QVariant.String, len=x),
              QgsField("Postcode", QVariant.String, len=x),
              QgsField("X", QVariant.Double, len=x, prec=y),
              QgsField("Y", QVariant.Double, len=x, prec=y),
              QgsField("Latitude", QVariant.Double, len=x, prec=y),
              QgsField("Longitude", QVariant.Double, len=x, prec=y),
              QgsField("ObjectID", QVariant.Double, len=x, prec=y),
              QgsField("length", QVariant.Double, len=x, prec=y)]

layer = iface.activeLayer()
layer.dataProvider().addAttributes(attributes)
layer.updateFields()

edit: PS : iface.activeLayer() gets the selected layer from the legend

2
  • Could also be done by a model incorporating refactor fields, but neither would be necessary, if @MKR creates the layer from scratch, then he'd simply create an empty template and copy it.
    – Erik
    Commented Sep 25, 2020 at 11:56
  • @Erik i answer with this because it works both for new layers and layers with fields already, sure there are better way to do this but i think that's enought. especially if you just want to add a few attributes on the fly
    – Kalak
    Commented Sep 25, 2020 at 12:55

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.