Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function to clip exposre to hazard #415

Open
Santonia27 opened this issue Nov 18, 2024 · 0 comments
Open

Add function to clip exposre to hazard #415

Santonia27 opened this issue Nov 18, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@Santonia27
Copy link
Collaborator

Kind of request

None

Enhancement Description

write optional function to clip exposure to hazard boundaries

Task list

def clip_exposure(self, da):
"""
Clip the exposure geometries to the bounding box of the given data array.

    This function modifies the exposure geometries by clipping them to the 
    bounding box of the provided data array. It differentiates between roads 
    and buildings, updating their respective geometries in the exposure object.

    Parameters
    ----------
    da : xarray.DataArray
        The data array whose bounding box is used to clip the exposure geometries.
    """
    gdf = self.exposure.get_full_gdf(self.exposure.exposure_db)
    
    da_bounding_box =da.rio.bounds() 
    bbox_coords = [
        (da_bounding_box[0], da_bounding_box[1]), 
        (da_bounding_box[2], da_bounding_box[1]),  
        (da_bounding_box[2], da_bounding_box[3]),  
        (da_bounding_box[0], da_bounding_box[3])  
    ]
    
    # Create the gdf from hazard polygons
    polygon = Polygon(bbox_coords)
    gdf_polygon  = gpd.GeoDataFrame(geometry=[polygon])
    crs = gdf.crs
    gdf_polygon.crs = crs

    # Clip the exposure geometries
    gdf = gpd.clip(gdf, gdf_polygon)
    if gdf["Primary Object Type"].str.contains("road").any():
        gdf_roads = gdf[gdf["Primary Object Type"].str.contains("road")]
        gdf_buildings= gdf[~gdf.isin(gdf_roads)]
        idx_buildings = self.exposure.geom_names.index("buildings")
        idx_roads = self.exposure.geom_names.index("roads")
        self.exposure.exposure_geoms[idx_buildings] = gdf_buildings[["Object ID", "geometry"]]
        self.exposure.exposure_geoms[idx_roads] = gdf_roads[["Object ID", "geometry"]]
    else:
        self.exposure.exposure_geoms[0] = gdf[["Object ID", "geometry"]]

    del gdf["geometry"]
    self.exposure.exposure_db = gdf

Use case

No response

Additional Context

No response

@Santonia27 Santonia27 added the enhancement New feature or request label Nov 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant