Filters¶
What is the Filter module?
The filter
module is responsible for filtering geospatial datasets based on specific criteria or conditions out
of your urban layer
.
Meanwhile, we recommend to look through the Example
's Filter for a more hands-on introduction about
the Filter module and its usage.
Documentation Under Alpha Construction
This documentation is in its early stages and still being developed. The API may therefore change, and some parts might be incomplete or inaccurate.
Use at your own risk, and please report anything that seems incorrect
/ outdated
you find.
GeoFilterBase
¶
Bases: ABC
Base class for all spatial filters in UrbanMapper
This abstract class defines the common interface that all filter implementations
must follow. Filters are used to subset or filter GeoDataFrames
based on spatial
criteria derived from an urban layer
.
Note
This is an abstract class and cannot be instantiated directly. Use concrete
implementations like BoundingBoxFilter
instead.
Source code in src/urban_mapper/modules/filter/abc_filter.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
|
_transform(input_geodataframe, urban_layer)
abstractmethod
¶
Internal implementation method for filtering a GeoDataFrame
Called by transform()
after input validation. Subclasses must override this
method to implement specific filtering logic.
To be implemented by subclasses
This method should contain the core logic for filter data given the
urban_layer
. It should be implemented in subclasses to handle the
specific filtering task (e.g., bounding box, polygonal area) and return the
modified GeoDataFrame
.
Usefulness of Filters?
Filters are essential for narrowing down large datasets to only those
relevant to a specific analysis or study area. Think of an analysis in
Downtown Brooklyn
but your dataset is having data points all over the New York City & Its Boroughs
.
In this case, you can use a filter to subset the data to only include points within Downtown Brooklyn
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_geodataframe
|
GeoDataFrame
|
The |
required |
urban_layer
|
UrbanLayerBase
|
The |
required |
Returns:
Type | Description |
---|---|
GeoDataFrame
|
gpd.GeoDataFrame: A filtered |
Raises:
Type | Description |
---|---|
ValueError
|
If the filtering operation cannot be performed due to invalid inputs. |
Source code in src/urban_mapper/modules/filter/abc_filter.py
transform(input_geodataframe, urban_layer)
¶
Filter a GeoDataFrame
based on spatial criteria from an urban layer
The primary public method for applying filters. It validates inputs and delegates
to the subclass-specific _transform()
method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_geodataframe
|
Union[Dict[str, GeoDataFrame], GeoDataFrame]
|
one or more |
required |
urban_layer
|
UrbanLayerBase
|
The |
required |
Returns:
Type | Description |
---|---|
Union[Dict[str, GeoDataFrame], GeoDataFrame]
|
Union[Dict[str, GeoDataFrame], GeoDataFrame]: one or more filtered |
Raises:
Type | Description |
---|---|
ValueError
|
If input_geodataframe or urban_layer is None. |
ValueError
|
If the filtering operation fails. |
Examples:
>>> from urban_mapper.modules.filter import BoundingBoxFilter
>>> from urban_mapper.modules.urban_layer import OSMNXStreets
>>> streets_layer = OSMNXStreets().from_place("Manhattan, New York")
>>> bbox_filter = BoundingBoxFilter()
>>> filtered_data = bbox_filter.transform(taxi_trips, streets_layer)
>>> filtered_data.head()
>>> # 👆This would show onloy data within the bounding box of the streets layer. I.e. `Manhattan, New York`.
Source code in src/urban_mapper/modules/filter/abc_filter.py
preview(format='ascii')
abstractmethod
¶
Generate a preview of the filter's configuration.
Provides a summary of the filter for inspection.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
format
|
str
|
The output format. Options are:
Defaults to "ascii". |
'ascii'
|
Returns:
Name | Type | Description |
---|---|---|
Any |
Any
|
A representation of the filter in the requested format (e.g., str or dict). |
Raises:
Type | Description |
---|---|
ValueError
|
If an unsupported format is specified. |
Abstract Method
Subclasses must implement this method to provide configuration details.
Source code in src/urban_mapper/modules/filter/abc_filter.py
BoundingBoxFilter
¶
Bases: GeoFilterBase
Filter that limits data to the bounding box of an urban layer
Retains only data points or geometries within the urban layer
’s bounding box,
using geopandas’ .cx accessor for efficient spatial indexing.
See further in https://geopandas.org/en/stable/docs/reference/api/geopandas.GeoDataFrame.cx.html
Note
The bounding box may include areas outside the urban layer
’s actual features.
Examples:
>>> from urban_mapper.modules.filter import BoundingBoxFilter
>>> from urban_mapper.modules.urban_layer import OSMNXStreets
>>> streets = OSMNXStreets()
>>> streets.from_place("Manhattan, New York")
>>> bbox_filter = BoundingBoxFilter()
>>> filtered_data = bbox_filter.transform(taxi_trips, streets)
Source code in src/urban_mapper/modules/filter/filters/bounding_box_filter.py
_transform(input_geodataframe, urban_layer)
¶
Filter data to the bounding box of the urban layer
Uses the urban layer
’s bounding box to filter the input GeoDataFrame
.
Tip
Ensure the urban layer
is fully loaded before applying the filter.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_geodataframe
|
GeoDataFrame
|
The |
required |
urban_layer
|
UrbanLayerBase
|
The |
required |
Returns:
Type | Description |
---|---|
GeoDataFrame
|
gpd.GeoDataFrame: Filtered |
Raises:
Type | Description |
---|---|
AttributeError
|
If |
Source code in src/urban_mapper/modules/filter/filters/bounding_box_filter.py
preview(format='ascii')
¶
Generate a preview of this bounding box filter.
Provides a summary of the filter’s configuration.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
format
|
str
|
The output format ("ascii" or "json"). Defaults to "ascii". |
'ascii'
|
Returns:
Name | Type | Description |
---|---|---|
Any |
Any
|
A string (for "ascii") or dict (for "json") representing the filter. |
Raises:
Type | Description |
---|---|
ValueError
|
If format is unsupported. |
Examples:
>>> bbox_filter = BoundingBoxFilter()
>>> print(bbox_filter.preview())
Filter: BoundingBoxFilter
Action: Filter data to the bounding box of the urban layer
Source code in src/urban_mapper/modules/filter/filters/bounding_box_filter.py
FilterFactory
¶
Factory class for creating and configuring spatial filters
Provides a fluent chaining-based-methods interface to instantiate filters
, configure settings
, and apply
them
to GeoDataFrames
.
Attributes:
Name | Type | Description |
---|---|---|
_filter_type |
Optional[str]
|
The type of filter to create. |
_extra_params |
Dict[str, Any]
|
Configuration parameters for the filter. |
_instance |
Optional[GeoFilterBase]
|
The filter instance (internal use). |
_preview |
Optional[dict]
|
Preview configuration (internal use). |
Examples:
>>> from urban_mapper import UrbanMapper
>>> import geopandas as gpd
>>> mapper = UrbanMapper()
>>> layer = mapper.urban_layer.region_neighborhoods().from_place("Brooklyn, New York")
>>> data = gpd.read_file("nyc_points.csv") # Example data
>>> filtered_data = mapper.filter.with_type("BoundingBoxFilter") ... .transform(data, layer)
Source code in src/urban_mapper/modules/filter/filter_factory.py
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
|
with_type(primitive_type)
¶
Specify the type of filter to use.
Configures the factory to create a specific filter type from FILTER_REGISTRY.
FILTER_REGISTRY looks like this
Open the folder filters
in src/urban_mapper/modules/filter
to see the available filter types
in FILTER_REGISTRY. Each filter class is registered under its class name.
You also can use list(FILTER_REGISTRY.keys())
to see available filter types.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
primitive_type
|
str
|
The name of the filter type (e.g., "BoundingBoxFilter"). |
required |
Returns:
Name | Type | Description |
---|---|---|
FilterFactory |
FilterFactory
|
Self for method chaining. |
Raises:
Type | Description |
---|---|
ValueError
|
If primitive_type is not in FILTER_REGISTRY. |
Examples:
Source code in src/urban_mapper/modules/filter/filter_factory.py
transform(input_geodataframe, urban_layer)
¶
Apply the filter to input data and return filtered results
Creates and applies a filter instance to the input GeoDataFrame
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_geodataframe
|
Union[Dict[str, GeoDataFrame], GeoDataFrame]
|
one or more |
required |
urban_layer
|
UrbanLayerBase
|
The urban layer for filtering criteria. |
required |
Returns:
Type | Description |
---|---|
Union[Dict[str, GeoDataFrame], GeoDataFrame]
|
Union[Dict[str, gpd.GeoDataFrame], gpd.GeoDataFrame]: The filtered data. |
Raises:
Type | Description |
---|---|
ValueError
|
If _filter_type is not set. |
Examples:
>>> layer = mapper.urban_layer.region_neighborhoods().from_place("Brooklyn, New York")
>>> data = gpd.read_file("nyc_points.csv") # Example data
>>> filtered_data = mapper.filter.with_type("BoundingBoxFilter") ... .transform(data, layer)
Source code in src/urban_mapper/modules/filter/filter_factory.py
build()
¶
Build and return a filter instance without applying it.
Creates a filter instance for use in pipelines or deferred execution.
Note
Prefer transform()
for immediate filtering; use build() for pipelines.
Returns:
Name | Type | Description |
---|---|---|
GeoFilterBase |
GeoFilterBase
|
A configured filter instance. |
Raises:
Type | Description |
---|---|
ValueError
|
If _filter_type is not set. |
Examples:
>>> filter_component = mapper.filter.with_type("BoundingBoxFilter").build()
>>> pipeline.add_filter(filter_component)
Source code in src/urban_mapper/modules/filter/filter_factory.py
preview(format='ascii')
¶
Display a preview of the filter configuration and settings.
Shows the filter’s configuration in the specified format.
Note
Requires a prior call to build() or transform().
Parameters:
Name | Type | Description | Default |
---|---|---|---|
format
|
str
|
The format to display ("ascii" or "json"). Defaults to "ascii". |
'ascii'
|
Raises:
Type | Description |
---|---|
ValueError
|
If format is unsupported. |
Examples:
>>> factory = mapper.filter.with_type("BoundingBoxFilter")
>>> factory.build()
>>> factory.preview(format="json")
Source code in src/urban_mapper/modules/filter/filter_factory.py
with_preview(format='ascii')
¶
Configure the factory to display a preview after building.
Enables automatic preview after build().
Parameters:
Name | Type | Description | Default |
---|---|---|---|
format
|
str
|
The preview format ("ascii" or "json"). Defaults to "ascii". |
'ascii'
|
Returns:
Name | Type | Description |
---|---|---|
FilterFactory |
FilterFactory
|
Self for chaining. |
Examples:
>>> filter_component = mapper.filter.with_type("BoundingBoxFilter") ... .with_preview(format="json") ... .build()