The above image shows example data points and a regular grid. As you can see, most of the grids are empty. I just wanted a regular grid in which every grid contains at least one data point. Can anyone support me? Thanks a lot!
#Below is the code:
import shapely
import geopandas
from geopandas import GeoDataFrame
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
df = pd.read_csv(r'D:\Academic\grid2.csv')
gdf = geopandas.GeoDataFrame(df,
geometry=geopandas.points_from_xy(df.Latitude, df.Longitude),
crs="+proj=sinu +lon_0=0 +x_0=0 +y_0=0 +a=6371007.181 +b=6371007.181 +units=m +no_defs")
# total area for the grid
xmin, ymin, xmax, ymax= gdf.total_bounds
# how many cells across and down
n_cells=30
cell_size = (xmax-xmin)/n_cells
# projection of the grid
crs = "+proj=sinu +lon_0=0 +x_0=0 +y_0=0 +a=6371007.181 +b=6371007.181 +units=m +no_defs"
# create the cells in a loop
grid_cells = []
for x0 in np.arange(xmin, xmax+cell_size, cell_size ):
for y0 in np.arange(ymin, ymax+cell_size, cell_size):
# bounds
x1 = x0-cell_size
y1 = y0+cell_size
grid_cells.append( shapely.geometry.box(x0, y0, x1, y1) ) cell = geopandas.GeoDataFrame(grid_cells, columns=['geometry'],
crs=crs)
ax = gdf.plot(markersize=.1, figsize=(12, 8), column='LTE_UE_PCI', cmap='jet')
plt.autoscale(False)
cell.plot(ax=ax, facecolor="none", edgecolor='grey')