I am trying to figure out how to generate a RAT for a 1-band raster with rasterio or GDAL given a GeoPandas dataframe.
My GeoPandas dataframe has 4 columns (X
, Y
, PERC_VALUE
, LANDUSE_LABEL
) which I would need to use to build my raster as so:
X
,Y
defines the coordinates of the pixel centroidPERC_VALUE
is the value which has to be stored in the pixelsLANDUSE_LABEL
has information associated to the pixels which I need to store in the RAT.
The Pandas dataframe that I am using to build the GeoPandas dataframe looks like this:
landuse value Y X
FOREST 0.0005 1386050 4559950
CITY 0.0001 1386250 4559650
FOREST 0.0009 1386250 4559750
FOREST 0.0025 1386250 4559850
CITY 0.0059 1386250 4561950
...
I am able to create a raster to store the coverage percentage values at each corresponding X,Y pixel (code is shown at the bottom of the question). What I don't know is how can I associate each pixel to the corresponding LANDUSE_LABEL
information in a RAT?
The idea is that when I open the final result with (e.g.) QGIS (if it can read RATs), I would be able to symbolize the raster per landuse label, meanwhile being able to access the percentage values of the pixels.
Is this possible with rasterio or GDAL and if so how? Do I have to use a specific raster extension for this aim (I am currently using GeoTIFF)?
Code I am using to write the PERC_VALUE
with rasterio as GeoTIFF:
# convert Pandas df to GeoPandas DF (Points)
gdf = GeoDataFrame(
dataframe.drop(['X', 'Y'], axis=1),
crs={'init': 'epsg:3035'},
geometry=[Point(xy) for xy in zip(dataframe.X.astype(int), dataframe.Y.astype(int))])
# write the raster
with rasterio.open(r'output.tiff',
'w+',
driver= 'GTiff',
height= arr.shape[0],
width= arr.shape[1],
count= 1,
dtype= numpy.dtype('float32'),
crs= SR,
transform= transform) as out:
out_arr = out.read(1)
# this is where we create a generator of geom, value pairs to use in rasterizing
shapes = ((geom,value) for geom, value in zip(gdf['geometry'], gdf['value']))
burned = features.rasterize(shapes=shapes, fill=0, out=out_arr, transform=transform)
out.write(burned, 1)
out.close()