Skip to content
Permalink
Browse files

Accept geometry-less features in a geometry layer

  • Loading branch information
elpaso authored and nyalldawson committed Sep 14, 2018
1 parent abcfcfd commit a60324b79b12685b5168f066af33aae77936855c
@@ -120,17 +120,17 @@ def make_features_compatible(new_features, input_layer):
new_f_geom_type = QgsWkbTypes.geometryType(new_f.geometry().wkbType())
new_f_has_geom = new_f_geom_type not in (QgsWkbTypes.UnknownGeometry, QgsWkbTypes.NullGeometry)
input_layer_has_geom = input_wkb_type not in (QgsWkbTypes.NoGeometry, QgsWkbTypes.Unknown)
if input_layer_has_geom and not new_f_has_geom:
continue # Skip this feature completely
elif not input_layer_has_geom and new_f_has_geom:
# Drop geometry

# Drop geometry if layer is geometry-less
if not input_layer_has_geom and new_f_has_geom:
f = QgsFeature(input_layer.fields())
f.setAttributes(new_f.attributes())
new_f = f
result_features.append(new_f)
continue # skip the rest

if input_layer_has_geom and new_f.geometry().wkbType() != input_wkb_type: # Fix geometry
if input_layer_has_geom and new_f_has_geom and \
new_f.geometry().wkbType() != input_wkb_type: # Fix geometry
# Single -> Multi
if (QgsWkbTypes.isMultiType(input_wkb_type) and not
new_f.geometry().isMultipart()):
@@ -303,22 +303,31 @@ def test_make_features_compatible_attributes(self):

def test_make_features_compatible_geometry(self):
"""Test corner cases for geometries"""

# Make a feature with no geometry
layer = self._make_layer('Point')
self.assertTrue(layer.isValid())
self.assertTrue(layer.startEditing())
f1 = QgsFeature(layer.fields())
f1.setAttributes([1])

# Check that it is accepted on a Point layer
new_features = make_features_compatible([f1], layer)
self.assertEqual(len(new_features), 0)
self.assertEqual(len(new_features), 1)
self.assertEqual(new_features[0].geometry().asWkt(), '')

# Make a geometry-less layer
nogeom_layer = QgsMemoryProviderUtils.createMemoryLayer(
'nogeom_layer', layer.fields(), QgsWkbTypes.NoGeometry, QgsCoordinateReferenceSystem(4326))
# Check that a geometry-less feature is accepted
new_features = make_features_compatible([f1], nogeom_layer)
self.assertEqual(len(new_features), 1)
self.assertEqual(new_features[0].geometry().asWkt(), '')

# Make a geometry-less layer
nogeom_layer = QgsMemoryProviderUtils.createMemoryLayer(
'nogeom_layer', layer.fields(), QgsWkbTypes.NoGeometry, QgsCoordinateReferenceSystem(4326))
# Check that a Point feature is accepted but geometry was dropped
f1.setGeometry(QgsGeometry.fromWkt('Point(9 45)'))
new_features = make_features_compatible([f1], nogeom_layer)
self.assertEqual(len(new_features), 1)

0 comments on commit a60324b

Please sign in to comment.
You can’t perform that action at this time.