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 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 grid_create_dummy(fASC): ### load all module libraries from a directory at once: if os.name == 'nt': # Windows os.environ[ 'PATH'] = os.environ['PATH'] + ';' + os.environ['SAGA_32'] + '/dll' saga_api.SG_Get_Module_Library_Manager().Add_Directory( os.environ['SAGA_32'] + '/modules', 0) else: # Linux saga_api.SG_Get_Module_Library_Manager().Add_Directory( os.environ['SAGA_MLB'], 0) print '__________________' print 'number of loaded libraries: ' + str( saga_api.SG_Get_Module_Library_Manager().Get_Count()) #print saga_api.SG_Get_Module_Library_Manager().Get_Summary(saga_api.SG_SUMMARY_FMT_FLAT_NO_INTERACTIVE).c_str() print '__________________' ### create a dummy DEM: m = saga_api.SG_Get_Module_Library_Manager().Get_Module( saga_api.CSG_String('grid_calculus'), 6) print m.Get_Name().c_str() p = m.Get_Parameters() p(saga_api.CSG_String('RADIUS')).Set_Value(25) p(saga_api.CSG_String('ITERATIONS')).Set_Value(250) p = m.Get_Parameters(saga_api.CSG_String('TARGET')) p(saga_api.CSG_String('USER_SIZE')).Set_Value(10) p(saga_api.CSG_String('USER_XMAX')).Set_Value(2000) p(saga_api.CSG_String('USER_YMAX')).Set_Value(2000) if m.Execute() == 0: print 'ERROR: executing module [' + m.Get_Name().c_str() + ']' return 0 dem = p(saga_api.CSG_String('OUT_GRID')).asGrid() ### save dummy to esri ascii grid file: m = saga_api.SG_Get_Module_Library_Manager().Get_Module( saga_api.CSG_String('io_grid'), 0) print m.Get_Name().c_str() p = m.Get_Parameters() p(saga_api.CSG_String('GRID')).Set_Value(dem) p(saga_api.CSG_String('FILE')).Set_Value(saga_api.CSG_String(fASC)) p(saga_api.CSG_String('PREC')).Set_Value(2) if m.Execute() == 0: print 'ERROR: executing module [' + m.Get_Name().c_str() + ']' return 0 print 'success' return 1
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_asc2sgrd(fASC): ### load all module libraries from a directory at once: # if os.name == 'nt': # Windows # saga_api.SG_Get_Module_Library_Manager().Add_Directory(os.environ['SAGA_32' ], 0) # else: # Linux # saga_api.SG_Get_Module_Library_Manager().Add_Directory(os.environ['SAGA_MLB'], 0) # print '__________________' # print 'number of loaded libraries: ' + str(saga_api.SG_Get_Module_Library_Manager().Get_Count()) # print saga_api.SG_Get_Module_Library_Manager().Get_Summary(saga_api.SG_SUMMARY_FMT_FLAT_NO_INTERACTIVE).c_str() # print '__________________' ### load just the needed module library: if os.name == 'nt': # Windows saga_api.SG_Get_Module_Library_Manager().Add_Library( os.environ['SAGA_32'] + '/modules/io_grid.dll') else: # Linux saga_api.SG_Get_Module_Library_Manager().Add_Library( os.environ['SAGA_MLB'] + '/libio_grid.so') sASC = saga_api.CSG_String(fASC) m = saga_api.SG_Get_Module_Library_Manager().Get_Module( saga_api.CSG_String('io_grid'), 1) print m.Get_Description().c_str() p = m.Get_Parameters() p(saga_api.CSG_String('FILE')).Set_Value(sASC) if m.Execute() == 0: print 'ERROR: executing module [' + m.Get_Name().c_str() + ']' return 0 if p(saga_api.CSG_String('GRID')).asGrid().Save(sASC) == 0: print 'ERROR: saving grid [' + sASC + ']' return 0 print 'success' return 1
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_Module_Library_Manager().Add_Library(os.environ['SAGA_32' ] + '/modules/io_shapes.dll') else: # Linux saga_api.SG_Get_Module_Library_Manager().Add_Library(os.environ['SAGA_MLB'] + '/libio_shapes.so') m = saga_api.SG_Get_Module_Library_Manager().Get_Module(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 module [' + m.Get_Name().c_str() + ']' return 0 # ------------------------------------ print 'success' return 1
########################################## if __name__ == '__main__': print 'Python - Version ' + sys.version print saga_api.SAGA_API_Get_Version() print print 'Usage: %s <in: filename>' print print 'This is a simple template, please edit the script and add the necessary input and output file(s)!' print 'We will exit the script for now.' sys.exit() # remove this line once you have edited the script # This might look like this: # fDEM = sys.argv[1] # if os.path.split(fDEM)[0] == '': # fDEM = './' + fDEM fDEM = './../test_data/test.sgrd' # remove this line once you have edited the script saga_api.SG_UI_Msg_Lock(1) if os.name == 'nt': # Windows os.environ['PATH'] = os.environ['PATH'] + ';' + os.environ[ 'SAGA'] + '/bin/saga_vc_Win32/dll' saga_api.SG_Get_Module_Library_Manager().Add_Directory( os.environ['SAGA'] + '/bin/saga_vc_Win32/modules', 0) else: # Linux saga_api.SG_Get_Module_Library_Manager().Add_Directory( os.environ['SAGA_MLB'], 0) saga_api.SG_UI_Msg_Lock(0) Call_SAGA_Module(fDEM) # pass your input file(s) here
# ------------------------------------ print 'success' return 1 ########################################## if __name__ == '__main__': print 'Python - Version ' + sys.version print saga_api.SAGA_API_Get_Version() print if len( sys.argv ) != 2: print 'Usage: morphometry.py <in: elevation>' print '... trying to run with test_data' fDEM = './test.sgrd' else: fDEM = sys.argv[1] if os.path.split(fDEM)[0] == '': fDEM = './' + fDEM saga_api.SG_UI_Msg_Lock(1) if os.name == 'nt': # Windows os.environ['PATH'] = os.environ['PATH'] + ';' + os.environ['SAGA_32'] + '/dll' saga_api.SG_Get_Module_Library_Manager().Add_Directory(os.environ['SAGA_32' ] + '/modules', 0) else: # Linux saga_api.SG_Get_Module_Library_Manager().Add_Directory(os.environ['SAGA_MLB'], 0) saga_api.SG_UI_Msg_Lock(0) morphometry(fDEM)
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