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

grid_to_raster should infer the number of cells from the provided meshgrid #262

Open
atmorling opened this issue Sep 3, 2024 · 1 comment · May be fixed by #263
Open

grid_to_raster should infer the number of cells from the provided meshgrid #262

atmorling opened this issue Sep 3, 2024 · 1 comment · May be fixed by #263
Labels
bug Something isn't working

Comments

@atmorling
Copy link
Contributor

Currently grid_to_raster takes in x and y len params which are used to calculate the raster grid, this can cause unexpected behaviour when combined with create_meshgrid as the calculated raster cells can be different to the meshgrid cells.

def grid_to_raster(grid=None, val_column="", out_dir="", raster_name=None, xlen=5000, ylen=5000):
"""
Save a GeoDataFrame grid to a raster.
"""
bounds = grid["geometry"].total_bounds
nrows = int((bounds[3] - bounds[1]) / ylen)
ncols = int((bounds[2] - bounds[0]) / xlen)
if val_column:
vals = grid[val_column].to_numpy()
else:
vals = np.zeros(len(grid))
vals = np.flip(vals.reshape(nrows, ncols, order="F"), axis=0)

@atmorling atmorling added the bug Something isn't working label Sep 3, 2024
@atmorling
Copy link
Contributor Author

If we assume the order of the geometry column is always per the output of create_meshgrid ie:

(xmin, ymin)
(xmin, ymin+1)
...
(xmin, ymax)
(xmin+1, ymin)
...
(xmax, ymax)

We could iterate though the geometry and check for the first non-intersection and know that will be the end of our axis, or max_y, then max_x = max_y / len(geometry)

@atmorling atmorling linked a pull request Sep 4, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant