[Qgis-user] Create virtual fields with pyQGIS with standalone scripts
Roland Berger
roland.berger at steinerpartner.com
Fri Jul 26 03:46:39 PDT 2024
Hi
I would like to add a virtual field to a PostGIS vector layer with
PyQGIS with standalone scripts
(https://docs.qgis.org/3.34/en/docs/pyqgis_developer_cookbook/intro.html#python-applications)
All examples I have found so far with google, chatgpt4 and JetBrains AI
Assistant gave me solutions that do not work. They only work in the
Python Console inside QGIS.
Does anybody know of a solution that works with PyQGIS with standalone
scripts?
See below what I have tried so far in different variations.
Thanks for any help
Roland
Example created by chatgpt:
...
field_name = "virtual_field"
expression = '"attribute_name" * 2' # Example expression
# Create and add the virtual field
layer.startEditing()
layer.addExpressionField(expression, QgsField(field_name, QVariant.Double))
layer.commitChanges()
...
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20240726/ad198d01/attachment.htm>
More information about the QGIS-User
mailing list