Skip to content
Snippets Groups Projects

Resolve "Introduce a new filter for when rules should be run based on the source ID of a building."

All threads resolved!
Compare and Show latest version
5 files
+ 85
106
Compare changes
  • Side-by-side
  • Inline
Files
5
+ 23
65
@@ -18,7 +18,6 @@
import abc
from typing import Union
import shapely
@@ -29,24 +28,25 @@ class AbstractRule(abc.ABC):
structure that is or could be necessary for all rules.
Args:
filters_dict (dict):
String representing the source_id_filter for the rule. May contain more than one
filter input, such as the `source_id` and the `geographic_filter_boundary`.
rule_source_ids (list):
A list of all valid source IDs for the rule. This is provided by the rule's XML file
if relevant.
geographic_filter_boundary (str):
Boundary polygon in WKT format defining the geographic boundary of the rule.
"""
def __init__(self, filters_dict: dict = None):
rule_source_ids, geographic_filter_boundary = self.parse_filters(filters_dict)
def __init__(
self,
rule_source_ids: list | None = None,
geographic_filter_boundary: str | None = None,
):
self.geographic_filter_boundary_geometry = None
if geographic_filter_boundary is not None:
self.geographic_filter_boundary_geometry = self.read_geometry_from_wkt(
self.geographic_filter_boundary_geometry = shapely.from_wkt(
geographic_filter_boundary
)
if rule_source_ids is not None:
rule_source_ids = [int(source_id) for source_id in rule_source_ids]
self.rule_source_ids = rule_source_ids
@abc.abstractmethod
@@ -55,7 +55,7 @@ class AbstractRule(abc.ABC):
The `__call__` method implements the core process of the rule.
"""
def parse_data(self, data: Union[str, bytes], source_file: str):
def parse_data(self, data: str | bytes, source_file: str):
"""
The `parse_data` method can be used to parse files that are attached to the
rule. As file types can widely vary, this method needs to be implemented for each
@@ -76,60 +76,18 @@ class AbstractRule(abc.ABC):
+ "please implement this class first."
)
@staticmethod
def parse_filters(filters_dict: dict) -> tuple | None:
"""
Parses the source_id_filter string input from the rule's XML file or the file attached
to it. Takes a single string separated by semicolons and splits it into the different
filter inputs it includes.
Args:
filters_dict (dict):
List with all the filter inputs.
Returns:
tuple | None:
A list of one or more strings each of which represents a different filter, or
None.
"""
if bool(filters_dict):
if "source_id" in filters_dict:
source_ids = filters_dict["source_id"].split(",")
else:
source_ids = None
return source_ids, filters_dict["boundary"]
return None, None
@staticmethod
def read_geometry_from_wkt(geographic_filter_boundary):
"""
Converts a WKT boundary into a Shapely polygon.
Args:
geographic_filter_boundary (str):
Boundary polygon in WKT format defining the geographic boundary of the rule.
Returns:
shapely.geometry.polygon.Polygon:
A Shapely polygon based on the WKT string geometry.
"""
return shapely.from_wkt(geographic_filter_boundary)
def filter(
self,
longitude: float = None,
latitude: float = None,
source_id: int = None,
longitude: float | None = None,
latitude: float | None = None,
source_id: int | None = None,
*args,
**kwargs
):
"""
Applies a spatial filter to ensure that only buildings within the provided geographic
polygon are processed and a source filter to ensure source specific rules only run
for inputs with a matching source ID.
polygon are processed and a source filter to ensure source-specific rules only run
for buildings with a matching source ID.
Args:
longitude (float):
@@ -137,24 +95,24 @@ class AbstractRule(abc.ABC):
latitude (float):
Latitude of the building being processed.
source_id (int):
Integer representing the data's source ID, and determines the rule's relevance.
Integer representing the building-source ID.
Returns:
bool:
True if all tests pass and False if at least one doesn't pass.
True if all tests pass and False if at least one does not pass.
"""
# Geographic filter, passes if the inputs coordinates lie within the boundary geometry.
# Geographic filter, passes if the building's coordinates lie within the boundary
# geometry.
if (
self.geographic_filter_boundary_geometry is not None
and not self.geographic_filter_boundary_geometry.contains(
and self.geographic_filter_boundary_geometry.disjoint(
shapely.geometry.Point(longitude, latitude)
)
):
return False
# Source ID filter, passes if the rules and the inputs source ID match.
# Source ID filter, passes if the rule's source ID and the building-source ID match.
if self.rule_source_ids is not None and source_id not in self.rule_source_ids:
return False
return True
Loading