Views in an offline QField project packaged by QFieldCloud

Context:
We have QGIS Project with a postgis database. We push from QGIS to QFiledCloud and pull it from a mobile device where the project is executed in QFieldApp in offline mode.

What to achieve: We have complex rule-based symbology requirements where symbols should update even in QField in offline mode when data has changed. I spent hours, even days to find expressions, joins etc. that would perform. So far without luck. Everything I tried is super slow.

Questions:
I can create a view in the Postgis Database that would prepare the data so that the symbology requirements could be met. But this view would not update after QFiledCloud has packaged it into GeoPackage *.gpkg files.

Now, AI tells me that it is possible to create a view inside a GeoPackage file and that the view would even update in a QField offline project.

  1. Is it true that QField would update the view if there was one in the GeoPackage file when data has changed in the tables used by the view?

  2. How could I manage to tell the QFieldCloud packaging tool to create such a view? Could I somehow tell the QField Cloud packager to create the view from the postgis database?

Thanks
Roland

I’m not sure if view is the correct terminology for a geopackage inside of QGIS/QField. I think that by “view”, it might be referring to just a “Filter”. This should work on any layer, and particularly well with geopackages.

You can find it in the menu Layer > Filter…, by selecting the layer and pressing Ctrl + F; by right clicking on the layer name and going to Filter…; and –what might be the source of this apparent ai error– for geopackages you have the extra option in the right-click menu of Execute SQL (since it is a SQLite database). This last one might be more comfortable for you since you seem to have a pretty good handle on Postgis already, but I think that at the end of the day it’s just doing a Filter and might not have all the capabilities of a heavy-weight database.

A geopackage can contain views and they should update when other tables are updated. That is what a view should do.
I don’t think there is a way yet to have the packaging process create a view in a gpkg for offline use of PostGIS data.
Besides, I assume your PostGIS view contains expressions to populate attributes, so any data changes will update those attributes? And those attributes are used in the symbology? To have better performing symbology rendering?
The thing is that the table created in the geopackage does not contain expression any more, just the data in the PostGIS view when converted. At least, that is what I expect. I can’t test this because I don’t have access to an online PostGIS (mine is only localhost)

Rendering of symbology can be slow in certain circumstances, especially with on the fly calculations. In my experience, rendering is often quicker if it uses data stored in a table.
So would it help if those calculations are not done in rule-based symbology renderer, but somewhere else?

Me, overthinking this:
Could you make use of a second geopackage in your project? One that you create manually inside your QGIS project with a table where attributes use expressions for default values that updates on changes (“apply default value on update”, an option within the attributes form). These expressions then use data from the offline editable gpkg to calculate the data needed for your symbology. And then have your symbology for the offline editable layers reference data from your second geopackage?

You can create a view in PostGIS just to add the fields needed for symbology and populate them with some sql. Then in QGIS, use the Attributes Form to set a default value to those added fields, basically using that same sql logic and check the checkbox “apply default value on update”.
Again, if my assumption is correct about how PostGIS layers are implemented, the Attributes Form settings, along with all the styling, will be applied to the gpkg copy of your view.
Any changes made to a feature will now result in updated values in the added fields. Then make sure your rule-based styling is using the values in those fields.

Your assumptions are mostly correct. Not sure what you mean with “PostGIS view contains expressions”. It would be just a regular SQL view joining and selecting data from other tables (with and without geo columns) to make the rule-based symbology super simple and therefore fast.

Meanwhile, I came across virtual layers. What I read is that they work just like views. What I did not find out is if QField supports them in a performant way and if QFieldCloud Packager is supporting them.

I need to think more about your suggestion if this could work. Right now the virtual Layer feature looks promising, and I will try it first.

Thanks

I see, the view is to join other tables. And the outcome of the SQL is different when data collected from the field has changed, because that data is part of the view query.
I thought you needed custom fields in your view, a field that is not an existing one, but one created with a query e.g a concatenation of two fields or something much more elaborate.

Maybe Virtual layers will do the trick. Are you thinking of using embedded layers to create the join with? I would love to hear if that works and if it is fast enough.
I have never used embedded layers. Would they be treated as ‘directly access data source’? Or would they still be copied into a gpkg? And is that something you can tweak?

If that doesn’t work maybe you can add the PostGIS tables to the project. If you worry about them ending up in the Layers list or Legend, you can set those layers to ‘Private’ in Project Properties > Data Sources.