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] == QVariant(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)
Ejemplo n.º 2
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]))
    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] == QVariant(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)

        provider = layer.dataProvider()
        allAttrs = layer.pendingAllAttributesList()
        layer.select(allAttrs)
        geomType = self.singleToMultiGeom(provider.geometryType())

        writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(layer.pendingFields(),
                     geomType, provider.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:
                #provider.rewind()
                multi_feature= []
                first = True
                layer.select(allAttrs)
                features = QGisLayers.features(layer)
                for inFeat in features:
                    atMap = inFeat.attributes()
                    idVar = atMap[index]
                    if idVar.toString().trimmed() == i.toString().trimmed():
                        if first:
                            attrs = atMap
                            print attrs
                            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 idVar.toString().trimmed() == i.toString().trimmed():
                        if first:
                            attrs = atMap
                            print attrs
                            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")
Ejemplo n.º 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 tempItem.toString().trimmed() == item.toString(
                 ).trimmed():
                     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
Ejemplo n.º 7
0
    def processAlgorithm(self, progress):
        layer = QGisLayers.getObjectFromUri(self.getParameterValue(self.POINTS))
        weightField = self.getParameterValue(self.WEIGHT)
        uniqueField = self.getParameterValue(self.UID)

        provider = layer.dataProvider()
        weightIndex = layer.fieldNameIndex(weightField)
        uniqueIndex = layer.fieldNameIndex(uniqueField)

        if uniqueIndex <> -1:
            uniqueValues = utils.getUniqueValues(layer, uniqueIndex)
            single = False
        else:
            uniqueValues = [QVariant(1)]
            single = True

        fieldList = [QgsField("MEAN_X", QVariant.Double, "", 24, 15),
                     QgsField("MEAN_Y", QVariant.Double, "", 24, 15),
                     QgsField("UID", QVariant.String, "", 255)
                    ]

        writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fieldList,
                     QGis.WKBPoint, layer.crs())

        current = 0
        total = 100.0 / float(provider.featureCount() * len(uniqueValues))

        outFeat = QgsFeature()

        for j in uniqueValues:
            cx = 0.00
            cy = 0.00
            points = []
            weights = []
            features = QGisLayers.features(layer)
            for feat in features:
                current += 1
                progress.setPercentage(current * total)

                if single:
                    check = j.toString().trimmed()
                else:
                    check = feat.attributes()[uniqueIndex].toString().trimmed()

                if check == j.toString().trimmed():
                    cx = 0.00
                    cy = 0.00
                    if weightIndex == -1:
                        weight = 1.00
                    else:
                        try:
                            weight = float(feat.attributes()[weightIndex].toDouble()[0])
                        except:
                            weight = 1.00

                    geom = QgsGeometry(feat.geometry())
                    geom = utils.extractPoints(geom)
                    for i in geom:
                        cx += i.x()
                        cy += i.y()
                    points.append(QgsPoint((cx / len(geom)), (cy / len(geom))))
                    weights.append(weight)

            sumWeight = sum(weights)
            cx = 0.00
            cy = 0.00
            item = 0
            for item, i in enumerate(points):
                cx += i.x() * weights[item]
                cy += i.y() * weights[item]

            cx = cx / sumWeight
            cy = cy / sumWeight
            meanPoint = QgsPoint(cx, cy)

            outFeat.setGeometry(QgsGeometry.fromPoint(meanPoint))
            outFeat.setAttributes([QVariant(cx), QVariant(cy), QVariant(j)])
            writer.addFeature(outFeat)

            if single:
                break

        del writer
Ejemplo n.º 8
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 tempItem.toString().trimmed() == item.toString().trimmed():
                     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
Ejemplo n.º 9
0
    def processAlgorithm(self, progress):
        useField = (self.getParameterValue(ConvexHull.METHOD) == 1)
        field = self.getParameterValue(ConvexHull.FIELD)
        vlayerA = QGisLayers.getObjectFromUri(self.getParameterValue(ConvexHull.INPUT))
        GEOS_EXCEPT = True
        FEATURE_EXCEPT = True
        vproviderA = vlayerA.dataProvider()
        #allAttrsA = vproviderA.attributeIndexes()
        #vproviderA.select(allAttrsA)
        fields = [QgsField("ID", QVariant.Int),
                    QgsField("Area", QVariant.Double),
                    QgsField("Perim", QVariant.Double)]
        writer = self.getOutputFromName(ConvexHull.OUTPUT).getVectorWriter(fields, QGis.WKBPolygon, vproviderA.crs())
        inFeat = QgsFeature()
        outFeat = QgsFeature()
        inGeom = QgsGeometry()
        outGeom = QgsGeometry()
        nElement = 0
        index = vproviderA.fieldNameIndex(field)

        features = QGisLayers.features(vlayerA)
        nFeat = len(features)

        if useField:
          unique = utils.getUniqueValues(vproviderA, index)
          nFeat = nFeat * len(unique)
          for i in unique:
            hull = []
            first = True
            outID = 0
            #vproviderA.select(allAttrsA)
            features = QGisLayers.features(vlayerA)
            for inFeat in features:
              atMap = inFeat.attributes()
              idVar = atMap[ index ]
              if idVar.toString().trimmed() == i.toString().trimmed():
                if first:
                  outID = idVar
                  first = False
                inGeom = QgsGeometry(inFeat.geometry())
                points = utils.extractPoints(inGeom)
                hull.extend(points)
              nElement += 1
              progress.setPercentage(int(nElement / nFeat * 100))
            if len(hull) >= 3:
              tmpGeom = QgsGeometry(outGeom.fromMultiPoint(hull))
              try:
                outGeom = tmpGeom.convexHull()
                outFeat.setGeometry(outGeom)
                (area, perim) = self.simpleMeasure(outGeom)
                outFeat.addAttribute(0, QVariant(outID))
                outFeat.addAttribute(1, QVariant(area))
                outFeat.addAttribute(2, QVariant(perim))
                writer.addFeature(outFeat)
              except:
                GEOS_EXCEPT = False
                continue
        else:
          hull = []
          #vproviderA.select(allAttrsA)
          features = QGisLayers.features(vlayerA)
          for inFeat in features:
            inGeom = QgsGeometry(inFeat.geometry())
            points = utils.extractPoints(inGeom)
            hull.extend(points)
            nElement += 1
            progress.setPercentage(int(nElement / nFeat * 100))
          tmpGeom = QgsGeometry(outGeom.fromMultiPoint(hull))
          try:
            outGeom = tmpGeom.convexHull()
            outFeat.setGeometry(outGeom)
            (area, perim) = self.simpleMeasure(outGeom)
            #outFeat.addAttribute(0, QVariant("1"))
            #outFeat.addAttribute(1, QVariant(area))
            #outFeat.addAttribute(2, QVariant(perim))
            writer.addFeature(outFeat)
          except:
            GEOS_EXCEPT = False

        del writer

        if not GEOS_EXCEPT:
            SextanteLog.addToLog(SextanteLog.LOG_WARNING, "Geometry exception while computing convex hull")
        if not FEATURE_EXCEPT:
            SextanteLog.addToLog(SextanteLog.LOG_WARNING, "Feature exception while computing convex hull")