wolfhece 2.1.27__py3-none-any.whl → 2.1.28__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -44,7 +44,8 @@ def parallel_gpd_clip(layer:list[str],
44
44
  def parallel_v2r(manager:Accept_Manager,
45
45
  attribute:str,
46
46
  pixel:float,
47
- number_procs:int = 1):
47
+ number_procs:int = 1,
48
+ convert_to_sparse:bool = False):
48
49
  """
49
50
  Convert the vector layers to raster.
50
51
 
@@ -63,14 +64,17 @@ def parallel_v2r(manager:Accept_Manager,
63
64
  layers = manager.get_layers_in_codevulne()
64
65
 
65
66
  if number_procs == 1:
67
+ result_list=[]
66
68
  for curlayer in layers:
67
- vector_to_raster(curlayer, manager, attribute, pixel)
69
+ result_list.append(vector_to_raster(curlayer, manager, attribute, pixel,convert_to_sparse))
70
+
68
71
  else:
69
72
  pool = multiprocessing.Pool(processes=number_procs)
70
73
  prod_x=partial(vector_to_raster,
71
74
  manager=manager,
72
75
  attribute=attribute,
73
- pixel_size=pixel)
76
+ pixel_size=pixel,
77
+ convert_to_sparse=convert_to_sparse)
74
78
 
75
79
  result_list = pool.map(prod_x, layers)
76
80
 
@@ -1,5 +1,7 @@
1
1
  from .Parallels import parallel_gpd_clip, parallel_v2r, parallel_datamod
2
- from .func import data_modification, compute_vulnerability, compute_vulnerability4scenario, match_vulnerability2sim, compute_acceptability, shp_to_raster, Accept_Manager, cleaning_directory, EXTENT, Vulnerability_csv
2
+ from .func import data_modification, compute_vulnerability, compute_vulnerability4scenario
3
+ from .func import match_vulnerability2sim, compute_acceptability, shp_to_raster
4
+ from .func import Accept_Manager, cleaning_directory, EXTENT, Vulnerability_csv, compute_code
3
5
 
4
6
  import pandas as pd
5
7
  import os
@@ -31,6 +33,8 @@ class steps_vulnerability(Enum):
31
33
  Enum for the steps in the vulnerability computation
32
34
  """
33
35
  CREATE_RASTERS = 1
36
+ CREATE_RASTERS_VULN = 10
37
+ CREATE_RASTERS_CODE = 11
34
38
  APPLY_MODIFS = 2
35
39
  MATCH_SIMUL = 3
36
40
 
@@ -42,7 +46,8 @@ def Base_data_creation(main_dir:str = 'Data',
42
46
  CE_IGN_top10v:str = 'CE_IGN_TOP10V/CE_IGN_TOP10V.shp',
43
47
  resolution:float = 1.,
44
48
  number_procs:int = 8,
45
- steps:list[int] | list[steps_base_data_creation] = [1,2,3,4,5,6,7]):
49
+ steps:list[int] | list[steps_base_data_creation] = [1,2,3,4,5,6,7],
50
+ Vulnerability_csv:str = 'Vulnerability.csv'):
46
51
  """
47
52
  Create the databse.
48
53
 
@@ -73,7 +78,8 @@ def Base_data_creation(main_dir:str = 'Data',
73
78
  Original_gdb=Original_gdb,
74
79
  CaPa_Walloon=CaPa_Walloon,
75
80
  PICC_Walloon=PICC_Walloon,
76
- CE_IGN_top10v=CE_IGN_top10v)
81
+ CE_IGN_top10v=CE_IGN_top10v,
82
+ Vulnerability_csv=Vulnerability_csv)
77
83
 
78
84
  if not manager.check_before_database_creation():
79
85
  logging.error("The necessary files are missing - Verify logs for more information")
@@ -194,7 +200,11 @@ def Base_data_creation(main_dir:str = 'Data',
194
200
  cleaning_directory(manager.TMP_RASTERS_CODE)
195
201
  cleaning_directory(manager.TMP_RASTERS_VULNE)
196
202
 
197
- Database_to_raster(main_dir, Study_area, resolution, number_procs=number_procs)
203
+ Database_to_raster(main_dir,
204
+ Study_area,
205
+ resolution,
206
+ number_procs=number_procs,
207
+ Vulnerability_csv=Vulnerability_csv)
198
208
 
199
209
  done.append(steps_base_data_creation.DATABASE_TO_RASTER)
200
210
 
@@ -205,7 +215,8 @@ def Base_data_creation(main_dir:str = 'Data',
205
215
  def Database_to_raster(main_dir:str = 'Data',
206
216
  Study_area:str = 'Bassin_Vesdre.shp',
207
217
  resolution:float = 1.,
208
- number_procs:int = 16):
218
+ number_procs:int = 16,
219
+ Vulnerability_csv:str = 'Vulnerability.csv'):
209
220
  """
210
221
  Convert the vector database to raster database based on their vulnerability values
211
222
 
@@ -224,7 +235,7 @@ def Database_to_raster(main_dir:str = 'Data',
224
235
  The parallel processing is safe as each layer is processed independently.
225
236
  """
226
237
 
227
- manager = Accept_Manager(main_dir, Study_area)
238
+ manager = Accept_Manager(main_dir, Study_area, Vulnerability_csv=Vulnerability_csv)
228
239
 
229
240
  resolution = float(resolution)
230
241
 
@@ -238,7 +249,7 @@ def Database_to_raster(main_dir:str = 'Data',
238
249
 
239
250
  attributes = ["Vulne", "Code"]
240
251
  for cur_attrib in attributes:
241
- parallel_v2r(manager, cur_attrib, resolution, number_procs)
252
+ parallel_v2r(manager, cur_attrib, resolution, number_procs, convert_to_sparse=True)
242
253
 
243
254
  manager.restore_dir()
244
255
 
@@ -246,7 +257,9 @@ def Vulnerability(main_dir:str = 'Data',
246
257
  scenario:str = 'Scenario1',
247
258
  Study_area:str = 'Bassin_Vesdre.shp',
248
259
  resolution:float = 1.,
249
- steps:list[int] | list[steps_vulnerability] = [1,2,3]):
260
+ steps:list[int] | list[steps_vulnerability] = [1,10,11,2,3],
261
+ Vulnerability_csv:str = 'Vulnerability.csv',
262
+ Intermediate_csv:str = 'Intermediate.csv'):
250
263
  """
251
264
  Compute the vulnerability for the study area and the scenario, if needed.
252
265
 
@@ -273,7 +286,11 @@ def Vulnerability(main_dir:str = 'Data',
273
286
 
274
287
  """
275
288
 
276
- manager = Accept_Manager(main_dir, Study_area, scenario=scenario)
289
+ manager = Accept_Manager(main_dir,
290
+ Study_area,
291
+ scenario=scenario,
292
+ Vulnerability_csv=Vulnerability_csv,
293
+ Intermediate_csv=Intermediate_csv)
277
294
 
278
295
  if not manager.check_before_vulnerability():
279
296
  logging.error("The necessary files are missing - Verify logs for more information")
@@ -292,7 +309,13 @@ def Vulnerability(main_dir:str = 'Data',
292
309
 
293
310
  cleaning_directory(manager.TMP_SCEN_DIR)
294
311
 
295
- compute_vulnerability(manager)
312
+ if 10 in steps or steps_vulnerability.CREATE_RASTERS_VULN in steps:
313
+ compute_vulnerability(manager)
314
+ done.append(steps_vulnerability.CREATE_RASTERS_VULN)
315
+
316
+ if 11 in steps or steps_vulnerability.CREATE_RASTERS_CODE in steps:
317
+ compute_code(manager)
318
+ done.append(steps_vulnerability.CREATE_RASTERS_CODE)
296
319
 
297
320
  done.append(steps_vulnerability.CREATE_RASTERS)
298
321
 
@@ -343,10 +366,15 @@ def Vulnerability(main_dir:str = 'Data',
343
366
 
344
367
  def Acceptability(main_dir:str = 'Vesdre',
345
368
  scenario:str = 'Scenario1',
346
- Study_area:str = 'Bassin_Vesdre.shp'):
369
+ Study_area:str = 'Bassin_Vesdre.shp',
370
+ coeff_auto:bool = True,
371
+ Ponderation_csv:str = 'Ponderation.csv'):
347
372
  """ Compute acceptability for the scenario """
348
373
 
349
- manager = Accept_Manager(main_dir, Study_area, scenario=scenario)
374
+ manager = Accept_Manager(main_dir,
375
+ Study_area,
376
+ scenario=scenario,
377
+ Ponderation_csv=Ponderation_csv)
350
378
 
351
379
  manager.change_dir()
352
380
 
@@ -446,10 +474,19 @@ def Acceptability(main_dir:str = 'Vesdre',
446
474
  # pond.set_index("Interval", inplace=True)
447
475
 
448
476
  # Get ponderations for the return periods
449
- pond = manager.get_ponderations()
477
+ if coeff_auto:
478
+ pond = manager.get_ponderations()
479
+ assert pond["Ponderation"].sum() > 0.999999 and pond["Ponderation"].sum()<1.0000001, "The sum of the ponderations is not equal to 1"
480
+
481
+ elif manager.is_valid_ponderation_csv:
482
+ pond = pd.read_csv(manager.PONDERATION_CSV)
483
+ pond.set_index("Interval", inplace=True)
484
+ else:
485
+ logging.error("The ponderation file is missing")
486
+ logging.info("Please provide the ponderation file or set 'coeff_auto' to True")
487
+ return
450
488
 
451
489
  assert len(pond) == len(return_periods), "The number of ponderations is not equal to the number of return periods"
452
- assert pond["Ponderation"].sum() > 0.999999 and pond["Ponderation"].sum()<1.0000001, "The sum of the ponderations is not equal to 1"
453
490
 
454
491
  # Initialize the combined acceptability matrix -- Ponderate mean of the local acceptability
455
492
  comb = np.zeros(q_dict["vm{}".format(return_periods[-1])].shape)
@@ -10,7 +10,6 @@ from tqdm import tqdm
10
10
  from pyogrio import list_layers, read_dataframe
11
11
  from enum import Enum
12
12
  import numba as nb
13
- from numba import cuda
14
13
 
15
14
  ENGINE = 'pyogrio' # or 'Fiona -- Pyogrio is faster
16
15
  EXTENT = '.gpkg'
@@ -134,7 +133,10 @@ class Accept_Manager():
134
133
  CaPa_Walloon:str = 'Cadastre_Walloon.gpkg',
135
134
  PICC_Walloon:str = 'PICC_vDIFF.gdb',
136
135
  CE_IGN_top10v:str = 'CE_IGN_TOP10V/CE_IGN_TOP10V.shp',
137
- EPU_Stations:str = 'AJOUT_PDET_EPU_DG03_STATIONS.shp'
136
+ EPU_Stations:str = 'AJOUT_PDET_EPU_DG03_STATIONS.shp',
137
+ Ponderation_csv:str = 'Ponderation.csv',
138
+ Vulnerability_csv:str = 'Vulnerability.csv',
139
+ Intermediate_csv:str = 'Intermediate.csv'
138
140
  ) -> None:
139
141
 
140
142
  self.old_dir:Path = Path(os.getcwd())
@@ -169,11 +171,11 @@ class Accept_Manager():
169
171
  self.CE_IGN_TOP10V = self.IN_DATABASE / self._ce_ign_top10v
170
172
  self.EPU_STATIONS = self.IN_EPU_STATIONS / EPU_Stations
171
173
 
172
- self.VULNERABILITY_CSV = self.IN_CSV / "Vulnerability.csv"
173
- self.POINTS_CSV = self.IN_CSV / "Intermediate.csv"
174
- # self.PONDERATION_CSV = self.IN_CSV / "Ponderation.csv"
174
+ self.VULNERABILITY_CSV = self.IN_CSV / Vulnerability_csv
175
+ self.POINTS_CSV = self.IN_CSV / Intermediate_csv
176
+ self.PONDERATION_CSV = self.IN_CSV / Ponderation_csv
175
177
 
176
- self._CSVs = [self.VULNERABILITY_CSV, self.POINTS_CSV] #, self.PONDERATION_CSV]
178
+ self._CSVs = [self.VULNERABILITY_CSV, self.POINTS_CSV]
177
179
  self._GPKGs= [self.CAPA_WALLOON, self.PICC_WALLOON]
178
180
  self._GDBs = [self.ORIGINAL_GDB]
179
181
  self._SHPs = [self.CE_IGN_TOP10V, self.EPU_STATIONS]
@@ -1072,79 +1074,6 @@ def data_modification(layer:str,
1072
1074
  logging.error("skipped" + str(layer) + "due to no polygon in the study area")
1073
1075
  return "skipped" + str(layer) + "due to no polygon in the study area"
1074
1076
 
1075
- def vector_to_raster(layer:str,
1076
- manager:Accept_Manager,
1077
- attribute:str,
1078
- pixel_size:float):
1079
- """
1080
- Convert a vector layer to a raster tiff file
1081
-
1082
- :param layer: the layer name in the GDB file
1083
- :param vector_input: the path to the vector file
1084
- :param extent: the path to the extent file
1085
- :param attribute: the attribute to rasterize
1086
- :param pixel_size: the pixel size of the raster
1087
-
1088
- """
1089
-
1090
- old_dir = os.getcwd()
1091
-
1092
- layer = str(layer)
1093
-
1094
- vector_input = str(manager.TMP_CODEVULNE / (layer + EXTENT))
1095
- extent = str(manager.SA)
1096
- attribute = str(attribute)
1097
- pixel_size = float(pixel_size)
1098
-
1099
- out_file = manager.TMP_RASTERS / attribute / (layer + ".tiff")
1100
-
1101
- if out_file.exists():
1102
- os.remove(out_file)
1103
-
1104
- out_file = str(out_file)
1105
-
1106
- NoData_value = 0
1107
-
1108
- extent_ds:ogr.DataSource = ogr.Open(extent)
1109
- extent_layer = extent_ds.GetLayer()
1110
-
1111
- x_min, x_max, y_min, y_max = extent_layer.GetExtent()
1112
-
1113
- x_min = float(int(x_min))
1114
- x_max = float(np.ceil(x_max))
1115
- y_min = float(int(y_min))
1116
- y_max = float(np.ceil(y_max))
1117
-
1118
- # Open the data sources and read the extents
1119
- source_ds:ogr.DataSource = ogr.Open(vector_input)
1120
- if source_ds is None:
1121
- logging.error(f"Could not open the data source {layer}")
1122
- return
1123
- source_layer = source_ds.GetLayer()
1124
-
1125
- # Create the destination data source
1126
- x_res = int((x_max - x_min) / pixel_size)
1127
- y_res = int((y_max - y_min) / pixel_size)
1128
- target_ds:gdal.Driver = gdal.GetDriverByName('GTiff').Create(out_file,
1129
- x_res, y_res, 1,
1130
- gdal.GDT_Byte,
1131
- options=["COMPRESS=LZW"])
1132
-
1133
- target_ds.SetGeoTransform((x_min, pixel_size, 0, y_max, 0, -pixel_size))
1134
- srs = osr.SpatialReference()
1135
- srs.ImportFromEPSG(31370)
1136
- target_ds.SetProjection(srs.ExportToWkt())
1137
-
1138
- band = target_ds.GetRasterBand(1)
1139
- band.SetNoDataValue(NoData_value)
1140
-
1141
- # Rasterize the areas
1142
- gdal.RasterizeLayer(target_ds, [1],
1143
- source_layer,
1144
- options=["ATTRIBUTE="+attribute,
1145
- "ALL_TOUCHED=TRUE"])
1146
- target_ds = None
1147
-
1148
1077
  def compute_vulnerability(manager:Accept_Manager):
1149
1078
  """
1150
1079
  Compute the vulnerability for the Study Area
@@ -1160,51 +1089,71 @@ def compute_vulnerability(manager:Accept_Manager):
1160
1089
 
1161
1090
  logging.info("Number of files",len(rasters_vuln))
1162
1091
 
1163
- ds:gdal.Dataset = gdal.Open(str(rasters_vuln[0]))
1092
+ ds:gdal.Dataset = gdal.OpenEx(str(rasters_vuln[0]), gdal.GA_ReadOnly, open_options=["SPARSE_OK=TRUE"])
1164
1093
 
1165
- tmp_vuln = np.array(ds.GetRasterBand(1).ReadAsArray())
1094
+ tmp_vuln = ds.GetRasterBand(1)
1166
1095
 
1167
- x, y = tmp_vuln.shape
1096
+ # REMARK: The XSize and YSize are the number of columns and rows
1097
+ col, row = tmp_vuln.XSize, tmp_vuln.YSize
1168
1098
 
1169
1099
  logging.info("Computing Vulnerability")
1170
1100
 
1171
- array_vuln = np.ones((x, y), dtype=np.int8)
1172
- array_code = np.ones((x, y), dtype=np.int8)
1101
+ array_vuln = np.ones((row, col), dtype=np.int8)
1173
1102
 
1174
1103
  # Create a JIT function to update the arrays
1175
1104
  # Faster than the classical Python loop or Numpy
1176
1105
  @nb.jit(nopython=True, boundscheck=False, inline='always')
1177
1106
  # @cuda.jit(device=True, inline=True)
1178
- def update_arrays_jit(tmp_vuln, loccode, array_vuln, array_code):
1107
+ def update_arrays_jit(tmp_vuln, array_vuln):
1179
1108
  for i in range(tmp_vuln.shape[0]):
1180
1109
  for j in range(tmp_vuln.shape[1]):
1181
1110
  if tmp_vuln[i, j] >= array_vuln[i, j]:
1182
1111
  array_vuln[i, j] = tmp_vuln[i, j]
1183
- array_code[i, j] = loccode
1184
1112
 
1185
- return array_vuln, array_code
1186
-
1187
- for i in tqdm(range(len(rasters_vuln)), 'Computing Vulnerability : '):
1188
- logging.info("Computing layer {} / {}".format(i, len(rasters_vuln)))
1189
- ds = gdal.Open(str(rasters_vuln[i]))
1113
+ return array_vuln
1190
1114
 
1191
- tmp_vuln = ds.GetRasterBand(1).ReadAsArray()
1192
-
1193
- loccode = vuln_csv.get_vulnerability_code(rasters_vuln[i].stem)
1115
+ @nb.jit(nopython=True, boundscheck=False, inline='always')
1116
+ # @cuda.jit(device=True, inline=True)
1117
+ def update_arrays_jit_coo(row, col, locvuln, array_vuln):
1118
+ for i,j in zip(row, col):
1119
+ if locvuln >= array_vuln[i, j]:
1120
+ array_vuln[i, j] = locvuln
1194
1121
 
1195
- # We use the jit
1196
- update_arrays_jit(tmp_vuln, loccode, array_vuln, array_code)
1122
+ return array_vuln
1123
+
1124
+ for i in tqdm(range(len(rasters_vuln)), 'Computing Vulnerability : '):
1125
+ logging.info("Computing layer {} / {}".format(i, len(rasters_vuln)))
1126
+
1127
+ locvuln = vuln_csv.get_vulnerability_level(rasters_vuln[i].stem)
1197
1128
 
1198
- # ij = np.where(array_vuln == 0)
1199
- # array_vuln[ij] = 1
1200
- # array_code[ij] = 1
1129
+ if locvuln == 1:
1130
+ logging.info("No need to apply the matrice, the vulnerability is 1 which is the lower value")
1131
+ continue
1132
+
1133
+ if rasters_vuln[i].with_suffix('.npz').exists():
1134
+ ij_npz = np.load(rasters_vuln[i].with_suffix('.npz'))
1135
+ ii = ij_npz['row']
1136
+ jj = ij_npz['col']
1137
+ # We use the jit
1138
+ update_arrays_jit_coo(ii, jj, locvuln, array_vuln)
1139
+
1140
+ else:
1141
+ ds = gdal.OpenEx(str(rasters_vuln[i]), open_options=["SPARSE_OK=TRUE"])
1142
+ tmp_vuln = ds.GetRasterBand(1).ReadAsArray()
1143
+ # We use the jit
1144
+ update_arrays_jit(tmp_vuln, array_vuln)
1201
1145
 
1202
1146
  logging.info("Saving the computed vulnerability")
1203
1147
  dst_filename= str(manager.SA_VULN)
1204
1148
  y_pixels, x_pixels = array_vuln.shape # number of pixels in x
1205
1149
 
1206
1150
  driver = gdal.GetDriverByName('GTiff')
1207
- dataset = driver.Create(dst_filename, x_pixels, y_pixels, gdal.GDT_Byte, 1, options=["COMPRESS=LZW"])
1151
+ dataset = driver.Create(dst_filename,
1152
+ x_pixels, y_pixels,
1153
+ gdal.GDT_Byte,
1154
+ 1,
1155
+ options=["COMPRESS=LZW"])
1156
+
1208
1157
  dataset.GetRasterBand(1).WriteArray(array_vuln.astype(np.int8))
1209
1158
  # follow code is adding GeoTranform and Projection
1210
1159
  geotrans = ds.GetGeoTransform() # get GeoTranform from existed 'data0'
@@ -1214,11 +1163,83 @@ def compute_vulnerability(manager:Accept_Manager):
1214
1163
  dataset.FlushCache()
1215
1164
  dataset = None
1216
1165
 
1166
+ logging.info("Computed Vulnerability for the Study Area - Done")
1167
+
1168
+ def compute_code(manager:Accept_Manager):
1169
+ """
1170
+ Compute the code for the Study Area
1171
+
1172
+ This function **will not modify** the data by the removed buildings/scenarios.
1173
+
1174
+ :param dirsnames: the Dirs_Names object from the calling function
1175
+ """
1176
+
1177
+ vuln_csv = Vulnerability_csv(manager.VULNERABILITY_CSV)
1178
+
1179
+ rasters_code = manager.get_files_in_rasters_code()
1180
+
1181
+ logging.info("Number of files",len(rasters_code))
1182
+
1183
+ ds:gdal.Dataset = gdal.OpenEx(str(rasters_code[0]), gdal.GA_ReadOnly, open_options=["SPARSE_OK=TRUE"])
1184
+
1185
+ tmp_code = ds.GetRasterBand(1)
1186
+
1187
+ # REMARK: The XSize and YSize are the number of columns and rows
1188
+ col, row = tmp_code.XSize, tmp_code.YSize
1189
+
1190
+ logging.info("Computing Code")
1191
+
1192
+ array_code = np.ones((row, col), dtype=np.int8)
1193
+
1194
+ # Create a JIT function to update the arrays
1195
+ # Faster than the classical Python loop or Numpy
1196
+ @nb.jit(nopython=True, boundscheck=False, inline='always')
1197
+ # @cuda.jit(device=True, inline=True)
1198
+ def update_arrays_jit(tmp_code, loccode, array_code):
1199
+ for i in range(tmp_code.shape[0]):
1200
+ for j in range(tmp_code.shape[1]):
1201
+ if tmp_code[i, j] >= array_code[i, j]:
1202
+ array_code[i, j] = loccode
1203
+
1204
+ return array_code
1205
+
1206
+ @nb.jit(nopython=True, boundscheck=False, inline='always')
1207
+ # @cuda.jit(device=True, inline=True)
1208
+ def update_arrays_jit_coo(row, col, loccode, array_code):
1209
+ for i,j in zip(row, col):
1210
+ if loccode >= array_code[i, j]:
1211
+ array_code[i, j] = loccode
1212
+
1213
+ return array_code
1214
+
1215
+ for i in tqdm(range(len(rasters_code)), 'Computing Code : '):
1216
+ logging.info("Computing layer {} / {}".format(i, len(rasters_code)))
1217
+
1218
+ loccode = vuln_csv.get_vulnerability_code(rasters_code[i].stem)
1219
+
1220
+ if rasters_code[i].with_suffix('.npz').exists():
1221
+ ij_npz = np.load(rasters_code[i].with_suffix('.npz'))
1222
+ ii = ij_npz['row']
1223
+ jj = ij_npz['col']
1224
+ # We use the jit
1225
+ update_arrays_jit_coo(ii, jj, loccode, array_code)
1226
+
1227
+ else:
1228
+ ds = gdal.OpenEx(str(rasters_code[i]), open_options=["SPARSE_OK=TRUE"])
1229
+ tmp_code = ds.GetRasterBand(1).ReadAsArray()
1230
+ # We use the jit
1231
+ update_arrays_jit(tmp_code, loccode, array_code)
1232
+
1217
1233
  logging.info("Saving the computed codes")
1218
1234
  dst_filename= str(manager.SA_CODE)
1219
1235
  y_pixels, x_pixels = array_code.shape # number of pixels in x
1220
1236
  driver = gdal.GetDriverByName('GTiff')
1221
- dataset = driver.Create(dst_filename, x_pixels, y_pixels, gdal.GDT_Byte, 1, options=["COMPRESS=LZW"])
1237
+ dataset = driver.Create(dst_filename,
1238
+ x_pixels, y_pixels,
1239
+ gdal.GDT_Byte,
1240
+ 1,
1241
+ options=["COMPRESS=LZW"])
1242
+
1222
1243
  dataset.GetRasterBand(1).WriteArray(array_code.astype(np.int8))
1223
1244
  # follow code is adding GeoTranform and Projection
1224
1245
  geotrans = ds.GetGeoTransform() # get GeoTranform from existed 'data0'
@@ -1228,7 +1249,7 @@ def compute_vulnerability(manager:Accept_Manager):
1228
1249
  dataset.FlushCache()
1229
1250
  dataset = None
1230
1251
 
1231
- logging.info("Computed Vulnerability for the Study Area - Done")
1252
+ logging.info("Computed Code for the Study Area - Done")
1232
1253
 
1233
1254
  def compute_vulnerability4scenario(manager:Accept_Manager):
1234
1255
  """ Compute the vulnerability for the scenario
@@ -1284,7 +1305,7 @@ def compute_vulnerability4scenario(manager:Accept_Manager):
1284
1305
  dataset.FlushCache()
1285
1306
  dataset = None
1286
1307
 
1287
- logging.info("Computed Vulnerability for the scenario")
1308
+ logging.info("Computed Vulnerability and code for the scenario")
1288
1309
 
1289
1310
  def match_vulnerability2sim(inRas:Path, outRas:Path, MODREC:Path):
1290
1311
  """
@@ -1408,8 +1429,12 @@ def shp_to_raster(vector_fn:str, raster_fn:str, pixel_size:float = 1.):
1408
1429
  x_res = int((x_max - x_min) / pixel_size)
1409
1430
  y_res = int((y_max - y_min) / pixel_size)
1410
1431
 
1411
- target_ds = gdal.GetDriverByName('GTiff').Create(raster_fn, x_res, y_res, 1, gdal.GDT_Byte,
1412
- options=["COMPRESS=LZW"])
1432
+ target_ds = gdal.GetDriverByName('GTiff').Create(raster_fn,
1433
+ x_res, y_res,
1434
+ 1,
1435
+ gdal.GDT_Byte,
1436
+ options=["COMPRESS=LZW",
1437
+ 'SPARSE_OK=TRUE'])
1413
1438
 
1414
1439
  target_ds.SetGeoTransform((x_min, pixel_size, 0, y_max, 0, -pixel_size))
1415
1440
  srs = osr.SpatialReference()
@@ -1425,3 +1450,85 @@ def shp_to_raster(vector_fn:str, raster_fn:str, pixel_size:float = 1.):
1425
1450
  options=["ALL_TOUCHED=TRUE"])
1426
1451
  target_ds = None
1427
1452
  vector_fn = raster_fn = None
1453
+
1454
+ def vector_to_raster(layer:str,
1455
+ manager:Accept_Manager,
1456
+ attribute:str,
1457
+ pixel_size:float,
1458
+ convert_to_sparse:bool = True):
1459
+ """
1460
+ Convert a vector layer to a raster tiff file
1461
+
1462
+ :param layer: the layer name in the GDB file
1463
+ :param vector_input: the path to the vector file
1464
+ :param extent: the path to the extent file
1465
+ :param attribute: the attribute to rasterize
1466
+ :param pixel_size: the pixel size of the raster
1467
+
1468
+ """
1469
+
1470
+ layer = str(layer)
1471
+
1472
+ vector_input = str(manager.TMP_CODEVULNE / (layer + EXTENT))
1473
+ extent = str(manager.SA)
1474
+ attribute = str(attribute)
1475
+ pixel_size = float(pixel_size)
1476
+
1477
+ out_file = manager.TMP_RASTERS / attribute / (layer + ".tiff")
1478
+
1479
+ if out_file.exists():
1480
+ os.remove(out_file)
1481
+
1482
+ out_file = str(out_file)
1483
+
1484
+ NoData_value = 0
1485
+
1486
+ extent_ds:ogr.DataSource = ogr.Open(extent)
1487
+ extent_layer = extent_ds.GetLayer()
1488
+
1489
+ x_min, x_max, y_min, y_max = extent_layer.GetExtent()
1490
+
1491
+ x_min = float(int(x_min))
1492
+ x_max = float(np.ceil(x_max))
1493
+ y_min = float(int(y_min))
1494
+ y_max = float(np.ceil(y_max))
1495
+
1496
+ # Open the data sources and read the extents
1497
+ source_ds:ogr.DataSource = ogr.Open(vector_input)
1498
+ if source_ds is None:
1499
+ logging.error(f"Could not open the data source {layer}")
1500
+ return
1501
+ source_layer = source_ds.GetLayer()
1502
+
1503
+ # Create the destination data source
1504
+ x_res = int((x_max - x_min) / pixel_size)
1505
+ y_res = int((y_max - y_min) / pixel_size)
1506
+ target_ds:gdal.Driver = gdal.GetDriverByName('GTiff').Create(out_file,
1507
+ x_res, y_res, 1,
1508
+ gdal.GDT_Byte,
1509
+ options=["COMPRESS=DEFLATE",
1510
+ 'SPARSE_OK=TRUE',])
1511
+
1512
+ target_ds.SetGeoTransform((x_min, pixel_size, 0, y_max, 0, -pixel_size))
1513
+ srs = osr.SpatialReference()
1514
+ srs.ImportFromEPSG(31370)
1515
+ target_ds.SetProjection(srs.ExportToWkt())
1516
+
1517
+ band = target_ds.GetRasterBand(1)
1518
+ band.SetNoDataValue(NoData_value)
1519
+
1520
+ # Rasterize the areas
1521
+ gdal.RasterizeLayer(target_ds, [1],
1522
+ source_layer,
1523
+ options=["ATTRIBUTE="+attribute,
1524
+ "ALL_TOUCHED=TRUE"])
1525
+
1526
+ if convert_to_sparse:
1527
+ # Convert the raster to a npz containing the row and col of the non-null values
1528
+ array = band.ReadAsArray()
1529
+ ij = np.nonzero(array)
1530
+ np.savez_compressed(Path(out_file).with_suffix('.npz'), row=ij[0].astype(np.int32), col=ij[1].astype(np.int32))
1531
+
1532
+ target_ds = None
1533
+
1534
+ return 0
wolfhece/apps/version.py CHANGED
@@ -5,7 +5,7 @@ class WolfVersion():
5
5
 
6
6
  self.major = 2
7
7
  self.minor = 1
8
- self.patch = 27
8
+ self.patch = 28
9
9
 
10
10
  def __str__(self):
11
11
 
wolfhece/wolf_array.py CHANGED
@@ -4771,6 +4771,12 @@ class WolfArray(Element_To_Draw, header_wolf):
4771
4771
  elif arr.dtype == np.float64:
4772
4772
  arr_type = gdal.GDT_Float64
4773
4773
  nullvalue = self.nullvalue
4774
+ elif arr.dtype == np.int8:
4775
+ arr_type = gdal.GDT_Byte
4776
+ nullvalue = int(self.nullvalue)
4777
+ elif arr.dtype == np.int16:
4778
+ arr_type = gdal.GDT_Int16
4779
+ nullvalue = int(self.nullvalue)
4774
4780
  else:
4775
4781
  arr_type = gdal.GDT_Int32
4776
4782
  nullvalue = int(self.nullvalue)
@@ -5024,11 +5030,16 @@ class WolfArray(Element_To_Draw, header_wolf):
5024
5030
  elif self.array.dtype == np.int32:
5025
5031
  self.wolftype = WOLF_ARRAY_FULL_INTEGER
5026
5032
  elif self.array.dtype == np.uint8:
5027
- logging.warning(_('Conversion of uint8 to int8'))
5033
+ logging.warning(_('***************************************************'))
5034
+ logging.warning(_(' Conversion of uint8 to int8'))
5035
+ logging.warning(_(' If you save this file, it will be converted to int8'))
5036
+ logging.warning(_('***************************************************'))
5028
5037
  self.array = self.array.astype(np.int8)
5029
5038
  self.wolftype = WOLF_ARRAY_FULL_INTEGER8
5030
5039
  elif self.array.dtype == np.int8:
5031
5040
  self.wolftype = WOLF_ARRAY_FULL_INTEGER8
5041
+ elif self.array.dtype == np.int16:
5042
+ self.wolftype = WOLF_ARRAY_FULL_INTEGER16
5032
5043
 
5033
5044
  except:
5034
5045
  logging.warning(_('Error during importing tif file'))
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: wolfhece
3
- Version: 2.1.27
3
+ Version: 2.1.28
4
4
  Author-email: Pierre Archambeau <pierre.archambeau@uliege.be>
5
5
  License: AGPL-v3 License
6
6
  Project-URL: Homepage, https://uee.uliege.be/hece
@@ -48,7 +48,7 @@ wolfhece/rain_SPWMI.py,sha256=YqsF-yFro3y_a6MfVRFfr-Rxi7NR1gl_i8VX7scmzes,13548
48
48
  wolfhece/test_Results2DGPU.py,sha256=NOJ_hFXrcLSQXS1dtsqXRQltqIZtDSHMz_EgAJ2_FHU,307
49
49
  wolfhece/textpillow.py,sha256=zEfLrKhfCDyMaVuQOUjHqz6MGKeQ4aewMxOsWi5-wKI,13832
50
50
  wolfhece/tools_mpl.py,sha256=q8Yc4aukPPiUcEzREvZRM_em67XqXaahdoaNt0DETfE,266
51
- wolfhece/wolf_array.py,sha256=fvZLKrQkglduI7m82C2c_cBPMhKwrBcHPKTNcB8DSEg,355054
51
+ wolfhece/wolf_array.py,sha256=ViEJkKxOBLjiPFZy01wcC7q4djBQcgl5ptRXWAMfcJ0,355676
52
52
  wolfhece/wolf_hist.py,sha256=JpRXvzJLUP-RkSkvth3DQWglgTMFI2ZEUDb4RYOfeeI,3284
53
53
  wolfhece/wolf_texture.py,sha256=llQ7aV8scWXIkhpri9XjaPejzoBJsGfsln2ZnlRbFkU,16270
54
54
  wolfhece/wolf_tiles.py,sha256=F2JsJHdAP8fIffNJdG_J26bonCIRtIwMmxKFqdSCRDA,10088
@@ -56,13 +56,13 @@ wolfhece/wolf_vrt.py,sha256=un5CKzAUmzSsjLXK7YLnQEWz8FLoafXJs8oqUvS_-h0,10271
56
56
  wolfhece/wolf_zi_db.py,sha256=Ok0MxQYZMMLRJN1QY-HSplLhUzzb6gkXgBQ3ihhLQHk,12669
57
57
  wolfhece/wolfresults_2D.py,sha256=wF-wIyqpTrUJX_fT-QCVuNxLZCgUsqK9ptGz8izpyIQ,165590
58
58
  wolfhece/xyz_file.py,sha256=aQOcTHkHRhXHxL_WxTHwzygp6e47San7SHSpxKQU0dw,5457
59
- wolfhece/acceptability/Parallels.py,sha256=IFXTuxgf8y2S8RTRZXr5DvPLB8zQjyEIKYKc56FehVo,3520
59
+ wolfhece/acceptability/Parallels.py,sha256=wpCdwkqR6PAFeRkV5TvSSL33Vf368j-bvYcl7D1Y-sc,3695
60
60
  wolfhece/acceptability/__init__.py,sha256=hfgoPKLDpX7drN1Vpvux-_5Lfyc_7feT2C2zQr5v-Os,258
61
- wolfhece/acceptability/acceptability.py,sha256=06xhkvHIB2xPsVl1e8X0i9UDnSD3Bhf8mVLopyLgsfk,20115
61
+ wolfhece/acceptability/acceptability.py,sha256=pXGsV8Rga-YtILhfsE90PjVfsbRl_z7RXLB8537dSfA,21882
62
62
  wolfhece/acceptability/acceptability1.py,sha256=rf1Bu2JuyOPwMxvez7z5vCXrePAV486hyVM5g1f40g4,13045
63
63
  wolfhece/acceptability/acceptability_gui.py,sha256=zzbHd_e90fLhbgrdBlnWmBWBO8ZBwb8vikhl-2Rdy0M,12020
64
64
  wolfhece/acceptability/cli.py,sha256=pIh9hIbM5RQFh3EBQJB2jWJ8F2M4l-D6qGoewXROE1M,7102
65
- wolfhece/acceptability/func.py,sha256=WkiFQyHq6vYC9WI3m9Dr6Ub4lpfEJ3TtrQOmd3j5mjs,53966
65
+ wolfhece/acceptability/func.py,sha256=E2zPgFRm3x5u6PDt6y8nUCyXL5eCHzbwHMIQoj8Bg8U,58238
66
66
  wolfhece/apps/ManageParams.py,sha256=heg5L4fMn0ettR7Bad_Q680o_JWnTbe3WFkL_9IziAk,312
67
67
  wolfhece/apps/Optimisation_hydro.py,sha256=mHazBazTUGyxPbHPXhaQim8vqIeOOuKPjH0B48VWduA,374
68
68
  wolfhece/apps/WolfPython.png,sha256=K3dcbeZUiJCFNwOAAlGMaRGLJ56yM8WD2I_0bk0xT1g,104622
@@ -73,7 +73,7 @@ wolfhece/apps/check_install.py,sha256=jrKR-njqnpIh6ZJqvP6KbDUPVCfwTNQj4glQhcyzs9
73
73
  wolfhece/apps/curvedigitizer.py,sha256=avWERHuVxPnJBOD_ibczwW_XG4vAenqWS8W1zjhBox8,4898
74
74
  wolfhece/apps/isocurrent.py,sha256=4XnNWPa8mYUK7V4zdDRFrHFIXNG2AN2og3TqWKKcqjY,3811
75
75
  wolfhece/apps/splashscreen.py,sha256=LkEVMK0eCc84NeCWD3CGja7fuQ_k1PrZdyqD3GQk_8c,2118
76
- wolfhece/apps/version.py,sha256=fCJirDzHsyPl2cZap4mimCySWPCBCEPVShHrl_tobLg,388
76
+ wolfhece/apps/version.py,sha256=QLSk_f340_pnubYC3LzPucHpjfHthXJURv8frE1CoG0,388
77
77
  wolfhece/apps/wolf.py,sha256=gqfm-ZaUJqNsfCzmdtemSeqLw-GVdSVix-evg5WArJI,293
78
78
  wolfhece/apps/wolf2D.py,sha256=gWD9ee2-1pw_nUxjgRaJMuSe4kUT-RWhOeoTt_Lh1mM,267
79
79
  wolfhece/apps/wolf_logo.bmp,sha256=ruJ4MA51CpGO_AYUp_dB4SWKHelvhOvd7Q8NrVOjDJk,3126
@@ -279,8 +279,8 @@ wolfhece/ui/wolf_multiselection_collapsiblepane.py,sha256=yGbU_JsF56jsmms0gh7mxa
279
279
  wolfhece/ui/wolf_times_selection_comparison_models.py,sha256=wCxGRnE3kzEkWlWA6-3X8ADOFux_B0a5QWJ2GnXTgJw,4709
280
280
  wolfhece/wintab/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
281
281
  wolfhece/wintab/wintab.py,sha256=8A-JNONV6ujgsgG3lM5Uw-pVgglPATwKs86oBzzljoc,7179
282
- wolfhece-2.1.27.dist-info/METADATA,sha256=C3iPLvGGk5G130CzmuuSnygz-PX_Rn2ZtfJNQglil8M,2327
283
- wolfhece-2.1.27.dist-info/WHEEL,sha256=Z4pYXqR_rTB7OWNDYFOm1qRk0RX6GFP2o8LgvP453Hk,91
284
- wolfhece-2.1.27.dist-info/entry_points.txt,sha256=MAG6NrF64fcxiVNb2g1JPYPGcn9C0HWtqqNurB83oX0,330
285
- wolfhece-2.1.27.dist-info/top_level.txt,sha256=EfqZXMVCn7eILUzx9xsEu2oBbSo9liWPFWjIHik0iCI,9
286
- wolfhece-2.1.27.dist-info/RECORD,,
282
+ wolfhece-2.1.28.dist-info/METADATA,sha256=h_mkaZJwq29PG6UesnWcQYnBewET8t47eBJvEMI3WJQ,2327
283
+ wolfhece-2.1.28.dist-info/WHEEL,sha256=YiKiUUeZQGmGJoR_0N1Y933DOBowq4AIvDe2-UIy8E4,91
284
+ wolfhece-2.1.28.dist-info/entry_points.txt,sha256=MAG6NrF64fcxiVNb2g1JPYPGcn9C0HWtqqNurB83oX0,330
285
+ wolfhece-2.1.28.dist-info/top_level.txt,sha256=EfqZXMVCn7eILUzx9xsEu2oBbSo9liWPFWjIHik0iCI,9
286
+ wolfhece-2.1.28.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (70.3.0)
2
+ Generator: setuptools (71.0.2)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5