Skip to content

Commit

Permalink
Create all layers before filling in the data
Browse files Browse the repository at this point in the history
QGIS (or GDAL) prefer the gpkg structure to not change after
first opening
  • Loading branch information
m-kuhn committed Jan 14, 2024
1 parent c965b4c commit 884497d
Showing 1 changed file with 28 additions and 12 deletions.
40 changes: 28 additions & 12 deletions libqfieldsync/offliners.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,17 +200,9 @@ def ogr_escape(self, data: str):
# CSLFetchNameValue compares the name of an option followed by a `=` or `:` and treats everything after as value.
return data

def convert_to_offline_layer(
self,
layer: QgsVectorLayer,
data_source: ogr.DataSource,
offline_gpkg_path: str,
feature_request: QgsFeatureRequest = QgsFeatureRequest(),
) -> str:
"""
Will create a copy of ``layer`` in the GeoPackage specified as ``data_source`` which is stored at ``offline_gpkg_path``.
It will replace the dataProvider of the original layer.
"""
def create_layer(
self, layer: QgsVectorLayer, data_source: ogr.DataSource, offline_gpkg_path: str
) -> None:

identifier = hashlib.sha256(
layer.dataProvider().dataSourceUri().encode()
Expand Down Expand Up @@ -253,7 +245,27 @@ def convert_to_offline_layer(
)

ogr_layer.SyncToDisk()
qgis_uri = f"{offline_gpkg_path}|layername={identifier}" # |option:QGIS_FORCE_WAL=ON' TODO: check why forcewal is enabled

def convert_to_offline_layer(
self,
layer: QgsVectorLayer,
data_source: ogr.DataSource,
offline_gpkg_path: str,
feature_request: QgsFeatureRequest = QgsFeatureRequest(),
) -> str:
"""
Will create a copy of ``layer`` in the GeoPackage specified as ``data_source`` which is stored at ``offline_gpkg_path``.
It will replace the dataProvider of the original layer.
"""

identifier = hashlib.sha256(
layer.dataProvider().dataSourceUri().encode()
).hexdigest()

# See https://github.com/qgis/QGIS/commit/fe671f8bba3825c6b6e83b7c4693d2a3113b90fa for background on QGIS_FORCE_WAL
qgis_uri = (
f"{offline_gpkg_path}|layername={identifier}|option:QGIS_FORCE_WAL=ON"
)

qgis_layer_options = QgsVectorLayer.LayerOptions(
QgsProject.instance().transformContext()
Expand Down Expand Up @@ -382,6 +394,10 @@ class LayerInfo(NamedTuple):

datasource_mapping[datasource_hash].append(LayerInfo(layer, subset_string))

for datasource_hash, layer_infos in datasource_mapping.items():
layer_to_offline = layer_infos[0].layer
self.create_layer(layer_to_offline, data_source, offline_gpkg_path)

for datasource_hash, layer_infos in datasource_mapping.items():
request = QgsFeatureRequest()
# All layers for given `datasource_hash` are pointing to the very same file/datasource.
Expand Down

0 comments on commit 884497d

Please sign in to comment.