@@ -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
0 commit comments