Creating polygon grid using Geopandas
Previous answer gives correct result, but I allowed myself to improve the code, to avoid many unnecessary lines, as well as iterating on indexes instead of values on the list.
import geopandas as gpd
from shapely.geometry import Polygon
import numpy as np
points = gpd.read_file('points.shp')
xmin,ymin,xmax,ymax = points.total_bounds
length = 1000
wide = 1200
cols = list(range(int(np.floor(xmin)), int(np.ceil(xmax)), wide))
rows = list(range(int(np.floor(ymin)), int(np.ceil(ymax)), length))
rows.reverse()
polygons = []
for x in cols:
for y in rows:
polygons.append( Polygon([(x,y), (x+wide, y), (x+wide, y-length), (x, y-length)]) )
grid = gpd.GeoDataFrame({'geometry':polygons})
grid.to_file("grid.shp")
The main idea might be the same, but we are creating now much less useless variables and whole code is clearer to understand
There are many solutions.
One of them
import geopandas as gpd
from shapely.geometry import Polygon
import numpy as np
points = gpd.read_file('points.shp')
xmin,ymin,xmax,ymax = points.total_bounds
width = 2000
height = 1000
rows = int(np.ceil((ymax-ymin) / height))
cols = int(np.ceil((xmax-xmin) / width))
XleftOrigin = xmin
XrightOrigin = xmin + width
YtopOrigin = ymax
YbottomOrigin = ymax- height
polygons = []
for i in range(cols):
Ytop = YtopOrigin
Ybottom =YbottomOrigin
for j in range(rows):
polygons.append(Polygon([(XleftOrigin, Ytop), (XrightOrigin, Ytop), (XrightOrigin, Ybottom), (XleftOrigin, Ybottom)]))
Ytop = Ytop - height
Ybottom = Ybottom - height
XleftOrigin = XleftOrigin + width
XrightOrigin = XrightOrigin + width
grid = gpd.GeoDataFrame({'geometry':polygons})
grid.to_file("grid.shp")
You can also truncate the grid (convex hull):
But one of the most interesting is to use the module gpd_lite_toolboox)