예제 #1
0
 def processAlgorithm(self, parameters, context, feedback):
     layer_a = self.parameterAsVectorLayer(parameters,self.LAYER_A,context)
     if not layer_a:
         raise QgsProcessingException("No Layer A")
     layer_b = self.parameterAsVectorLayer(parameters,self.LAYER_B,context)
     if not layer_b:
         raise QgsProcessingException("No Layer B")
     output = self.parameterAsOutputLayer(parameters,self.OUTPUT,context)
     # dest_crs = self.parameterAsCrs(parameters,self.CRS,context)
     
     out_fields = QgsFields()
     out_crs = layer_a.sourceCrs()
     (sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT,
             context, out_fields, QgsWkbTypes.MultiPolygon, out_crs)
             
     feedback = QgsProcessingMultiStepFeedback(2,feedback)
            
     diff = QgsProcessingUtils.generateTempFilename('diff.gpkg')
     qgsTreatments.applyDifference(layer_a,layer_b,diff,context=context,feedback=feedback)
     diff_layer = qgsUtils.loadVectorLayer(diff)
     
     feedback.setCurrentStep(1)
     
     layers = [ layer_a, diff_layer ]
     params = { MergeGeometryAlgorithm.LAYERS : layers,
         MergeGeometryAlgorithm.CRS : out_crs,
         MergeGeometryAlgorithm.OUTPUT : output }
     qgsTreatments.applyProcessingAlg("LPT",MergeGeometryAlgorithm.ALG_NAME,
         parameters,context=context,feedback=feedback)
             
     feedback.setCurrentStep(2)
     
     return {self.OUTPUT: output}
예제 #2
0
    def processAlgorithm(self, parameters, context, model_feedback):
        parameters['lsi'] = self.parameterAsRasterLayer(
            parameters, self.INPUT, context)
        if parameters['lsi'] is None:
            raise QgsProcessingException(
                self.invalidSourceError(parameters, self.INPUT))

        parameters['class'] = self.parameterAsFile(parameters, self.FILE,
                                                   context).source()
        if parameters['class'] is None:
            raise QgsProcessingException(
                self.invalidSourceError(parameters, self.FILE))

        feedback = QgsProcessingMultiStepFeedback(1, model_feedback)
        results = {}
        outputs = {}
        # Input
        alg_params = {'INPUT': inLayer.source()}
        outputs['open'] = self.raster2array(alg_params)
        #list_of_values=list(np.arange(10))
        #self.list_of_values=outputs['open'][outputs['open']>-9999].reshape(-1)
        #QgsMessageLog.logMessage(str(len(self.list_of_values)), 'MyPlugin', level=Qgis.Info)

        alg_params = {'INPUT': outputs['open'], 'INPUT1': parameters['class']}
        outputs['class'] = self.classification(alg_params)

        feedback.setCurrentStep(1)
        if feedback.isCanceled():
            return {}
        return results
예제 #3
0
def joinAll(validInputs, params, context, feedback):
    outp = QgsProcessing.TEMPORARY_OUTPUT
    totalValidInputs = len(validInputs)
    step = 0
    steps = totalValidInputs
    feedback = QgsProcessingMultiStepFeedback(steps, feedback)
    i = 0
    for k, v in validInputs.items():
        i = i + 1
        # if i == totalValidInputs:
        #   outp = params['OUTPUT']
        feedback.pushConsoleInfo(str(i))
        val = validInputs[k]
        layer = str(val[0])
        attr = str(val[1])
        if i == 1:
            result = layer
        else:
            step = step + 1
            feedback.setCurrentStep(step)
            result = joinAttrByLocation(result, layer, attr, [IGUALA],
                                        UNDISCARD_NONMATCHING, context,
                                        feedback)
            result = result['OUTPUT']

    return result
예제 #4
0
    def processAlgorithm(self, parameters, context, feedback):
        """
        Here is where the processing itself takes place.
        """
        layerHandler = LayerHandler()
        algRunner = AlgRunner()
        inputPolygonLyrList = self.parameterAsLayerList(
            parameters, self.INPUT_POLYGONS, context)
        constraintLineLyrList = self.parameterAsLayerList(
            parameters, self.CONSTRAINT_LINE_LAYERS, context)
        constraintPolygonLyrList = self.parameterAsLayerList(
            parameters, self.CONSTRAINT_POLYGON_LAYERS, context)
        if set(constraintPolygonLyrList).intersection(
                set(inputPolygonLyrList)):
            raise QgsProcessingException(
                self.
                tr('Input polygon layers must not be in constraint polygon list.'
                   ))
        onlySelected = self.parameterAsBool(parameters, self.SELECTED, context)
        boundaryLyr = self.parameterAsLayer(parameters,
                                            self.GEOGRAPHIC_BOUNDARY, context)
        # Compute the number of steps to display within the progress bar and
        # get features from source
        # alg steps:
        # 1- Build single polygon layer
        # 2- Compute center points
        # 3- Compute boundaries
        multiStepFeedback = QgsProcessingMultiStepFeedback(3, feedback)
        multiStepFeedback.setCurrentStep(0)
        multiStepFeedback.pushInfo(self.tr('Building single polygon layer'))
        singlePolygonLayer = layerHandler.getMergedLayer(
            inputPolygonLyrList,
            onlySelected=onlySelected,
            feedback=multiStepFeedback,
            context=context,
            algRunner=algRunner)
        multiStepFeedback.setCurrentStep(1)
        (output_center_point_sink,
         output_center_point_sink_id) = self.parameterAsSink(
             parameters, self.OUTPUT_CENTER_POINTS, context,
             singlePolygonLayer.fields(), QgsWkbTypes.Point,
             singlePolygonLayer.sourceCrs())
        (output_boundaries_sink,
         output_boundaries_sink_id) = self.parameterAsSink(
             parameters, self.OUTPUT_BOUNDARIES, context, QgsFields(),
             QgsWkbTypes.LineString, singlePolygonLayer.sourceCrs())
        layerHandler.getCentroidsAndBoundariesFromPolygons(
            singlePolygonLayer,
            output_center_point_sink,
            output_boundaries_sink,
            constraintLineLyrList=constraintLineLyrList,
            constraintPolygonLyrList=constraintPolygonLyrList,
            context=context,
            feedback=multiStepFeedback,
            algRunner=algRunner)

        return {
            self.OUTPUT_CENTER_POINTS: output_center_point_sink_id,
            self.OUTPUT_BOUNDARIES: output_boundaries_sink_id
        }
예제 #5
0
    def processAlgorithm(self, parameters, context, feedback):
        """
        Here is where the processing itself takes place.
        """
        layerHandler = LayerHandler()
        inputLyr = self.parameterAsVectorLayer(parameters, self.INPUT, context)
        if inputLyr is None:
            raise QgsProcessingException(
                self.invalidSourceError(parameters, self.INPUT))
        onlySelected = self.parameterAsBool(parameters, self.SELECTED, context)
        searchRadius = self.parameterAsDouble(parameters, self.SEARCH_RADIUS,
                                              context)
        # output flag type is a polygon because the flag will be a circle with
        # radius tol and center as the vertex
        self.prepareFlagSink(parameters, inputLyr, QgsWkbTypes.Point, context)
        # Compute the number of steps to display within the progress bar and
        # get features from source
        multiStepFeedback = QgsProcessingMultiStepFeedback(2, feedback)
        multiStepFeedback.setCurrentStep(0)
        vertexNearEdgeFlagDict = layerHandler.getVertexNearEdgeDict(
            inputLyr,
            searchRadius,
            onlySelected=onlySelected,
            feedback=multiStepFeedback,
            context=context)
        multiStepFeedback.setCurrentStep(1)
        self.raiseFeaturesFlags(inputLyr, vertexNearEdgeFlagDict,
                                multiStepFeedback)

        return {self.FLAGS: self.flag_id}
예제 #6
0
    def processAlgorithm(self, parameters, context, feedback):
        """
        Here is where the processing itself takes place.
        """
        layerHandler = LayerHandler()
        inputLineLyrList = self.parameterAsLayerList(parameters,
                                                     self.INPUT_LINES, context)
        inputPolygonLyrList = self.parameterAsLayerList(
            parameters, self.INPUT_POLYGONS, context)
        if inputLineLyrList + inputPolygonLyrList == []:
            raise QgsProcessingException(self.tr('Select at least one layer'))
        onlySelected = self.parameterAsBool(parameters, self.SELECTED, context)
        self.prepareFlagSink(parameters,
                             (inputLineLyrList + inputPolygonLyrList)[0],
                             QgsWkbTypes.Point, context)
        # Compute the number of steps to display within the progress bar and
        # get features from source
        multiStepFeedback = QgsProcessingMultiStepFeedback(2, feedback)
        multiStepFeedback.setCurrentStep(0)
        usharedIntersectionSet = layerHandler.getUnsharedVertexOnIntersections(
            inputLineLyrList,
            inputPolygonLyrList,
            onlySelected=onlySelected,
            feedback=multiStepFeedback)
        multiStepFeedback.setCurrentStep(1)
        self.raiseFeaturesFlags(usharedIntersectionSet, multiStepFeedback)

        return {self.FLAGS: self.flag_id}
예제 #7
0
 def getDuplicatedFeaturesDict(self, lyr, onlySelected = False, attributeBlackList = None, ignoreVirtualFields = True, excludePrimaryKeys = True, useAttributes=False, feedback = None):
     """
     returns geomDict = {
         'bbox_geom' : {geomKey : -list of duplicated feats-}
     }
     """
     geomDict = dict()
     isMulti = QgsWkbTypes.isMultiType(int(lyr.wkbType()))
     iterator, featCount = self.getFeatureList(lyr, onlySelected=onlySelected)
     size = 100/featCount if featCount else 0
     columns = self.getAttributesFromBlackList(lyr, attributeBlackList=attributeBlackList, ignoreVirtualFields=ignoreVirtualFields, excludePrimaryKeys=excludePrimaryKeys)
     multiStepFeedback = QgsProcessingMultiStepFeedback(2, feedback) if feedback else None
     multiStepFeedback.setCurrentStep(0)
     #builds bounding box dict to do a geos comparison for each feat in list
     bbDict = self.getFeaturesWithSameBoundingBox(iterator, isMulti, featCount, columns=columns, feedback=feedback)
     multiStepFeedback.setCurrentStep(1)
     for current, (key, featList) in enumerate(bbDict.items()):
         if feedback is not None and feedback.isCanceled():
             break
         if len(featList) > 1:
             duplicatedDict = self.searchDuplicatedFeatures(featList, columns=columns, useAttributes=useAttributes)
             geomDict.update(duplicatedDict)
         if feedback is not None:
             feedback.setProgress(size * current)
     return geomDict
예제 #8
0
    def processAlgorithm(self, parameters, context, feedback):
        """
        Here is where the processing itself takes place.
        """
        layerHandler = LayerHandler()
        inputLyr = self.parameterAsVectorLayer(parameters, self.INPUT, context)
        onlySelected = self.parameterAsBool(parameters, self.SELECTED, context)
        ignoreClosed = self.parameterAsBool(parameters, self.IGNORE_CLOSED,
                                            context)
        fixInput = self.parameterAsBool(parameters, self.TYPE, context)
        self.prepareFlagSink(parameters, inputLyr, QgsWkbTypes.Point, context)

        multiStepFeedback = QgsProcessingMultiStepFeedback(2, feedback)
        multiStepFeedback.setCurrentStep(0)
        flagDict = layerHandler.identifyAndFixInvalidGeometries(
            inputLyr=inputLyr,
            ignoreClosed=ignoreClosed,
            fixInput=fixInput,
            onlySelected=onlySelected,
            feedback=multiStepFeedback)
        multiStepFeedback.setCurrentStep(1)
        itemSize = len(flagDict)
        progressSize = 100 / itemSize if itemSize else 0
        for current, (key, outDict) in enumerate(flagDict.items()):
            if multiStepFeedback.isCanceled():
                break
            self.flagFeature(flagGeom=outDict['geom'],
                             flagText=outDict['reason'])
            multiStepFeedback.setProgress(current * progressSize)

        return {self.FLAGS: self.flag_id, self.OUTPUT: inputLyr}
예제 #9
0
    def processAlgorithm(self, params, context, feedback):
        steps = 0
        totalStpes = 4
        DISCARD = True
        UNDISCARD = False

        feedback = QgsProcessingMultiStepFeedback(totalStpes, feedback)

        steps = steps + 1
        feedback.setCurrentStep(steps)
        grid = createGrid(params['STUDY'], params['CELL_SIZE'], context,
                          feedback)

        gridNeto = grid

        # steps = steps+1
        # feedback.setCurrentStep(steps)
        # gridNeto = calculateArea(gridNeto['OUTPUT'], 'area_grid', context,
        #                          feedback)

        steps = steps + 1
        feedback.setCurrentStep(steps)
        gridNeto = selectByLocation(gridNeto['OUTPUT'], params['STUDY'],
                                    [INTERSECTA], context, feedback,
                                    params['OUTPUT'])

        # steps = steps+1
        # feedback.setCurrentStep(steps)
        # gridNeto = calculateField(gridNeto['OUTPUT'], 'id_grid', '$id', context,
        #                           feedback, params['OUTPUT'], type=1)

        return gridNeto
예제 #10
0
    def processAlgorithm(self, parameters, context, feedback):
        """
        Here is where the processing itself takes place.
        """
        layerHandler = LayerHandler()
        inputLyr = self.parameterAsVectorLayer(parameters, self.INPUT, context)
        if inputLyr is None:
            raise QgsProcessingException(
                self.invalidSourceError(parameters, self.INPUT))
        onlySelected = self.parameterAsBool(parameters, self.SELECTED, context)
        attributeBlackList = self.parameterAsFields(parameters,
                                                    self.ATTRIBUTE_BLACK_LIST,
                                                    context)
        ignoreVirtual = self.parameterAsBool(parameters,
                                             self.IGNORE_VIRTUAL_FIELDS,
                                             context)
        ignorePK = self.parameterAsBool(parameters, self.IGNORE_PK_FIELDS,
                                        context)
        # Compute the number of steps to display within the progress bar and
        # get features from source
        multiStepFeedback = QgsProcessingMultiStepFeedback(2, feedback)
        multiStepFeedback.setCurrentStep(0)
        duplicatedGeomDict = layerHandler.getDuplicatedFeaturesDict(
            inputLyr,
            onlySelected=onlySelected,
            attributeBlackList=attributeBlackList,
            excludePrimaryKeys=ignorePK,
            ignoreVirtualFields=ignoreVirtual,
            feedback=multiStepFeedback)
        multiStepFeedback.setCurrentStep(1)
        self.deleteDuplicatedFeaturesFlags(inputLyr, duplicatedGeomDict,
                                           multiStepFeedback)

        return {self.OUTPUT: inputLyr}
예제 #11
0
 def filterDangles(self, lyr, searchRadius, feedback = None):
     deleteList = []
     if feedback is not None:
         multiStepFeedback = QgsProcessingMultiStepFeedback(2, feedback)
         multiStepFeedback.setCurrentStep(0)
     else:
         multiStepFeedback = None
     spatialIdx, idDict = self.buildSpatialIndexAndIdDict(lyr, feedback=multiStepFeedback)
     if multiStepFeedback is not None:
         multiStepFeedback.setCurrentStep(1)
     featSize = len(idDict)
     size = 100 / featSize if featSize else 0
     for current, (id, feat) in enumerate(idDict.items()):
         if feedback is not None and feedback.isCanceled():
             break
         if id not in deleteList:
             buffer = feat.geometry().buffer(searchRadius, -1)
             bufferBB = buffer.boundingBox()
             #gets candidates from spatial index
             candidateIds = spatialIdx.intersects(bufferBB)
             for fid in candidateIds:
                 if fid != id and fid not in deleteList and buffer.intersects(feat.geometry()):
                     deleteList.append(fid)
         if feedback is not None:
             multiStepFeedback.setProgress(size * current)
     
     lyr.startEditing()
     lyr.beginEditCommand('Filter dangles')
     lyr.deleteFeatures(deleteList)
     lyr.commitChanges()
예제 #12
0
 def getUnifiedLayerFeatures(self, unifiedLyr, layerList, attributeTupple=False, attributeBlackList=None, onlySelected=False, parameterDict=None, feedback=None):
     parameterDict = {} if parameterDict is None else parameterDict
     featList = []
     blackList = attributeBlackList.split(',') if attributeBlackList is not None and ',' in attributeBlackList else []
     if feedback:
         multiStepFeedback = QgsProcessingMultiStepFeedback(len(layerList), feedback)
     for i, layer in enumerate(layerList):
         if feedback:
             if feedback.isCanceled():
                 break
             multiStepFeedback.setCurrentStep(i)
         # recording class name
         layername = layer.name()
         coordinateTransformer = self.getCoordinateTransformer(unifiedLyr, layer)
         iterator, size = self.getFeatureList(layer, onlySelected=onlySelected, returnSize=True)
         for current, feature in enumerate(iterator):
             if feedback:
                 if multiStepFeedback.isCanceled():
                     break
             newFeats = self.featureHandler.createUnifiedFeature(unifiedLyr, feature, layername,\
                                                                bList=blackList, \
                                                                attributeTupple=attributeTupple, \
                                                                parameterDict=parameterDict, \
                                                                coordinateTransformer=coordinateTransformer)
             featList += newFeats
             if feedback:
                 multiStepFeedback.setProgress(current*size)
     return featList
    def processAlgorithm(self, parameters, context, feedback):
        """
        Here is where the processing itself takes place.
        """
        algRunner = AlgRunner()
        inputLyr = self.parameterAsVectorLayer(parameters, self.INPUT, context)
        if inputLyr is None:
            raise QgsProcessingException(
                self.invalidSourceError(parameters, self.INPUT))
        onlySelected = self.parameterAsBool(parameters, self.SELECTED, context)
        multiStepFeedback = QgsProcessingMultiStepFeedback(2, feedback)
        multiStepFeedback.setCurrentStep(0)
        multiStepFeedback.pushInfo(
            self.tr(
                'Identifying duplicated geometries in layer {0}...').format(
                    inputLyr.name()))
        flagLyr = algRunner.runIdentifyDuplicatedGeometries(
            inputLyr,
            context,
            feedback=multiStepFeedback,
            onlySelected=onlySelected)

        multiStepFeedback.setCurrentStep(1)
        multiStepFeedback.pushInfo(
            self.tr('Removing duplicated geometries in layer {0}...').format(
                inputLyr.name()))
        self.removeFeatures(inputLyr, flagLyr, multiStepFeedback)

        return {self.OUTPUT: inputLyr}
예제 #14
0
 def runOverlay(self, lyr, overlayLyr, behavior, context, feedback):
     nSteps = 2 if behavior == 2 else 1
     localFeedback = QgsProcessingMultiStepFeedback(nSteps, feedback)
     localFeedback.setCurrentStep(0)
     # Intentional ifs not if else.
     if behavior in [
             OverlayElementsWithAreasAlgorithm.RemoveOutside,
             OverlayElementsWithAreasAlgorithm.OverlayAndKeep
     ]:
         outputLyr = self.algRunner.runClip(lyr,
                                            overlayLyr,
                                            context,
                                            feedback=localFeedback)
         if behavior == OverlayElementsWithAreasAlgorithm.RemoveOutside:
             return outputLyr
         localFeedback.setCurrentStep(1)
     if behavior in [
             OverlayElementsWithAreasAlgorithm.RemoveInside,
             OverlayElementsWithAreasAlgorithm.OverlayAndKeep
     ]:
         outputDiffLyr = self.algRunner.runSymDiff(lyr,
                                                   overlayLyr,
                                                   context,
                                                   feedback=localFeedback)
         if behavior == OverlayElementsWithAreasAlgorithm.RemoveInside:
             return outputDiffLyr
     if behavior == OverlayElementsWithAreasAlgorithm.OverlayAndKeep:
         outsideFeats = [i for i in outputDiffLyr.getFeatures()]
         outputLyr.startEditing()
         outputLyr.beginEditCommand('')
         outputLyr.addFeatures(outsideFeats)
         outputLyr.endEditCommand()
         outputLyr.commitChanges()
         return outputLyr
예제 #15
0
    def processAlgorithm(self, parameters, context, model_feedback):
        # Use a multi-step feedback, so that individual child algorithm progress reports are adjusted for the
        # overall progress through the model
        feedback = QgsProcessingMultiStepFeedback(2, model_feedback)
        results = {}
        outputs = {}

        # Download file
        alg_params = {
            'URL': parameters['DownloadURL'],
            'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT
        }
        outputs['DownloadFile'] = processing.run('native:filedownloader',
                                                 alg_params,
                                                 context=context,
                                                 feedback=feedback,
                                                 is_child_algorithm=True)

        feedback.setCurrentStep(1)
        if feedback.isCanceled():
            return {}

        # unzipconvert
        alg_params = {
            'INPUT': outputs['DownloadFile']['OUTPUT'],
            'OUTPUT': parameters['Outputfolder'],
            'TYPE': '.tif'
        }
        outputs['Unzipconvert'] = processing.run('script:unzipconvert',
                                                 alg_params,
                                                 context=context,
                                                 feedback=feedback,
                                                 is_child_algorithm=True)
        return results
예제 #16
0
    def processAlgorithm(self, parameters, context, feedback):

        feedback = QgsProcessingMultiStepFeedback(1, feedback)
        results = {}
        outputs = {}

        parameters['grid'] = self.parameterAsRasterLayer(
            parameters, self.INPUT1, context).source()
        if parameters['grid'] is None:
            raise QgsProcessingException(
                self.invalidSourceError(parameters, self.INPUT1))

        source = self.parameterAsVectorLayer(parameters, self.INPUT, context)
        parameters['points'] = source.source()
        #parameters['points']=self.asPythonString(parameters,self.INPUT, context)
        #print(parameters['points'])
        if parameters['points'] is None:
            raise QgsProcessingException(
                self.invalidSourceError(parameters, self.INPUT))

        parameters['poly'] = self.parameterAsExtent(parameters, self.EXTENT,
                                                    context)
        if parameters['poly'] is None:
            raise QgsProcessingException(
                self.invalidSourceError(parameters, self.EXTENT))

        parameters['field'] = self.parameterAsString(parameters, self.STRING,
                                                     context)
        if parameters['field'] is None:
            raise QgsProcessingException(
                self.invalidSourceError(parameters, self.STRING))

        # outFile = self.parameterAsOutputLayer(parameters, self.OUTPUT, context)
        # parameters['out'], outputFormat = GdalUtils.ogrConnectionStringAndFormat(outFile, context)
        # if parameters['out'] is None:
        #     raise QgsProcessingException(self.invalidSourceError(parameters, self.OUTPUT))

        parameters['out'] = self.parameterAsFileOutput(parameters, self.OUTPUT,
                                                       context)
        if parameters['out'] is None:
            raise QgsProcessingException(
                self.invalidSourceError(parameters, self.OUTPUT))

        # Intersectionpoly
        alg_params = {
            'STRING': parameters['field'],
            'INPUT_RASTER_LAYER': parameters['grid'],
            'INPUT_EXTENT': parameters['poly'],
            'INPUT_VECTOR_LAYER': parameters['points'],
            'OUTPUT': parameters['out']
        }
        self.extent(alg_params)
        outputs['cleaninventory'] = self.importingandcounting(alg_params)
        #del self.raster
        feedback.setCurrentStep(1)
        if feedback.isCanceled():
            return {}

        return results
예제 #17
0
 def make_inventory_from_processing(self,
                                    parent_folder,
                                    format_list,
                                    destination_folder=None,
                                    make_copy=False,
                                    onlyGeo=True,
                                    feedback=None):
     featList = []
     fileList = []
     format_set = self.get_format_set(format_list)
     tuple_list = [i for i in os.walk(parent_folder)]
     nSteps = len(tuple_list) if make_copy else len(tuple_list) - 1
     multiStepFeedback = QgsProcessingMultiStepFeedback(
         nSteps, feedback) if feedback else None
     for current_step, [root, dirs, files] in enumerate(tuple_list):
         if feedback is not None:
             if feedback.isCanceled():
                 return []
             multiStepFeedback.setCurrentStep(current_step)
         n_files = len(files)
         files_progress = 100 / n_files if n_files else 0
         for current, current_file in enumerate(files):
             if multiStepFeedback is not None and multiStepFeedback.isCanceled(
             ):
                 break
             extension = current_file.split('.')[-1]
             if extension not in format_set:
                 continue
             full_path = self.get_full_path(current_file, root)
             if gdal.Open(full_path) or ogr.Open(full_path):
                 bbox_geom, attributes = self.computeBoxAndAttributes(
                     None, full_path, extension, insertIntoMemory=False)
                 new_feat = self.get_new_feat(bbox_geom, attributes)
                 featList.append(new_feat)
                 fileList.append(full_path)
             if multiStepFeedback is not None:
                 multiStepFeedback.setProgress(files_progress * current)
     if make_copy:
         if multiStepFeedback is not None:
             multiStepFeedback.setCurrentStep(nSteps)
         copy_len = len(fileList)
         for current, file_ in fileList:
             if multiStepFeedback is not None and multiStepFeedback.isCanceled:
                 break
             try:
                 self.copy_single_file(file_, destination_folder)
                 if multiStepFeedback is not None:
                     multiStepFeedback.pushInfo(
                         self.tr(
                             'File {file} copied to {destination}').format(
                                 file=file_,
                                 destination=destination_folder))
             except Exception as e:
                 if multiStepFeedback is not None:
                     multiStepFeedback.pushInfo(
                         self.tr('Error copying file {file}: {exception}\n'
                                 ).format(file=file_,
                                          exception='\n'.join(e.args)))
     return featList
    def processAlgorithm(self, parameters, context, feedback):
        communes = self.parameterAsString(parameters, self.LISTE_CODE_INSEE,
                                          context)
        filtre = self.parameterAsString(parameters, self.FILTRE, context)
        date = self.parameterAsString(parameters, self.DATE, context)
        url = self.parameterAsString(parameters, self.URL_TEMPLATE, context)
        directory = Path(
            self.parameterAsString(parameters, self.DOSSIER, context))

        if not directory.exists():
            feedback.pushDebugInfo(
                "Création du répertoire {}".format(directory))
            os.makedirs(directory, exist_ok=True)

        filtre = [c.strip() for c in filtre.split(',')]

        communes = [c.strip() for c in communes.split(',')]
        departements = []
        self.results = {
            self.DOSSIER: str(directory),
            self.NB_COMMUNES: len(communes),
            self.NB_FEUILLES: 0,
            self.DEPARTEMENTS: "",
        }

        multi_feedback = QgsProcessingMultiStepFeedback(
            len(communes), feedback)

        for i, commune_insee in enumerate(communes):

            commune = Commune(commune_insee, date=date, base_url=url)
            if not self.download_commune(directory, commune, filtre,
                                         multi_feedback, context):
                multi_feedback.reportError("Erreur sur la commune {}".format(
                    commune.insee))
                break

            if multi_feedback.isCanceled():
                break

            multi_feedback.setCurrentStep(i)

            if commune.departement not in departements:
                departements.append(commune.departement)

        self.results[self.DEPARTEMENTS] = ','.join(departements)

        multi_feedback.pushInfo("\n")
        multi_feedback.pushInfo("\n")
        multi_feedback.pushInfo(
            "Téléchargement terminé pour {} communes".format(len(communes)))
        multi_feedback.pushInfo("{} feuilles".format(
            self.results[self.NB_FEUILLES]))
        multi_feedback.pushInfo("dans {}".format(str(directory)))
        multi_feedback.pushInfo("\n")
        multi_feedback.pushInfo("\n")
        return self.results
예제 #19
0
 def prepareInputLayers(self,
                        inputLayers,
                        stepConversionMap,
                        context=None,
                        feedback=None):
     """
     Prepare layers for a translation unit (step) to be executed (e.g. applies filters).
     :param inputLayers: (dict) a map from layer name to each vector layer contained by the
                         input datasource. 
     :param stepConversionMap: (dict) conversion map generated by Datasource Conversion tool
                               for a conversion step.
     :param context: (QgsProcessingContext) environment parameters in which processing tools are used.
     :param feedback: (QgsProcessingMultiStepFeedback) QGIS tool for progress tracking.
     :return: (dict) map of layers to have its features mapped to output format.
     """
     lh = LayerHandler()
     context = context if context is not None else QgsProcessingContext()
     layerFilters = stepConversionMap["filter"]["layer_filter"]
     spatialFilters = stepConversionMap["filter"]["spatial_filter"]
     # in case a selection of layers was made, only chosen layers should be translated
     inputLayers = inputLayers if layerFilters == {} else {
         layer: inputLayers[layer]
         for layer in layerFilters
     }
     multiStepFeedback = QgsProcessingMultiStepFeedback(
         len(inputLayers), feedback)
     if spatialFilters:
         # spatial filtering behaviour is set based on the modes defined in convertLayer2LayerAlgorithm
         behaviour = self.getSpatialFilterBehaviour(
             spatialFilters["predicate"] if "predicate" in
             spatialFilters else None)
         spatialFilterlLayer = self.prepareSpatialFilterLayer(
             spatialFilters, context)
     else:
         behaviour = None
         spatialFilters = None
     preparedLayers = dict()
     currentStep = 0
     for layer, vl in inputLayers.items():
         if feedback is not None:
             multiStepFeedback.setCurrentStep(currentStep)
             if multiStepFeedback.isCanceled():
                 break
             currentStep += 1
         translatedLayer = lh.prepareConversion(
             inputLyr=vl,
             context=context,
             inputExpression=layerFilters[layer]["expression"]
             if layer in layerFilters else None,
             filterLyr=spatialFilterlLayer,
             behavior=behaviour,
             conversionMap=stepConversionMap,
             feedback=multiStepFeedback if feedback is not None else None)
         if translatedLayer.featureCount() > 0:
             preparedLayers[layer] = translatedLayer
     return preparedLayers
예제 #20
0
    def processAlgorithm(self, parameters, context, model_feedback):
        # Use a multi-step feedback, so that individual child algorithm progress reports are adjusted for the
        # overall progress through the model
        feedback = QgsProcessingMultiStepFeedback(3, model_feedback)
        results = {}
        outputs = {}

        # Reproject layer
        alg_params = {
            'INPUT': parameters['roadnetwork'],
            'TARGET_CRS': 'ProjectCrs',
            'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT
        }
        outputs['ReprojectLayer'] = processing.run('native:reprojectlayer',
                                                   alg_params,
                                                   context=context,
                                                   feedback=feedback,
                                                   is_child_algorithm=True)

        feedback.setCurrentStep(1)
        if feedback.isCanceled():
            return {}

        # Reproject layer
        alg_params = {
            'INPUT': parameters['region'],
            'TARGET_CRS': 'ProjectCrs',
            'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT
        }
        outputs['ReprojectLayer'] = processing.run('native:reprojectlayer',
                                                   alg_params,
                                                   context=context,
                                                   feedback=feedback,
                                                   is_child_algorithm=True)

        feedback.setCurrentStep(2)
        if feedback.isCanceled():
            return {}

        # Sum line lengths
        alg_params = {
            'COUNT_FIELD': 'COUNT',
            'LEN_FIELD': 'LENGTH',
            'LINES': outputs['ReprojectLayer']['OUTPUT'],
            'POLYGONS': outputs['ReprojectLayer']['OUTPUT'],
            'OUTPUT': parameters['OutputSumRoadNetworkLength']
        }
        outputs['SumLineLengths'] = processing.run('qgis:sumlinelengths',
                                                   alg_params,
                                                   context=context,
                                                   feedback=feedback,
                                                   is_child_algorithm=True)
        results['OutputSumRoadNetworkLength'] = outputs['SumLineLengths'][
            'OUTPUT']
        return results
    def processAlgorithm(self, parameters, context, feedback):
        """
        Here is where the processing itself takes place.
        """
        layerHandler = LayerHandler()
        algRunner = AlgRunner()
        inputLyrList = self.parameterAsLayerList(parameters, self.INPUTLAYERS,
                                                 context)
        if inputLyrList is None or inputLyrList == []:
            raise QgsProcessingException(
                self.invalidSourceError(parameters, self.INPUTLAYERS))
        onlySelected = self.parameterAsBool(parameters, self.SELECTED, context)
        tol = self.parameterAsDouble(parameters, self.TOLERANCE, context)

        multiStepFeedback = QgsProcessingMultiStepFeedback(5, feedback)
        multiStepFeedback.setCurrentStep(0)
        multiStepFeedback.pushInfo(self.tr('Building unified layer...'))
        coverage = layerHandler.createAndPopulateUnifiedVectorLayer(
            inputLyrList,
            geomType=QgsWkbTypes.MultiPolygon,
            onlySelected=onlySelected,
            feedback=multiStepFeedback)

        multiStepFeedback.setCurrentStep(1)
        multiStepFeedback.pushInfo(
            self.tr('Identifying dangles on {layer}...').format(
                layer=coverage.name()))
        dangleLyr = algRunner.runIdentifyDangles(coverage,
                                                 tol,
                                                 context,
                                                 feedback=multiStepFeedback,
                                                 onlySelected=onlySelected)

        multiStepFeedback.setCurrentStep(2)
        layerHandler.filterDangles(dangleLyr, tol, feedback=multiStepFeedback)

        multiStepFeedback.setCurrentStep(3)
        multiStepFeedback.pushInfo(
            self.tr('Snapping layer {layer} to dangles...').format(
                layer=coverage.name()))
        algRunner.runSnapLayerOnLayer(coverage,
                                      dangleLyr,
                                      tol,
                                      context,
                                      feedback=multiStepFeedback,
                                      onlySelected=onlySelected,
                                      behavior=0)

        multiStepFeedback.setCurrentStep(4)
        multiStepFeedback.pushInfo(self.tr('Updating original layers...'))
        layerHandler.updateOriginalLayersFromUnifiedLayer(
            inputLyrList, coverage, feedback=multiStepFeedback)

        return {self.INPUTLAYERS: inputLyrList}
    def processAlgorithm(self, parameters, context, model_feedback):
        # Use a multi-step feedback, so that individual child algorithm progress reports are adjusted for the
        # overall progress through the model
        feedback = QgsProcessingMultiStepFeedback(3, model_feedback)
        results = {}
        outputs = {}

        # Da poligoni a linee
        alg_params = {
            'INPUT': parameters['poligonidellaisocrona'],
            'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT
        }
        outputs['DaPoligoniALinee'] = processing.run('native:polygonstolines',
                                                     alg_params,
                                                     context=context,
                                                     feedback=feedback,
                                                     is_child_algorithm=True)

        feedback.setCurrentStep(1)
        if feedback.isCanceled():
            return {}

        # Poligonizza
        alg_params = {
            'INPUT': outputs['DaPoligoniALinee']['OUTPUT'],
            'KEEP_FIELDS': False,
            'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT
        }
        outputs['Poligonizza'] = processing.run('qgis:polygonize',
                                                alg_params,
                                                context=context,
                                                feedback=feedback,
                                                is_child_algorithm=True)

        feedback.setCurrentStep(2)
        if feedback.isCanceled():
            return {}

        # Unione
        alg_params = {
            'INPUT': parameters['poligonidellaisocrona'],
            'OVERLAY': outputs['Poligonizza']['OUTPUT'],
            'OVERLAY_FIELDS_PREFIX': '',
            'OUTPUT': parameters['Output']
        }
        outputs['Unione'] = processing.run('native:union',
                                           alg_params,
                                           context=context,
                                           feedback=feedback,
                                           is_child_algorithm=True)
        results['Output'] = outputs['Unione']['OUTPUT']
        return results
예제 #23
0
    def processAlgorithm(self, parameters, context, feedback):
        """
        Here is where the processing itself takes place.
        """
        layerCsv = self.parameterAsString(parameters, self.INPUTLAYERS,
                                          context)
        algParameterDict = self.loadAlgorithmParametersDict(
            parameters, context, feedback)
        algName = self.parameterAsString(parameters, self.ALG_NAME, context)
        inputKey = self.parameterAsString(parameters,
                                          self.INPUT_LAYER_PARAMETER_NAME,
                                          context)
        outputKey = self.parameterAsString(parameters,
                                           self.OUTPUT_LAYER_PARAMETER_NAME,
                                           context)
        layerNameList = layerCsv.split(',')
        nSteps = len(layerNameList)
        if not nSteps:
            return {self.OUTPUT: None}
        multiStepFeedback = QgsProcessingMultiStepFeedback(nSteps, feedback)
        for idx, layerName in enumerate(layerNameList):
            multiStepFeedback.setCurrentStep(idx)
            multiStepFeedback.pushInfo(
                self.
                tr('Step {idx}/{total}: Running algorithm {algName} on {layerName}'
                   ).format(idx=idx,
                            total=nSteps,
                            algName=algName,
                            layerName=layerName))
            if QgsProcessingUtils.mapLayerFromString(layerName,
                                                     context) is None:
                multiStepFeedback.pushInfo(
                    self.tr('Layer {layerName} not found. Skipping step.').
                    format(layerName=layerName))
                continue

            currentDict = dict(algParameterDict)  #copy of the dict
            currentDict[inputKey] = layerName
            output = self.runProcessingAlg(algName,
                                           outputKey,
                                           currentDict,
                                           context=context,
                                           feedback=multiStepFeedback)
            outputLyr = QgsProcessingUtils.mapLayerFromString(
                output, context) if isinstance(output, str) else output
            if outputLyr is None:
                continue
            if self.flagSink is None:
                self.prepareFlagSink(parameters, outputLyr, context)
            self.flagFeatures(outputLyr, algName, layerName, context)

        return {self.OUTPUT: self.flag_id}
예제 #24
0
    def processAlgorithm(self, parameters, context, feedback):
        """
        Here is where the processing itself takes place.
        """

        inputLyrList = self.parameterAsLayerList(parameters, self.INPUTLAYERS,
                                                 context)
        if inputLyrList == []:
            raise QgsProcessingException(
                self.invalidSourceError(parameters, self.INPUTLAYERS))
        onlySelected = self.parameterAsBool(parameters, self.SELECTED, context)
        self.prepareFlagSink(parameters, inputLyrList[0], QgsWkbTypes.Polygon,
                             context)
        # Compute the number of steps to display within the progress bar and
        # get features from source
        geomDict = dict()
        multiStepFeedback = QgsProcessingMultiStepFeedback(
            len(inputLyrList) + 1, feedback)
        for currentLyrIdx, lyr in enumerate(inputLyrList):
            multiStepFeedback.setCurrentStep(currentLyrIdx)
            featIterator, total = self.getIteratorAndFeatureCount(
                lyr, onlySelected=onlySelected)
            size = 100 / total if total else 0
            lyrName = lyr.name()
            for current, feat in enumerate(featIterator):
                # Stop the algorithm if cancel button has been clicked
                if multiStepFeedback.isCanceled():
                    break
                geom = feat.geometry()
                geomKey = geom.asWkb()
                if geomKey not in geomDict:
                    geomDict[geomKey] = []
                geomDict[geomKey].append({'feat': feat, 'layerName': lyrName})
                # # Update the progress bar
                multiStepFeedback.setProgress(current * size)
        for v in geomDict.values():
            if multiStepFeedback.isCanceled():
                break
            if len(v) > 1:
                flagStrList = [
                    '{lyrName} (id={id})'.format(lyrName=featDict['layerName'],
                                                 id=featDict['feat'].id())
                    for featDict in v
                ]
                flagStr = ', '.join(flagStrList)
                flagText = self.tr(
                    'Features from coverage with same geometry: {0}.').format(
                        flagStr)
                self.flagFeature(v[0]['feat'].geometry(), flagText)

        return {self.FLAGS: self.flag_id}
예제 #25
0
    def processAlgorithm(self, parameters, context, model_feedback):
        # Use a multi-step feedback, so that individual child algorithm progress reports are adjusted for the
        # overall progress through the model
        steps = len(parameters['INPUT'])
        feedback = QgsProcessingMultiStepFeedback(steps, model_feedback)

        results = {'OUTPUT': []}
        outputs = {'ClipRasterByMaskLayer': []}
        i = 0
        feedback.pushDebugInfo("OUTFOLDER: " + str(parameters['OUTFOLDER']))
        for filename in parameters['INPUT']:
            if os.path.isdir(str(parameters['OUTFOLDER'])):
                file, ext = os.path.splitext(os.path.basename(filename))
                outfilename = os.path.join(parameters['OUTFOLDER'],
                                           file + "_clipped" + ext)
            else:
                outfilename = QgsProcessing.TEMPORARY_OUTPUT
            # Clip raster by mask layer
            alg_params = {
                'ALPHA_BAND': False,
                'CROP_TO_CUTLINE': True,
                'DATA_TYPE': 0,
                'EXTRA': '',
                'INPUT': filename,
                'KEEP_RESOLUTION': False,
                'MASK': parameters['Masklayer'],
                'MULTITHREADING': False,
                'NODATA': None,
                'OPTIONS': '',
                'SET_RESOLUTION': False,
                'SOURCE_CRS': None,
                'TARGET_CRS': None,
                'X_RESOLUTION': None,
                'Y_RESOLUTION': None,
                'OUTPUT': outfilename
            }
            outputs['ClipRasterByMaskLayer'].append(
                processing.run('gdal:cliprasterbymasklayer',
                               alg_params,
                               context=context,
                               feedback=feedback,
                               is_child_algorithm=True))
            i = i + 1
            feedback.setCurrentStep(i)
            if feedback.isCanceled():
                return {}

        for clipresult in outputs['ClipRasterByMaskLayer']:
            results['OUTPUT'].append(clipresult['OUTPUT'])
        return results
예제 #26
0
    def processAlgorithm(self, params, context, feedback):
        steps = 0
        totalStpes = 5
        feedback = QgsProcessingMultiStepFeedback(totalStpes, feedback)

        p = str(params['VALUE'])

        if p == 'PQ1' or p == 'PQ2' or p == 'PQ3' or p == 'PQ4' or p == 'Pc' or p == 'Po':
            self.PT = str('Pt')
        elif p == 'VQ1' or p == 'VQ2' or p == 'VQ3' or p == 'VQ4' or p == 'Vc' or p == 'Vo':
            self.PT = str('Vt')
        else:
            self.PT = str('0')

        pt = self.PT

        print("El valor para " + p + " para pt es " + str(pt))

        steps = steps + 1
        feedback.setCurrentStep(steps)
        pp = '(' + p + ' * 100) / sum(' + p + ')'
        result = calculateField(params['ICV'], 'pp', pp, context, feedback)

        steps = steps + 1
        feedback.setCurrentStep(steps)
        ppt = '(' + pt + ' * 100) / sum(' + pt + ')'
        result = calculateField(result['OUTPUT'], 'ppt', ppt, context,
                                feedback)

        steps = steps + 1
        feedback.setCurrentStep(steps)
        iseg = 'sum(abs(' + pp + ' - ' + ppt + ')) * 0.5'
        result = calculateField(result['OUTPUT'], 'ISEG', iseg, context,
                                feedback)

        steps = steps + 1
        feedback.setCurrentStep(steps)
        isea = 'pp / ppt'
        result = calculateField(result['OUTPUT'], 'ISEA', isea, context,
                                feedback)

        steps = steps + 1
        feedback.setCurrentStep(steps)
        index = 'isea * 1.0'
        result = calculateField(result['OUTPUT'], NAMES_INDEX['ID17'][0],
                                index, context, feedback, params['OUTPUT'])

        self.FINAL_NAME = result['OUTPUT']

        return result
예제 #27
0
    def processAlgorithm(self, parameters, context, model_feedback):
        # Use a multi-step feedback, so that individual child algorithm progress reports are adjusted for the
        # overall progress through the model
        feedback = QgsProcessingMultiStepFeedback(2, model_feedback)
        results = {}
        outputs = {}

        # Reclassify with table
        alg_params = {
            'DATA_TYPE': 5,
            'INPUT_RASTER': parameters['RastertoReclassify'],
            'NODATA_FOR_MISSING': False,
            'NO_DATA': -9999,
            'RANGE_BOUNDARIES': 0,
            'RASTER_BAND': 1,
            'TABLE': [-1, 0, 0, 0, 1, 1],
            'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT
        }
        outputs['ReclassifyWithTable'] = processing.run(
            'native:reclassifybytable',
            alg_params,
            context=context,
            feedback=feedback,
            is_child_algorithm=True)

        feedback.setCurrentStep(1)
        if feedback.isCanceled():
            return {}

        # Zonal Histogram
        alg_params = {
            'COLUMN_PREFIX':
            QgsExpression('substr( parameter(\'RastertoReclassify\'), 10, 8)').
            evaluate(),
            'INPUT_RASTER':
            outputs['ReclassifyWithTable']['OUTPUT'],
            'INPUT_VECTOR':
            parameters['inputsitespolygon'],
            'RASTER_BAND':
            1,
            'OUTPUT':
            parameters['PolygonsZonalHistogram']
        }
        outputs['ZonalHistogram'] = processing.run('native:zonalhistogram',
                                                   alg_params,
                                                   context=context,
                                                   feedback=feedback,
                                                   is_child_algorithm=True)
        results['PolygonsZonalHistogram'] = outputs['ZonalHistogram']['OUTPUT']
        return results
예제 #28
0
    def processAlgorithm(self, parameters, context, model_feedback):
        # Use a multi-step feedback, so that individual child algorithm progress reports are adjusted for the
        # overall progress through the model
        feedback = QgsProcessingMultiStepFeedback(2, model_feedback)
        results = {}
        outputs = {}

        # v.split
        temporary_path = os.path.join(gettempdir(), 'segmented_layer.gpkg')
        try:  #valid since 3.6
            # Division des lignes par longueur maximale
            alg_params = {
                'INPUT': parameters[self.LINES],
                'LENGTH': parameters[self.SEGMENT_SIZE],
                'OUTPUT': temporary_path
            }
            output = processing.run('native:splitlinesbylength',
                                    alg_params,
                                    context=context,
                                    feedback=feedback,
                                    is_child_algorithm=True)
            outputs['segmentedLayer'] = output['OUTPUT']
        except:  #valid for 3.4-ltr + Grass
            # Division des lignes par longueur maximale
            alg_params = {
                'INPUT': parameters[self.LINES],
                'DISTANCE': parameters[self.SEGMENT_SIZE],
                'OUTPUT': temporary_path
            }
            output = processing.run('native:segmentizebymaxdistance',
                                    alg_params,
                                    context=context,
                                    feedback=feedback,
                                    is_child_algorithm=True)
            outputs['segmentedLayer'] = output['OUTPUT']

        feedback.setCurrentStep(1)
        if feedback.isCanceled():
            return {}
        parameters['LINES'] = outputs['segmentedLayer']
        # Graduated Lines Map
        outputs['GraduatedLinesMap'] = processing.run(
            'visualist:graduatedlinemap',
            parameters,
            context=context,
            feedback=feedback,
            is_child_algorithm=True)
        results['LineMap'] = outputs['GraduatedLinesMap']['OUTPUT_LINE']
        return results
예제 #29
0
    def processAlgorithm(self, parameters, context, feedback):
        """
        Here is where the processing itself takes place.
        """
        layerHandler = LayerHandler()
        self.algRunner = AlgRunner()

        inputLyr = self.parameterAsVectorLayer(parameters, self.INPUT, context)
        if inputLyr is None:
            raise QgsProcessingException(
                self.invalidSourceError(parameters, self.INPUT))
        overlayLyr = self.parameterAsVectorLayer(parameters, self.OVERLAY,
                                                 context)
        if overlayLyr is None:
            raise QgsProcessingException(
                self.invalidSourceError(parameters, self.OVERLAY))
        onlySelected = self.parameterAsBool(parameters, self.SELECTED, context)
        onlySelectedOverlay = self.parameterAsBool(parameters,
                                                   self.SELECTED_OVERLAY,
                                                   context)
        behavior = self.parameterAsEnum(parameters, self.BEHAVIOR, context)

        multiStepFeedback = QgsProcessingMultiStepFeedback(4, feedback)
        multiStepFeedback.setCurrentStep(0)
        multiStepFeedback.pushInfo(self.tr('Populating temp layer...'))
        auxLyr = layerHandler.createAndPopulateUnifiedVectorLayer(
            [inputLyr],
            geomType=inputLyr.wkbType(),
            onlySelected=onlySelected,
            feedback=multiStepFeedback)
        multiStepFeedback.setCurrentStep(1)
        if onlySelectedOverlay:
            overlayLyr = layerHandler.createAndPopulateUnifiedVectorLayer(
                [overlayLyr],
                geomType=overlayLyr.wkbType(),
                onlySelected=onlySelectedOverlay,
                feedback=multiStepFeedback)
            overlayLyr.startEditing()
            overlayLyr.renameAttribute(0, 'fid')
            overlayLyr.renameAttribute(1, 'cl')
            overlayLyr.commitChanges()
        # 1- check method
        # 2- if overlay and keep, use clip and symetric difference
        # 3- if remove outside, use clip
        # 4- if remove inside, use symetric difference
        multiStepFeedback.setCurrentStep(2)
        multiStepFeedback.pushInfo(self.tr('Running overlay...'))
        outputLyr = self.runOverlay(auxLyr, overlayLyr, behavior, context,
                                    multiStepFeedback)
        multiStepFeedback.setCurrentStep(3)
        multiStepFeedback.pushInfo(self.tr('Updating original layer...'))
        layerHandler.updateOriginalLayersFromUnifiedLayer(
            [inputLyr],
            outputLyr,
            feedback=multiStepFeedback,
            onlySelected=onlySelected)

        return {self.OUTPUT: inputLyr}
    def processAlgorithm(self, parameters, context, model_feedback):
        # Use a multi-step feedback, so that individual child algorithm progress reports are adjusted for the
        # overall progress through the model
        steps = len(parameters['INPUT'])
        feedback = QgsProcessingMultiStepFeedback(steps, model_feedback)
        results = {'OUTFOLDER': "", 'OUTPUT': []}

        i = 0
        outputs = {'cliprasterbyextent': {}, 'LISTOFCLIPPEDRASTERS': []}
        i = 0

        #pattern = re.compile(r".+_([0-9]{8})T[0-9]{6}_(B[0-9]{1}[0-9,A]{1})\.jp2$")
        pattern = re.compile(parameters['MASK'])
        for filename in parameters['INPUT']:
            match = pattern.match(filename)
            if not match:
                continue
            clippedfilename = os.path.join(
                parameters['OUTFOLDER'],
                match.group(1) + "_" + match.group(2) + ".tif")

            alg_params = {
                'DATA_TYPE': 0,
                'EXTRA': '',
                'INPUT': filename,
                'NODATA': None,
                'OPTIONS': '',
                'PROJWIN': parameters['CLIPEXTENT'],
                'OUTPUT': clippedfilename,
            }
            if feedback.isCanceled():
                return {}
            outputs['cliprasterbyextent'][filename] = processing.run(
                'gdal:cliprasterbyextent',
                alg_params,
                context=context,
                feedback=feedback,
                is_child_algorithm=True)['OUTPUT']
            feedback.setCurrentStep(i)
            i = i + 1
            outputs['LISTOFCLIPPEDRASTERS'].append(clippedfilename)

        results['OUTFOLDER'] = parameters['OUTFOLDER']
        results['OUTPUT'] = outputs['LISTOFCLIPPEDRASTERS']
        return results
예제 #31
0
    def runAlgorithm(self):
        alg_parameters = []

        feedback = self.createFeedback()

        load_layers = self.mainWidget().checkLoadLayersOnCompletion.isChecked()
        project = QgsProject.instance() if load_layers else None

        for row in range(self.mainWidget().tblParameters.rowCount()):
            col = 0
            parameters = {}
            for param in self.algorithm().parameterDefinitions():
                if param.flags() & QgsProcessingParameterDefinition.FlagHidden or param.isDestination():
                    continue
                wrapper = self.mainWidget().wrappers[row][col]
                parameters[param.name()] = wrapper.parameterValue()
                if not param.checkValueIsAcceptable(wrapper.parameterValue()):
                    self.messageBar().pushMessage("", self.tr('Wrong or missing parameter value: {0} (row {1})').format(
                        param.description(), row + 1),
                        level=Qgis.Warning, duration=5)
                    return
                col += 1
            count_visible_outputs = 0
            for out in self.algorithm().destinationParameterDefinitions():
                if out.flags() & QgsProcessingParameterDefinition.FlagHidden:
                    continue

                count_visible_outputs += 1
                widget = self.mainWidget().tblParameters.cellWidget(row, col)
                text = widget.getValue()
                if out.checkValueIsAcceptable(text):
                    if isinstance(out, (QgsProcessingParameterRasterDestination,
                                        QgsProcessingParameterVectorDestination,
                                        QgsProcessingParameterFeatureSink)):
                        # load rasters and sinks on completion
                        parameters[out.name()] = QgsProcessingOutputLayerDefinition(text, project)
                    else:
                        parameters[out.name()] = text
                    col += 1
                else:
                    self.messageBar().pushMessage("", self.tr('Wrong or missing output value: {0} (row {1})').format(
                        out.description(), row + 1),
                        level=Qgis.Warning, duration=5)
                    return

            alg_parameters.append(parameters)

        task = QgsScopedProxyProgressTask(self.tr('Batch Processing - {0}').format(self.algorithm().displayName()))
        multi_feedback = QgsProcessingMultiStepFeedback(len(alg_parameters), feedback)
        feedback.progressChanged.connect(task.setProgress)

        with OverrideCursor(Qt.WaitCursor):

            self.mainWidget().setEnabled(False)
            self.cancelButton().setEnabled(True)

            # Make sure the Log tab is visible before executing the algorithm
            try:
                self.showLog()
                self.repaint()
            except:
                pass

            start_time = time.time()

            algorithm_results = []
            for count, parameters in enumerate(alg_parameters):
                if feedback.isCanceled():
                    break
                self.setProgressText(QCoreApplication.translate('BatchAlgorithmDialog', '\nProcessing algorithm {0}/{1}…').format(count + 1, len(alg_parameters)))
                self.setInfo(self.tr('<b>Algorithm {0} starting&hellip;</b>').format(self.algorithm().displayName()), escapeHtml=False)
                multi_feedback.setCurrentStep(count)

                parameters = self.algorithm().preprocessParameters(parameters)

                feedback.pushInfo(self.tr('Input parameters:'))
                feedback.pushCommandInfo(pformat(parameters))
                feedback.pushInfo('')

                # important - we create a new context for each iteration
                # this avoids holding onto resources and layers from earlier iterations,
                # and allows batch processing of many more items then is possible
                # if we hold on to these layers
                context = dataobjects.createContext(feedback)

                alg_start_time = time.time()
                ret, results = execute(self.algorithm(), parameters, context, multi_feedback)
                if ret:
                    self.setInfo(QCoreApplication.translate('BatchAlgorithmDialog', 'Algorithm {0} correctly executed…').format(self.algorithm().displayName()), escapeHtml=False)
                    feedback.pushInfo(
                        self.tr('Execution completed in {0:0.2f} seconds'.format(time.time() - alg_start_time)))
                    feedback.pushInfo(self.tr('Results:'))
                    feedback.pushCommandInfo(pformat(results))
                    feedback.pushInfo('')
                    algorithm_results.append(results)
                else:
                    break

                handleAlgorithmResults(self.algorithm(), context, multi_feedback, False)

        feedback.pushInfo(self.tr('Batch execution completed in {0:0.2f} seconds'.format(time.time() - start_time)))
        task = None

        self.finish(algorithm_results)
        self.cancelButton().setEnabled(False)
예제 #32
0
    def runAlgorithm(self):
        alg_parameters = []

        feedback = self.createFeedback()

        load_layers = self.mainWidget().checkLoadLayersOnCompletion.isChecked()
        project = QgsProject.instance() if load_layers else None

        for row in range(self.mainWidget().batchRowCount()):
            parameters = self.mainWidget().parametersForRow(row, destinationProject=project, warnOnInvalid=True)
            alg_parameters.append(parameters)

        task = QgsScopedProxyProgressTask(self.tr('Batch Processing - {0}').format(self.algorithm().displayName()))
        multi_feedback = QgsProcessingMultiStepFeedback(len(alg_parameters), feedback)
        feedback.progressChanged.connect(task.setProgress)

        with OverrideCursor(Qt.WaitCursor):

            self.mainWidget().setEnabled(False)
            self.cancelButton().setEnabled(True)

            # Make sure the Log tab is visible before executing the algorithm
            try:
                self.showLog()
                self.repaint()
            except:
                pass

            start_time = time.time()

            algorithm_results = []
            for count, parameters in enumerate(alg_parameters):
                if feedback.isCanceled():
                    break
                self.setProgressText(
                    QCoreApplication.translate('BatchAlgorithmDialog', '\nProcessing algorithm {0}/{1}…').format(
                        count + 1, len(alg_parameters)))
                self.setInfo(self.tr('<b>Algorithm {0} starting&hellip;</b>').format(self.algorithm().displayName()),
                             escapeHtml=False)
                multi_feedback.setCurrentStep(count)

                parameters = self.algorithm().preprocessParameters(parameters)

                feedback.pushInfo(self.tr('Input parameters:'))
                feedback.pushCommandInfo(pformat(parameters))
                feedback.pushInfo('')

                # important - we create a new context for each iteration
                # this avoids holding onto resources and layers from earlier iterations,
                # and allows batch processing of many more items then is possible
                # if we hold on to these layers
                context = dataobjects.createContext(feedback)

                alg_start_time = time.time()
                ret, results = execute(self.algorithm(), parameters, context, multi_feedback)
                if ret:
                    self.setInfo(
                        QCoreApplication.translate('BatchAlgorithmDialog', 'Algorithm {0} correctly executed…').format(
                            self.algorithm().displayName()), escapeHtml=False)
                    feedback.pushInfo(
                        self.tr('Execution completed in {0:0.2f} seconds'.format(time.time() - alg_start_time)))
                    feedback.pushInfo(self.tr('Results:'))
                    feedback.pushCommandInfo(pformat(results))
                    feedback.pushInfo('')
                    algorithm_results.append(results)
                else:
                    break

                handleAlgorithmResults(self.algorithm(), context, multi_feedback, False, parameters)

        feedback.pushInfo(self.tr('Batch execution completed in {0:0.2f} seconds'.format(time.time() - start_time)))
        task = None

        self.finish(algorithm_results)
        self.cancelButton().setEnabled(False)