I have written a little script to raster to GeoPandas dataframe:
with rasterio.open("INPUT FILE", 'r') as raster:
df = pd.DataFrame()
puntos_calados_combinados=[]
puntos=[]
xmin,ymax=raster.transform* (0, 0)
arr = raster.read(1) # read all raster values
for row in range(0,raster.width ):
for col in range(0,raster.height):
x=xmin+ (0.5*raster.transform[0] + row*raster.transform[0])
y=ymax+ (0.5*raster.transform[4] + col*raster.transform[4])
valor= arr[col,row]
if arr[col,row]>0:
x_y=[x,y]
puntos_calados_combinados.append(x_y)
But it's extremely slow, any idea to improve it?
Best Answer
Rather than looping through each pixel, you could try creating numpy arrays of X & Y coordinates then dumping those (plus the array of the raster values) into a DataFrame, then converting to a GeoDataFrame.
Below is a runnable example (Note, you do not want to do this with large rasters, you can run out of memory...):
Output: