Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion libqfieldsync/offliners.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,12 +403,13 @@ def _convert_to_offline_project(
driver = ogr.GetDriverByName("GPKG")
data_source = driver.CreateDataSource(offline_gpkg_path)
datasource_mapping = self._get_datasource_mapping(project, offline_layers)
filters_mapping = self._get_filters_mapping(datasource_mapping)

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

for layer_infos in datasource_mapping.values():
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.
# Here we get the first layer for convenience, but it doesn't really matter.
Expand Down Expand Up @@ -439,6 +440,9 @@ def _convert_to_offline_project(
layer_bbox = tr.transform(bbox)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not related to the change in this PR, but since I see it now and it's an important one, I shall speak ;)

You should wrap this transform around a try / except. If the transform fails, QGIS throws a QgsCsException. Without catching that, I suspect here what will happen is that we'll either silently fail or the whole offline will fail due to a parent try / except.

If we catch it here, it'll allow us to decide whether we want to a/ stop the export and provide clear guidance on how to resolve failure (i.e., fix your extent), b/ offline the whole layer without being filtered by a bounding box, or c/ offline the layer with zero feature in it.

I'm leaning towards option b or c, and I can think of multiple reasons why either are good 😆

Copy link
Collaborator Author

@suricactus suricactus Mar 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see #139

request.setFilterRect(layer_bbox)

if filters_mapping[datasource_hash]:
request.setFilterExpression(filters_mapping[datasource_hash])

source = self.convert_to_offline_layer(
layer_to_offline, data_source, offline_gpkg_path, request
)
Expand All @@ -465,6 +469,26 @@ def _convert_to_offline_project(
project.writePath(offline_gpkg_path),
)

def _get_filters_mapping(
self, datasource_mapping: Dict[str, List[LayerInfo]]
) -> Dict[str, str]:
"""Get mapping of filter strings to be applied for each datasource. If no filters to be applied, the value will be an empty string."""
filters_by_datasource = {}

for datasource_hash, layer_infos in datasource_mapping.items():
filters = []
for layer_info in layer_infos:
if layer_info.subset_string.strip():
filters.append(f"({layer_info.subset_string})")
else:
# if there is no subset string, we don't need to apply any filter, as it means "all features"
filters = []
break

filters_by_datasource[datasource_hash] = " OR ".join(filters)

return filters_by_datasource

def _get_datasource_mapping(
self,
project: QgsProject,
Expand Down
Loading