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

Helper to save some attribute/calculation from split geometries back to raster #60

Open
tomalrussell opened this issue May 10, 2024 · 0 comments

Comments

@tomalrussell
Copy link
Member

Given a GeoDataFrame of split geometries, and some attribute (e.g. length_km) that we want to save, provide a helper function to aggregate those split vector elements back to a raster data structure - along the lines of:

raster_height = 100
raster_width = 100

length_raster = numpy.zeros((raster_height, raster_width))

splits_df = # data frame of split geometries, with length_km calculated
length_per_cell = splits_df[['cell_index', 'length_km']].groupby('cell_index').sum()

for row in length_per_cell.reset_index().itertuples():
    col, row = row.cell_index
    length_raster[row, col] = row.length_km

with rasterio.open(
        f'outputs/length.tif',
        'w',
        driver='GTiff',
        height=length_raster.shape[0],
        width=length_raster.shape[1],
        count=1,
        dtype=length_raster.dtype,
        crs='+proj=latlong',
        transform=raster_transform,
        compress='lzw'
    ) as dataset:
    dataset.write(length_raster, 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant