Skip to content

Plotting with plot_eai_exp_geom fails due to exposures vs impact shape mismatch  #992

@luseverin

Description

@luseverin

Describe the bug
I am using the lines_polys_handler handler to compute impacts using lines as exposures and plotting the resulting EAI using plot_eai_exp_geom. However, the plot_eai_exp_geom fails with a ValueError: Length of values (861875) does not match length of index (861881). Apparently, this comes from a mismatch between the shape of the original exposures and the reaggregated impacts causing the error when attempting to do the following value assignement:

File ~/Documents/PhD/workspace/climada_python/climada/util/lines_polys_handler.py:357, in plot_eai_exp_geom(imp_geom, centered, figsize, **kwargs)
    [355](https://file+.vscode-resource.vscode-cdn.net/Users/lseverino/Documents/PhD/workspace/dev/~/Documents/PhD/workspace/climada_python/climada/util/lines_polys_handler.py:355)     kwargs["legend"] = True
    [356](https://file+.vscode-resource.vscode-cdn.net/Users/lseverino/Documents/PhD/workspace/dev/~/Documents/PhD/workspace/climada_python/climada/util/lines_polys_handler.py:356) gdf_plot = gpd.GeoDataFrame(imp_geom.geom_exp)
--> [357](https://file+.vscode-resource.vscode-cdn.net/Users/lseverino/Documents/PhD/workspace/dev/~/Documents/PhD/workspace/climada_python/climada/util/lines_polys_handler.py:357) gdf_plot["impact"] = imp_geom.eai_exp`

I have had a look, and apparently the error happens when the index of the exposures' geodataframe has non-unique values, i.e. see lines 5 to 8:
Screenshot 2025-01-14 at 09 33 52

The error comes from an ill-defined Exposures but I assume we would want to catch that in a way? Any suggestion on a fix?

To Reproduce
Code example:

from shapely.geometry import Point, LineString, Polygon
gdf_test = gpd.GeoDataFrame
data = {
    "name": ["A", "B", "C"],
    "value": [10, 20, 30],
    "geometry": [
        Point(1, 1),            
        Point(1, 2),
        Point(1, 3)
    ],
}
gdf_test = gpd.GeoDataFrame(data, crs="EPSG:4326") 
gdf_test.index = [0,1,0] #need to specify non-unique index to cause the error!

#hazard and impf
from climada.util.api_client import Client
import climada.util.lines_polys_handler as u_lp
from climada.entity.impact_funcs import ImpactFuncSet
from climada.entity.impact_funcs.storm_europe import ImpfStormEurope
from climada.entity import Exposures


haz = Client().get_hazard("storm_europe", name="test_haz_WS_nl", status="test_dataset")

#exposures
exp_line = Exposures(gdf_test)

#impact function
impf_line = ImpfStormEurope.from_welker()
impf_set = ImpactFuncSet([impf_line])

exp_line.data["impf_WS"] = 1  # specify impact function

impact = u_lp.calc_geom_impact(
    exp=exp_line,
    impf_set=impf_set,
    haz=haz,
    res=500,
    to_meters=True,
    disagg_met=u_lp.DisaggMethod.FIX,
    disagg_val=1e5,
    agg_met=u_lp.AggMethod.SUM,
);

u_lp.plot_eai_exp_geom(impact);

Climada Version: [5.0.1.dev0]

System Information (please complete the following information):

  • Operating system and version: [macOS 14.3.1]
  • Python version: [3.9.18]

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions