Skip to content

"Difference" produce incorrect result when using a point layer and a polygon overlay layer #61205

@jfbourdon

Description

@jfbourdon

What is the bug or the crash?

Using points as an input layer and a polygon as an overlay layer with the "Difference" processing tool produces an incorrect result which is that points inside the polygon aren't shown on the map but are still present in the attribute table.

Probably related to:

Steps to reproduce the issue

# Create point layer
pts = QgsVectorLayer("Point?crs=EPSG:3857", "pts", "memory")
for wkt in ('Point (0 0)', 'Point (0 2)'):
    geom = QgsGeometry.fromWkt(wkt)
    fet = QgsFeature()
    fet.setGeometry(geom)
    pts.dataProvider().addFeature(fet)


# Create polygon layer
poly = QgsVectorLayer("Polygon?crs=EPSG:3857", "polygon", "memory")
wkt = 'Polygon ((-1 1, 1 1, 1 -1, -1 -1, -1 1))'
geom = QgsGeometry.fromWkt(wkt)
fet = QgsFeature()
fet.setGeometry(geom)
poly.dataProvider().addFeature(fet)


# Keep point outside polygon using "Difference"
pts_diff = processing.run("native:difference", {
    'INPUT':pts,
    'OVERLAY':poly,
    'OUTPUT':'TEMPORARY_OUTPUT'
    })["OUTPUT"]


# Keep point outside polygon using "Extract by Location" with "disjoint" predicate
pts_extract = processing.run("native:extractbylocation", {
    'INPUT':pts,
    'PREDICATE':[2],
    'INTERSECT':poly,
    'OUTPUT':'TEMPORARY_OUTPUT'
    })["OUTPUT"]


# Check remaining points
pts_diff.dataProvider().featureCount()     # 2 (bad)
pts_extract.dataProvider().featureCount()  # 1 (ok)

QgsProject.instance().addMapLayer(poly)
QgsProject.instance().addMapLayer(pts_diff)  # show one point on map but two points in attribute table
QgsProject.instance().addMapLayer(pts_extract)  # show one point on map and in attribute table

Versions

QGIS 3.40.4
GEOS 3.13.0-CAPI-1.19.0

Windows 11 Enterprise 23H2

Supported QGIS version

  • I'm running a supported QGIS version according to the roadmap.

New profile

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugEither a bug report, or a bug fix. Let's hope for the latter!ProcessingRelating to QGIS Processing framework or individual Processing algorithms

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions