Exemple #1
0
    def processAlgorithm(self, progress):
        layer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT))
        output = self.getOutputValue(self.OUTPUT)

        provider = layer.dataProvider()
        layer.select(layer.pendingAllAttributesList())

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

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

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

        while layer.nextFeature(inFeat):
            inGeom = inFeat.geometry()
            atMap = inFeat.attributeMap()

            points = utils.extractPoints(inGeom)
            outFeat.setAttributeMap(atMap)

            for i in points:
                outFeat.setGeometry(outGeom.fromPoint(i))
                writer.addFeature(outFeat)

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

        del writer
Exemple #2
0
    def processAlgorithm(self, progress):
        settings = QSettings()
        encoding = settings.value( "/UI/encoding", "System" ).toString()

        layer = QGisLayers.getObjectFromUri(self.getParameterValue(self.POINTS))
        weightField = self.getParameterValue(self.WEIGHT)
        uniqueField = self.getParameterValue(self.UID)

        output = self.getOutputValue(self.OUTPUT)

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

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

        fieldList = {0 : QgsField("MEAN_X", QVariant.Double, "", 24, 15),
                     1 : QgsField("MEAN_Y", QVariant.Double, "", 24, 15),
                     2 : QgsField("UID", QVariant.String, "", 255)
                    }

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

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

        feat = QgsFeature()
        outFeat = QgsFeature()

        for j in uniqueValues:
            provider.rewind()
            layer.select([weightIndex, uniqueIndex])
            cx = 0.00
            cy = 0.00
            points = []
            weights = []
            while layer.nextFeature(feat):
                current += 1
                progress.setPercentage(current * total)

                if single:
                    check = j.toString().trimmed()
                else:
                    check = feat.attributeMap()[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.attributeMap()[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.addAttribute(0, QVariant(cx))
            outFeat.addAttribute(1, QVariant(cy))
            outFeat.addAttribute(2, QVariant(j))
            writer.addFeature(outFeat)

            if single:
                break

        del writer
Exemple #3
0
    def processAlgorithm(self, progress):
        layer = QGisLayers.getObjectFromUri(self.getParameterValue(self.POINTS))
        weightField = self.getParameterValue(self.WEIGHT)
        uniqueField = self.getParameterValue(self.UID)

        output = self.getOutputValue(self.OUTPUT)

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

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

        fieldList = {0 : QgsField("MEAN_X", QVariant.Double, "", 24, 15),
                     1 : QgsField("MEAN_Y", QVariant.Double, "", 24, 15),
                     2 : QgsField("UID", QVariant.String, "", 255)
                    }

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

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

        feat = QgsFeature()
        outFeat = QgsFeature()

        for j in uniqueValues:
            provider.rewind()
            layer.select([weightIndex, uniqueIndex])
            cx = 0.00
            cy = 0.00
            points = []
            weights = []
            while layer.nextFeature(feat):
                current += 1
                progress.setPercentage(current * total)

                if single:
                    check = j.toString().trimmed()
                else:
                    check = feat.attributeMap()[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.attributeMap()[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.addAttribute(0, QVariant(cx))
            outFeat.addAttribute(1, QVariant(cy))
            outFeat.addAttribute(2, QVariant(j))
            writer.addFeature(outFeat)

            if single:
                break

        del writer