def Call_SAGA_Module(fDEM): # pass your input file(s) here # ------------------------------------ # initialize input dataset(s) dem = saga_api.SG_Get_Data_Manager().Add_Grid(unicode(fDEM)) if dem == None or dem.is_Valid() == 0: print 'ERROR: loading grid [' + fDEM + ']' return 0 # ------------------------------------ # initialize output dataset(s) outgrid = saga_api.SG_Get_Data_Manager().Add_Grid(dem.Get_System()) # ------------------------------------ # call module: Angular Distance Weighted Module = saga_api.SG_Get_Module_Library_Manager().Get_Module( 'grid_gridding', '7') Parms = Module.Get_Parameters() # default parameter list Parms.Get(unicode('SHAPES')).Set_Value( use_variable_of_dataset_here) # input NOT optional shapes Parms.Get(unicode('FIELD')).Set_Value(0) Parms.Get(unicode('TARGET_DEFINITION')).Set_Value(0) Parms.Get(unicode('TARGET_USER_XMIN')).Set_Value(0.000000) Parms.Get(unicode('TARGET_USER_XMAX')).Set_Value(100.000000) Parms.Get(unicode('TARGET_USER_YMIN')).Set_Value(0.000000) Parms.Get(unicode('TARGET_USER_YMAX')).Set_Value(100.000000) Parms.Get(unicode('TARGET_USER_SIZE')).Set_Value(1.000000) Parms.Get(unicode('TARGET_USER_COLS')).Set_Value(100) Parms.Get(unicode('TARGET_USER_ROWS')).Set_Value(100) Parms.Get(unicode('TARGET_USER_FITS')).Set_Value(1) Parms.Get(unicode('SEARCH_RANGE')).Set_Value(0) Parms.Get(unicode('SEARCH_RADIUS')).Set_Value(1000.000000) Parms.Get(unicode('SEARCH_POINTS_ALL')).Set_Value(0) Parms.Get(unicode('SEARCH_POINTS_MIN')).Set_Value(1) Parms.Get(unicode('SEARCH_POINTS_MAX')).Set_Value(20) Parms.Get(unicode('SEARCH_DIRECTION')).Set_Value(0) Parms.Get(unicode('DW_WEIGHTING')).Set_Value(1) Parms.Get(unicode('DW_IDW_POWER')).Set_Value(2.000000) Parms.Get(unicode('DW_IDW_OFFSET')).Set_Value(0) if Module.Execute() == 0: print 'Module execution failed!' return 0 print print 'The module has been executed.' print 'Now you would like to save your output datasets, please edit the script to do so.' return 0 # remove this line once you have edited the script # ------------------------------------ # save results path = os.path.split(fDEM)[0] if path == '': path = './' outgrid.Save(saga_api.CSG_String(path + '/outgrid')) print print 'Module successfully executed!' return 1
def grid_contour(fGrid, fLines): Grid = saga_api.SG_Get_Data_Manager().Add_Grid(unicode(fGrid)) if Grid == None or Grid.is_Valid() == 0: print 'ERROR: loading grid [' + fGrid + ']' return 0 Lines = saga_api.SG_Get_Data_Manager().Add_Shapes() # ------------------------------------ if os.name == 'nt': # Windows saga_api.SG_Get_Module_Library_Manager().Add_Library( os.environ['SAGA_32'] + '/modules/shapes_grid.dll') else: # Linux saga_api.SG_Get_Module_Library_Manager().Add_Library( os.environ['SAGA_MLB'] + '/libshapes_grid.so') m = saga_api.SG_Get_Module_Library_Manager().Get_Module( saga_api.CSG_String('shapes_grid'), 5) # 'Contour Lines from Grid' p = m.Get_Parameters() p.Get_Grid_System().Assign( Grid.Get_System()) # module needs to use conformant grid system! p(saga_api.CSG_String('GRID')).Set_Value(Grid) p(saga_api.CSG_String('CONTOUR')).Set_Value(Lines) p(saga_api.CSG_String('ZSTEP')).Set_Value(25.0) if m.Execute() == 0: print 'ERROR: executing module [' + m.Get_Name().c_str() + ']' return 0 # ------------------------------------ Lines.Save(saga_api.CSG_String(fLines)) print 'success' return 1
def xyz2shp(fTable): table = saga_api.SG_Get_Data_Manager().Add_Table() if table.Create(saga_api.CSG_String(fTable)) == 0: table.Add_Field(saga_api.CSG_String('X'), saga_api.SG_DATATYPE_Double) table.Add_Field(saga_api.CSG_String('Y'), saga_api.SG_DATATYPE_Double) table.Add_Field(saga_api.CSG_String('Z'), saga_api.SG_DATATYPE_Double) rec = table.Add_Record() rec.Set_Value(0, 0) rec.Set_Value(1, 0) rec.Set_Value(2, 2) rec = table.Add_Record() rec.Set_Value(0, 0) rec.Set_Value(1, 1) rec.Set_Value(2, 2) rec = table.Add_Record() rec.Set_Value(0, 1) rec.Set_Value(1, 1) rec.Set_Value(2, 1) rec = table.Add_Record() rec.Set_Value(0, 1) rec.Set_Value(1, 0) rec.Set_Value(2, 1) points = saga_api.SG_Get_Data_Manager().Add_Shapes( saga_api.SHAPE_TYPE_Point) # ------------------------------------ if os.name == 'nt': # Windows saga_api.SG_Get_Tool_Library_Manager().Add_Library( os.environ['SAGA_32'] + '/tools/shapes_points.dll') else: # Linux saga_api.SG_Get_Tool_Library_Manager().Add_Library( os.environ['SAGA_MLB'] + '/libshapes_points.so') m = saga_api.SG_Get_Tool_Library_Manager().Get_Tool( saga_api.CSG_String('shapes_points'), 0) # 'Convert Table to Points' p = m.Get_Parameters() p(saga_api.CSG_String('TABLE')).Set_Value(table) p(saga_api.CSG_String('POINTS')).Set_Value(points) p(saga_api.CSG_String('X')).Set_Value(0) p(saga_api.CSG_String('Y')).Set_Value(1) if m.Execute() == 0: print 'ERROR: executing tool [' + m.Get_Name().c_str() + ']' return 0 # ------------------------------------ points.Save(saga_api.CSG_String(fTable)) print 'success' return 1
def grid_contour(File): # ------------------------------------ Grid = saga_api.SG_Get_Data_Manager().Add_Grid(File) if Grid == None or Grid.is_Valid() == False: print('Error: loading grid [' + File + ']') return False # ------------------------------------ # 'Contour Lines from Grid' Tool = saga_api.SG_Get_Tool_Library_Manager().Get_Tool('shapes_grid', 5) Tool.Set_Parameter('GRID', Grid) Tool.Set_Parameter('ZSTEP', Grid.Get_Range() / 10.) if Tool.Execute() == False: print('Error: executing tool [' + Tool.Get_Name().c_str() + ']') return False File = saga_api.CSG_String(File) saga_api.SG_File_Set_Extension(File, saga_api.CSG_String('geojson')) Tool.Get_Parameter('CONTOUR').asShapes().Save(File) # ------------------------------------ print('Success') return True
def shp2xyz(fshp, fxyz): shp = saga_api.SG_Get_Data_Manager().Add_Shapes(unicode(fshp)) if shp == None or shp.is_Valid() == 0: print 'ERROR: loading shapes [' + fshp + ']' return 0 # ------------------------------------ if os.name == 'nt': # Windows saga_api.SG_Get_Tool_Library_Manager().Add_Library( os.environ['SAGA_32'] + '/tools/io_shapes.dll') else: # Linux saga_api.SG_Get_Tool_Library_Manager().Add_Library( os.environ['SAGA_MLB'] + '/libio_shapes.so') m = saga_api.SG_Get_Tool_Library_Manager().Get_Tool( saga_api.CSG_String('io_shapes'), 2) # 'Export Shapes to XYZ' p = m.Get_Parameters() p(saga_api.CSG_String('POINTS')).Set_Value(shp) p(saga_api.CSG_String('FILENAME')).Set_Value(saga_api.CSG_String(fxyz)) if m.Execute() == 0: print 'ERROR: executing tool [' + m.Get_Name().c_str() + ']' return 0 # ------------------------------------ print 'success' return 1
def grid_contour(fGrid, fLines): Grid = saga_api.SG_Get_Data_Manager().Add_Grid(unicode(fGrid)) if Grid == None or Grid.is_Valid() == 0: print 'ERROR: loading grid [' + fGrid + ']' return 0 # ------------------------------------ if os.name == 'nt': # Windows saga_api.SG_Get_Tool_Library_Manager().Add_Library( os.environ['SAGA_32'] + '/tools/shapes_grid.dll') else: # Linux saga_api.SG_Get_Tool_Library_Manager().Add_Library( os.environ['SAGA_MLB'] + '/libshapes_grid.so') zStep = Grid.Get_Range() / 10 m = saga_api.SG_Get_Tool_Library_Manager().Get_Tool( saga_api.CSG_String('shapes_grid'), 5) # 'Contour Lines from Grid' p = m.Get_Parameters() p(saga_api.CSG_String('GRID')).Set_Value(Grid) p(saga_api.CSG_String('ZSTEP')).Set_Value(zStep) if m.Execute() == 0: print 'ERROR: executing tool [' + m.Get_Name().c_str() + ']' return 0 # ------------------------------------ Lines = p(saga_api.CSG_String('CONTOUR')).asShapes() Lines.Save(saga_api.CSG_String(fLines)) print 'success' return 1
def run_xyz2shp(File): # ----------------------------------- Table = saga_api.SG_Get_Data_Manager().Add_Table(File) if Table != None and Table.is_Valid() == True and Table.Get_Count() > 0: print('Table loaded: ' + File) else: Table = saga_api.SG_Get_Data_Manager().Add_Table() Table.Add_Field(saga_api.CSG_String('X'), saga_api.SG_DATATYPE_Double) Table.Add_Field(saga_api.CSG_String('Y'), saga_api.SG_DATATYPE_Double) Table.Add_Field(saga_api.CSG_String('Z'), saga_api.SG_DATATYPE_Double) Random = saga_api.CSG_Random() for i in range(0, 100): Record = Table.Add_Record() Record.Set_Value(0, Random.Get_Gaussian(0, 100)) Record.Set_Value(1, Random.Get_Gaussian(0, 100)) Record.Set_Value(2, Random.Get_Gaussian(0, 100)) print('Number of records: ' + str(Table.Get_Count())) # ----------------------------------- # 'Convert Table to Points' Tool = saga_api.SG_Get_Tool_Library_Manager().Get_Tool('shapes_points', 0) Tool.Set_Parameter('TABLE', Table) Tool.Set_Parameter( 'X', 0) Tool.Set_Parameter( 'Y', 1) Tool.Set_Parameter( 'Z', 2) if Tool.Execute() == False: print('Error: executing tool [' + Tool.Get_Name().c_str() + ']') return False if Tool.Get_Parameter('POINTS').asShapes().Save(os.path.split(File)[0] + '/points.geojson') == False: print('Error: saving points') return False # ------------------------------------ print('Success') return True
def grid_tpi(fDEM): # ------------------------------------ # initializations dem = saga_api.SG_Get_Data_Manager().Add_Grid(unicode(fDEM)) if dem == None or dem.is_Valid() == 0: print 'ERROR: loading grid [' + fDEM + ']' return 0 print 'grid file [' + fDEM + '] has been loaded' path = os.path.split(fDEM)[0] # if path == '': # path = './' landforms = saga_api.SG_Get_Data_Manager().Add_Grid(dem.Get_System()) # ------------------------------------ # 'TPI Based Landform Classification' m = saga_api.SG_Get_Tool_Library_Manager().Get_Tool( saga_api.CSG_String('ta_morphometry'), 19) p = m.Get_Parameters() p.Get_Grid_System().Assign( dem.Get_System()) # grid tool needs to use conformant grid system! p(saga_api.CSG_String('DEM')).Set_Value(dem) p(saga_api.CSG_String('LANDFORMS')).Set_Value(landforms) p(saga_api.CSG_String('RADIUS_A')).asRange().Set_Range(0, 100) p(saga_api.CSG_String('RADIUS_B')).asRange().Set_Range(0, 1000) p(saga_api.CSG_String('DW_WEIGHTING')).Set_Value(0) if m.Execute() == 0: print 'ERROR: executing tool [' + m.Get_Name().c_str() + ']' return 0 landforms.Save(saga_api.CSG_String(path + '/landforms' + '.sg-grd-z'), 0) # ------------------------------------ print 'success' return 1
def morphometry(fDEM): # ------------------------------------ # initializations dem = saga_api.SG_Get_Data_Manager().Add_Grid(unicode(fDEM)) if dem == None or dem.is_Valid() == 0: print 'ERROR: loading grid [' + fDEM + ']' return 0 path = os.path.split(fDEM)[0] if path == '': path = './' slope = saga_api.SG_Get_Data_Manager().Add_Grid(dem.Get_System()) aspect = saga_api.SG_Get_Data_Manager().Add_Grid(dem.Get_System()) hcurv = saga_api.SG_Get_Data_Manager().Add_Grid(dem.Get_System()) vcurv = saga_api.SG_Get_Data_Manager().Add_Grid(dem.Get_System()) ccurv = saga_api.SG_Get_Data_Manager().Add_Grid(dem.Get_System()) # ------------------------------------ # 'Slope, Aspect, Curvature' m = saga_api.SG_Get_Module_Library_Manager().Get_Module(saga_api.CSG_String('ta_morphometry'), 0) p = m.Get_Parameters() p.Get_Grid_System().Assign(dem.Get_System()) # grid module needs to use conformant grid system! p(saga_api.CSG_String('ELEVATION')).Set_Value(dem) p(saga_api.CSG_String('SLOPE' )).Set_Value(slope) p(saga_api.CSG_String('ASPECT' )).Set_Value(aspect) p(saga_api.CSG_String('C_CROS' )).Set_Value(hcurv) p(saga_api.CSG_String('C_LONG' )).Set_Value(vcurv) if m.Execute() == 0: print 'ERROR: executing module [' + m.Get_Name().c_str() + ']' return 0 slope .Save(saga_api.CSG_String(path + '/slope' )) aspect.Save(saga_api.CSG_String(path + '/aspect')) hcurv .Save(saga_api.CSG_String(path + '/hcurv' )) vcurv .Save(saga_api.CSG_String(path + '/vcurv' )) # ------------------------------------ # 'Curvature Classification' m = saga_api.SG_Get_Module_Library_Manager().Get_Module(saga_api.CSG_String('ta_morphometry'), 4) p = m.Get_Parameters() p.Get_Grid_System().Assign(dem.Get_System()) # grid module needs to use conformant grid system! p(saga_api.CSG_String('DEM' )).Set_Value(dem) p(saga_api.CSG_String('CLASS' )).Set_Value(ccurv) if m.Execute() == 0: print 'ERROR: executing module [' + m.Get_Name().c_str() + ']' return 0 ccurv .Save(saga_api.CSG_String(path + '/ccurv' )) # ------------------------------------ print 'success' return 1
def run_morphometry(File): Grid = saga_api.SG_Get_Data_Manager().Add(File) if Grid == None or Grid.is_Valid() == False: print('Error: loading grid [' + File + ']') return False Path = os.path.split(File)[0] # ----------------------------------- # 'Slope, Aspect, Curvature' Tool = saga_api.SG_Get_Tool_Library_Manager().Get_Tool('ta_morphometry', 0) Tool.Get_Parameters().Reset_Grid_System() Tool.Set_Parameter('ELEVATION', Grid) Tool.Set_Parameter( 'C_CROS', saga_api.SG_Get_Create_Pointer()) # optional grid output Tool.Set_Parameter( 'C_LONG', saga_api.SG_Get_Create_Pointer()) # optional grid output if Tool.Execute() == False: print('Error: executing tool [' + Tool.Get_Name().c_str() + ']') return False Tool.Get_Parameter('SLOPE').asGrid().Save(Path + '/slope' + '.sg-grd-z') Tool.Get_Parameter('ASPECT').asGrid().Save(Path + '/aspect' + '.sg-grd-z') Tool.Get_Parameter('C_CROS').asGrid().Save(Path + '/hcurv' + '.sg-grd-z') Tool.Get_Parameter('C_LONG').asGrid().Save(Path + '/vcurv' + '.sg-grd-z') # ----------------------------------- # 'Curvature Classification' Tool = saga_api.SG_Get_Tool_Library_Manager().Get_Tool('ta_morphometry', 4) Tool.Get_Parameters().Reset_Grid_System() Tool.Set_Parameter('DEM', Grid) if Tool.Execute() == False: print('Error: executing tool [' + Tool.Get_Name().c_str() + ']') return False Tool.Get_Parameter('CLASSES').asGrid().Save(Path + '/ccurv' + '.sg-grd-z') # ----------------------------------- print('Success') return True
def grid_tpi(File): # ------------------------------------ Grid = saga_api.SG_Get_Data_Manager().Add_Grid(File) if Grid == None or Grid.is_Valid() == False: print('Error: loading grid [' + File + ']') return False # ------------------------------------ # 'TPI Based Landform Classification' Tool = saga_api.SG_Get_Tool_Library_Manager().Get_Tool( saga_api.CSG_String('ta_morphometry'), 19) Tool.Get_Parameters().Reset_Grid_System( ) # grid tool needs to use conformant grid system! Tool.Set_Parameter('DEM', Grid) Tool.Set_Parameter('DW_WEIGHTING', 0) Tool.Get_Parameter('RADIUS_A').asRange().Set_Range( 0., 2. * Grid.Get_Cellsize()) Tool.Get_Parameter('RADIUS_B').asRange().Set_Range( 0., 10. * Grid.Get_Cellsize()) if Tool.Execute() == False: print('Error: executing tool [' + Tool.Get_Name().c_str() + ']') return False Grid = Tool.Get_Parameter('LANDFORMS').asGrid() File = os.path.split(File)[0] + '/landforms.sg-grd-z' if Grid.Save(File) == False: print('Error: saving grid [' + File + ']') return False # ------------------------------------ print('Success') return True
def Call_SAGA_Module(fDEM): # pass your input file(s) here # ------------------------------------ # initialize input dataset(s) dem = saga_api.SG_Get_Data_Manager().Add_Grid(unicode(fDEM)) if dem == None or dem.is_Valid() == 0: print 'ERROR: loading grid [' + fDEM + ']' return 0 # ------------------------------------ # initialize output dataset(s) outgrid = saga_api.SG_Get_Data_Manager().Add_Grid(dem.Get_System()) # ------------------------------------ # call module: Regression Kriging Module = saga_api.SG_Get_Module_Library_Manager().Get_Module( 'statistics_kriging', '3') Module.Get_Parameters().Get_Grid_System().Assign(dem.Get_System()) Parms = Module.Get_Parameters() # default parameter list Parms.Get(unicode('POINTS')).Set_Value( use_variable_of_dataset_here) # input NOT optional shapes Parms.Get(unicode('FIELD')).Set_Value(0) Parms.Get(unicode('PREDICTORS')).Set_Value( use_variable_of_dataset_here) # data object list Parms.Get(unicode('REGRESSION')).Set_Value( use_variable_of_dataset_here) # output NOT optional grid Parms.Get(unicode('PREDICTION')).Set_Value( use_variable_of_dataset_here) # output NOT optional grid Parms.Get(unicode('RESIDUALS')).Set_Value( use_variable_of_dataset_here) # output optional grid Parms.Get(unicode('VARIANCE')).Set_Value( use_variable_of_dataset_here) # output optional grid Parms.Get(unicode('TQUALITY')).Set_Value(0) Parms.Get(unicode('LOG')).Set_Value(0) Parms.Get(unicode('BLOCK')).Set_Value(0) Parms.Get(unicode('DBLOCK')).Set_Value(100.000000) Parms.Get(unicode('INFO_COEFF')).Set_Value( use_variable_of_dataset_here) # output optional table Parms.Get(unicode('INFO_MODEL')).Set_Value( use_variable_of_dataset_here) # output optional table Parms.Get(unicode('INFO_STEPS')).Set_Value( use_variable_of_dataset_here) # output optional table Parms.Get(unicode('COORD_X')).Set_Value(0) Parms.Get(unicode('COORD_Y')).Set_Value(0) Parms.Get(unicode('INTERCEPT')).Set_Value(1) Parms.Get(unicode('METHOD')).Set_Value(3) Parms.Get(unicode('P_VALUE')).Set_Value(5.000000) Parms.Get(unicode('INTERPOL')).Set_Value(4) Parms.Get(unicode('SEARCH_RANGE')).Set_Value(0) Parms.Get(unicode('SEARCH_RADIUS')).Set_Value(1000.000000) Parms.Get(unicode('SEARCH_POINTS_ALL')).Set_Value(0) Parms.Get(unicode('SEARCH_POINTS_MIN')).Set_Value(16) Parms.Get(unicode('SEARCH_POINTS_MAX')).Set_Value(20) Parms.Get(unicode('SEARCH_DIRECTION')).Set_Value(0) if Module.Execute() == 0: print 'Module execution failed!' return 0 print print 'The module has been executed.' print 'Now you would like to save your output datasets, please edit the script to do so.' return 0 # remove this line once you have edited the script # ------------------------------------ # save results path = os.path.split(fDEM)[0] if path == '': path = './' outgrid.Save(saga_api.CSG_String(path + '/outgrid')) print print 'Module successfully executed!' return 1
##################################### #### MULTIPATH FLOW ACCUMULATION #### ##################################### # tools importieren import saga_api import numpy as np DSM = saga_api.SG_Get_Data_Manager().Add_Grid( "C:/Users/Anwender/Documents/ABStudium/Python/Kursunterlagen/9_Projekte/MulipathFlowAcc/Praxmar_DGM1m.sgrd" ) #Metadaten Cellsize = DSM.Get_Cellsize() LLX = DSM.Get_XMin() LLY = DSM.Get_YMin() NX = DSM.Get_NX() NY = DSM.Get_NY() Nodata = DSM.Get_NoData_Value() #print(Nodata) print("Reading Raster...") # Accumulation Array erstellen DSMArray = np.empty((NY, NX)) AccumulationArray = np.empty((NY, NX)) HeightList = [] # Liste mit Höhenangabe (z-Wert) für jedes Pixel for gy in range(NY):