[Qgis-user] Create virtual fields with pyQGIS with standalone scripts
Ujaval Gandhi
ujaval at spatialthoughts.com
Fri Jul 26 05:01:15 PDT 2024
For running a PyQGIS script as a standalone script, you need to set
appropriate environment variables and initialize qgis in a headless mode. See
the full example with setup for Windows and Mac
at https://courses.spatialthoughts.com/pyqgis-masterclass.html#writing-standalone-python-scripts
[https://mailtrack.io/l/54a2bb23bb2e216749cf31c77063d79fd7f518f8?url=https%3A%2F%2Fcourses.spatialthoughts.com%2Fpyqgis-masterclass.html%23writing-standalone-python-scripts&u=8747767&signature=20c626c64e491be4]
---
Ujaval Gandhi
Spatial Thoughts
www.spatialthoughts.com
[https://mailtrack.io/l/0759060cce5dfd1efebd0082630133103cf22d2a?url=http%3A%2F%2Fwww.spatialthoughts.com&u=8747767&signature=8d3fc0a0dc5f7c99]
[data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=3D]
On Fri, Jul 26, 2024 at 4:16 PM Roland Berger via QGIS-User
<qgis-user at lists.osgeo.org> wrote:
> 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
> [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()
>
> ...
>
>
>
> _______________________________________________
> QGIS-User mailing list
> QGIS-User at lists.osgeo.org [QGIS-User at lists.osgeo.org]
> List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
> [https://lists.osgeo.org/mailman/listinfo/qgis-user]
> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user
> [https://lists.osgeo.org/mailman/listinfo/qgis-user]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20240726/e3bee0ba/attachment.htm>
More information about the QGIS-User
mailing list