Пример #1
0
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
Пример #2
0
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
Пример #3
0
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
Пример #4
0
    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)