wolfhece 2.1.27__py3-none-any.whl → 2.1.29__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/PyDraw.py +3 -3
- wolfhece/PyPalette.py +8 -0
- wolfhece/acceptability/Parallels.py +7 -3
- wolfhece/acceptability/acceptability.py +58 -17
- wolfhece/acceptability/func.py +300 -109
- wolfhece/apps/splashscreen.py +15 -1
- wolfhece/apps/version.py +1 -1
- wolfhece/libs/WolfOGL.c +2677 -1859
- wolfhece/libs/WolfOGL.pyx +17 -0
- wolfhece/libs/verify_license.cp310-win_amd64.pyd +0 -0
- wolfhece/libs/wolfogl.cp310-win_amd64.pyd +0 -0
- wolfhece/models/vulnerability.pal +25 -0
- wolfhece/wolf_array.py +13 -1
- {wolfhece-2.1.27.dist-info → wolfhece-2.1.29.dist-info}/METADATA +2 -1
- {wolfhece-2.1.27.dist-info → wolfhece-2.1.29.dist-info}/RECORD +18 -16
- {wolfhece-2.1.27.dist-info → wolfhece-2.1.29.dist-info}/WHEEL +1 -1
- {wolfhece-2.1.27.dist-info → wolfhece-2.1.29.dist-info}/entry_points.txt +0 -0
- {wolfhece-2.1.27.dist-info → wolfhece-2.1.29.dist-info}/top_level.txt +0 -0
wolfhece/acceptability/func.py
CHANGED
@@ -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
|
+
Vuln_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 /
|
173
|
-
self.POINTS_CSV = self.IN_CSV /
|
174
|
-
|
174
|
+
self.VULNERABILITY_CSV = self.IN_CSV / Vuln_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]
|
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]
|
@@ -852,6 +854,53 @@ class Accept_Manager():
|
|
852
854
|
else:
|
853
855
|
return Modif_Type.COPY
|
854
856
|
|
857
|
+
def check_origin_shape(self) -> list[str]:
|
858
|
+
|
859
|
+
code = self.get_files_in_rasters_code()
|
860
|
+
vuln = self.get_files_in_rasters_vulne()
|
861
|
+
|
862
|
+
if len(code) == 0:
|
863
|
+
logging.error("The code rasters do not exist")
|
864
|
+
return False
|
865
|
+
|
866
|
+
if len(vuln) == 0:
|
867
|
+
logging.error("The vulnerability rasters do not exist")
|
868
|
+
return False
|
869
|
+
|
870
|
+
if len(code) != len(vuln):
|
871
|
+
logging.error("The number of code and vulnerability rasters do not match")
|
872
|
+
return False
|
873
|
+
|
874
|
+
# we take a reference raster
|
875
|
+
ref = gdal.Open(str(code[0]))
|
876
|
+
band_ref = ref.GetRasterBand(1)
|
877
|
+
proj_ref = ref.GetProjection()
|
878
|
+
geo_ref = ref.GetGeoTransform()
|
879
|
+
col_ref, row_ref = band_ref.XSize, band_ref.YSize
|
880
|
+
|
881
|
+
# we compare the reference raster with the others
|
882
|
+
diff = []
|
883
|
+
for cur in code + vuln + [self.SA_MASKED_RIVER]:
|
884
|
+
cur_ = gdal.Open(str(cur))
|
885
|
+
band_cur = cur_.GetRasterBand(1)
|
886
|
+
proj_cur = cur_.GetProjection()
|
887
|
+
geo_cur = cur_.GetGeoTransform()
|
888
|
+
col_cur, row_cur = band_cur.XSize, band_cur.YSize
|
889
|
+
|
890
|
+
if geo_ref != geo_cur:
|
891
|
+
logging.error("The geotransforms do not match {}".format(cur))
|
892
|
+
diff.append(cur)
|
893
|
+
|
894
|
+
if proj_ref != proj_cur:
|
895
|
+
logging.error("The projections do not match {}".format(cur))
|
896
|
+
diff.append(cur)
|
897
|
+
|
898
|
+
if col_ref != col_cur or row_ref != row_cur:
|
899
|
+
logging.error("The dimensions do not match {}".format(cur))
|
900
|
+
diff.append(cur)
|
901
|
+
|
902
|
+
return diff
|
903
|
+
|
855
904
|
|
856
905
|
def clip_layer(layer:str,
|
857
906
|
file_path:str,
|
@@ -1072,79 +1121,6 @@ def data_modification(layer:str,
|
|
1072
1121
|
logging.error("skipped" + str(layer) + "due to no polygon in the study area")
|
1073
1122
|
return "skipped" + str(layer) + "due to no polygon in the study area"
|
1074
1123
|
|
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
1124
|
def compute_vulnerability(manager:Accept_Manager):
|
1149
1125
|
"""
|
1150
1126
|
Compute the vulnerability for the Study Area
|
@@ -1160,51 +1136,73 @@ def compute_vulnerability(manager:Accept_Manager):
|
|
1160
1136
|
|
1161
1137
|
logging.info("Number of files",len(rasters_vuln))
|
1162
1138
|
|
1163
|
-
ds:gdal.Dataset = gdal.
|
1139
|
+
ds:gdal.Dataset = gdal.OpenEx(str(rasters_vuln[0]), gdal.GA_ReadOnly, open_options=["SPARSE_OK=TRUE"])
|
1164
1140
|
|
1165
|
-
tmp_vuln =
|
1141
|
+
tmp_vuln = ds.GetRasterBand(1)
|
1166
1142
|
|
1167
|
-
|
1143
|
+
# REMARK: The XSize and YSize are the number of columns and rows
|
1144
|
+
col, row = tmp_vuln.XSize, tmp_vuln.YSize
|
1168
1145
|
|
1169
1146
|
logging.info("Computing Vulnerability")
|
1170
1147
|
|
1171
|
-
array_vuln = np.ones((
|
1172
|
-
array_code = np.ones((x, y), dtype=np.int8)
|
1148
|
+
array_vuln = np.ones((row, col), dtype=np.int8)
|
1173
1149
|
|
1174
1150
|
# Create a JIT function to update the arrays
|
1175
1151
|
# Faster than the classical Python loop or Numpy
|
1176
1152
|
@nb.jit(nopython=True, boundscheck=False, inline='always')
|
1177
|
-
|
1178
|
-
def update_arrays_jit(tmp_vuln, loccode, array_vuln, array_code):
|
1153
|
+
def update_arrays_jit(tmp_vuln, array_vuln):
|
1179
1154
|
for i in range(tmp_vuln.shape[0]):
|
1180
1155
|
for j in range(tmp_vuln.shape[1]):
|
1181
1156
|
if tmp_vuln[i, j] >= array_vuln[i, j]:
|
1182
1157
|
array_vuln[i, j] = tmp_vuln[i, j]
|
1183
|
-
array_code[i, j] = loccode
|
1184
1158
|
|
1185
|
-
return array_vuln
|
1186
|
-
|
1159
|
+
return array_vuln
|
1160
|
+
|
1161
|
+
@nb.jit(nopython=True, boundscheck=False, inline='always')
|
1162
|
+
def update_arrays_jit_csr(row, col, locvuln, array_vuln):
|
1163
|
+
for k in range(len(row)-1):
|
1164
|
+
i = k
|
1165
|
+
j1 = row[k]
|
1166
|
+
j2 = row[k+1]
|
1167
|
+
for j in col[j1:j2]:
|
1168
|
+
if locvuln >= array_vuln[i, j]:
|
1169
|
+
array_vuln[i, j] = locvuln
|
1170
|
+
|
1171
|
+
return array_vuln
|
1172
|
+
|
1187
1173
|
for i in tqdm(range(len(rasters_vuln)), 'Computing Vulnerability : '):
|
1188
1174
|
logging.info("Computing layer {} / {}".format(i, len(rasters_vuln)))
|
1189
|
-
|
1190
|
-
|
1191
|
-
tmp_vuln = ds.GetRasterBand(1).ReadAsArray()
|
1192
|
-
|
1193
|
-
loccode = vuln_csv.get_vulnerability_code(rasters_vuln[i].stem)
|
1194
|
-
|
1195
|
-
# We use the jit
|
1196
|
-
update_arrays_jit(tmp_vuln, loccode, array_vuln, array_code)
|
1175
|
+
|
1176
|
+
locvuln = vuln_csv.get_vulnerability_level(rasters_vuln[i].stem)
|
1197
1177
|
|
1198
|
-
|
1199
|
-
|
1200
|
-
|
1178
|
+
if locvuln == 1:
|
1179
|
+
logging.info("No need to apply the matrice, the vulnerability is 1 which is the lower value")
|
1180
|
+
continue
|
1181
|
+
|
1182
|
+
if rasters_vuln[i].with_suffix('.npz').exists():
|
1183
|
+
ij_npz = np.load(rasters_vuln[i].with_suffix('.npz'))
|
1184
|
+
ii = ij_npz['row']
|
1185
|
+
jj = ij_npz['col']
|
1186
|
+
# We use the jit
|
1187
|
+
update_arrays_jit_csr(ii, jj, locvuln, array_vuln)
|
1188
|
+
|
1189
|
+
else:
|
1190
|
+
ds = gdal.OpenEx(str(rasters_vuln[i]), open_options=["SPARSE_OK=TRUE"])
|
1191
|
+
tmp_vuln = ds.GetRasterBand(1).ReadAsArray()
|
1192
|
+
# We use the jit
|
1193
|
+
update_arrays_jit(tmp_vuln, array_vuln)
|
1201
1194
|
|
1202
1195
|
logging.info("Saving the computed vulnerability")
|
1203
1196
|
dst_filename= str(manager.SA_VULN)
|
1204
1197
|
y_pixels, x_pixels = array_vuln.shape # number of pixels in x
|
1205
1198
|
|
1206
1199
|
driver = gdal.GetDriverByName('GTiff')
|
1207
|
-
dataset = driver.Create(dst_filename,
|
1200
|
+
dataset = driver.Create(dst_filename,
|
1201
|
+
x_pixels, y_pixels,
|
1202
|
+
gdal.GDT_Byte,
|
1203
|
+
1,
|
1204
|
+
options=["COMPRESS=LZW"])
|
1205
|
+
|
1208
1206
|
dataset.GetRasterBand(1).WriteArray(array_vuln.astype(np.int8))
|
1209
1207
|
# follow code is adding GeoTranform and Projection
|
1210
1208
|
geotrans = ds.GetGeoTransform() # get GeoTranform from existed 'data0'
|
@@ -1214,11 +1212,85 @@ def compute_vulnerability(manager:Accept_Manager):
|
|
1214
1212
|
dataset.FlushCache()
|
1215
1213
|
dataset = None
|
1216
1214
|
|
1215
|
+
logging.info("Computed Vulnerability for the Study Area - Done")
|
1216
|
+
|
1217
|
+
def compute_code(manager:Accept_Manager):
|
1218
|
+
"""
|
1219
|
+
Compute the code for the Study Area
|
1220
|
+
|
1221
|
+
This function **will not modify** the data by the removed buildings/scenarios.
|
1222
|
+
|
1223
|
+
:param dirsnames: the Dirs_Names object from the calling function
|
1224
|
+
"""
|
1225
|
+
|
1226
|
+
vuln_csv = Vulnerability_csv(manager.VULNERABILITY_CSV)
|
1227
|
+
|
1228
|
+
rasters_code = manager.get_files_in_rasters_code()
|
1229
|
+
|
1230
|
+
logging.info("Number of files",len(rasters_code))
|
1231
|
+
|
1232
|
+
ds:gdal.Dataset = gdal.OpenEx(str(rasters_code[0]), gdal.GA_ReadOnly, open_options=["SPARSE_OK=TRUE"])
|
1233
|
+
|
1234
|
+
tmp_code = ds.GetRasterBand(1)
|
1235
|
+
|
1236
|
+
# REMARK: The XSize and YSize are the number of columns and rows
|
1237
|
+
col, row = tmp_code.XSize, tmp_code.YSize
|
1238
|
+
|
1239
|
+
logging.info("Computing Code")
|
1240
|
+
|
1241
|
+
array_code = np.ones((row, col), dtype=np.int8)
|
1242
|
+
|
1243
|
+
# Create a JIT function to update the arrays
|
1244
|
+
# Faster than the classical Python loop or Numpy
|
1245
|
+
@nb.jit(nopython=True, boundscheck=False, inline='always')
|
1246
|
+
def update_arrays_jit(tmp_code, loccode, array_code):
|
1247
|
+
for i in range(tmp_code.shape[0]):
|
1248
|
+
for j in range(tmp_code.shape[1]):
|
1249
|
+
if tmp_code[i, j] >= array_code[i, j]:
|
1250
|
+
array_code[i, j] = loccode
|
1251
|
+
|
1252
|
+
return array_code
|
1253
|
+
|
1254
|
+
@nb.jit(nopython=True, boundscheck=False, inline='always')
|
1255
|
+
def update_arrays_jit_csr(row, col, loccode, array_code):
|
1256
|
+
for k in range(len(row)-1):
|
1257
|
+
i = k
|
1258
|
+
j1 = row[k]
|
1259
|
+
j2 = row[k+1]
|
1260
|
+
for j in col[j1:j2]:
|
1261
|
+
if loccode >= array_code[i, j]:
|
1262
|
+
array_code[i, j] = loccode
|
1263
|
+
|
1264
|
+
return array_code
|
1265
|
+
|
1266
|
+
for i in tqdm(range(len(rasters_code)), 'Computing Code : '):
|
1267
|
+
logging.info("Computing layer {} / {}".format(i, len(rasters_code)))
|
1268
|
+
|
1269
|
+
loccode = vuln_csv.get_vulnerability_code(rasters_code[i].stem)
|
1270
|
+
|
1271
|
+
if rasters_code[i].with_suffix('.npz').exists():
|
1272
|
+
ij_npz = np.load(rasters_code[i].with_suffix('.npz'))
|
1273
|
+
ii = ij_npz['row']
|
1274
|
+
jj = ij_npz['col']
|
1275
|
+
# We use the jit
|
1276
|
+
update_arrays_jit_csr(ii, jj, loccode, array_code)
|
1277
|
+
|
1278
|
+
else:
|
1279
|
+
ds = gdal.OpenEx(str(rasters_code[i]), open_options=["SPARSE_OK=TRUE"])
|
1280
|
+
tmp_code = ds.GetRasterBand(1).ReadAsArray()
|
1281
|
+
# We use the jit
|
1282
|
+
update_arrays_jit(tmp_code, loccode, array_code)
|
1283
|
+
|
1217
1284
|
logging.info("Saving the computed codes")
|
1218
1285
|
dst_filename= str(manager.SA_CODE)
|
1219
1286
|
y_pixels, x_pixels = array_code.shape # number of pixels in x
|
1220
1287
|
driver = gdal.GetDriverByName('GTiff')
|
1221
|
-
dataset = driver.Create(dst_filename,
|
1288
|
+
dataset = driver.Create(dst_filename,
|
1289
|
+
x_pixels, y_pixels,
|
1290
|
+
gdal.GDT_Byte,
|
1291
|
+
1,
|
1292
|
+
options=["COMPRESS=LZW"])
|
1293
|
+
|
1222
1294
|
dataset.GetRasterBand(1).WriteArray(array_code.astype(np.int8))
|
1223
1295
|
# follow code is adding GeoTranform and Projection
|
1224
1296
|
geotrans = ds.GetGeoTransform() # get GeoTranform from existed 'data0'
|
@@ -1228,7 +1300,7 @@ def compute_vulnerability(manager:Accept_Manager):
|
|
1228
1300
|
dataset.FlushCache()
|
1229
1301
|
dataset = None
|
1230
1302
|
|
1231
|
-
logging.info("Computed
|
1303
|
+
logging.info("Computed Code for the Study Area - Done")
|
1232
1304
|
|
1233
1305
|
def compute_vulnerability4scenario(manager:Accept_Manager):
|
1234
1306
|
""" Compute the vulnerability for the scenario
|
@@ -1284,7 +1356,7 @@ def compute_vulnerability4scenario(manager:Accept_Manager):
|
|
1284
1356
|
dataset.FlushCache()
|
1285
1357
|
dataset = None
|
1286
1358
|
|
1287
|
-
logging.info("Computed Vulnerability for the scenario")
|
1359
|
+
logging.info("Computed Vulnerability and code for the scenario")
|
1288
1360
|
|
1289
1361
|
def match_vulnerability2sim(inRas:Path, outRas:Path, MODREC:Path):
|
1290
1362
|
"""
|
@@ -1375,7 +1447,7 @@ def compute_acceptability(manager:Accept_Manager,
|
|
1375
1447
|
dataset.FlushCache()
|
1376
1448
|
dataset = None
|
1377
1449
|
|
1378
|
-
def shp_to_raster(vector_fn:str, raster_fn:str, pixel_size:float = 1
|
1450
|
+
def shp_to_raster(vector_fn:str, raster_fn:str, pixel_size:float = 1., manager:Accept_Manager = None):
|
1379
1451
|
"""
|
1380
1452
|
Convert a vector layer to a raster tiff file.
|
1381
1453
|
|
@@ -1392,12 +1464,23 @@ def shp_to_raster(vector_fn:str, raster_fn:str, pixel_size:float = 1.):
|
|
1392
1464
|
# Force the input to be a string
|
1393
1465
|
vector_fn = str(vector_fn)
|
1394
1466
|
raster_fn = str(raster_fn)
|
1467
|
+
|
1468
|
+
if manager is None:
|
1469
|
+
extent_fn = vector_fn
|
1470
|
+
logging.warning("The extent file is not provided, the extent will be the same as the vector file")
|
1471
|
+
else:
|
1472
|
+
extent_fn = str(manager.SA)
|
1473
|
+
logging.info("The extent file is provided")
|
1395
1474
|
|
1396
1475
|
NoData_value = 0 # np.nan is not necessary a good idea
|
1476
|
+
|
1397
1477
|
# Open the data sources and read the extents
|
1398
|
-
source_ds = ogr.Open(vector_fn)
|
1478
|
+
source_ds:ogr.DataSource = ogr.Open(vector_fn)
|
1399
1479
|
source_layer = source_ds.GetLayer()
|
1400
|
-
|
1480
|
+
|
1481
|
+
extent_ds:ogr.DataSource = ogr.Open(extent_fn)
|
1482
|
+
extent_layer = extent_ds.GetLayer()
|
1483
|
+
x_min, x_max, y_min, y_max = extent_layer.GetExtent()
|
1401
1484
|
|
1402
1485
|
x_min = float(int(x_min))
|
1403
1486
|
x_max = float(np.ceil(x_max))
|
@@ -1408,8 +1491,12 @@ def shp_to_raster(vector_fn:str, raster_fn:str, pixel_size:float = 1.):
|
|
1408
1491
|
x_res = int((x_max - x_min) / pixel_size)
|
1409
1492
|
y_res = int((y_max - y_min) / pixel_size)
|
1410
1493
|
|
1411
|
-
target_ds = gdal.GetDriverByName('GTiff').Create(raster_fn,
|
1412
|
-
|
1494
|
+
target_ds = gdal.GetDriverByName('GTiff').Create(raster_fn,
|
1495
|
+
x_res, y_res,
|
1496
|
+
1,
|
1497
|
+
gdal.GDT_Byte,
|
1498
|
+
options=["COMPRESS=LZW",
|
1499
|
+
'SPARSE_OK=TRUE'])
|
1413
1500
|
|
1414
1501
|
target_ds.SetGeoTransform((x_min, pixel_size, 0, y_max, 0, -pixel_size))
|
1415
1502
|
srs = osr.SpatialReference()
|
@@ -1425,3 +1512,107 @@ def shp_to_raster(vector_fn:str, raster_fn:str, pixel_size:float = 1.):
|
|
1425
1512
|
options=["ALL_TOUCHED=TRUE"])
|
1426
1513
|
target_ds = None
|
1427
1514
|
vector_fn = raster_fn = None
|
1515
|
+
|
1516
|
+
def vector_to_raster(layer:str,
|
1517
|
+
manager:Accept_Manager,
|
1518
|
+
attribute:str,
|
1519
|
+
pixel_size:float,
|
1520
|
+
convert_to_sparse:bool = True):
|
1521
|
+
"""
|
1522
|
+
Convert a vector layer to a raster tiff file
|
1523
|
+
|
1524
|
+
FIXME: Test de vulerability value and return immedialty if it is 1 if attribute == "Vulne"
|
1525
|
+
|
1526
|
+
:param layer: the layer name in the GDB file
|
1527
|
+
:param vector_input: the path to the vector file
|
1528
|
+
:param extent: the path to the extent file
|
1529
|
+
:param attribute: the attribute to rasterize
|
1530
|
+
:param pixel_size: the pixel size of the raster
|
1531
|
+
|
1532
|
+
"""
|
1533
|
+
|
1534
|
+
layer = str(layer)
|
1535
|
+
|
1536
|
+
vector_input = str(manager.TMP_CODEVULNE / (layer + EXTENT))
|
1537
|
+
extent = str(manager.SA)
|
1538
|
+
attribute = str(attribute)
|
1539
|
+
pixel_size = float(pixel_size)
|
1540
|
+
|
1541
|
+
out_file = manager.TMP_RASTERS / attribute / (layer + ".tiff")
|
1542
|
+
|
1543
|
+
if out_file.exists():
|
1544
|
+
os.remove(out_file)
|
1545
|
+
|
1546
|
+
out_file = str(out_file)
|
1547
|
+
|
1548
|
+
NoData_value = 0
|
1549
|
+
|
1550
|
+
extent_ds:ogr.DataSource = ogr.Open(extent)
|
1551
|
+
extent_layer = extent_ds.GetLayer()
|
1552
|
+
|
1553
|
+
x_min, x_max, y_min, y_max = extent_layer.GetExtent()
|
1554
|
+
|
1555
|
+
x_min = float(int(x_min))
|
1556
|
+
x_max = float(np.ceil(x_max))
|
1557
|
+
y_min = float(int(y_min))
|
1558
|
+
y_max = float(np.ceil(y_max))
|
1559
|
+
|
1560
|
+
# Open the data sources and read the extents
|
1561
|
+
source_ds:ogr.DataSource = ogr.Open(vector_input)
|
1562
|
+
if source_ds is None:
|
1563
|
+
logging.error(f"Could not open the data source {layer}")
|
1564
|
+
return
|
1565
|
+
source_layer = source_ds.GetLayer()
|
1566
|
+
|
1567
|
+
# Create the destination data source
|
1568
|
+
x_res = int((x_max - x_min) / pixel_size)
|
1569
|
+
y_res = int((y_max - y_min) / pixel_size)
|
1570
|
+
target_ds:gdal.Driver = gdal.GetDriverByName('GTiff').Create(out_file,
|
1571
|
+
x_res, y_res, 1,
|
1572
|
+
gdal.GDT_Byte,
|
1573
|
+
options=["COMPRESS=DEFLATE",
|
1574
|
+
'SPARSE_OK=TRUE',])
|
1575
|
+
|
1576
|
+
target_ds.SetGeoTransform((x_min, pixel_size, 0, y_max, 0, -pixel_size))
|
1577
|
+
srs = osr.SpatialReference()
|
1578
|
+
srs.ImportFromEPSG(31370)
|
1579
|
+
target_ds.SetProjection(srs.ExportToWkt())
|
1580
|
+
|
1581
|
+
band = target_ds.GetRasterBand(1)
|
1582
|
+
band.SetNoDataValue(NoData_value)
|
1583
|
+
|
1584
|
+
# Rasterize the areas
|
1585
|
+
gdal.RasterizeLayer(target_ds, [1],
|
1586
|
+
source_layer,
|
1587
|
+
options=["ATTRIBUTE="+attribute,
|
1588
|
+
"ALL_TOUCHED=TRUE"])
|
1589
|
+
|
1590
|
+
if convert_to_sparse:
|
1591
|
+
SPARSITY_THRESHOLD = 0.02
|
1592
|
+
# Convert the raster to a npz containing the row and col of the non-null values
|
1593
|
+
array = band.ReadAsArray()
|
1594
|
+
ij = np.nonzero(array)
|
1595
|
+
|
1596
|
+
if len(ij[0]) < int(x_res * y_res * SPARSITY_THRESHOLD):
|
1597
|
+
i,j = convert_to_csr(ij[0], ij[1], y_res)
|
1598
|
+
np.savez_compressed(Path(out_file).with_suffix('.npz'), row=np.asarray(i, dtype=np.int32), col=np.asarray(j, dtype=np.int32))
|
1599
|
+
else:
|
1600
|
+
logging.info("The raster is not sparse enough to be converted to a CSR forma {}".format(layer))
|
1601
|
+
|
1602
|
+
target_ds = None
|
1603
|
+
|
1604
|
+
return 0
|
1605
|
+
|
1606
|
+
@nb.jit(nopython=True, boundscheck=False, inline='always')
|
1607
|
+
def convert_to_csr(i_indices, j_indices, num_rows):
|
1608
|
+
row_ptr = [0] * (num_rows + 1)
|
1609
|
+
col_idx = []
|
1610
|
+
|
1611
|
+
for i in range(len(i_indices)):
|
1612
|
+
row_ptr[i_indices[i] + 1] += 1
|
1613
|
+
col_idx.append(j_indices[i])
|
1614
|
+
|
1615
|
+
for i in range(1, len(row_ptr)):
|
1616
|
+
row_ptr[i] += row_ptr[i - 1]
|
1617
|
+
|
1618
|
+
return row_ptr, col_idx
|
wolfhece/apps/splashscreen.py
CHANGED
@@ -7,6 +7,13 @@ import wx
|
|
7
7
|
import time
|
8
8
|
from wx.adv import SplashScreen as SplashScreen,SPLASH_CENTRE_ON_SCREEN,SPLASH_TIMEOUT,Sound
|
9
9
|
|
10
|
+
# try:
|
11
|
+
# from ..libs import wolfogl
|
12
|
+
# except:
|
13
|
+
# print("** WolfOGL not found **")
|
14
|
+
# print("Without WolfOGL, the application will not work !")
|
15
|
+
# print("Please reinstall 'wolfhece' package -- pip install wolfhece")
|
16
|
+
|
10
17
|
class WolfLauncher(SplashScreen):
|
11
18
|
"""
|
12
19
|
Wolf Splashcreen
|
@@ -15,10 +22,17 @@ class WolfLauncher(SplashScreen):
|
|
15
22
|
done = False
|
16
23
|
|
17
24
|
def __init__(self, parent=None, play_sound=True):
|
18
|
-
|
25
|
+
|
19
26
|
if self.done:
|
20
27
|
return
|
21
28
|
|
29
|
+
# try:
|
30
|
+
# wolfogl.init()
|
31
|
+
# except Exception as e:
|
32
|
+
# print("Error initializing WolfOGL -- We must stop here !")
|
33
|
+
# print("Please reinstall 'wolfhece' package -- pip install wolfhece")
|
34
|
+
# exit()
|
35
|
+
|
22
36
|
mydir=dirname(__file__)
|
23
37
|
mybitmap = wx.Bitmap(name=join(mydir,".\\WolfPython2.png"), type=wx.BITMAP_TYPE_PNG)
|
24
38
|
|