Skip to content
Extraits de code Groupes Projets
Valider a2a81a8a rédigé par Dries De Bièvre's avatar Dries De Bièvre
Parcourir les fichiers

change rasterize function and add scripts validation and zonalstats

parent cbbbede1
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
......@@ -2,15 +2,19 @@ import numpy as np
import math, time
import rasterio
from rasterio import features
import os
def rasterize(in_situ_gdf, in_situ_cal_tif, img_temp_path, no_data = -10000):
def rasterize(in_situ_gdf, in_situ_tif, img_temp_path, no_data = -10000, column_name='labelID'):
print(f'Raster template file : {img_temp_path}')
src = rasterio.open(img_temp_path, "r")
if os.path.exists(in_situ_tif):
os.remove(in_situ_tif)
print("Deleted existing .tif file")
# Update metadata
out_meta = src.meta
......@@ -29,13 +33,13 @@ def rasterize(in_situ_gdf, in_situ_cal_tif, img_temp_path, no_data = -10000):
# Burn the features into the raster and write it out
dst = rasterio.open(in_situ_cal_tif, 'w+', **out_meta)
dst = rasterio.open(in_situ_tif, 'w+', **out_meta)
dst_arr = dst.read(1)
# This is where we create a generator of geom, value pairs to use in rasterizing
geom_col = in_situ_gdf.geometry
code_col = in_situ_gdf["labelID"].astype(int)
code_col = in_situ_gdf[column_name].astype(int)
shapes = ((geom,value) for geom, value in zip(geom_col, code_col))
......@@ -44,7 +48,7 @@ def rasterize(in_situ_gdf, in_situ_cal_tif, img_temp_path, no_data = -10000):
out=dst_arr,
transform=dst.transform)
dst.write_band(1, in_situ_arr)
print(f'Rasterize is done : {in_situ_cal_tif}')
print(f'Rasterize is done : {in_situ_tif}')
# Close rasterio objects
src.close()
......@@ -67,7 +71,7 @@ def apply_rf_to_raster(rf, img, no_data = -10000):
class_prediction = rf.predict(img_as_array)
# predictions with no_data in any of the input features are replaced by the no_data value
class_prediction[np.any(img_as_array == no_data, axis=1)] = no_data
# class_prediction[np.any(img_as_array == no_data, axis=1)] = no_data
# Reshape our classification map
class_prediction = class_prediction.reshape(img[:, :, 0].shape)
......
Impossible d'afficher diff de source : il est trop volumineux. Options pour résoudre ce problème : voir le blob.
Impossible d'afficher diff de source : il est trop volumineux. Options pour résoudre ce problème : voir le blob.
Impossible d'afficher diff de source : il est trop volumineux. Options pour résoudre ce problème : voir le blob.
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter