Skip to content

Commit

Permalink
Use NOT ST_Intersects instead of ST_Disjoint for performance
Browse files Browse the repository at this point in the history
Closes #18
  • Loading branch information
Johannes Kröger committed Oct 27, 2022
1 parent 184c9c6 commit 10f50e0
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,25 @@ def geometry(self) -> QgsGeometry:
return QgsGeometry.fromWkt(self.wkt)

def filterString(self, layer: QgsVectorLayer) -> str:
template = "ST_{predicate}({geom_name}, ST_TRANSFORM(ST_GeomFromText('{wkt}', {srid}), {layer_srid}))"
"""Returns a layer filter string corresponding to the filter definition.
Args:
layer (QgsVectorLayer): The layer for which the filter should be applied
Returns:
str: A layer filter string
"""

# ST_DISJOINT does not use spatial indexes, but we can use its opposite "NOT ST_INTERSECTS" which does
if self.predicate == Predicate.DISJOINT:
spatial_predicate = "NOT ST_INTERSECTS"
else:
spatial_predicate = f"ST_{Predicate(self.predicate).name}"

template = "{spatial_predicate}({geom_name}, ST_TRANSFORM(ST_GeomFromText('{wkt}', {srid}), {layer_srid}))"
geom_name = getLayerGeomName(layer)
return template.format(
predicate=Predicate(self.predicate).name,
spatial_predicate=spatial_predicate,
geom_name=geom_name,
wkt=self.wkt,
srid=self.crs.postgisSrid(),
Expand Down Expand Up @@ -112,4 +127,4 @@ def askOverwrite(self, name: str) -> bool:
def askDelete(self, name: str) -> bool:
txt = self.tr('Delete filter')
return QMessageBox.question(iface.mainWindow(), self.tr('Delete?'), f'{txt} <i>{name}</i>?',
QMessageBox.Yes, QMessageBox.No) == QMessageBox.Yes
QMessageBox.Yes, QMessageBox.No) == QMessageBox.Yes

0 comments on commit 10f50e0

Please sign in to comment.