diff --git a/pyramid_oereb/contrib/data_sources/interlis_2_3/sources/plr.py b/pyramid_oereb/contrib/data_sources/interlis_2_3/sources/plr.py index 0306a86b29..b3a5664814 100644 --- a/pyramid_oereb/contrib/data_sources/interlis_2_3/sources/plr.py +++ b/pyramid_oereb/contrib/data_sources/interlis_2_3/sources/plr.py @@ -470,7 +470,7 @@ def collect_related_geometries_by_real_estate(self, session, real_estate): .selectinload(self.models.MultilingualUri.localised_uri) ).all() - def collect_legend_entries_by_bbox(self, session, bbox, law_status): + def collect_legend_entries_by_bbox(self, session, bbox): """ Extracts all legend entries in the topic which have spatial relation with the passed bounding box of visible extent. @@ -478,12 +478,11 @@ def collect_legend_entries_by_bbox(self, session, bbox, law_status): Args: session (sqlalchemy.orm.Session): The requested clean session instance ready for use bbox (shapely.geometry.base.BaseGeometry): The bbox to search the records. - law_status (str): String of the law status for which the legend entries should be queried. Returns: list: The result of the related geometries unique by the public law restriction id and law status """ - distinct_legend_entry_ids = [] + # Select the legend entries of all plr within bbox geometries = session.query(self._model_).filter( or_( self._model_.point.ST_Intersects(from_shape(bbox, srid=Config.get('srid'))), @@ -493,13 +492,31 @@ def collect_legend_entries_by_bbox(self, session, bbox, law_status): selectinload(self.models.Geometry.public_law_restriction) ).all() + # Compile a list of unique legend entry ids for each law status + legend_entry_ids = dict() for geometry in geometries: - if geometry.public_law_restriction.legend_entry_id not in distinct_legend_entry_ids\ - and geometry.public_law_restriction.law_status == law_status: - distinct_legend_entry_ids.append(geometry.public_law_restriction.legend_entry_id) + if geometry.public_law_restriction.law_status not in legend_entry_ids.keys(): + legend_entry_ids[geometry.public_law_restriction.law_status] = { + geometry.public_law_restriction.legend_entry_id + } + else: + legend_entry_ids[geometry.public_law_restriction.law_status].add( + geometry.public_law_restriction.legend_entry_id + ) + + # Retrieve legend entries + legend_entries_from_db = [] + for law_status in legend_entry_ids.keys(): + legend_entries_from_db.append( + [ + session.query(self.legend_entry_model).filter( + self.legend_entry_model.t_id.in_(list(legend_entry_ids[law_status])) + ).all(), + law_status + ] + ) - return session.query(self.legend_entry_model).filter( - self.legend_entry_model.t_id.in_((distinct_legend_entry_ids))).all() + return legend_entries_from_db def read(self, params, real_estate, bbox): """ @@ -543,14 +560,8 @@ def read(self, params, real_estate, bbox): if (geometry.public_law_restriction.law_status not in law_status_of_geometry): law_status_of_geometry.append(geometry.public_law_restriction.law_status) - legend_entries_from_db = [] # get legend_entries per law_status - for law_status in law_status_of_geometry: - legend_entry_with_law_status = [ - self.collect_legend_entries_by_bbox(session, bbox, law_status), - law_status - ] - legend_entries_from_db.append(legend_entry_with_law_status) + legend_entries_from_db = self.collect_legend_entries_by_bbox(session, bbox) self.records = [] for geometry_result in geometry_results: diff --git a/pyramid_oereb/contrib/data_sources/standard/sources/plr.py b/pyramid_oereb/contrib/data_sources/standard/sources/plr.py index 3aa9028ae1..ab5c2a653c 100644 --- a/pyramid_oereb/contrib/data_sources/standard/sources/plr.py +++ b/pyramid_oereb/contrib/data_sources/standard/sources/plr.py @@ -614,7 +614,7 @@ def collect_related_geometries_by_real_estate(self, session, real_estate): .selectinload(self.models.PublicLawRestriction.responsible_office), ).all() - def collect_legend_entries_by_bbox(self, session, bbox, law_status): + def collect_legend_entries_by_bbox(self, session, bbox): """ Extracts all legend entries in the topic which have spatial relation with the passed bounding box of visible extent. @@ -622,22 +622,41 @@ def collect_legend_entries_by_bbox(self, session, bbox, law_status): Args: session (sqlalchemy.orm.Session): The requested clean session instance ready for use bbox (shapely.geometry.base.BaseGeometry): The bbox to search the records. - law_status (str): String of the law status for which the legend entries should be queried. Returns: list: The result of the related geometries unique by the public law restriction id and law status """ - distinct_legend_entry_ids = [] + # Select the legend entries of all plr within bbox geometries = self.handle_collection(session, bbox).options( selectinload(self.models.Geometry.public_law_restriction) ).all() + + # Compile a list of unique legend entry ids for each law status + legend_entry_ids = dict() for geometry in geometries: - if geometry.public_law_restriction.legend_entry_id not in distinct_legend_entry_ids \ - and geometry.public_law_restriction.law_status == law_status: - distinct_legend_entry_ids.append(geometry.public_law_restriction.legend_entry_id) - return session.query(self.legend_entry_model).filter( - self.legend_entry_model.id.in_((distinct_legend_entry_ids))).all() + if geometry.public_law_restriction.law_status not in legend_entry_ids.keys(): + legend_entry_ids[geometry.public_law_restriction.law_status] = { + geometry.public_law_restriction.legend_entry_id + } + else: + legend_entry_ids[geometry.public_law_restriction.law_status].add( + geometry.public_law_restriction.legend_entry_id + ) + + # Retrieve legend entries + legend_entries_from_db = [] + for law_status in legend_entry_ids.keys(): + legend_entries_from_db.append( + [ + session.query(self.legend_entry_model).filter( + self.legend_entry_model.id.in_(list(legend_entry_ids[law_status])) + ).all(), + law_status + ] + ) + + return legend_entries_from_db def read(self, params, real_estate, bbox): # pylint: disable=W:0221 """ @@ -680,14 +699,8 @@ def read(self, params, real_estate, bbox): # pylint: disable=W:0221 if (geometry.public_law_restriction.law_status not in law_status_of_geometry): law_status_of_geometry.append(geometry.public_law_restriction.law_status) - legend_entries_from_db = [] # get legend_entries per law_status - for law_status in law_status_of_geometry: - legend_entry_with_law_status = [ - self.collect_legend_entries_by_bbox(session, bbox, law_status), - law_status - ] - legend_entries_from_db.append(legend_entry_with_law_status) + legend_entries_from_db = self.collect_legend_entries_by_bbox(session, bbox) self.records = [] for geometry_result in geometry_results: