def getMinCoveringExtent(self):
     first = True
     found = False
     for param in self.params:
         if param.value:
             if isinstance(param, (ParameterRaster, ParameterVector)):
                 if isinstance(param.value, (QgsRasterLayer,
                               QgsVectorLayer)):
                     layer = param.value
                 else:
                     layer = dataobjects.getObject(param.value)
                 if layer:
                     found = True
                     self.addToRegion(layer, first)
                     first = False
             elif isinstance(param, ParameterMultipleInput):
                 layers = param.value.split(';')
                 for layername in layers:
                     layer = dataobjects.getObject(layername, first)
                     if layer:
                         found = True
                         self.addToRegion(layer, first)
                         first = False
     if found:
         return '{},{},{},{}'.format(
             self.xmin, self.xmax, self.ymin, self.ymax)
     else:
         return None
    def updateLayer(self):
        self.layer = dataobjects.getObject(self.cmbInputLayer.currentText())

        self.builder.setLayer(self.layer)
        self.builder.loadFieldNames()

        self.populateFields()
Exemple #3
0
 def _checkParameterValuesBeforeExecuting(self):
     for param in self.parameters:
         if isinstance(param, (ParameterRaster, ParameterVector, ParameterMultipleInput)):
             if param.value:
                 if isinstance(param, ParameterMultipleInput):
                     inputlayers = param.value.split(";")
                 else:
                     inputlayers = [param.value]
                 for inputlayer in inputlayers:
                     obj = dataobjects.getObject(inputlayer)
                     if obj is None:
                         return "Wrong parameter value: " + param.value
     return self.checkParameterValuesBeforeExecuting()
    def updateLayer(self):
        self.layer = dataobjects.getObject(self.cmbInputLayer.currentText())

        self.builder.setLayer(self.layer)
        self.builder.loadFieldNames()

        exp_context = QgsExpressionContext()
        exp_context.appendScope(QgsExpressionContextUtils.globalScope())
        exp_context.appendScope(QgsExpressionContextUtils.projectScope())
        exp_context.appendScope(QgsExpressionContextUtils.layerScope(self.layer))
        exp_context.lastScope().setVariable("row_number", 1)
        exp_context.setHighlightedVariables(["row_number"])
        self.builder.setExpressionContext(exp_context)

        self.populateFields()
Exemple #5
0
 def checkInputCRS(self):
     """It checks that all input layers use the same CRS. If so,
     returns True. False otherwise.
     """
     crsList = []
     for param in self.parameters:
         if isinstance(param, (ParameterRaster, ParameterVector,
                       ParameterMultipleInput)):
             if param.value:
                 layers = param.value.split(';')
                 for item in layers:
                     crs = dataobjects.getObject(item).crs()
                     if crs not in crsList:
                         crsList.append(crs)
     return len(crsList) == 1
Exemple #6
0
 def checkInputCRS(self):
     """It checks that all input layers use the same CRS. If so,
     returns True. False otherwise.
     """
     crsList = []
     for param in self.parameters:
         if isinstance(
                 param,
             (ParameterRaster, ParameterVector, ParameterMultipleInput)):
             if param.value:
                 if isinstance(param, ParameterMultipleInput):
                     layers = param.value.split(';')
                 else:
                     layers = [param.value]
                 for item in layers:
                     crs = dataobjects.getObject(item).crs()
                     if crs not in crsList:
                         crsList.append(crs)
     return len(crsList) < 2
Exemple #7
0
 def updateLayer(self):
     self.layer = dataobjects.getObject(self.cmbInputLayer.currentText())
     self.builder.setLayer(self.layer)
     self.builder.loadFieldNames()
     self.populateFields()
Exemple #8
0
def createTest(text):
    s = ''
    tokens = text[len('processing.runalg('):-1].split(',')
    cmdname = (tokens[0])[1:-1]
    methodname = 'test_' + cmdname.replace(':', '')
    s += 'def ' + methodname + '(self):\n'
    alg = Processing.getAlgorithm(cmdname)
    execcommand = 'processing.runalg('
    i = 0
    for token in tokens:
        if i < alg.getVisibleParametersCount() + 1:
            if os.path.exists(token[1:-1]):
                token = os.path.basename(token[1:-1])[:-4] + '()'
            execcommand += token + ','
        else:
            execcommand += 'None,'
        i += 1
    s += '\toutputs=' + execcommand[:-1] + ')\n'

    i = -1 * len(alg.outputs)
    for out in alg.outputs:
        filename = (tokens[i])[1:-1]
        if tokens[i] == unicode(None):
            QMessageBox.critical(None, tr('Error'),
                                 tr('Cannot create unit test for that algorithm execution. The '
                                    'output cannot be a temporary file'))
            return
        s += "\toutput=outputs['" + out.name + "']\n"
        if isinstance(out, (OutputNumber, OutputString)):
            s += 'self.assertTrue(' + unicode(out) + ', output.value)\n'
        if isinstance(out, OutputRaster):
            dataset = gdal.Open(filename, GA_ReadOnly)
            strhash = hash(unicode(dataset.ReadAsArray(0).tolist()))
            s += '\tself.assertTrue(os.path.isfile(output))\n'
            s += '\tdataset=gdal.Open(output, GA_ReadOnly)\n'
            s += '\tstrhash=hash(unicode(dataset.ReadAsArray(0).tolist()))\n'
            s += '\tself.assertEqual(strhash,' + unicode(strhash) + ')\n'
        if isinstance(out, OutputVector):
            layer = dataobjects.getObject(filename)
            fields = layer.pendingFields()
            s += '\tlayer=dataobjects.getObjectFromUri(output, True)\n'
            s += '\tfields=layer.pendingFields()\n'
            s += '\texpectednames=[' + ','.join(["'" + unicode(f.name()) + "'"
                                                 for f in fields]) + ']\n'
            s += '\texpectedtypes=[' + ','.join(["'" + unicode(f.typeName()) + "'"
                                                 for f in fields]) + ']\n'
            s += '\tnames=[unicode(f.name()) for f in fields]\n'
            s += '\ttypes=[unicode(f.typeName()) for f in fields]\n'
            s += '\tself.assertEqual(expectednames, names)\n'
            s += '\tself.assertEqual(expectedtypes, types)\n'
            features = vector.features(layer)
            numfeat = len(features)
            s += '\tfeatures=processing.features(layer)\n'
            s += '\tself.assertEqual(' + unicode(numfeat) + ', len(features))\n'
            if numfeat > 0:
                feature = features.next()
                attrs = feature.attributes()
                s += '\tfeature=features.next()\n'
                s += '\tattrs=feature.attributes()\n'
                s += '\texpectedvalues=[' + ','.join(['"' + unicode(attr) + '"'
                                                      for attr in attrs]) + ']\n'
                s += '\tvalues=[unicode(attr) for attr in attrs]\n'
                s += '\tself.assertEqual(expectedvalues, values)\n'
                s += "\twkt='" + unicode(feature.geometry().exportToWkt()) + "'\n"
                s += '\tself.assertEqual(wkt, \
                      unicode(feature.geometry().exportToWkt()))'

    dlg = ShowTestDialog(s)
    dlg.exec_()
Exemple #9
0
def createTest(text):
    s = ''
    tokens = text[len('processing.runalg('):-1].split(',')
    cmdname = (tokens[0])[1:-1]
    methodname = 'test_' + cmdname.replace(':', '')
    s += 'def ' + methodname + '(self):\n'
    alg = Processing.getAlgorithm(cmdname)
    execcommand = 'processing.runalg('
    i = 0
    for token in tokens:
        if i < alg.getVisibleParametersCount() + 1:
            if os.path.exists(token[1:-1]):
                token = os.path.basename(token[1:-1])[:-4] + '()'
            execcommand += token + ','
        else:
            execcommand += 'None,'
        i += 1
    s += '\toutputs=' + execcommand[:-1] + ')\n'

    i = -1 * len(alg.outputs)
    for out in alg.outputs:
        filename = (tokens[i])[1:-1]
        if tokens[i] == unicode(None):
            QMessageBox.critical(
                None, tr('Error'),
                tr('Cannot create unit test for that algorithm execution. The '
                   'output cannot be a temporary file'))
            return
        s += "\toutput=outputs['" + out.name + "']\n"
        if isinstance(out, (OutputNumber, OutputString)):
            s += 'self.assertTrue(' + unicode(out) + ', output.value)\n'
        if isinstance(out, OutputRaster):
            dataset = gdal.Open(filename, GA_ReadOnly)
            strhash = hash(unicode(dataset.ReadAsArray(0).tolist()))
            s += '\tself.assertTrue(os.path.isfile(output))\n'
            s += '\tdataset=gdal.Open(output, GA_ReadOnly)\n'
            s += '\tstrhash=hash(unicode(dataset.ReadAsArray(0).tolist()))\n'
            s += '\tself.assertEqual(strhash,' + unicode(strhash) + ')\n'
        if isinstance(out, OutputVector):
            layer = dataobjects.getObject(filename)
            fields = layer.pendingFields()
            s += '\tlayer=dataobjects.getObjectFromUri(output, True)\n'
            s += '\tfields=layer.pendingFields()\n'
            s += '\texpectednames=[' + ','.join(
                ["'" + unicode(f.name()) + "'" for f in fields]) + ']\n'
            s += '\texpectedtypes=[' + ','.join(
                ["'" + unicode(f.typeName()) + "'" for f in fields]) + ']\n'
            s += '\tnames=[unicode(f.name()) for f in fields]\n'
            s += '\ttypes=[unicode(f.typeName()) for f in fields]\n'
            s += '\tself.assertEqual(expectednames, names)\n'
            s += '\tself.assertEqual(expectedtypes, types)\n'
            features = vector.features(layer)
            numfeat = len(features)
            s += '\tfeatures=processing.features(layer)\n'
            s += '\tself.assertEqual(' + unicode(
                numfeat) + ', len(features))\n'
            if numfeat > 0:
                feature = features.next()
                attrs = feature.attributes()
                s += '\tfeature=features.next()\n'
                s += '\tattrs=feature.attributes()\n'
                s += '\texpectedvalues=[' + ','.join(
                    ['"' + unicode(attr) + '"' for attr in attrs]) + ']\n'
                s += '\tvalues=[unicode(attr) for attr in attrs]\n'
                s += '\tself.assertEqual(expectedvalues, values)\n'
                s += "\twkt='" + unicode(
                    feature.geometry().exportToWkt()) + "'\n"
                s += '\tself.assertEqual(wkt, \
                      unicode(feature.geometry().exportToWkt()))'

    dlg = ShowTestDialog(s)
    dlg.exec_()
##Burn cloud mask=name
##ParameterRaster|dataFile|An image file to burn the cloud mask into|False
##ParameterRaster|maskFile|Could mask from FMask|False
##*ParameterBoolean|maskNull|Mask FMask null pixels|True
##*ParameterBoolean|maskCloud|Mask FMask cloud pixels|True
##*ParameterBoolean|maskShadow|Mask FMask shadow pixels|True
##*ParameterBoolean|maskSnow|Mask FMask snow pixels|True
##*ParameterBoolean|maskWater|Mask FMask water pixels|False
##*ParameterBoolean|maskLand|Mask FMask land pixels|False
##OutputRaster|outputFile|Maked output image

from processing.tools import dataobjects, system

# Run GDAL warp to make sure that the mask file exactly aligns with the image file
progress.setText("Aligning mask raster to image raster...")
dataRaster = dataobjects.getObject(dataFile)
proj = dataRaster.crs().authid()
resolution = dataRaster.rasterUnitsPerPixelX()
bandCount = dataRaster.bandCount()
extent = dataobjects.extent([dataRaster])
warpMask = system.getTempFilename("tif")
params = {
    "INPUT": maskFile,
    "DEST_SRS": proj,
    "TR": resolution,
    "USE_RASTER_EXTENT": True,
    "RASTER_EXTENT": extent,
    "EXTENT_CRS": proj,
    "METHOD": 0,
    "RTYPE": 0,
    "OUTPUT": warpMask,