Folium Visualization System Base¶
base_viz_system
¶
PropertyMapper
¶
Maps a data item to the value of one visual property.
A PropertyMapper wraps a function that takes a VisualizableDataItem and
returns a value, such as a color, a width or a geometry. Feature resolvers
call it for each map element. The class methods build mappers from a static
value, from an attribute of the model object or from the KPI value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mapping
|
Callable[[Union[VisualizableDataItem, KPIDataItem, ModelDataItem]], Any]
|
Function that takes a data item and returns the property value. |
required |
Examples:
>>> from mesqual.kpis import Aggregations, FlagAggKPIBuilder
>>> from mesqual.visualizations.folium_viz_system import KPIDataItem
>>> dataset.add_kpis_from_definitions(
... FlagAggKPIBuilder().for_flag('Country.Results.price').with_aggregation(Aggregations.Mean).build()
... )
>>> price = KPIDataItem(dataset.kpi_collection.filter(object_name='C1')[0])
>>> PropertyMapper.from_static_value('#000000').map_data_item(price)
'#000000'
A mapper with its own function:
>>> label = PropertyMapper(lambda item: f"{item.get_object_attribute('name')}: {item.get_text_representation()}")
>>> label.map_data_item(price)
'Country 1: 42.1 €/MWh'
Source code in src/mesqual/visualizations/folium_viz_system/base_viz_system.py
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 | |
map_data_item
¶
map_data_item(data_item: VisualizableDataItem) -> Any
Map data item to property value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_item
|
VisualizableDataItem
|
Data item to map |
required |
Returns:
| Type | Description |
|---|---|
Any
|
Mapped property value |
Source code in src/mesqual/visualizations/folium_viz_system/base_viz_system.py
59 60 61 62 63 64 65 66 67 68 69 | |
from_static_value
classmethod
¶
from_static_value(value: Any) -> PropertyMapper
Create mapper that returns the same value for all data items.
Used for consistent styling across all map elements (e.g., all borders the same color, all markers the same size).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
Static value to return for all data items |
required |
Returns:
| Type | Description |
|---|---|
PropertyMapper
|
PropertyMapper that always returns the static value |
Source code in src/mesqual/visualizations/folium_viz_system/base_viz_system.py
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | |
from_item_attr
classmethod
¶
from_item_attr(attribute: str, mapping: Callable[[Any], Any] = None) -> PropertyMapper
Create mapper from model/object attribute with optional transformation.
Extracts values from model data attributes (geometry, capacity, name, etc.) and optionally applies a transformation function. The attribute is resolved from the underlying model DataFrame or object data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
attribute
|
str
|
Name of the attribute to extract (e.g., 'geometry', 'capacity'), must be an entry in the object pd.Series |
required |
mapping
|
Callable[[Any], Any]
|
Optional transformation function to apply to the attribute value |
None
|
Returns:
| Type | Description |
|---|---|
PropertyMapper
|
PropertyMapper that extracts and optionally transforms the attribute |
Examples:
>>> from mesqual.visualizations.folium_viz_system import ModelDataItem
>>> generator = ModelDataItem(dataset.fetch('Generator.Model').loc['G3'], object_type='Generator')
>>> PropertyMapper.from_item_attr('fuel').map_data_item(generator)
'gas'
>>> print(PropertyMapper.from_item_attr('capacity', lambda mw: mw / 100).map_data_item(generator))
10.0
Source code in src/mesqual/visualizations/folium_viz_system/base_viz_system.py
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 | |
from_kpi_value
classmethod
¶
from_kpi_value(mapping: Callable[[Any], Any], use_abs_kpi_value: bool = False) -> PropertyMapper
Create mapper from KPI values with transformation function.
Specifically designed for KPIDataItem objects, extracts the computed KPI value and applies a transformation. Used for styling based on energy system metrics like power flows, prices, or emissions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mapping
|
Callable[[Any], Any]
|
Transformation function applied to the KPI value |
required |
Returns:
| Type | Description |
|---|---|
PropertyMapper
|
PropertyMapper that transforms KPI values |
Examples:
>>> from mesqual.kpis import Aggregations, FlagAggKPIBuilder
>>> from mesqual.visualizations.folium_viz_system import KPIDataItem
>>> dataset.add_kpis_from_definitions(
... FlagAggKPIBuilder().for_flag('CountryBorder.Results.flow_sent_net').with_aggregation(Aggregations.Mean).build()
... )
>>> flow = KPIDataItem(dataset.kpi_collection.filter(object_name='C1 - C2')[0])
>>> print(PropertyMapper.from_kpi_value(lambda v: v < 0).map_data_item(flow))
True
A colorscale on the absolute flow:
>>> from mesqual.visualizations.value_mapping_system import SegmentedContinuousColorscale
>>> colors = SegmentedContinuousColorscale(segments={(0, 1000): 'YlOrRd'})
>>> PropertyMapper.from_kpi_value(colors, use_abs_kpi_value=True).map_data_item(flow)
'#fd923e'
Source code in src/mesqual/visualizations/folium_viz_system/base_viz_system.py
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 | |
ResolvedFeature
dataclass
¶
Container for resolved feature properties.
Source code in src/mesqual/visualizations/folium_viz_system/base_viz_system.py
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 | |
FeatureResolver
¶
Bases: Generic[ResolvedFeatureType]
Resolves the visual properties of a data item.
The resolver calls one PropertyMapper per property and returns the values
as a resolved feature. A property value that is not a PropertyMapper is a
static value for all items. The subclasses for areas, lines and markers set
the default mappers of their properties.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
feature_type
|
Type[ResolvedFeatureType]
|
Class of the resolved feature. |
None
|
**property_mappers
|
PropertyMapper | Any
|
|
{}
|
Examples:
>>> from mesqual.kpis import Aggregations, FlagAggKPIBuilder
>>> from mesqual.visualizations.folium_viz_system import AreaFeatureResolver, KPIDataItem
>>> dataset.add_kpis_from_definitions(
... FlagAggKPIBuilder().for_flag('Country.Results.price').with_aggregation(Aggregations.Mean).build()
... )
>>> price = KPIDataItem(dataset.kpi_collection.filter(object_name='C1')[0])
>>> resolver = AreaFeatureResolver(
... fill_color=PropertyMapper.from_kpi_value(lambda v: 'red' if v > 45 else 'blue'),
... fill_opacity=0.8,
... )
>>> feature = resolver.resolve_feature(price)
>>> feature.fill_color, feature.fill_opacity, feature.border_color
('blue', 0.8, 'white')
>>> feature.geometry
<POLYGON ((9 46, 9 50, 5 50, 5 46, 9 46))>
Source code in src/mesqual/visualizations/folium_viz_system/base_viz_system.py
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 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 | |
resolve_feature
¶
resolve_feature(data_item: VisualizableDataItem) -> ResolvedFeatureType
Resolve feature properties from data item.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_item
|
VisualizableDataItem
|
Data item to resolve |
required |
Returns:
| Type | Description |
|---|---|
ResolvedFeatureType
|
Resolved feature with all computed properties |
Source code in src/mesqual/visualizations/folium_viz_system/base_viz_system.py
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 | |
FoliumObjectGenerator
¶
Bases: Generic[FeatureResolverType], ABC
Abstract base class for generating folium map objects from data items.
A generator adds one folium object per data item to a feature group, for example an area, a line or a marker. Its feature resolver gives the visual properties. The generators take the rows of a model DataFrame or the KPIs of a KPI collection.
Examples:
Areas from the country model, lines from the border model, in one feature group:
>>> import folium
>>> from mesqual.visualizations.folium_viz_system import AreaGenerator, LineFeatureResolver, LineGenerator
>>> fg = folium.FeatureGroup('Model')
>>> fg = AreaGenerator().generate_objects_for_model_df(dataset.fetch('Country.Model'), fg)
>>> lines = LineGenerator(LineFeatureResolver(geometry=PropertyMapper.from_item_attr('geo_line_string')))
>>> fg = lines.generate_objects_for_model_df(dataset.fetch('CountryBorder.Model'), fg)
>>> m = folium.Map(location=[48, 9], zoom_start=6)
>>> fg = fg.add_to(m)
Source code in src/mesqual/visualizations/folium_viz_system/base_viz_system.py
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 | |
generate
abstractmethod
¶
generate(data_item: VisualizableDataItem, feature_group: FeatureGroup) -> None
Generate folium object and add it to the feature group.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_item
|
VisualizableDataItem
|
Data item to visualize |
required |
feature_group
|
FeatureGroup
|
Feature group to add to |
required |
Source code in src/mesqual/visualizations/folium_viz_system/base_viz_system.py
450 451 452 453 454 455 456 457 458 459 | |
generate_objects_for_model_df
¶
generate_objects_for_model_df(model_df: DataFrame, feature_group: FeatureGroup, **kwargs) -> FeatureGroup
Add model DataFrame data to the map.
Source code in src/mesqual/visualizations/folium_viz_system/base_viz_system.py
461 462 463 464 465 466 467 468 469 470 | |
generate_objects_for_kpi_collection
¶
generate_objects_for_kpi_collection(kpi_collection: KPICollection, feature_group: FeatureGroup, **kwargs) -> FeatureGroup
Add KPI data to the map.
Source code in src/mesqual/visualizations/folium_viz_system/base_viz_system.py
472 473 474 475 476 477 478 479 | |
generate_object_for_single_kpi
¶
generate_object_for_single_kpi(kpi: KPI, feature_group: FeatureGroup, kpi_collection: KPICollection = None, **kwargs) -> FeatureGroup
Add a single KPI to the map with optional context.
Source code in src/mesqual/visualizations/folium_viz_system/base_viz_system.py
481 482 483 484 485 486 487 | |