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 grid_difference(fA, fB, fC, bPixelWise): A = saga_api.SG_Create_Grid() if A.Create(saga_api.CSG_String(fA)) == 0: print 'ERROR: loading grid [' + fA + ']' return 0 B = saga_api.SG_Create_Grid() if B.Create(saga_api.CSG_String(fB)) == 0: print 'ERROR: loading grid [' + fB + ']' return 0 if A.is_Compatible(B) == 0: print 'ERROR: grids [' + fA + '] and [' + fB + '] are not compatible' return 0 if bPixelWise == 1: # pixelwise calculation, slower than second solution C = saga_api.SG_Create_Grid(A.Get_System()) for y in range(0, C.Get_NY()): for x in range(0, C.Get_NX()): if A.is_NoData(x, y) or B.is_NoData(x, y): C.Set_NoData(x, y) else: C.Set_Value(x, y, A.asDouble(x, y) - B.asDouble(x, y)) print '.', C.Save(saga_api.CSG_String(fC)) print else: # using built-in CSG_Grid function 'Subtract()' C = saga_api.SG_Create_Grid(A) C.Subtract(B) C.Save(saga_api.CSG_String(fC)) 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 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 shp2xyz(fshp, fxyz): # fmlb = '/usr/local/lib/saga/libio_shapes.so' # Linux fmlb = os.environ[ 'SAGA'] + '/bin/saga_vc_Win32/modules/io_shapes.dll' # Windows mlb = saga_api.CSG_Module_Library() shp = saga_api.SG_Create_Shapes() print 'load module library: ' + fmlb if mlb.Create(saga_api.CSG_String(fmlb)) == 0: print '... failed' return 0 print '... success' m = mlb.Get_Module(2) p = m.Get_Parameters() print 'load shape file: ' + fshp if shp.Create(saga_api.CSG_String(fshp)) == 0: print '... failed' return 0 print '... success' p('SHAPES').Set_Value(shp) p('FILENAME').Set_Value(fxyz) print p('SHAPES').Get_Name() + ' >> ' + p('SHAPES').asString() print p('FILENAME').Get_Name() + ' >> ' + p('FILENAME').asString() print 'execute module: ' + m.Get_Name() if m.Execute() == 0: print '... failed' return 0 print '... success' return 1
def grid_asc2sgrd(fASC): # fmlb = '/usr/local/lib/saga/libio_grid.so' # Linux fmlb = os.environ[ 'SAGA'] + '/bin/saga_vc_Win32/modules/io_grid.dll' # Windows mlb = saga_api.CSG_Module_Library() print 'load module library: ' + fmlb if mlb.Create(saga_api.CSG_String(fmlb)) == 0: print '... failed' return 0 print '... success' m = mlb.Get_Module(1) p = m.Get_Parameters() p('FILE').Set_Value(fASC) print m.Get_Name() + ': ' + p('FILE').asString() if m.Execute() == 0: print '... failed' return 0 print '... success' print 'save as SAGA grid' if p('GRID').asGrid().Save(saga_api.CSG_String(fASC)) == 0: print '... failed' return 0 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 grid_asc2sgrd(fASC): ### load all tool libraries from a directory at once: # if os.name == 'nt': # Windows # saga_api.SG_Get_Tool_Library_Manager().Add_Directory(os.environ['SAGA_32' ], 0) # else: # Linux # saga_api.SG_Get_Tool_Library_Manager().Add_Directory(os.environ['SAGA_MLB'], 0) # print '__________________' # print 'number of loaded libraries: ' + str(saga_api.SG_Get_Tool_Library_Manager().Get_Count()) # print saga_api.SG_Get_Tool_Library_Manager().Get_Summary(saga_api.SG_SUMMARY_FMT_FLAT_NO_INTERACTIVE).c_str() # print '__________________' sASC = saga_api.CSG_String(fASC) m = saga_api.SG_Get_Tool_Library_Manager().Get_Tool(saga_api.CSG_String('io_gdal'), 0) p = m.Get_Parameters() p(saga_api.CSG_String('FILES')).Set_Value(sASC) if m.Execute() == 0: print 'ERROR: executing tool [' + m.Get_Name().c_str() + ']' return 0 saga_api.SG_File_Set_Extension(sASC, saga_api.CSG_String('sgrd')) if p(saga_api.CSG_String('GRIDS')).asGridList().Get_Grid(0).Save(sASC) == 0: print 'ERROR: saving grid [' + sASC + ']' return 0 print 'success' return 1
def xyz2shp(fTable): print saga_api.SAGA_API_Get_Version() # fmlb = '/usr/local/lib/saga/libshapes_points.so' # Linux fmlb = os.environ[ 'SAGA'] + '/bin/saga_vc_Win32/modules/shapes_points.dll' # Windows mlb = saga_api.CSG_Module_Library() print 'load module library: ' + fmlb if mlb.Create(saga_api.CSG_String(fmlb)) == 0: print '... failed' return 0 print '... success' # 1. load table from file or create a test data set table = saga_api.SG_Create_Table() if table.Create(saga_api.CSG_String(fTable)) == 0: table.Add_Field('X', saga_api.TABLE_FIELDTYPE_Float) table.Add_Field('Y', saga_api.TABLE_FIELDTYPE_Float) table.Add_Field('Z', saga_api.TABLE_FIELDTYPE_Float) 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) # 2. convert table to points m = mlb.Get_Module(0) p = m.Get_Parameters() p('TABLE').Set_Value(table) p('POINTS').Set_Value(saga_api.SG_Create_Shapes(saga_api.SHAPE_TYPE_Point)) p('X').Set_Value(0) p('Y').Set_Value(1) print 'execute module: ' + m.Get_Name() if m.Execute() == 0: print '... failed' return 0 print '... success' p('POINTS').asShapes().Save(saga_api.CSG_String(fTable)) 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 __init__(self, filename, iface=None): self.sagalib = saga.CSG_Module_Library(saga.CSG_String(str(filename))) if not self.sagalib.is_Valid(): raise InvalidLibrary(filename) self._modules = None self.iface = iface processing.framework.registerModuleProvider(self)
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 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 __init__(self, filename): self.sagalib = saga.CSG_Module_Library(saga.CSG_String(str(filename))) if not self.sagalib.is_Valid(): raise ImportError(filename) self.libname = filename.split(os.sep)[-1].split(".")[0] if self.libname.startswith("lib"): self.libname = self.libname[3:] self.name = self.sagalib.Get_Name().c_str() self._modules = None
def morphometry(fDEM, fSlope, fAspect): # fmlb = '/usr/local/lib/saga/libta_morphometry.so' # Linux fmlb = os.environ['SAGA'] + '/bin/saga_vc_Win32/modules/ta_morphometry.dll' # Windows mlb = saga_api.CSG_Module_Library() print 'load module library: ' + fmlb if mlb.Create(saga_api.CSG_String(fmlb)) == 0: print '... failed' return 0 print '... success' m = mlb.Get_Module_Grid(0) p = m.Get_Parameters() DEM = saga_api.SG_Create_Grid() print 'load grid file: ' + fDEM if DEM.Create(saga_api.CSG_String(fDEM)) == 0: print '... failed' return 0 print '... success' Slope = saga_api.SG_Create_Grid(DEM.Get_System()) Aspect = saga_api.SG_Create_Grid(DEM.Get_System()) m.Get_System().Assign(DEM.Get_System()) # module needs to use conformant grid system! p('ELEVATION').Set_Value(DEM) p('SLOPE') .Set_Value(Slope) p('ASPECT') .Set_Value(Aspect) print 'execute module: ' + m.Get_Name() if m.Execute() == 0: print '... failed' return 0 print '... success' Slope .Set_Name('Slope') Aspect.Set_Name('Aspect') Slope .Save(saga_api.CSG_String(fSlope)) Aspect.Save(saga_api.CSG_String(fAspect)) return 1
def run_grid_asc2sgrd(File): Tool = saga_api.SG_Get_Tool_Library_Manager().Get_Tool('io_gdal', 0) Tool.Set_Parameter('FILES', File) if Tool.Execute() == False: print('Error: executing tool [' + Tool.Get_Name().c_str() + ']') return False Grid = Tool.Get_Parameter('GRIDS').asGridList().Get_Grid(0) File = saga_api.CSG_String(File) saga_api.SG_File_Set_Extension(File, saga_api.CSG_String('sg-grd-z')) if Grid.Save(File) == False: print('Error: saving grid [' + File.c_str() + ']') return False # ----------------------------------- print('Success') return True
def grid_contour(fGrid, fLines): # fmlb = '/usr/local/lib/saga/libshapes_grid.so' # Linux fmlb = os.environ[ 'SAGA'] + '/bin/saga_vc_Win32/modules/shapes_grid.dll' # Windows mlb = saga_api.CSG_Module_Library() print 'load module library: ' + fmlb if mlb.Create(saga_api.CSG_String(fmlb)) == 0: print '... failed' return 0 print '... success' m = mlb.Get_Module_Grid('Contour Lines from Grid') p = m.Get_Parameters() Grid = saga_api.SG_Create_Grid() print 'load grid file: ' + fGrid if Grid.Create(saga_api.CSG_String(fGrid)) == 0: print '... failed' return 0 print '... success' Lines = saga_api.SG_Create_Shapes() m.Get_System().Assign( Grid.Get_System()) # module needs to use conformant grid system! p('INPUT').Set_Value(Grid) p('CONTOUR').Set_Value(Lines) p('ZSTEP').Set_Value(25.0) print 'execute module: ' + m.Get_Name() if m.Execute() == 0: print '... failed' return 0 print '... success' Lines.Save(saga_api.CSG_String(fLines)) 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 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 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 onParameterChanged(self, qgisParam, value): sagaParam = qgisParam.sagaParameter typ = sagaParam.Get_Type() pc = qgisParam.__class__ # special cases - layers, choices, files, etc. # for layers we only set the saga param on excecution if (pc == LayerParameter or pc == VectorLayerParameter or pc == RasterLayerParameter): qgisParam.layer = value elif pc == RangeParameter: low, high = value sagaParam.asRange().Set_Range(low, high) elif pc == GridSystemParameter: cellsize = value.cellsize xMin, xMax = value.xRange yMin, yMax = value.yRange self.module.Get_System().Assign(cellsize, xMin, yMin, xMax, yMax) elif pc == StringParameter: # convert QString to CSG_String string = saga.CSG_String(str(value)) sagaParam.Get_Data().Set_Value(string) else: # generic case - numerics, booleans, etc. sagaParam.Set_Value(value)
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 morphometry(fDEM, fSlope, fAspect, fCurvCls): # fmlb = '/usr/local/lib/saga/libta_morphometry.so' # Linux fmlb = os.environ[ 'SAGA'] + '/bin/saga_vc_Win32/modules/ta_morphometry.dll' # Windows mlb = saga_api.CSG_Module_Library() print 'load module library: ' + fmlb if mlb.Create(saga_api.CSG_String(fmlb)) == 0: print '... failed' return 0 print '... success' m = mlb.Get_Module_Grid(0) p = m.Get_Parameters() DEM = saga_api.SG_Create_Grid() print 'load grid file: ' + fDEM if DEM.Create(saga_api.CSG_String(fDEM)) == 0: print '... failed' return 0 print '... success' Slope = saga_api.SG_Create_Grid(DEM.Get_System()) Aspect = saga_api.SG_Create_Grid(DEM.Get_System()) hCurv = saga_api.SG_Create_Grid(DEM.Get_System()) vCurv = saga_api.SG_Create_Grid(DEM.Get_System()) CurvCls = saga_api.SG_Create_Grid(DEM.Get_System()) m.Get_System().Assign( DEM.Get_System()) # module needs to use conformant grid system! p('ELEVATION').Set_Value(DEM) p('SLOPE').Set_Value(Slope) p('ASPECT').Set_Value(Aspect) p('HCURV').Set_Value(hCurv) p('VCURV').Set_Value(vCurv) print 'execute module: ' + m.Get_Name() if m.Execute() == 0: print '... failed' return 0 print '... success' ###################################### m = mlb.Get_Module_Grid('Curvature Classification') p = m.Get_Parameters() p('CPLAN').Set_Value(hCurv) p('CPROF').Set_Value(vCurv) p('CLASS').Set_Value(CurvCls) print 'execute module: ' + m.Get_Name() if m.Execute() == 0: print '... failed' return 0 print '... success' Slope.Set_Name('Slope') Aspect.Set_Name('Aspect') CurvCls.Set_Name('Cuvature Classification') Slope.Save(saga_api.CSG_String(fSlope)) Aspect.Save(saga_api.CSG_String(fAspect)) CurvCls.Save(saga_api.CSG_String(fCurvCls)) hCurv.Save(saga_api.CSG_String('./hcurv')) vCurv.Save(saga_api.CSG_String('./vcurv')) ###################################### return 1
min_gx = window_gx min_gy = window_gy # Abfluss proportional verteilen AccumulationArray[gy, gx] += DiffHeight / DiffHeight.sum( ) * AccumulationArray[gy + 1, gx + 1] # Differenz / Summe aller Differenzen im Window * Ablufss ########### end moving window ######### ############################################################################# ######## Output ############################################################# ############################################################################# Out = saga_api.SG_Create_Grid(saga_api.SG_DATATYPE_Float, NX, NY, Cellsize, LLX, LLY, False) Out.Assign_NoData() print("Writing Raster...") for gy in range(NY): for gx in range(NX): z = AccumulationArray[gy, gx] Out.Set_Value(gx, gy, z) Out.Set_Name(saga_api.CSG_String( "MultipathFlowAccumulation")) # Name, den man in der GUI sieht Out.Save( "C:/Users/Anwender/Documents/ABStudium/Python/Kursunterlagen/9_Projekte/MulipathFlowAcc/MultipathFlowAcc.tif" )
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
def grid_create_dummy(fASC): print '__________________' print 'number of loaded libraries: ' + str( saga_api.SG_Get_Tool_Library_Manager().Get_Count()) #print saga_api.SG_Get_Tool_Library_Manager().Get_Summary(saga_api.SG_SUMMARY_FMT_FLAT_NO_INTERACTIVE).c_str() print '__________________' ### create a dummy DEM: m = saga_api.SG_Get_Tool_Library_Manager().Get_Tool( 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(saga_api.CSG_String('TARGET_USER_SIZE')).Set_Value(10) p(saga_api.CSG_String('TARGET_USER_XMAX')).Set_Value(2000) p(saga_api.CSG_String('TARGET_USER_YMAX')).Set_Value(2000) if m.Execute() == 0: print 'ERROR: executing tool [' + m.Get_Name().c_str() + ']' return 0 dem = p(saga_api.CSG_String('TARGET_OUT_GRID')).asGrid() ### save dummy to esri ascii grid file: m = saga_api.SG_Get_Tool_Library_Manager().Get_Tool( 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 tool [' + m.Get_Name().c_str() + ']' return 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 print 'grid file [' + fDEM + '] ahs been loaded' 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_Tool_Library_Manager().Get_Tool( saga_api.CSG_String('ta_morphometry'), 0) 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('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 tool [' + m.Get_Name().c_str() + ']' return 0 slope.Save(saga_api.CSG_String(path + '/slope' + '.sgrd')) aspect.Save(saga_api.CSG_String(path + '/aspect' + '.sgrd')) hcurv.Save(saga_api.CSG_String(path + '/hcurv' + '.sgrd')) vcurv.Save(saga_api.CSG_String(path + '/vcurv' + '.sgrd')) # ------------------------------------ # 'Curvature Classification' m = saga_api.SG_Get_Tool_Library_Manager().Get_Tool( saga_api.CSG_String('ta_morphometry'), 4) 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('CLASS')).Set_Value(ccurv) if m.Execute() == 0: print 'ERROR: executing tool [' + m.Get_Name().c_str() + ']' return 0 ccurv.Save(saga_api.CSG_String(path + '/ccurv' + '.sgrd')) # ------------------------------------ print 'success' return 1
def sagaTempFilename(basename, extension): return saga.CSG_String(qgisTempFilename(basename, extension))
def stateParameterValueChanged(self, state): """ Only reacts to start running state, ignore others. """ sm = self.module().module # the SAGA module if state != StateParameter.State.running: return modName = self.module().name() inputGrid = None # set values of saga parameters... # ...and in the case of input layers, # also export from qgis to saga for param in self.inLayer: pc = param.__class__ if not param.layer and param.isMandatory(): msg = "Mandatory parameter %s not set." % param.name() self.setFeedback(msg, critical=True) return elif not param.layer: continue basename = "qgis-saga%s" % id(param.layer) dpUri = str(param.layer.dataProvider().dataSourceUri()) dpDescription = param.layer.dataProvider().description() if pc == VectorLayerParameter: isLocal = dpDescription.startsWith('OGR data provider') if isLocal: fn = saga.CSG_String(dpUri) else: fn = sagaTempFilename(basename, "shp") QgsVectorFileWriter.writeAsVectorFormat( param.layer, fn.c_str(), "CP1250", param.layer.crs()) param.sagaParameter.Set_Value(saga.SG_Create_Shapes(fn)) if pc == RasterLayerParameter: isLocal = dpDescription.startsWith('GDAL provider') if isLocal: sagaFn = sagaTempFilename(basename, "sgrd") # GDAL & QGIS use the sdat file as reference, # unlike SAGA qgisFn = qgisTempFilename(basename, "sdat") else: msg = "Sorry. Only local raster layers supported." self.setFeedback(msg, critical=True) return self.setFeedback("Converting raster to SAGA format...", progress=0) # convert input to saga grid file -- adapted from # GDAL tutorial driver = gdal.GetDriverByName("SAGA") self.setFeedback("Converting raster to SAGA format...", progress=25) source = gdal.Open(dpUri) self.setFeedback("Converting raster to SAGA format...", progress=50) destination = driver.CreateCopy(qgisFn, source, 0) self.setFeedback("Converting raster to SAGA format...", progress=100) # Once we're done, close properly the dataset source = None destination = None grid = saga.SG_Create_Grid() if grid.Create(sagaFn) == 0: self.setFeedback("Couldn't create SAGA input raster.", critical=True) return # Store first input grid for output. if not inputGrid: inputGrid = grid param.sagaParameter.Set_Value(grid) for param in self.outLayer: pc = param.__class__ if pc == VectorLayerParameter: param.sagaLayer = saga.SG_Create_Shapes() param.sagaParameter.Set_Value(param.sagaLayer) if pc == RasterLayerParameter: # use an input grid to get the grid system if not inputGrid: self.setFeedback("No input raster specified.", True) return param.sagaLayer = saga.SG_Create_Grid(inputGrid) sm.Get_System().Assign(inputGrid.Get_System()) param.sagaParameter.Set_Value(param.sagaLayer) self.setFeedback("Module '%s' execution started." % modName) if sm.Execute() != 0: self.setFeedback("SAGA Module execution suceeded.") # umm- what if there is no iface? iface = self.module().iface # now import output layers for param in self.outLayer: basename = "saga-qgis%s" % id(param.sagaLayer) pc = param.__class__ if pc == VectorLayerParameter: # no implicit conversion! fn = sagaTempFilename(basename, "shp") # tell SAGA to save the layer param.sagaLayer.Save(fn) # load it into QGIS. # TODO: where? iface.addVectorLayer(fn.c_str(), basename, "ogr") elif pc == RasterLayerParameter: # no implicit conversion! sagaFn = sagaTempFilename(basename, "sgrd") qgisFn = qgisTempFilename(basename, "sdat") # tell SAGA to save the layer param.sagaLayer.Save(sagaFn) # load it into QGIS. # TODO: where? iface.addRasterLayer(qgisFn, basename) else: self.setFeedback("Module execution failed.") self.setState(StateParameter.State.stopped)