Your script was generally correct, but you didn't change the name of the attribute field you wanted to rasterize.
In the example you posted, you set ['ATTRIBUTE=ID']
as field, but it doesn't exists in your shapefile. You only have "Habitats"
and "surface"
as fields, so you need properly edit the code.
Therefore, you needed to edit the folders for both shapefile and rasterized layers, and the crs.
I slightly edited the code in this way:
import gdal
from osgeo import osr
from osgeo import ogr
def layer(shapefile):
# 1) opening the shapefile
source_ds = ogr.Open(shapefile)
source_layer = source_ds.GetLayer()
# 2) Creating the destination raster data source
pixelWidth = pixelHeight = 1 # depending how fine you want your raster
x_min, x_max, y_min, y_max = source_layer.GetExtent()
cols = int((x_max - x_min) / pixelHeight)
rows = int((y_max - y_min) / pixelWidth)
target_ds = gdal.GetDriverByName('GTiff').Create(raster_path, cols, rows, 1, gdal.GDT_Byte)
target_ds.SetGeoTransform((x_min, pixelWidth, 0, y_min, 0, pixelHeight))
band = target_ds.GetRasterBand(1)
NoData_value = 255
band.SetNoDataValue(NoData_value)
band.FlushCache()
# 4) Instead of setting a general burn_value, use optionsand set it to the attribute that contains the relevant unique value ["ATTRIBUTE=ID"]
gdal.RasterizeLayer(target_ds, [1], source_layer, options = ['ATTRIBUTE=surface'])
# 5) Adding a spatial reference
target_dsSRS = osr.SpatialReference()
target_dsSRS.ImportFromEPSG(2975)
target_ds.SetProjection(target_dsSRS.ExportToWkt())
return gdal.Open(raster_path).ReadAsArray()
raster_path = 'C:/Users/path_to_the_rasterized_output/temp.tif'
shapefile = 'C:/Users/path_to_the_shapefile/shapefile_maido_tipe.shp'
print layer(shapefile)
and I think it is working by now because I obtain this rasterized layer (which overlaps the shapefile):
and this return from the print layer(shapefile)
line (you see only '255' value because you set it as nodata value):
[[255 255 255 ..., 255 255 255]
[255 255 255 ..., 255 255 255]
[255 255 255 ..., 255 255 255]
...,
[255 255 255 ..., 255 255 255]
[255 255 255 ..., 255 255 255]
[255 255 255 ..., 255 255 255]]
Best Answer
Once you have georeferenced the raster, it is still only a temporary layer. You must export each georeferenced raster in your project with a world file.
Select them one at a time in the layers panel and click this button:
Choose a save location:
Repeat this for every georeferenced raster in your project.
Find your exported images and drag these into your project as new layers.
Then you should be able to send the project as you did before.