A basic introduction to Geospatial Grids
import geopandas as gpd
import pandas as pd
from geowrangler import grids
region3_gdf = gpd.read_file("../data/region3_admin.geojson")
This geopandas dataframe has the size:
display(region3_gdf)
Show the original plot
import matplotlib.pyplot as plt
ax = region3_gdf.plot(ax=plt.axes())
region3_gdf.crs # CRS info
aoi_total_bounds = region3_gdf.total_bounds
aoi_total_bounds
Square Grid Generator
Creating Grids
Create a grid generator with a size of 5,000
m.
The units of the grid size are dependent on the projection parameter
of the grid generator. In this case, the default is EPSG:3857
.
grids.SquareGridGenerator?
grid_generator5k = grids.SquareGridGenerator(5_000) # 5 km x 5 km square cells
Generate square grids
Notice the time taken to grid the multi polygon at 5K resolution
%%time
# slow
grid_gdf5k = grid_generator5k.generate_grid(region3_gdf)
grid_gdf5k.plot()
Show gridded version of sample geojson file at 5K resolution
ax = region3_gdf.plot(facecolor="none", edgecolor="blue")
ax = grid_gdf5k.plot(ax=ax, facecolor="none", edgecolor="green")
In addition to the grid cells, there are 2 extra columns x
and y
when combined are unique per grid. It can also tell us which grids are adjacent to each other.
grid_gdf5k.head()
ax = region3_gdf.plot(facecolor="none", edgecolor="blue")
ax = grid_gdf5k[grid_gdf5k["x"] == 10].plot(ax=ax, facecolor="none", edgecolor="green")
grid_generator15k = grids.SquareGridGenerator(15_000) # 15 km x 15 km grids
Generate square grids
Notice the time taken to grid the multi polygon at 15K resolution (compared to 5K resolution)
%%time
grid_gdf15k = grid_generator15k.generate_grid(region3_gdf)
ax = region3_gdf.plot(facecolor="none", edgecolor="blue")
grid_gdf15k.plot(ax=ax, facecolor="none", edgecolor="green")
Show gridded version of sample geojson file at 15K resolution
Let's load some grids that are from each other
cell1 = grid_gdf15k.head(1)
cell2 = grid_gdf15k.tail(1)
pd.concat([cell1, cell2])
ax = region3_gdf.plot(facecolor="none", edgecolor="red")
ax = grid_gdf15k.plot(ax=ax, facecolor="none", edgecolor="green")
ax = pd.concat([cell1, cell2]).plot(ax=ax)
grid_generator1k = grids.SquareGridGenerator(1_000, boundary=aoi_total_bounds)
%%time
gridcell1 = grid_generator1k.generate_grid(cell1)
len(gridcell1)
gridcell1.head()
ax = gridcell1.plot(facecolor="none", edgecolor="green")
%%time
gridcell2 = grid_generator1k.generate_grid(cell2)
len(gridcell2)
gridcell2.head()
ax = gridcell2.plot(facecolor="none", edgecolor="red")
ax = region3_gdf.plot(facecolor="none", edgecolor="blue")
ax = gridcell1.plot(ax=ax, color="green")
ax = gridcell2.plot(ax=ax, color="red")
sparse_aois = grid_gdf15k.iloc[
0:1000:3,
] # Get areas that far from each other
ax = region3_gdf.plot(facecolor="none", edgecolor="blue")
ax = sparse_aois.plot(ax=ax, facecolor="none", edgecolor="green")
sparse_grid = grid_generator1k.generate_grid(sparse_aois)
ax = region3_gdf.plot(facecolor="none", edgecolor="blue")
ax = sparse_grid.plot(ax=ax, facecolor="none", edgecolor="green")
It is a bit hard to see the grids so, we get a subset to verify.
ax = sparse_grid.head(1000).plot(facecolor="none", edgecolor="green")
H3 Grid Generator
Generating grids
Let us generate grids of resolution 5. To learn more about the different resolution, visit: https://h3geo.org/docs/core-library/restable/
h3_generator = grids.H3GridGenerator(resolution=5)
%%time
h3_5_gdf = h3_generator.generate_grid(region3_gdf)
ax = region3_gdf.plot(aspect="equal")
ax = h3_5_gdf.plot(ax=ax, facecolor="none", edgecolor="blue", aspect="equal")
h3_generator_no_geom = grids.H3GridGenerator(resolution=5, return_geometry=False)
%%time
h3_region3_no_geom = h3_generator_no_geom.generate_grid(region3_gdf)
len(h3_region3_no_geom)
h3_region3_no_geom.head()
Bing Tile Grid Generator
Generating grids
Let us generate grids of zoom_level 12. To learn more about the different resolution, visit: https://docs.microsoft.com/en-us/bingmaps/articles/bing-maps-tile-system
bing_tile_grid_generator = grids.BingTileGridGenerator(12)
%%time
# slow
bing_tile_gdf = bing_tile_grid_generator.generate_grid(region3_gdf)
ax = region3_gdf.plot(facecolor="none", edgecolor="blue")
ax = bing_tile_gdf.plot(ax=ax, facecolor="none", edgecolor="green")
bing_tile_grid_generator_no_geom = grids.BingTileGridGenerator(
12, return_geometry=False
)
%%time
# slow
bing_region3_keys = bing_tile_grid_generator_no_geom.generate_grid(region3_gdf)
bing_region3_keys.head()