geopandas.make_grid#

geopandas.make_grid(input_geometry, /, cell_size, grid_type='square', feature_type='polygons', offset=None, intersect=True, flat_topped=False)[source]#

Provide the centers, corners, or polygons of a square or hexagonal grid.

The output covers the area of the input_geometry. By default, only grid elements that spatially overlap with the input_geometry are returned. This filtering can be disabled by setting the intersect parameter to False.

The origin of the grid is at the lower left corner of the bounding box of the input_geometry.

If there are multiple geometries in a GeoSeries/GeoDataFrame, the grid will be created over the total bounds of the GeoSeries/GeoDataFrame. Subsequently, the grid is filtered to only include elements that spatially overlap with any of the individual geometries when intersect is True.

Added in version 1.2.

Parameters:
input_geometry(Multi)Polygon, GeoSeries, GeoDataFrame

Polygon within its boundaries the grid is made.

cell_sizefloat

Side length of the square. For hexagonal cells the distance between opposite edges (edge length is cellsize/sqrt(3)).

grid_typestr, one of “square”, “hexagon”, default “square”

Grid type that is returned. All cell types reflect naive tiling of a plane, not a tiling of the globe (like H3 or S2).

feature_typestr, one of “centers”, “corners”, “polygons”, default “polygons”

Grid feature that is returned. "centers" returns points at the center of each grid cell. "corners" returns points at all unique vertices of the grid cells (i.e., the points where cell edges meet). "polygons" returns the grid cell polygons.

offsettuple, optional

Lower left corner coordinates (x, y) of the grid. By default uses the lower left corner of the bounding box of the input geometry.

intersectbool, default True

If False, the grid is not filtered by the input_geometry and the full grid covering the bounding box is returned.

flat_toppedbool, default False

If True, generate flat topped hexagons. By default (False), the orientation of the hexagonal cells is such that a corner points upwards.

Returns:
GeoSeries

The returned GeoSeries contains the grid-cell centers, corners, or polygons.

Notes

When intersect=True, the grid is filtered using the "intersects" spatial predicate. The filtering behavior depends on feature_type:

  • "polygons": All grid cell polygons that share any area or boundary with the input geometries are returned.

  • "centers": Only center points that fall within or on the boundary of the input geometries are returned.

  • "corners": Only corner points that fall within or on the boundary of the input geometries are returned.

As a result, the set of corners returned when feature_type="corners" may not correspond exactly to the vertices of the polygons returned when feature_type="polygons".

Examples

>>> import geopandas
>>> import geodatasets
>>> world = geopandas.read_file(
...     geodatasets.get_path('naturalearth land'))
>>> madagascar = world.cx[45:50, -25:-15]
>>> sq_grid = geopandas.make_grid(madagascar, cell_size=1)
>>> sq_grid.head(3)
0    POLYGON ((43.25419 -25.60143, 44.25419 -25.601...
1    POLYGON ((43.25419 -24.60143, 44.25419 -24.601...
2    POLYGON ((43.25419 -23.60143, 44.25419 -23.601...
dtype: geometry

By default, grid is aligned with the bounding box of the input geometries. If instead you want to a fixed grid aligning with specific coordinates, use the offset keyword. For example, to have a grid of round degrees:

>>> sq_grid2 = geopandas.make_grid(madagascar, cell_size=1, offset=(30, -30))
>>> sq_grid2.head(3)
0    POLYGON ((43 -25, 44 -25, 44 -24, 43 -24, 43 -...
1    POLYGON ((43 -24, 44 -24, 44 -23, 43 -23, 43 -...
2    POLYGON ((43 -23, 44 -23, 44 -22, 43 -22, 43 -...
dtype: geometry

(Source code, png, hires.png, pdf)

../../../_images/make_grid.png

Specify the feature_type keyword to get the centers or corners of the grid cells, instead of the polygons:

>>> sq_grid_centers = geopandas.make_grid(
...     madagascar, cell_size=1, feature_type="centers"
... )
>>> sq_grid_centers.head(3)
0    POINT (43.75419 -24.10143)
1    POINT (43.75419 -23.10143)
2    POINT (43.75419 -22.10143)
dtype: geometry

Specify the grid_type="hexagon keyword to get hexagons instead of the default squares.

(Source code, png, hires.png, pdf)

../../../_images/make_grid_types.png