I'm using this GDAL based rasterize function in python in order to create raster from shapefile based on numerical class values.
so I use those clases for the rasterizing as following (the code is from here: https://pcjericks.github.io/py-gdalogr-cookbook/raster_layers.html):
#Rasterize function def rasterise_me(raster, vector, attribute, fname_out="", format="MEM"): """Rasterises a vector dataset by attribute to match a given raster dataset. This functions allows for the raster and vector to have different projections, and will ensure that the output is consistent with the input raster. By default, it returns a handle to an open GDAL dataset that you can e.g. `ReadAsArray`. If you want to generate a GTiff on disk, set format to `GTiff` and `fname_out` to a sensible filename. Parameters ---------- raster: str The raster filaname used as input. It will not be overwritten. vector: str The vector filename attribute: str The attribute that you want to rasterize. Ideally, this is numeric. fname_out: str, optional The output filename. format: str, optional The output file format, such as GTiff, or whatever else GDAL understands """ # Open input raster file. Need to do this to figure out # extent, projection & resolution. gdal.UseExceptions() g = gdal.Open(raster) geoT = g.GetGeoTransform() nx, ny = g.RasterXSize, g.RasterYSize srs = g.GetProjection() min_x = min(geoT, geoT+nx*geoT) max_x = max(geoT, geoT+nx*geoT) min_y = min(geoT, geoT + geoT[-1]*ny) max_y = max(geoT, geoT + geoT[-1]*ny) # Reproject vector to match raster file vector_tmp = gdal.VectorTranslate("", vector, format="Memory", dstSRS=srs) # Do the magic ds_dst= gdal.Rasterize(fname_out, vector_tmp, attribute=attribute, outputSRS=srs, xRes=geoT, yRes=geoT[-1], outputBounds=[min_x, min_y, max_x, max_y], format=format, outputType=gdal.GDT_Int32) return ds_dst rasterize=rasterise_me(r"29052019.tif", r"shape.shp", "shape ID",fname_out="raster.tif")
after that I concat this data about the pixels to big table but then when I check the classes in the big table I can see that they are wrong- they are all the same negative number:
I don't know where do I lose the numbers values or why. I have tried to check the dtype of this column but it's int64 before and after the rasterize process.
My end goal: to preserve the original class attributes in the final results