Skip to content
Open
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/psf/black
rev: 25.1.0 # Replace by any tag/version: https://github.com/psf/black/tags
rev: 26.1.0 # Replace by any tag/version: https://github.com/psf/black/tags
hooks:
- id: black
language_version: python3
Expand Down
4 changes: 2 additions & 2 deletions gridkit-py/examples/cell_centric_operations/downslope_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
def downslope_path(cell_id):
visited_cells.append(cell_id)
# add new neigbours and their values to their respective lists
neighbours = dem.grid.neighbours(cell_id, connect_corners=True)
neighbours = dem.get_grid().neighbours(cell_id, connect_corners=True)
for cell, value in zip(neighbours, dem.value(neighbours)):
cell = tuple(cell.index)
if not cell in visited_cells and not cell in all_neighbours:
Expand All @@ -80,7 +80,7 @@ def downslope_path(cell_id):

# %%
# Determine the starting cell and call the function
start_cell_id = dem.grid.cell_at_point((29330, 167848))
start_cell_id = dem.get_grid().cell_at_point((29330, 167848))
downslope_path(tuple(start_cell_id.index))


Expand Down
14 changes: 7 additions & 7 deletions gridkit-py/examples/patterns/game_of_life.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
Cellular automata are a small set of rules relating to cells on a grid that can create complex patterns when applied repeatedly.
There is a near infinite veriety of rules, but far and away the most well known set of rules is that of the so called Conway's game of life.
The setup is as follows:
- The board is a 2d rectangular grid
- The 8 neighbouring cells are considered, so this includes the diagonal neighbours
- A cell has two possbile states: alive or dead
- The board is a 2d rectangular grid
- The 8 neighbouring cells are considered, so this includes the diagonal neighbours
- A cell has two possbile states: alive or dead

The rules as taken from https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life:

1. Any live cell with fewer than two live neighbours dies, as if by underpopulation.
2. Any live cell with two or three live neighbours lives on to the next generation.
3. Any live cell with more than three live neighbours dies, as if by overpopulation.
4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
1. Any live cell with fewer than two live neighbours dies, as if by underpopulation.
2. Any live cell with two or three live neighbours lives on to the next generation.
3. Any live cell with more than three live neighbours dies, as if by overpopulation.
4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.

Initial conditions
------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@

utm_epsg = 32629
transformer = Transformer.from_crs(
data_tile_wgs84.grid.crs, CRS.from_user_input(utm_epsg), always_xy=True
data_tile_wgs84.get_grid().crs, CRS.from_user_input(utm_epsg), always_xy=True
) # UTM zone 28N

wgs84_centroids_utm = transformer.transform(
Expand Down
2 changes: 1 addition & 1 deletion gridkit-py/examples/raster_operations/ndvi.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@
fig.colorbar(im_ndvi, ax=ax, fraction=0.022, pad=0.01)
ax.set_xlabel("lon")
ax.set_ylabel("lat")
ax.set_title(f"NDVI of scene in the alps \n EPSG:{ndvi.grid.crs.to_epsg()}")
ax.set_title(f"NDVI of scene in the alps \n EPSG:{ndvi.get_grid().crs.to_epsg()}")

plt.show()
14 changes: 9 additions & 5 deletions gridkit-py/examples/raster_operations/resampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
dem = raster_to_data_tile(
"../../tests/data/alps_dem.tiff", bounds=(28300, 167300, 28700, 167700)
)
print(f"Original resolution in (dx, dy): ({dem.grid.dx}, {dem.grid.dy})")
print(f"Original resolution in (dx, dy): ({dem.get_grid().dx}, {dem.get_grid().dy})")


# %%
Expand All @@ -49,8 +49,10 @@
# Let's define a rectangular grid with a cell size of 10x10 degrees.
# I am calling it rectdem for later on I will define hexagonal ones as well.
# To make sure it worked we can print out the cellsize after resampling
rectdem = dem.resample(RectGrid(dx=10, dy=10, crs=dem.grid.crs), method="linear")
print(f"Downsampled resolution in (dx, dy): ({rectdem.grid.dx}, {rectdem.grid.dy})")
rectdem = dem.resample(RectGrid(dx=10, dy=10, crs=dem.get_grid().crs), method="linear")
print(
f"Downsampled resolution in (dx, dy): ({rectdem.get_grid().dx}, {rectdem.get_grid().dy})"
)

# %%
#
Expand All @@ -71,10 +73,12 @@
# for a fair visual comparison.
#
hexdem_flat = dem.resample(
HexGrid(area=rectdem.grid.area, shape="flat", crs=dem.grid.crs), method="linear"
HexGrid(area=rectdem.get_grid().area, shape="flat", crs=dem.get_grid().crs),
method="linear",
)
hexdem_pointy = dem.resample(
HexGrid(area=rectdem.grid.area, shape="pointy", crs=dem.grid.crs), method="linear"
HexGrid(area=rectdem.get_grid().area, shape="pointy", crs=dem.get_grid().crs),
method="linear",
)


Expand Down
14 changes: 10 additions & 4 deletions gridkit-py/examples/vector_synergy/aggregate_dask.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,16 @@
grid = HexGrid(size=1, shape="flat")


def index_1d_at_point(df):
def index_1d_at_point(df, **grid_params):
"""Find the cell for each point in the dataframe and return the 1D index.
This operation is meant to map to each DataFrame partition
"""
# Note: Define 'grid' here based on primitives so Dask does not have to worry about serializing the Grid object.
grid = HexGrid(**grid_params)
return grid.cell_at_point(df[["pnt_x", "pnt_y"]]).index_1d


df["cell_id"] = df.map_partitions(index_1d_at_point)
df["cell_id"] = df.map_partitions(index_1d_at_point, **grid.definition)
df["nr_points"] = 1
print(df)

Expand All @@ -121,14 +123,18 @@ def index_1d_at_point(df):
# Also, for the sake of the example I will plot the data with matplotlib.


def shapely_geom_from_index_1d(id_1d):
def shapely_geom_from_index_1d(id_1d, **grid_params):
"""Generate the Shapely geometry for each 1D index.
This operation is meant to map to each DataFrame partition
"""
# Note: Define 'grid' here based on primitives so Dask does not have to worry about serializing the Grid object.
grid = HexGrid(**grid_params)
return numpy.array(grid.to_shapely(GridIndex.from_index_1d(id_1d)).geoms)


polygons = occurrences.index.map_partitions(shapely_geom_from_index_1d)
polygons = occurrences.index.map_partitions(
shapely_geom_from_index_1d, **grid.definition
)
geoms, points_per_cell = dask.compute(polygons, occurrences.nr_points)

# %%
Expand Down
8 changes: 8 additions & 0 deletions gridkit-py/gridkit/base_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -751,3 +751,11 @@ def update(
**kwargs,
):
pass

def __eq__(self, other):
if type(self) == type(other):
return self._grid.equals(other._grid)
return False

def __ne__(self, other):
return not self == other
4 changes: 2 additions & 2 deletions gridkit-py/gridkit/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def read_geotiff(*args, bands=1, **kwargs):

def _write_data_tile_to_raster(data_tile, path):
"""Intended to be called only through :func:`.write_raster`"""
if data_tile.grid.rotation:
if data_tile.get_grid().rotation:
raise ValueError(
"Cannot write a data tile as raster if the grid has a rotaion."
)
Expand Down Expand Up @@ -122,7 +122,7 @@ def _write_data_tile_to_raster(data_tile, path):
width=data_tile.nx,
count=1, # nr bands
dtype=data_tile.dtype,
crs=data_tile.grid.crs,
crs=data_tile.get_grid().crs,
nodata=data_tile.nodata_value,
transform=transform,
) as dst:
Expand Down
Loading