How do I create a point_grid.shp over the extent of a polygon_extent.shp?

Spacing between the points must be in meters.

This is a pseudo-code adaptation of code found to create a polygon grid (I just want the points though):

dir = Path("").absolute() # working directory
bounds = gpd.read_file(dir / 'boundaries.shp')

def shp_to_point_grid(bounds, spacing_x, spacing_y):
    polygons = list()

    # code

    return pointgrid_shp

shape_file_here = shp_to_point_grid(bounds, 2000, 2000)

grid = gpd.GeoDataFrame(shape_file_here) # not sure how to format this
grid.to_file(dir / "pointgrid.shp")

1 Answer 1


Find the total bounds (xmin, ymin, xmax, ymax), create two lists of x and y coordinates within them. List all combinations of the x and y coordinates, create points.

import geopandas as gpd
import numpy as np

df = gpd.read_file(r'E:\Data\testdata\polygon.shp') #The polygon shapefile
x_spacing = 3000 #The point spacing you want
y_spacing = 5000

xmin, ymin, xmax, ymax = df.total_bounds #Find the bounds of all polygons in the df
xcoords = [c for c in np.arange(xmin, xmax, x_spacing)] #Create x coordinates
ycoords = [c for c in np.arange(ymin, ymax, y_spacing)] #And y

coordinate_pairs = np.array(np.meshgrid(xcoords, ycoords)).T.reshape(-1, 2) #Create all combinations of xy coordinates
geometries = gpd.points_from_xy(coordinate_pairs[:,0], coordinate_pairs[:,1]) #Create a list of shapely points

pointdf = gpd.GeoDataFrame(geometry=geometries, crs=df.crs) #Create the point df

enter image description here


Not the answer you're looking for? Browse other questions tagged or ask your own question.