Skip to content

Commit 1c48e64

Browse files
committed
fix: wrap coordinate transform in try/catch so we don't get random failures
If the coordinate transform of a given bbox fails, just dump all data from that layer.
1 parent 5c0dc3d commit 1c48e64

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

libqfieldsync/offliners.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from pathlib import Path
66
from typing import Dict, List, NamedTuple, NewType, Optional
77

8+
import qgis.core
89
from osgeo import gdal, ogr, osr
910
from qgis.core import (
1011
Qgis,
@@ -136,8 +137,14 @@ def convert_to_offline(
136137
layer.removeSelection()
137138
else:
138139
tr = QgsCoordinateTransform(project.crs(), layer.crs(), project)
139-
layer_bbox = tr.transform(bbox)
140-
layer.selectByRect(layer_bbox)
140+
141+
try:
142+
layer_bbox = tr.transform(bbox)
143+
layer.selectByRect(layer_bbox)
144+
except qgis.core.QgsCsException as err:
145+
logger.warning(
146+
f"Failed to transform project CRS {project.crs().authid()} bbox to layer {layer.name()} CRS {layer.crs().authid()} bbox within `QgisCoreOffliner`: {err}. All features will be offlined for this layer."
147+
)
141148

142149
# If the selection by BBOX did not select anything, make sure we fool `QgsOfflineEditing` something is selected.
143150
# Otherwise when `layer.selectedFeatureIds().isEmpty()`, `QgsOfflineEditing` dumps all features.
@@ -442,8 +449,14 @@ def _convert_to_offline_project(
442449
tr = QgsCoordinateTransform(
443450
project.crs(), layer_to_offline.crs(), project
444451
)
445-
layer_bbox = tr.transform(bbox)
446-
request.setFilterRect(layer_bbox)
452+
453+
try:
454+
layer_bbox = tr.transform(bbox)
455+
request.setFilterRect(layer_bbox)
456+
except qgis.core.QgsCsException as err:
457+
logger.warning(
458+
f"Failed to transform project CRS {project.crs().authid()} bbox to layer {layer_to_offline.name()} CRS {layer_to_offline.crs().authid()} bbox within `PythonMiniOffliner`: {err}. All features will be offlined for this layer."
459+
)
447460

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

0 commit comments

Comments
 (0)