Skip to content

Commit

Permalink
[Bugfix] Filter by polygon: table could be a query
Browse files Browse the repository at this point in the history
The layer filtered by polygon could be a query `_features_ids_with_sql_query`

The polygon layer  could be a query `_polygon_for_groups_with_sql_query`
  • Loading branch information
rldhont committed Nov 20, 2024
1 parent 718ac0a commit 99848e2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
22 changes: 16 additions & 6 deletions lizmap_server/filter_by_polygon.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ def _polygon_for_groups_with_sql_query(self, groups_or_user: tuple) -> QgsGeomet
Only for QGIS >= 3.10
"""
uri = QgsDataSourceUri(self.polygon.source())

try:
sql = r"""
WITH current_groups AS (
Expand All @@ -298,7 +299,7 @@ def _polygon_for_groups_with_sql_query(self, groups_or_user: tuple) -> QgsGeomet
SELECT
1 AS id, ST_AsBinary(ST_Union("{geom}")) AS geom
FROM
"{schema}"."{table}" AS p,
{table_name} AS p,
current_groups AS c
WHERE
c.user_group && (
Expand All @@ -318,8 +319,7 @@ def _polygon_for_groups_with_sql_query(self, groups_or_user: tuple) -> QgsGeomet
polygon_field=self.group_field,
groups_or_user=','.join(groups_or_user),
geom=uri.geometryColumn(),
schema=uri.schema(),
table=uri.table(),
table_name=FilterByPolygon._format_table_name(uri),
)
Logger.info(
f"Requesting the database about polygons for the current groups or user with : \n{sql}")
Expand Down Expand Up @@ -400,10 +400,9 @@ def _features_ids_with_sql_query(self, st_intersect: str) -> str:
"""
uri = QgsDataSourceUri(self.layer.source())

sql = 'SELECT "{pk}" FROM "{schema}"."{table}" WHERE {st_intersect}'.format(
sql = 'SELECT "{pk}" FROM {table_name} WHERE {st_intersect}'.format(
pk=self.primary_key,
schema=uri.schema(),
table=uri.table(),
table_name=FilterByPolygon._format_table_name(uri),
st_intersect=st_intersect,
)
Logger.info(
Expand Down Expand Up @@ -495,3 +494,14 @@ def _format_qgis_expression_relationship(
{current_geometry}
)"""
return expression

@classmethod
def _format_table_name(cls, uri: QgsDataSourceUri) -> str:
# is the datasource a query or a table ?
table_name = uri.table()
# is the datasource a query or a table ?
if not uri.schema() and table_name.startswith('(') and table_name.endswith(')'):
# it is a query
return table_name
# it is a table
return uri.quotedTablename()
17 changes: 17 additions & 0 deletions test/test_filter_by_polygon.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from qgis.core import (
QgsCoordinateReferenceSystem,
QgsDataSourceUri,
QgsFeature,
QgsGeometry,
QgsProject,
Expand Down Expand Up @@ -280,3 +281,19 @@ def test_subset_string_postgres(self):
ST_Centroid(\"geom\")
)"""
self.assertEqual(expected, sql)

def test_format_table_name(self):
""" Test format table name. """
uri = QgsDataSourceUri()
uri.setConnection("localhost", "5432", "dbname", "johny", "xxx")
uri.setDataSource("public", "roads", "the_geom", "cityid = 2643", "primary_key_field")

self.assertEqual('"public"."roads"', FilterByPolygon._format_table_name(uri))

uri.setDataSource("", "roads", "the_geom", "cityid = 2643", "primary_key_field")

self.assertEqual('"roads"', FilterByPolygon._format_table_name(uri))

uri.setDataSource("", '(SELECT * FROM "public"."roads")', "the_geom", "cityid = 2643", "primary_key_field")

self.assertEqual('(SELECT * FROM "public"."roads")', FilterByPolygon._format_table_name(uri))

0 comments on commit 99848e2

Please sign in to comment.