2

I want to work with python API and SAGA GIS. I am very new with this work. The first step I find one module for example Slope, Aspect, Curvature and I take with right click one python script.

python script :

import saga_api, sys, os

##########################################
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: Slope, Aspect, Curvature
    Module = saga_api.SG_Get_Module_Library_Manager().Get_Module('ta_morphometry','0')
    Module.Get_Parameters().Get_Grid_System().Assign(dem.Get_System())

    Parms = Module.Get_Parameters() # default parameter list
    Parms.Get(unicode('ELEVATION')).Set_Value(use_variable_of_dataset_here) # input NOT optional grid
    Parms.Get(unicode('SLOPE')).Set_Value(use_variable_of_dataset_here) # output NOT optional grid
    Parms.Get(unicode('ASPECT')).Set_Value(use_variable_of_dataset_here) # output NOT optional grid
    Parms.Get(unicode('C_GENE')).Set_Value(use_variable_of_dataset_here) # output optional grid
    Parms.Get(unicode('C_PROF')).Set_Value(use_variable_of_dataset_here) # output optional grid
    Parms.Get(unicode('C_PLAN')).Set_Value(use_variable_of_dataset_here) # output optional grid
    Parms.Get(unicode('C_TANG')).Set_Value(use_variable_of_dataset_here) # output optional grid
    Parms.Get(unicode('C_LONG')).Set_Value(use_variable_of_dataset_here) # output optional grid
    Parms.Get(unicode('C_CROS')).Set_Value(use_variable_of_dataset_here) # output optional grid
    Parms.Get(unicode('C_MINI')).Set_Value(use_variable_of_dataset_here) # output optional grid
    Parms.Get(unicode('C_MAXI')).Set_Value(use_variable_of_dataset_here) # output optional grid
    Parms.Get(unicode('C_TOTA')).Set_Value(use_variable_of_dataset_here) # output optional grid
    Parms.Get(unicode('C_ROTO')).Set_Value(use_variable_of_dataset_here) # output optional grid
    Parms.Get(unicode('METHOD')).Set_Value(6)
    Parms.Get(unicode('UNIT_SLOPE')).Set_Value(0)
    Parms.Get(unicode('UNIT_ASPECT')).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

##########################################
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

If I run this script i get this message :

Python - Version 2.7.11 (v2.7.11:6d1b6a68f775, Dec 5 2015, 20:32:19) [MSC v.1500 32 bit (Intel)]
SAGA Application Programming Interface - Version: 2.2.7
Usage: %s <in: filename="">
This is a simple template, please edit the script and add the necessary input and output file(s)!
We will exit the script for now.

but how to add input and output files ? I try to use this

fDEM='C:\Python27\test\srtm_41_04.tif'
SLOPE='C:\Python27\test\slope.tif'
ASPECT='C:\Python27\test\aspect.tif'

But it doesn't work. Any ideas to help me?

Is this the right way to work with saga and python api?


i try this to fix the input and output but not work

import saga_api, sys, os

##########################################

def Call_SAGA_Module(fDEM,slope,aspect):            # pass your input file(s) here
    fDEM='C:\Python27\test\srtm_41_04.tif'
    slope='C:\Python27\test\slope.tif'
    aspect='C:\Python27\test\aspect.tif'
    # ------------------------------------
    # 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: Slope, Aspect, Curvature
    Module = saga_api.SG_Get_Module_Library_Manager().Get_Module('ta_morphometry','0')
    Module.Get_Parameters().Get_Grid_System().Assign(dem.Get_System())

    Parms = Module.Get_Parameters() # default parameter list
    Parms.Get(unicode('ELEVATION')).Set_Value(fDEM) # input NOT optional grid
    Parms.Get(unicode('SLOPE')).Set_Value(slope) # output NOT optional grid
    Parms.Get(unicode('ASPECT')).Set_Value(aspect) # output NOT optional grid
    Parms.Get(unicode('C_GENE')).Set_Value() # output optional grid
    Parms.Get(unicode('C_PROF')).Set_Value() # output optional grid
    Parms.Get(unicode('C_PLAN')).Set_Value() # output optional grid
    Parms.Get(unicode('C_TANG')).Set_Value() # output optional grid
    Parms.Get(unicode('C_LONG')).Set_Value() # output optional grid
    Parms.Get(unicode('C_CROS')).Set_Value() # output optional grid
    Parms.Get(unicode('C_MINI')).Set_Value() # output optional grid
    Parms.Get(unicode('C_MAXI')).Set_Value() # output optional grid
    Parms.Get(unicode('C_TOTA')).Set_Value() # output optional grid
    Parms.Get(unicode('C_ROTO')).Set_Value() # output optional grid
    Parms.Get(unicode('METHOD')).Set_Value(6)
    Parms.Get(unicode('UNIT_SLOPE')).Set_Value(0)
    Parms.Get(unicode('UNIT_ASPECT')).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

##########################################
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,slope,aspect)             # pass your input file(s) here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.