@@ -656,7 +656,7 @@ def runStatic(ncFile, levels, epsgOut, pathOut, grassVer, pathRasFiles, rasterF
656656 same path of the extracted shape file (pathout).
657657 createGrassLocation: boolean. DEFAULT True
658658 True for creating a new location and loading DEMs, false to use an existing location with DEMs already imported
659- createLocMethod: str. DEFAULT 'from_raster'
659+ createLocMethod: str. DEFAULT 'from_raster'
660660 Two options "from_epsg" (default) or "from_raster" otherwise an error will be thrown.
661661 attrCol: str. DEFAULT 'avgVal'
662662 name of the attribute column
@@ -762,7 +762,7 @@ def runStatic(ncFile, levels, epsgOut, pathOut, grassVer, pathRasFiles, rasterF
762762 logger .info (f'Output files saved on: { pathaux } ' ) # Changed
763763
764764def meshRepLen2raster (fort14 , epsgIn , epsgOut , pathOut , grassVer , pathRasFiles , rasterFiles , subDomain = None ,
765- nameGrassLocation = None , createGrassLocation = True , createLocMethod = 'from_raster' ):
765+ nameGrassLocation = None , createGrassLocation = True , createLocMethod = 'from_raster' , exportDEM = False ):
766766 ''' Function to rasterize mesh shapefile created from the fort.14 file
767767 Parameters
768768 fort14: str
@@ -786,10 +786,10 @@ def meshRepLen2raster(fort14, epsgIn, epsgOut, pathOut, grassVer, pathRasFiles,
786786 same path of the extracted shape file (pathout).
787787 createGrassLocation: boolean. DEFAULT True
788788 True for creating a new location and loading DEMs, false to use an existing location with DEMs already imported
789- createLocMethod: str. DEFAULT 'from_raster'
790- Two options "from_epsg" (default) or "from_raster" otherwise an error will be thrown.
789+ createLocMethod: str. DEFAULT 'from_raster'
790+ Two options "from_epsg" (default) or "from_raster" otherwise an error will be thrown.
791791 Returns
792- NOne
792+ None
793793 '''
794794 ## create gdf from fort14 file with elements as geometries
795795 t0 = time .time ()
@@ -829,6 +829,9 @@ def meshRepLen2raster(fort14, epsgIn, epsgOut, pathOut, grassVer, pathRasFiles,
829829 ## setup grass env
830830 setGrassEnv (grassVer , pathGrassLocation , createGrassLocation , gs , gsetup ,
831831 pathRasFiles , rasterFiles , createLocMethod , epsgOut )
832+ if exportDEM == True :
833+ gs .run_command ('r.out.gdal' , input = 'dem' , flags = 'cm' , format = 'GTiff' , nodata = - 9999 ,
834+ output = os .path .join (os .path .dirname (pathOut ), 'downscaling_dem.tif' ))
832835 ## get minimum area
833836 minArea = gdfMesh .elemArea .min ()
834837 t0 = time .time ()
@@ -907,3 +910,46 @@ def reprojectRas(filein, pathout, epsgOut=None, res='same'):
907910 rasOut .rio .to_raster (os .path .join (pathout , bname + f'_epsg{ epsgOut } _res{ res } .tif' ), driver = "COG" ) # Changed
908911 logger .info ('Wrote ' + bname + ' to COG' ) # Changed
909912
913+ def mergeDEMs (grassVer , pathRasFiles , rasterFiles , pathOut , epsgOut ,
914+ nameGrassLocation = None , createGrassLocation = True , createLocMethod = 'from_raster' ):
915+ ''' Function to rasterize mesh shapefile created from the fort.14 file
916+ Parameters
917+ grassVer: float
918+ Version of the grass software (The code was writen for v8.0).
919+ pathRasters: str
920+ path of the raster files
921+ rasterFiles: list or str
922+ name(s) of the raster file(s). If 'all' is input, all files in pathRasters are used
923+ pathOut: str
924+ full path of the output DEM
925+ nameGrassLocation: str. DEFAULT None
926+ path and name of the grass location. If None the grass location will be called 'grassLoc' and save in the
927+ same path of the extracted shape file (pathout).
928+ createGrassLocation: boolean. DEFAULT True
929+ True for creating a new location and loading DEMs, false to use an existing location with DEMs already imported
930+ createLocMethod: str. DEFAULT 'from_raster'
931+ Two options "from_epsg" (default) or "from_raster" otherwipathOut, epsgOut,se an error will be thrown.
932+ Returns
933+ None
934+ '''
935+ pathaux = os .path .dirname (pathOut )
936+ ## add grass to the environment variables
937+ grassEnvVar (grassVer )
938+ ## import grass
939+ import grass .script as gs
940+ import grass .script .setup as gsetup
941+ ## grass location path
942+ if nameGrassLocation == None :
943+ pathGrassLocation = os .path .join (pathaux , 'grassLoc' )
944+ else :
945+ pathGrassLocation = os .path .join (pathaux , nameGrassLocation )
946+
947+ if rasterFiles == 'all' :
948+ rasterFiles = os .listdir (pathRasFiles )
949+
950+ ## setup grass env
951+ setGrassEnv (grassVer , pathGrassLocation , createGrassLocation , gs , gsetup ,
952+ pathRasFiles , rasterFiles , createLocMethod , epsgOut )
953+
954+ gs .run_command ('r.out.gdal' , input = 'dem' , flags = 'cm' , format = 'GTiff' , nodata = - 9999 ,
955+ output = pathOut )
0 commit comments