Skip to content

Commit

Permalink
make some more assertion when adding layers to qgis
Browse files Browse the repository at this point in the history
  • Loading branch information
cnheider committed Apr 29, 2024
1 parent b8d85a5 commit c978c8a
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 9 deletions.
2 changes: 1 addition & 1 deletion jord/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

__project__ = "Jord"
__author__ = "Christian Heider Lindbjerg"
__version__ = "0.5.5"
__version__ = "0.5.6"
__doc__ = r"""
.. module:: jord
:platform: Unix, Windows
Expand Down
13 changes: 13 additions & 0 deletions jord/qgis_utilities/helpers/garbage_collection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
def gc_layers():
# noinspection PyUnresolvedReferences
import qgis

# noinspection PyUnresolvedReferences
from qgis.core import QgsProject

registry_layers = QgsProject.instance().mapLayers().keys()
legend_layers = [
layer.id() for layer in qgis.utils.iface.legendInterface().layers()
]
layers_to_remove = set(registry_layers) - set(legend_layers)
QgsProject.instance().removeMapLayers(list(layers_to_remove))
65 changes: 57 additions & 8 deletions jord/qgis_utilities/layer_creation.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python3
import json
import logging
import time
from typing import Any, Iterable, List, Mapping, Optional, Union

Expand All @@ -24,6 +25,8 @@
"add_qgis_multi_feature_layer",
]

logger = logging.getLogger(__name__)


def add_qgis_single_feature_layer(
qgis_instance_handle: Any,
Expand Down Expand Up @@ -199,7 +202,16 @@ def add_qgis_single_feature_layer(
layer_data_provider = (
layer.dataProvider()
) # DEFAULT DATA PROVIDER, TODO: MAYBE CHANGE THIS
layer_data_provider.addFeatures([feat])
assert feat.isValid(), f"{feat} was invalid"
res, out_feats = layer_data_provider.addFeatures([feat])

if not res:
logger.error(f"{layer_data_provider.lastError()}")

assert (
res
), f"Failure while adding features {res} {layer_data_provider.lastError()}: {[o.isValid() for o in out_feats]}"

layer_data_provider.updateExtents()

if SKIP_MEMORY_LAYER_CHECK_AT_CLOSE:
Expand Down Expand Up @@ -291,11 +303,7 @@ def add_qgis_multi_feature_layer(
from .categorisation import categorise_layer

# noinspection PyUnresolvedReferences
from qgis.core import (
QgsFeature,
QgsVectorLayer,
QgsProject,
)
from qgis.core import QgsFeature, QgsVectorLayer, QgsProject, QgsFeatureSink

# noinspection PyUnresolvedReferences
import qgis
Expand All @@ -318,7 +326,6 @@ def add_qgis_multi_feature_layer(

geom_type = None
uri = None
features = []

sample_row = None
num_cols = None
Expand Down Expand Up @@ -351,8 +358,10 @@ def add_qgis_multi_feature_layer(
), f"{categorise_by_attribute} was not found in {fields}"

if not geoms:
logger.warning("Found no geometries")
return # No geometry

features = []
for geom in geoms:
geom_type_ = json.loads(geom.asJson())["type"]

Expand Down Expand Up @@ -395,8 +404,14 @@ def add_qgis_multi_feature_layer(
)

feat.setGeometry(geom)

assert feat.isValid(), f"{feat} was invalid"
features.append(feat)

assert len(list(geoms)) == len(
features
), f"Some features where dropped! {len(list(geoms))} != {len(features)}"

if uri is None:
raise Exception("uri is None")

Expand All @@ -416,7 +431,31 @@ def add_qgis_multi_feature_layer(

layer = QgsVectorLayer(uri, layer_name, "memory")
layer_data_provider = layer.dataProvider()
layer_data_provider.addFeatures(features)
# pr.addAttributes([QgsField("name", QVariant.String),QgsField("age", QVariant.Int),QgsField("size", QVariant.Double)])

(res, out_feats) = layer_data_provider.addFeatures(
features
# , QgsFeatureSink.RollBackOnErrors
)

if not res:
logger.error(f"{layer_data_provider.lastError()}")

assert (
res
), f"Failure while adding features {res} {layer_data_provider.lastError()}: {[o.isValid() for o in out_feats]}"

assert len(list(geoms)) == len(
out_feats
), f"Some features where dropped! return status {res}: {len(list(geoms))} != {len(out_feats)}"

if len(list(geoms)) == layer.featureCount():
logger.error(f"{features}")

assert (
len(list(geoms)) == layer.featureCount()
), f"Some features where dropped! {len(list(geoms))} != {layer.featureCount()}"

layer_data_provider.updateExtents()

if SKIP_MEMORY_LAYER_CHECK_AT_CLOSE:
Expand All @@ -425,10 +464,18 @@ def add_qgis_multi_feature_layer(
if categorise_by_attribute:
categorise_layer(layer, categorise_by_attribute)

assert (
len(list(geoms)) == layer.featureCount()
), f"Some features where dropped! {len(list(geoms))} != {layer.featureCount()}"

layer.commitChanges()
layer.updateFields()
layer.updateExtents()

assert (
len(list(geoms)) == layer.featureCount()
), f"Some features where dropped! {len(list(geoms))} != {layer.featureCount()}"

if qgis_instance_handle is None:
qgis_project = QgsProject.instance()
elif not isinstance(qgis_instance_handle, QgsProject):
Expand All @@ -452,6 +499,8 @@ def add_qgis_multi_feature_layer(

return_collection.append(layer)

assert len(return_collection) > 0, f"Return collection was empty"

return return_collection


Expand Down

0 comments on commit c978c8a

Please sign in to comment.