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."

Compare and Show latest version
3 files
+ 42
54
Compare changes
  • Side-by-side
  • Inline
Files
3
+ 16
28
@@ -18,7 +18,7 @@
import abc
from typing import Union
from typing import Union, Optional
import shapely
@@ -30,16 +30,21 @@ class AbstractRule(abc.ABC):
Args:
rule_source_ids (list):
A list of integers representing all the valid source ID's for the rule.
A list of all valid source IDs for the rule. This is provided by the rule's XML
if relevant.
geographic_filter_boundary (str):
Boundary polygon in WKT format defining the geographic boundary of the rule.
"""
def __init__(self, rule_source_ids: list = None, geographic_filter_boundary: str = None):
def __init__(
self,
rule_source_ids: Optional[list | None] = None,
geographic_filter_boundary: Optional[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
)
@@ -72,33 +77,17 @@ class AbstractRule(abc.ABC):
+ "please implement this class first."
)
@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: Optional[float | None] = None,
latitude: Optional[float | None] = None,
source_id: Optional[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
polygon are processed and a source filter to ensure source-specific rules only run
for inputs with a matching source ID.
Args:
@@ -107,11 +96,11 @@ 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 input-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 doesn't pass.
"""
# Geographic filter, passes if the inputs coordinates lie within the boundary geometry.
@@ -123,8 +112,7 @@ class AbstractRule(abc.ABC):
):
return False
# Source ID filter, passes if the rules and the inputs source ID match.
# Source ID filter, passes if the rule's and the 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