Skip to content

Commit 934b54f

Browse files
authored
Merge pull request #38 from ClarkCGA/yayao
Fix replace_ref_system function when input is tif and output is rst
2 parents 78bfb4f + 07e1a00 commit 934b54f

3 files changed

Lines changed: 102 additions & 34 deletions

File tree

allocation_tool.py

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -363,28 +363,49 @@ def replace_ref_system(self, in_fn, out_fn):
363363
:param out_fn: rst raster file
364364
'''
365365
if out_fn.split('.')[-1] == 'rst':
366-
read_file_name, _ = os.path.splitext(in_fn)
367-
write_file_name, _ = os.path.splitext(out_fn)
368-
temp_file_path = 'rdc_temp.rdc'
366+
if in_fn.split('.')[-1] == 'rst':
367+
read_file_name, _ = os.path.splitext(in_fn)
368+
write_file_name, _ = os.path.splitext(out_fn)
369+
temp_file_path = 'rdc_temp.rdc'
369370

370-
with open(read_file_name + '.rdc', 'r') as read_file:
371-
for line in read_file:
372-
if line.startswith("ref. system :"):
373-
correct_name=line
374-
break
371+
with open(read_file_name + '.rdc', 'r') as read_file:
372+
for line in read_file:
373+
if line.startswith("ref. system :"):
374+
correct_name = line
375+
break
376+
377+
if correct_name:
378+
with open(write_file_name + '.rdc', 'r') as read_file, open(temp_file_path, 'w') as write_file:
379+
for line in read_file:
380+
if line.startswith("ref. system :"):
381+
write_file.write(correct_name)
382+
else:
383+
write_file.write(line)
384+
385+
# Move the temp file to replace the original
386+
shutil.move(temp_file_path, write_file_name + '.rdc')
387+
388+
elif in_fn.split('.')[-1] == 'tif':
389+
# Read projection information from the .tif file using GDAL
390+
dataset = gdal.Open(in_fn)
391+
projection = dataset.GetProjection()
392+
dataset = None
393+
394+
# Extract the reference system name from the wkt projection
395+
ref_system_name = projection.split('PROJCS["')[1].split('"')[0]
396+
397+
write_file_name, _ = os.path.splitext(out_fn)
398+
temp_file_path = 'rdc_temp.rdc'
375399

376-
if correct_name:
377400
with open(write_file_name + '.rdc', 'r') as read_file, open(temp_file_path, 'w') as write_file:
378401
for line in read_file:
379402
if line.startswith("ref. system :"):
380-
write_file.write(correct_name)
403+
write_file.write(f"ref. system : {ref_system_name}\n")
381404
else:
382405
write_file.write(line)
383406

384-
# Move the temp file to replace the original
385407
shutil.move(temp_file_path, write_file_name + '.rdc')
386408

387-
388409
def execute_workflow_fit(self, directory,risk30_hrp,municipality, deforestation_hrp, csv_name, out_fn1, out_fn2):
389410
'''
390411
Create workflow function for CAL and HRP

model_evaluation.py

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,25 +77,47 @@ def replace_ref_system(self, in_fn, out_fn):
7777
:param out_fn: rst raster file
7878
'''
7979
if out_fn.split('.')[-1] == 'rst':
80-
read_file_name, _ = os.path.splitext(in_fn)
81-
write_file_name, _ = os.path.splitext(out_fn)
82-
temp_file_path = 'rdc_temp.rdc'
80+
if in_fn.split('.')[-1] == 'rst':
81+
read_file_name, _ = os.path.splitext(in_fn)
82+
write_file_name, _ = os.path.splitext(out_fn)
83+
temp_file_path = 'rdc_temp.rdc'
8384

84-
with open(read_file_name + '.rdc', 'r') as read_file:
85-
for line in read_file:
86-
if line.startswith("ref. system :"):
87-
correct_name=line
88-
break
85+
with open(read_file_name + '.rdc', 'r') as read_file:
86+
for line in read_file:
87+
if line.startswith("ref. system :"):
88+
correct_name = line
89+
break
90+
91+
if correct_name:
92+
with open(write_file_name + '.rdc', 'r') as read_file, open(temp_file_path, 'w') as write_file:
93+
for line in read_file:
94+
if line.startswith("ref. system :"):
95+
write_file.write(correct_name)
96+
else:
97+
write_file.write(line)
98+
99+
# Move the temp file to replace the original
100+
shutil.move(temp_file_path, write_file_name + '.rdc')
101+
102+
elif in_fn.split('.')[-1] == 'tif':
103+
# Read projection information from the .tif file using GDAL
104+
dataset = gdal.Open(in_fn)
105+
projection = dataset.GetProjection()
106+
dataset = None
107+
108+
# Extract the reference system name from the wkt projection
109+
ref_system_name = projection.split('PROJCS["')[1].split('"')[0]
110+
111+
write_file_name, _ = os.path.splitext(out_fn)
112+
temp_file_path = 'rdc_temp.rdc'
89113

90-
if correct_name:
91114
with open(write_file_name + '.rdc', 'r') as read_file, open(temp_file_path, 'w') as write_file:
92115
for line in read_file:
93116
if line.startswith("ref. system :"):
94-
write_file.write(correct_name)
117+
write_file.write(f"ref. system : {ref_system_name}\n")
95118
else:
96119
write_file.write(line)
97120

98-
# Move the temp file to replace the original
99121
shutil.move(temp_file_path, write_file_name + '.rdc')
100122

101123
def replace_legend(self, out_fn):

vulnerability_map.py

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -247,24 +247,49 @@ def replace_ref_system(self, in_fn, out_fn):
247247
:param out_fn: rst raster file
248248
'''
249249
if out_fn.split('.')[-1] == 'rst':
250-
read_file_name, _ = os.path.splitext(in_fn)
251-
write_file_name, _ = os.path.splitext(out_fn)
252-
temp_file_path = 'rdc_temp.rdc'
250+
if in_fn.split('.')[-1] == 'rst':
251+
read_file_name, _ = os.path.splitext(in_fn)
252+
write_file_name, _ = os.path.splitext(out_fn)
253+
temp_file_path = 'rdc_temp.rdc'
253254

254-
with open(read_file_name + '.rdc', 'r') as read_file:
255-
for line in read_file:
256-
if line.startswith("ref. system :"):
257-
correct_name=line
258-
break
255+
with open(read_file_name + '.rdc', 'r') as read_file:
256+
for line in read_file:
257+
if line.startswith("ref. system :"):
258+
correct_name = line
259+
break
260+
261+
if correct_name:
262+
with open(write_file_name + '.rdc', 'r') as read_file, open(temp_file_path, 'w') as write_file:
263+
for line in read_file:
264+
if line.startswith("ref. system :"):
265+
write_file.write(correct_name)
266+
else:
267+
write_file.write(line)
268+
269+
# Move the temp file to replace the original
270+
shutil.move(temp_file_path, write_file_name + '.rdc')
271+
self.progress_updated.emit(100)
272+
273+
elif in_fn.split('.')[-1] == 'tif':
274+
# Read projection information from the .tif file using GDAL
275+
dataset = gdal.Open(in_fn)
276+
projection = dataset.GetProjection()
277+
dataset = None
278+
279+
# Extract the reference system name from the wkt projection
280+
ref_system_name = projection.split('PROJCS["')[1].split('"')[0]
281+
282+
write_file_name, _ = os.path.splitext(out_fn)
283+
temp_file_path = 'rdc_temp.rdc'
259284

260-
if correct_name:
261285
with open(write_file_name + '.rdc', 'r') as read_file, open(temp_file_path, 'w') as write_file:
262286
for line in read_file:
263287
if line.startswith("ref. system :"):
264-
write_file.write(correct_name)
288+
write_file.write(f"ref. system : {ref_system_name}\n")
265289
else:
266290
write_file.write(line)
267291

268-
# Move the temp file to replace the original
269292
shutil.move(temp_file_path, write_file_name + '.rdc')
293+
self.progress_updated.emit(100)
294+
else:
270295
self.progress_updated.emit(100)

0 commit comments

Comments
 (0)