예제 #1
0
    def checkSagaIsInstalled(cls):
        if SextanteUtils.isWindows():
            path = SagaUtils.sagaPath()
            if path == "":
                return "SAGA folder is not configured.\nPlease configure it before running SAGA algorithms."
            cmdpath = os.path.join(path, "saga_cmd.exe")
            if not os.path.exists(cmdpath):
                return ("The specified SAGA folder does not contain a valid SAGA executable.\n" 
                        + "Please, go to the SEXTANTE settings dialog, and check that the SAGA\n" 
                        + "folder is correctly configured")
                                    
        SAGA_INSTALLED = "/SextanteQGIS/SagaInstalled"
        settings = QSettings()
        if settings.contains(SAGA_INSTALLED):
            return
        
        try:
            qgis = QGisLayers.iface
            crs = qgis.mapCanvas().mapRenderer().destinationCrs()
            fields = []
            fields.append(QgsField("NUM_FIELD", QVariant.Int))
            filename = SextanteUtils.getTempFilename("shp")
            writer = SextanteVectorWriter(filename, None, fields, QGis.WKBPoint, crs)
            for x in range(5):
                for y in range(5):
                    attrs = []
                    attrs.append(QVariant(x))
                    outFeat = QgsFeature()
                    pt = QgsPoint(x, y)
                    outFeat.setGeometry(QgsGeometry.fromPoint(pt))
                    outFeat.setAttributes(attrs)
                    writer.addFeature(outFeat)   
            del writer.writer
            del writer       
            from sextante.core.Sextante import runalg              
            result = runalg("saga:thiessenpolygons", filename, None)
            if not os.path.exists(result['POLYGONS']):
                return "It seems that SAGA is not correctly installed in your system.\nPlease install it before running SAGA algorithms."
        except:
            s = traceback.format_exc()
            return "Error while checking SAGA installation. SAGA might not be correctly configured.\n" + s;
            

        settings.setValue("/SextanteQGIS/SagaInstalled", True)        
        
#Here we define the input and outputs
#====================================
##[Example scripts]=group
##input=vector
##output=output vector

#And here is the body of the algorithm
#=======================================

#input layers values are always a string with its location.
#That string can be converted into a QGIS object (a QgsVectorLayer in this case))
#using the Sextante.getObjectFromUri() method
vectorLayer = QGisLayers.getObjectFromUri(input)

#And now we can process

#First we create the output layer.
#To do so, we create a SextanteVectorWriter, that we can later use to add features.
provider = vectorLayer.dataProvider()
writer = SextanteVectorWriter(output, None, provider.fields(),
                              provider.geometryType(), provider.crs())

#Now we take the selected features and add them to the output layer
selection = vectorLayer.selectedFeatures()
for feat in selection:
    writer.addFeature(feat)
del writer

#There is nothing more to do here. We do not have to open the layer that we have created.
#SEXTANTE will take care of that, or will handle it if this algorithm is executed within
#a complex model
예제 #3
0
from sextante.core.SextanteVectorWriter import SextanteVectorWriter

inputLayer = sextante.getobject(input)
features = sextante.getfeatures(inputLayer)
fields = inputLayer.pendingFields().toList()
outputLayer = SextanteVectorWriter(output, None, fields, QGis.WKBPoint, inputLayer.crs())
count = 0
mean = [0 for field in fields]
x = 0
y = 0
for ft in features:
    c = ft.geometry().centroid().asPoint()
    x += c.x()
    y += c.y()
    attrs = ft.attributes()
    for f in range(len(fields)):
        try:
            mean[f] += float(attrs[f].toDouble()[0])
        except:
            pass
    count += 1
if count != 0:
    mean = [value / count for value in mean]
    x /= count
    y /= count
outFeat = QgsFeature()
meanPoint = QgsPoint(x, y)
outFeat.setGeometry(QgsGeometry.fromPoint(meanPoint))
outFeat.setAttributes([QVariant(v) for v in mean])
outputLayer.addFeature(outFeat)
# Iterate over input layer to count unique values in each class

feats = getfeatures(layer)
nFeat = len(feates)
for inFeat in feats:
    progress.setPercentage(int((100 * nElement) / nFeat))
    nElement += 1
    attrs = inFeat.attributes()
    clazz = attrs[class_field_index].toString()
    value = attrs[value_field_index].toString()
    if clazz not in classes:
        classes[clazz] = []
    if value not in classes[clazz]:
        classes[clazz].append(value)

# Create output vector layer with additional attribute
feats = getfeatures(layer)
nElement = 0
for inFeat in feats:
    progress.setPercentage(int((100 * nElement) / nFeat))
    nElement += 1
    inGeom = inFeat.geometry()
    outFeat.setGeometry(inGeom)
    attrs = inFeat.attributes()
    clazz = attrs[class_field_index].toString()
    attrs.append(QVariant(len(classes[clazz])))
    outFeat.setAttributes(attrs)
    writer.addFeature(outFeat)

del writer
예제 #5
0
#Here we define the input and outputs
#====================================
##[Example scripts]=group
##input=vector
##output=output vector

#And here is the body of the algorithm
#=======================================

#input layers values are always a string with its location.
#That string can be converted into a QGIS object (a QgsVectorLayer in this case))
#using the Sextante.getObjectFromUri() method
vectorLayer = QGisLayers.getObjectFromUri(input)

#And now we can process

#First we create the output layer.
#To do so, we create a SextanteVectorWriter, that we can later use to add features.
provider = vectorLayer.dataProvider()
writer = SextanteVectorWriter(output, None, provider.fields(), provider.geometryType(), provider.crs() )

#Now we take the selected features and add them to the output layer
selection = vectorLayer.selectedFeatures()
for feat in selection:
    writer.addFeature(feat)
del writer

#There is nothing more to do here. We do not have to open the layer that we have created.
#SEXTANTE will take care of that, or will handle it if this algorithm is executed within
#a complex model
예제 #6
0
inputLayer = sextante.getobject(input)
features = sextante.getfeatures(inputLayer)
fields = inputLayer.pendingFields().toList()
outputLayer = SextanteVectorWriter(output, None, fields, QGis.WKBPoint,
                                   inputLayer.crs())
count = 0
mean = [0 for field in fields]
x = 0
y = 0
for ft in features:
    c = ft.geometry().centroid().asPoint()
    x += c.x()
    y += c.y()
    attrs = ft.attributes()
    for f in range(len(fields)):
        try:
            mean[f] += float(attrs[f].toDouble()[0])
        except:
            pass
    count += 1
if count != 0:
    mean = [value / count for value in mean]
    x /= count
    y /= count
outFeat = QgsFeature()
meanPoint = QgsPoint(x, y)
outFeat.setGeometry(QgsGeometry.fromPoint(meanPoint))
outFeat.setAttributes([QVariant(v) for v in mean])
outputLayer.addFeature(outFeat)
예제 #7
0
dividend_field_index = provider.fieldNameIndex(dividend)
divisor_field_index = provider.fieldNameIndex(divisor)

# Output
outFields = fields
outFields[len(outFields)] = QgsField("RATIO", QVariant.Double)
writer = SextanteVectorWriter(output, None, outFields, provider.geometryType(),
                              provider.crs())

inFeat = QgsFeature()
outFeat = QgsFeature()

# Create output vector layer with additional attribute
while provider.nextFeature(inFeat):
    inGeom = inFeat.geometry()

    outFeat.setGeometry(inGeom)
    atMap = inFeat.attributeMap()
    dividend = float(atMap[dividend_field_index].toString())
    divisor = float(atMap[divisor_field_index].toString())

    outFeat.setAttributeMap(atMap)
    if divisor != 0:
        outFeat.addAttribute(len(provider.fields()),
                             QVariant(dividend / divisor))
    else:
        outFeat.addAttribute(len(provider.fields()), QVariant(None))
    writer.addFeature(outFeat)

del writer