Example #1
0
 def processAlgorithm(self, progress):
     layer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT_LAYER))
     fieldName = self.getParameterValue(self.FIELD_NAME)
     outputFile = self.getOutputValue(self.OUTPUT)
     values = utils.getUniqueValues(layer, layer.fieldNameIndex(fieldName))
     self.createHTML(outputFile, values)
     self.setOutputValue(self.TOTAL_VALUES, len(values))
     self.setOutputValue(self.UNIQUE_VALUES, ";".join([unicode(v) for v in values]))
Example #2
0
    def processAlgorithm(self, progress):
        filename = self.getParameterValue(self.INPUT)

        layer = QGisLayers.getObjectFromUri(filename)
        field = self.getParameterValue(self.FIELD)
        method = self.getParameterValue(self.METHOD)

        layer.removeSelection()
        index = layer.fieldNameIndex(field)

        unique = utils.getUniqueValues(layer, index)
        featureCount = layer.featureCount()

        value = int(self.getParameterValue(self.NUMBER))
        if method == 0:
            if value > featureCount:
                raise GeoAlgorithmExecutionException("Selected number is greater that feature count. Choose lesser value and try again.")
        else:
            if value > 100:
                raise GeoAlgorithmExecutionException("Persentage can't be greater than 100. Set corrent value and try again.")
            value = value / 100.0

        selran = []
        inFeat = QgsFeature()

        current = 0
        total = 100.0 / float(featureCount * len(unique))

        features = QGisLayers.features(layer)

        if not len(unique) == featureCount:
            for i in unique:
                FIDs= []
                for inFeat in features:
                    attrs = inFeat.attributes()
                    if attrs[index] == i:
                        FIDs.append(inFeat.id())
                    current += 1
                    progress.setPercentage(int(current * total))

                if method == 1:
                    selValue = int(round(value * len(FIDs), 0))
                else:
                    selValue = value

                if selValue >= len(FIDs):
                    selFeat = FIDs
                else:
                    selFeat = random.sample(FIDs, selValue)

                selran.extend(selFeat)
            layer.setSelectedFeatures(selran)
        else:
            layer.setSelectedFeatures(range(0, featureCount))

        self.setOutputValue(self.OUTPUT, filename)
    def processAlgorithm(self, progress):
        layer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT))
        output = self.getOutputValue(self.OUTPUT)
        fieldName = self.getParameterValue(self.FIELD)

        geomType = self.singleToMultiGeom(layer.dataProvider().geometryType())

        writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(
            layer.pendingFields().toList(), geomType, layer.crs())

        inFeat = QgsFeature()
        outFeat = QgsFeature()
        inGeom = QgsGeometry()
        outGeom = QgsGeometry()

        index = layer.fieldNameIndex(fieldName)
        unique = utils.getUniqueValues(layer, index)

        current = 0
        features = QGisLayers.features(layer)
        total = 100.0 / float(len(features) * len(unique))

        if not len(unique) == layer.featureCount():
            for i in unique:
                multi_feature = []
                first = True
                features = QGisLayers.features(layer)
                for inFeat in features:
                    atMap = inFeat.attributes()
                    idVar = atMap[index]
                    if unicode(idVar).strip() == unicode(i).strip():
                        if first:
                            attrs = atMap
                            first = False
                        inGeom = QgsGeometry(inFeat.geometry())
                        vType = inGeom.type()
                        feature_list = self.extractAsMulti(inGeom)
                        multi_feature.extend(feature_list)

                    current += 1
                    progress.setPercentage(int(current * total))

                outFeat.setAttributes(attrs)
                outGeom = QgsGeometry(
                    self.convertGeometry(multi_feature, vType))
                outFeat.setGeometry(outGeom)
                writer.addFeature(outFeat)

            del writer
        else:
            raise GeoAlgorithmExecutionException("Invalid unique ID field")
    def processAlgorithm(self, progress):
        layer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT))
        output = self.getOutputValue(self.OUTPUT)
        fieldName = self.getParameterValue(self.FIELD)

        geomType = self.singleToMultiGeom(layer.dataProvider().geometryType())

        writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(layer.pendingFields().toList(),
                     geomType, layer.crs())

        inFeat = QgsFeature()
        outFeat = QgsFeature()
        inGeom = QgsGeometry()
        outGeom = QgsGeometry()

        index = layer.fieldNameIndex(fieldName)
        unique = utils.getUniqueValues(layer, index)

        current = 0
        features = QGisLayers.features(layer)
        total = 100.0 / float(len(features) * len(unique))

        if not len(unique) == layer.featureCount():
            for i in unique:
                multi_feature= []
                first = True
                features = QGisLayers.features(layer)
                for inFeat in features:
                    atMap = inFeat.attributes()
                    idVar = atMap[index]
                    if unicode(idVar).strip() == unicode(i).strip():
                        if first:
                            attrs = atMap
                            first = False
                        inGeom = QgsGeometry(inFeat.geometry())
                        vType = inGeom.type()
                        feature_list = self.extractAsMulti(inGeom)
                        multi_feature.extend(feature_list)

                    current += 1
                    progress.setPercentage(int(current * total))

                outFeat.setAttributes(attrs)
                outGeom = QgsGeometry(self.convertGeometry(multi_feature, vType))
                outFeat.setGeometry(outGeom)
                writer.addFeature(outFeat)

            del writer
        else:
            raise GeoAlgorithmExecutionException("Invalid unique ID field")
Example #5
0
 def processAlgorithm(self, progress):
     useField = not self.getParameterValue(Dissolve.DISSOLVE_ALL)
     fieldname = self.getParameterValue(Dissolve.FIELD)
     vlayerA = QGisLayers.getObjectFromUri(
         self.getParameterValue(Dissolve.INPUT))
     field = vlayerA.fieldNameIndex(fieldname)
     vproviderA = vlayerA.dataProvider()
     fields = vproviderA.fields()
     writer = self.getOutputFromName(Dissolve.OUTPUT).getVectorWriter(
         fields, vproviderA.geometryType(), vproviderA.crs())
     outFeat = QgsFeature()
     nElement = 0
     nFeat = vproviderA.featureCount()
     if not useField:
         first = True
         features = QGisLayers.features(vlayerA)
         for inFeat in features:
             nElement += 1
             progress.setPercentage(int(nElement / nFeat * 100))
             if first:
                 attrs = inFeat.attributes()
                 tmpInGeom = QgsGeometry(inFeat.geometry())
                 outFeat.setGeometry(tmpInGeom)
                 first = False
             else:
                 tmpInGeom = QgsGeometry(inFeat.geometry())
                 tmpOutGeom = QgsGeometry(outFeat.geometry())
                 try:
                     tmpOutGeom = QgsGeometry(tmpOutGeom.combine(tmpInGeom))
                     outFeat.setGeometry(tmpOutGeom)
                 except:
                     raise GeoAlgorithmExecutionException(
                         "Geometry exception while dissolving")
                 outFeat.setAttributes(attrs)
                 writer.addFeature(outFeat)
     else:
         unique = utils.getUniqueValues(vlayerA, int(field))
         nFeat = nFeat * len(unique)
         for item in unique:
             first = True
             add = True
             features = QGisLayers.features(vlayerA)
             for inFeat in features:
                 nElement += 1
                 progress.setPercentage(int(nElement / nFeat * 100))
                 atMap = inFeat.attributes()
                 tempItem = atMap[field]
                 if unicode(tempItem).strip() == unicode(item).strip():
                     if first:
                         QgsGeometry(inFeat.geometry())
                         tmpInGeom = QgsGeometry(inFeat.geometry())
                         outFeat.setGeometry(tmpInGeom)
                         first = False
                         attrs = inFeat.attributes()
                     else:
                         tmpInGeom = QgsGeometry(inFeat.geometry())
                         tmpOutGeom = QgsGeometry(outFeat.geometry())
                         try:
                             tmpOutGeom = QgsGeometry(
                                 tmpOutGeom.combine(tmpInGeom))
                             outFeat.setGeometry(tmpOutGeom)
                         except:
                             raise GeoAlgorithmExecutionException(
                                 "Geometry exception while dissolving")
             if add:
                 outFeat.setAttributes(attrs)
                 writer.addFeature(outFeat)
     del writer
Example #6
0
 def processAlgorithm(self, progress):
     useField = not self.getParameterValue(Dissolve.DISSOLVE_ALL)
     fieldname = self.getParameterValue(Dissolve.FIELD)
     vlayerA = QGisLayers.getObjectFromUri(self.getParameterValue(Dissolve.INPUT))
     field = vlayerA.fieldNameIndex(fieldname)
     vproviderA = vlayerA.dataProvider()
     fields = vproviderA.fields()
     writer = self.getOutputFromName(Dissolve.OUTPUT).getVectorWriter(
         fields, vproviderA.geometryType(), vproviderA.crs()
     )
     outFeat = QgsFeature()
     nElement = 0
     nFeat = vproviderA.featureCount()
     if not useField:
         first = True
         features = QGisLayers.features(vlayerA)
         for inFeat in features:
             nElement += 1
             progress.setPercentage(int(nElement / nFeat * 100))
             if first:
                 attrs = inFeat.attributes()
                 tmpInGeom = QgsGeometry(inFeat.geometry())
                 outFeat.setGeometry(tmpInGeom)
                 first = False
             else:
                 tmpInGeom = QgsGeometry(inFeat.geometry())
                 tmpOutGeom = QgsGeometry(outFeat.geometry())
                 try:
                     tmpOutGeom = QgsGeometry(tmpOutGeom.combine(tmpInGeom))
                     outFeat.setGeometry(tmpOutGeom)
                 except:
                     raise GeoAlgorithmExecutionException("Geometry exception while dissolving")
                 outFeat.setAttributes(attrs)
                 writer.addFeature(outFeat)
     else:
         unique = utils.getUniqueValues(vlayerA, int(field))
         nFeat = nFeat * len(unique)
         for item in unique:
             first = True
             add = True
             features = QGisLayers.features(vlayerA)
             for inFeat in features:
                 nElement += 1
                 progress.setPercentage(int(nElement / nFeat * 100))
                 atMap = inFeat.attributes()
                 tempItem = atMap[field]
                 if unicode(tempItem).strip() == unicode(item).strip():
                     if first:
                         QgsGeometry(inFeat.geometry())
                         tmpInGeom = QgsGeometry(inFeat.geometry())
                         outFeat.setGeometry(tmpInGeom)
                         first = False
                         attrs = inFeat.attributes()
                     else:
                         tmpInGeom = QgsGeometry(inFeat.geometry())
                         tmpOutGeom = QgsGeometry(outFeat.geometry())
                         try:
                             tmpOutGeom = QgsGeometry(tmpOutGeom.combine(tmpInGeom))
                             outFeat.setGeometry(tmpOutGeom)
                         except:
                             raise GeoAlgorithmExecutionException("Geometry exception while dissolving")
             if add:
                 outFeat.setAttributes(attrs)
                 writer.addFeature(outFeat)
     del writer