xugrid.create_snap_to_grid_dataframe#
- xugrid.create_snap_to_grid_dataframe(lines: GeoDataFrame, grid: DataArray | UgridDataArray, max_snap_distance: float) DataFrame [source]#
Create a dataframe required to snap line geometries to a Ugrid2d topology.
A line is included and snapped to a grid edge when the line separates the centroid of the cell with the centroid of the edge.
- Parameters:
lines (gpd.GeoDataFrame) – Line data. Geometry colum should contain exclusively LineStrings.
grid (xugrid.Ugrid2d) – Grid of cells to snap lines to.
max_snap_distance (float)
- Returns:
result – DataFrame with columns:
line_index: the index of the geodataframe geometry.
edge_index: the index of the
x0: start x-coordinate of edge segment.
y0: start y-coordinate of edge segment.
x1: end x-coordinate of edge segment.
y1: end y-coordinate of edge segment.
length: length of the edge.
- Return type:
pd.DataFrame
Examples
First create data frame:
>>> snapping_df = create_snap_to_grid_dataframe(lines, grid2d, max_snap_distance=0.5)
Use the
line_index
column to assign values fromlines
to this new dataframe:>>> snapping_df["my_variable"] = lines["my_variable"].iloc[snapping_df["line_index"]].to_numpy()
Run some reduction on the variable, to create an aggregated value per grid edge:
>>> aggregated = snapping_df.groupby("edge_index").sum()
Assign the aggregated values to a Ugrid2d topology:
>>> new = xu.full_like(edge_data, np.nan) >>> new.data[aggregated.index] = aggregated["my_variable"]