wolfhece 2.1.107__py3-none-any.whl → 2.1.109__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.
wolfhece/PyWMS.py CHANGED
@@ -42,8 +42,10 @@ def getWalonmap(cat:Literal['IMAGERIE/ORTHO_2021', 'ALEA', 'CADMAP', 'LIDAXES',
42
42
  wms=WebMapService('https://geoservices.wallonie.be/arcgis/services/'
43
43
  + catloc+'/MapServer/WMSServer',version='1.3.0')
44
44
  except:
45
- wms=WebMapService('http://ccff02.minfin.fgov.be/geoservices/arcgis/services/'
45
+ wms=WebMapService('https://eservices.minfin.fgov.be/arcgis/services/'
46
46
  + catloc+'/MapServer/WMSServer',version='1.3.0')
47
+ # wms=WebMapService('http://ccff02.minfin.fgov.be/geoservices/arcgis/services/'
48
+ # + catloc+'/MapServer/WMSServer',version='1.3.0')
47
49
 
48
50
  ppkm = 300
49
51
  if w is None and h is None:
@@ -84,6 +86,9 @@ def getWalonmap(cat:Literal['IMAGERIE/ORTHO_2021', 'ALEA', 'CADMAP', 'LIDAXES',
84
86
  elif cat.find('CADMAP')>0:
85
87
  curcont=['0,1']
86
88
  curstyles=['default,default']
89
+ elif cat.find('LIMITES_ADMINISTRATIVES')>0:
90
+ curcont=['0,1,2,3']
91
+ curstyles=['default,default,default,default']
87
92
  elif cat.find('wms')>0:
88
93
  curcont=['1,2,3,4,5']
89
94
  curstyles=['default,default,default,default,default']
@@ -108,6 +113,61 @@ def getWalonmap(cat:Literal['IMAGERIE/ORTHO_2021', 'ALEA', 'CADMAP', 'LIDAXES',
108
113
  except:
109
114
  logging.warning(_('Impossible to get data from web services'))
110
115
 
116
+ def getVlaanderen(cat:Literal['Adpf'],
117
+ xl:float,
118
+ yl:float,
119
+ xr:float,
120
+ yr:float,
121
+ w:int = None,
122
+ h:int = None,
123
+ tofile=True) -> BytesIO:
124
+
125
+ catloc=cat
126
+
127
+ try:
128
+ wms=WebMapService('https://geo.api.vlaanderen.be/'
129
+ + catloc+'/wms',version='1.3.0')
130
+ except:
131
+ logging.warning(_('Impossible to get data from web services'))
132
+ return
133
+
134
+ ppkm = 300
135
+ if w is None and h is None:
136
+ real_w = (xr-xl)/1000
137
+ real_h = (yr-yl)/1000
138
+ w = int(real_w * ppkm)
139
+ h = int(real_h * ppkm)
140
+ elif w is None:
141
+ real_w = (xr-xl)/1000
142
+ real_h = (yr-yl)/1000
143
+ ppkm = h/real_h
144
+ w = int(real_w * ppkm)
145
+ # h = int(real_h * ppkm)
146
+ elif h is None:
147
+ real_w = (xr-xl)/1000
148
+ real_h = (yr-yl)/1000
149
+ ppkm = w/real_w
150
+ # w = int(real_w * ppkm)
151
+ h = int(real_h * ppkm)
152
+
153
+ if tofile:
154
+ img=wms.getmap(layers=['0'],styles=['default'],srs='EPSG:31370',bbox=(xl,yl,xr,yr),size=(w,h),format='image/png',transparent=True)
155
+ out = open('aqualim.png', 'wb')
156
+ out.write(img.read())
157
+ out.close()
158
+ return BytesIO(b'1')
159
+ else:
160
+ mycontents=list(wms.contents)
161
+ curcont=['Adpf']
162
+ curstyles=['default']
163
+
164
+ try:
165
+ img=wms.getmap(layers=curcont,styles=curstyles,srs='EPSG:31370',bbox=(xl,yl,xr,yr),size=(w,h),format='image/png',transparent=True)
166
+ return BytesIO(img.read())
167
+ except:
168
+ logging.warning(_('Impossible to get data from web services'))
169
+
170
+
111
171
  def getIGNFrance(cat:str,epsg:str,xl,yl,xr,yr,w,h,tofile=True) -> BytesIO:
112
172
 
113
173
  if epsg!='EPSG:4326':
@@ -51,6 +51,7 @@ class steps_vulnerability(Enum):
51
51
  CREATE_RASTERS_CODE = 11
52
52
  APPLY_MODIFS = 2
53
53
  MATCH_SIMUL = 3
54
+ APPLY_SCENARIOSVULN = 4
54
55
 
55
56
  @classmethod
56
57
  def get_list_names(cls):
@@ -63,8 +64,9 @@ class steps_acceptability(Enum):
63
64
  COMPUTE_LOCAL_ACCEPT = 1
64
65
  LOAD_FROM_FILES = 2
65
66
  COMPUTE_MEAN_ACCEPT = 3
66
- COMPUTE_WITHOUT_SCENARIOS = 4
67
+ COMPUTE_BASELINE_WITHOUT_SCENARIOS = 4
67
68
  COMPUTE_WITH_SCENARIOS = 5
69
+ RESAMPLING = 6
68
70
 
69
71
  @classmethod
70
72
  def get_list_names(cls):
@@ -106,12 +108,12 @@ def Base_data_creation(main_dir:str = 'Data',
106
108
  LAYER_BATIEMPRISE = "CONSTR_BATIEMPRISE"
107
109
 
108
110
  manager = Accept_Manager(main_dir,
109
- Study_area,
110
- Original_gdb=Original_gdb,
111
- CaPa_Walloon=CaPa_Walloon,
112
- PICC_Walloon=PICC_Walloon,
113
- CE_IGN_top10v=CE_IGN_top10v,
114
- Vuln_csv=Vuln_csv)
111
+ Study_area,
112
+ Original_gdb=Original_gdb,
113
+ CaPa_Walloon=CaPa_Walloon,
114
+ PICC_Walloon=PICC_Walloon,
115
+ CE_IGN_top10v=CE_IGN_top10v,
116
+ Vuln_csv=Vuln_csv)
115
117
 
116
118
  if not manager.check_before_database_creation():
117
119
  logging.error("The necessary files are missing - Verify logs for more information")
@@ -284,7 +286,7 @@ def Vulnerability(main_dir:str = 'Data',
284
286
  scenario:str = 'Scenario1',
285
287
  Study_area:str = 'Bassin_Vesdre.shp',
286
288
  resolution:float = 1.,
287
- steps:list[int] | list[steps_vulnerability] = [1,10,11,2,3],
289
+ steps:list[int] | list[steps_vulnerability] = [1,10,11,2,3,4],
288
290
  Vuln_csv:str = 'Vulnerability.csv',
289
291
  Intermediate_csv:str = 'Intermediate.csv'):
290
292
  """
@@ -310,6 +312,7 @@ def Vulnerability(main_dir:str = 'Data',
310
312
  - [1,2,3] : All steps are computed - Necessary for the first time
311
313
  - [2,3] : Only the scenario and clipping steps are computed -- Useful for scenario changes
312
314
  - [3] : Only the clipping step is computed -- Useful if simulation area changes but scenario is the same
315
+ - [4] : Compute the vulnerability for vuln_ scenarios
313
316
 
314
317
  """
315
318
 
@@ -337,11 +340,11 @@ def Vulnerability(main_dir:str = 'Data',
337
340
  cleaning_directory(manager.TMP_SCEN_DIR)
338
341
 
339
342
  if 10 in steps or steps_vulnerability.CREATE_RASTERS_VULN in steps:
340
- compute_vulnerability(manager) #global
343
+ compute_vulnerability(manager)
341
344
  done.append(steps_vulnerability.CREATE_RASTERS_VULN)
342
345
 
343
346
  if 11 in steps or steps_vulnerability.CREATE_RASTERS_CODE in steps:
344
- compute_code(manager) #global
347
+ compute_code(manager)
345
348
  done.append(steps_vulnerability.CREATE_RASTERS_CODE)
346
349
 
347
350
  done.append(steps_vulnerability.CREATE_RASTERS)
@@ -383,27 +386,30 @@ def Vulnerability(main_dir:str = 'Data',
383
386
  match_vulnerability2sim(manager.SA_MASKED_RIVER, manager.OUT_MASKED_RIVER, TMAX)
384
387
  match_vulnerability2sim(manager.SA_VULN, manager.OUT_VULN, TMAX)
385
388
  match_vulnerability2sim(manager.SA_CODE, manager.OUT_CODE, TMAX)
386
-
387
- # Scenarios including change in vulnerability
388
- existence=False
389
- existence = manager.create_vrtIfExists()
390
-
391
- if existence == True :
392
- logging.info("Scenarios have been applied to the vulnerability matrix see _scenarios")
393
- manager.translate_vrt2tif()
394
-
395
389
  done.append(steps_vulnerability.MATCH_SIMUL)
396
-
390
+
391
+ if 4 in steps or steps_vulnerability.APPLY_SCENARIOSVULN in steps:
392
+ if os.path.exists(manager.OUT_VULN):
393
+ existence=False
394
+ existence = manager.create_vrtIfExists(manager.OUT_VULN, manager.IN_CH_SA_SC, manager.OUT_VULN_VRT, name="vuln")
395
+ if existence == True :
396
+ logging.info("Scenarios have been applied to the vulnerability matrix see _scenarios")
397
+ manager.translate_vrt2tif(manager.OUT_VULN_VRT, manager.OUT_VULN_S)
398
+ else :
399
+ logging.error(f"The baseline vulnerability does not exist ({manager.OUT_VULN}). Please, compute first the vulnerability without scenarios vuln_.")
400
+ done.append(steps_vulnerability.APPLY_SCENARIOSVULN)
401
+ #Delete _scenario folder is no scenario
402
+ if os.path.isdir(manager.OUT_WITHVULN) and not os.listdir(manager.OUT_WITHVULN):
403
+ os.rmdir(manager.OUT_WITHVULN)
397
404
  return done
398
405
 
399
-
400
406
  def Acceptability(main_dir:str = 'Vesdre',
401
407
  scenario:str = 'Scenario1',
402
408
  Study_area:str = 'Bassin_Vesdre.shp',
403
409
  coeff_auto:bool = True,
404
410
  Ponderation_csv:str = 'Ponderation.csv',
405
411
  resample_size:int = 100,
406
- steps:list[int] | list[steps_acceptability] = [1,2,3,4,5]):
412
+ steps:list[int] | list[steps_acceptability] = [1,2,3,4,5,6]):
407
413
  """ Compute acceptability for the scenario """
408
414
 
409
415
  done = []
@@ -416,27 +422,27 @@ def Acceptability(main_dir:str = 'Vesdre',
416
422
  # Load the vulnerability raster **for the scenario**, and check if an assembly exists and is asked by the user
417
423
  # Initialization of lists to read/ write according to the needed steps
418
424
  VulneToCompute, PathsToSaveA, PathsToSaveA100 = [], [], []
419
- if 4 in steps or steps_acceptability.COMPUTE_WITHOUT_SCENARIOS in steps:
425
+ if 4 in steps or steps_acceptability.COMPUTE_BASELINE_WITHOUT_SCENARIOS in steps:
420
426
  VulneToCompute.append(manager.OUT_VULN)
421
427
  PathsToSaveA.append(manager.OUT_ACCEPT)
422
- PathsToSaveA100.append(manager.OUT_ACCEPT_100M)
428
+ PathsToSaveA100.append(manager.OUT_ACCEPT_RESAMP)
429
+ river_trace = manager.OUT_MASKED_RIVER
423
430
 
424
-
425
431
  if 5 in steps or steps_acceptability.COMPUTE_WITH_SCENARIOS in steps:
426
- change_vuln_files = [Path(a) for a in glob.glob(str(manager.IN_CH_VULN / "*.tiff"))]
432
+ river_trace = manager.wich_river_trace()
433
+ change_vuln_files = [Path(a) for a in glob.glob(str(manager.IN_CH_SA_SC / "vuln_*.tif")) + glob.glob(str(manager.IN_CH_SA_SC / "vuln_*.tiff"))]
427
434
  if len(change_vuln_files) != 0:
428
435
  VulneToCompute.append(manager.OUT_VULN_Stif)
429
436
  PathsToSaveA.append(manager.OUT_ACCEPT_Stif)
430
- PathsToSaveA100.append(manager.OUT_ACCEPT_100M_Stif)
437
+ PathsToSaveA100.append(manager.OUT_ACCEPT_RESAMP_Stif)
431
438
  else :
432
439
  logging.info("No vulnerability rasters in CHANGE_VULNE. The code goes on without them.")
433
-
434
440
  for i in range(len(VulneToCompute)) :
435
441
  vulne = gdal.Open(str(VulneToCompute[i]))
436
442
  saveA = PathsToSaveA[i]
437
443
  saveA100 = PathsToSaveA100[i]
438
- # Load the river mask
439
- riv = gdal.Open(str(manager.OUT_MASKED_RIVER))
444
+ # Load the river mask
445
+ riv = gdal.Open(str(river_trace))
440
446
 
441
447
  # Get the geotransform and projection for the output tiff
442
448
  geotrans = riv.GetGeoTransform()
@@ -462,8 +468,9 @@ def Acceptability(main_dir:str = 'Vesdre',
462
468
 
463
469
  if 1 in steps or steps_acceptability.COMPUTE_LOCAL_ACCEPT in steps:
464
470
  # Compute acceptability for each return period
471
+ message="Some adjustments have been done :"
472
+ message2=""
465
473
  for curT in tqdm(return_periods):
466
-
467
474
  # Load the **FILLED** modelled water depth for the return period
468
475
  model_h = gdal.Open(str(manager.get_sim_file_for_return_period(curT)))
469
476
  # Convert to numpy array
@@ -479,15 +486,13 @@ def Acceptability(main_dir:str = 'Vesdre',
479
486
  assert model_h[ij_riv[0][0], ij_riv[0][1]] == 0, "The river bed is not set to 0 in the modelled water depth"
480
487
  assert model_h.max() > 0, "The maximum water depth is 0"
481
488
  if model_h.min() < 0:
482
- logging.warning("The minimum water depth is negative - {} cells".format(np.count_nonzero(model_h<0)))
483
- logging.warning("Setting the negative values to 0")
489
+ message2+= f"\nFor T{curT}, the minimum water depth is negative - {np.count_nonzero(model_h<0)} cells, these values were set to 0."
484
490
  model_h[model_h < 0] = 0
485
-
486
- logging.info("Return period {}".format(curT))
487
-
491
+ #logging.info("Return period {}".format(curT))
488
492
  # Compute the local acceptability for the return period
489
493
  part_accept[curT] = compute_acceptability(manager, model_h, vulne, curT, (geotrans, proj))
490
-
494
+ if message2 != "":
495
+ logging.error(message + message2)
491
496
  done.append(steps_acceptability.COMPUTE_LOCAL_ACCEPT)
492
497
 
493
498
  # At this point, the local acceptability for each return period is computed
@@ -500,8 +505,7 @@ def Acceptability(main_dir:str = 'Vesdre',
500
505
  if 1 in steps or steps_acceptability.COMPUTE_LOCAL_ACCEPT in steps:
501
506
  # We have computed/updted the acceptibility values.
502
507
  # We do not need to reload them.
503
- logging.warning("The acceptability values have been computed in step 1 - avoid reloading")
504
- logging.info("If you want to reload the acceptability values, please remove step 1 from the list of steps")
508
+ logging.warning("The acceptability values have been computed in step 1 reloading is unnecessary. To avoid this, exclude step 1.")
505
509
  else:
506
510
 
507
511
  # Get the list of Q files
@@ -509,7 +513,7 @@ def Acceptability(main_dir:str = 'Vesdre',
509
513
 
510
514
  # Iterate over the return periods
511
515
  for curT in return_periods:
512
- logging.info(curT)
516
+ #logging.info(curT)
513
517
 
514
518
  # We set the filename from the return period, not the "qs" list
515
519
  q_filename = manager.TMP_QFILES / "Q{}.tif".format(curT)
@@ -580,16 +584,20 @@ def Acceptability(main_dir:str = 'Vesdre',
580
584
  dataset.SetProjection(proj)
581
585
  dataset.FlushCache()
582
586
  dataset=None
583
-
584
- # Resample to XXm
585
- Agg = gdal.Warp(str(saveA100),
586
- str(saveA),
587
- xRes=resample_size,
588
- yRes=resample_size,
589
- resampleAlg='Average')
590
- Agg.FlushCache()
591
- Agg = None
592
-
587
+
593
588
  done.append(steps_acceptability.COMPUTE_MEAN_ACCEPT)
594
-
589
+
590
+ if 6 in steps or steps_acceptability.RESAMPLING in steps:
591
+ if os.path.exists(manager.OUT_ACCEPT):
592
+ Agg = gdal.Warp(str(saveA100),
593
+ str(saveA),
594
+ xRes=resample_size,
595
+ yRes=resample_size,
596
+ resampleAlg='Average')
597
+ Agg.FlushCache()
598
+ Agg = None
599
+ else :
600
+ logging.error(f"The acceptability without resampling does not exist ({manager.OUT_ACCEPT}). Please, compute it first to agglomerate it afterwards.")
601
+ if os.path.isdir(manager.OUT_WITHVULN) and not os.listdir(manager.OUT_WITHVULN):
602
+ os.rmdir(manager.OUT_WITHVULN)
595
603
  return done