Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bugfix] Filter by polygon: table could be a query #98

Merged
merged 1 commit into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:
""" Format the table name according to the URI. """
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))