def processAlgorithm(self, progress): polyLayer = QGisLayers.getObjectFromUri(self.getParameterValue(self.POLYGONS)) pointLayer = QGisLayers.getObjectFromUri(self.getParameterValue(self.POINTS)) fieldName = self.getParameterValue(self.FIELD) polyProvider = polyLayer.dataProvider() pointProvider = pointLayer.dataProvider() if polyProvider.crs() != pointProvider.crs(): SextanteLog.addToLog(SextanteLog.LOG_WARNING, "CRS warning: Input layers have non-matching CRS. This may cause unexpected results.") idxCount, fieldList = utils.findOrCreateField(polyLayer, polyLayer.pendingFields(), fieldName) writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fieldList, polyProvider.geometryType(), polyProvider.crs()) spatialIndex = utils.createSpatialIndex(pointLayer) pointProvider.rewind() pointProvider.select() allAttrs = polyLayer.pendingAllAttributesList() polyLayer.select(allAttrs) ftPoly = QgsFeature() ftPoint = QgsFeature() outFeat = QgsFeature() geom = QgsGeometry() current = 0 hasIntersections = False features = QGisLayers.features(polyLayer) total = 100.0 / float(len(features)) for ftPoly in features: geom = ftPoly.geometry() atMap = ftPoly.attributeMap() count = 0 hasIntersections = False points = spatialIndex.intersects(geom.boundingBox()) if len(points) > 0: hasIntersections = True if hasIntersections: for i in points: pointLayer.featureAtId(int(i), ftPoint, True, False) tmpGeom = QgsGeometry(ftPoint.geometry()) if geom.contains(tmpGeom): count += 1 outFeat.setGeometry(geom) outFeat.setAttributeMap(atMap) outFeat.addAttribute(idxCount, QVariant(count)) writer.addFeature(outFeat) current += 1 progress.setPercentage(int(current * total)) del writer
def processAlgorithm(self, progress): layer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT)) provider = layer.dataProvider() layer.select(layer.pendingAllAttributesList()) writer = self.getOutputFromName(self.OUTPUT).getVectorWriter( layer.pendingFields(), QGis.WKBPoint, provider.crs()) outFeat = QgsFeature() inGeom = QgsGeometry() outGeom = QgsGeometry() current = 0 features = QGisLayers.features(layer) total = 100.0 / float(len(features)) for inFeat in features: 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
def processAlgorithm(self, progress): layer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT)) provider = layer.dataProvider() layer.select(layer.pendingAllAttributesList()) writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(layer.pendingFields(), QGis.WKBPoint, provider.crs()) outFeat = QgsFeature() inGeom = QgsGeometry() outGeom = QgsGeometry() current = 0 features = QGisLayers.features(layer) total = 100.0 / float(len(features)) for inFeat in features: 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
def processAlgorithm(self, progress): layer = QGisLayers.getObjectFromUri(self.getParameterValue( self.POINTS)) output = self.getOutputValue(self.OUTPUT) provider = layer.dataProvider() spatialIndex = utils.createSpatialIndex(layer) provider.rewind() provider.select() neighbour = QgsFeature() distance = QgsDistanceArea() sumDist = 0.00 A = layer.extent() A = float(A.width() * A.height()) current = 0 features = QGisLayers.features(layer) total = 100.0 / float(len(features)) for feat in features: neighbourID = spatialIndex.nearestNeighbor( feat.geometry().asPoint(), 2)[1] provider.featureAtId(neighbourID, neighbour, True) sumDist += distance.measureLine(neighbour.geometry().asPoint(), feat.geometry().asPoint()) current += 1 progress.setPercentage(int(current * total)) count = provider.featureCount() do = float(sumDist) / count de = float(0.5 / math.sqrt(count / A)) d = float(do / de) SE = float(0.26136 / math.sqrt((count**2) / A)) zscore = float((do - de) / SE) data = [] data.append("Observed mean distance: " + unicode(do)) data.append("Expected mean distance: " + unicode(de)) data.append("Nearest neighbour index: " + unicode(d)) data.append("Number of points: " + unicode(count)) data.append("Z-Score: " + unicode(zscore)) self.createHTML(output, data) self.setOutputValue(self.OBSERVED_MD, float(data[0].split(": ")[1])) self.setOutputValue(self.EXPECTED_MD, float(data[1].split(": ")[1])) self.setOutputValue(self.NN_INDEX, float(data[2].split(": ")[1])) self.setOutputValue(self.POINT_COUNT, float(data[3].split(": ")[1])) self.setOutputValue(self.Z_SCORE, float(data[4].split(": ")[1]))
def processAlgorithm(self, progress): layer = QGisLayers.getObjectFromUri(self.getParameterValue(self.POINTS)) output = self.getOutputValue(self.OUTPUT) provider = layer.dataProvider() spatialIndex = utils.createSpatialIndex(layer) provider.rewind() provider.select() neighbour = QgsFeature() distance = QgsDistanceArea() sumDist = 0.00 A = layer.extent() A = float(A.width() * A.height()) current = 0 features = QGisLayers.features(layer) total = 100.0 / float(len(features)) for feat in features: neighbourID = spatialIndex.nearestNeighbor(feat.geometry().asPoint(), 2)[1] provider.featureAtId(neighbourID, neighbour, True) sumDist += distance.measureLine(neighbour.geometry().asPoint(), feat.geometry().asPoint()) current += 1 progress.setPercentage(int(current * total)) count = provider.featureCount() do = float(sumDist) / count de = float(0.5 / math.sqrt(count / A)) d = float(do / de) SE = float(0.26136 / math.sqrt(( count ** 2) / A)) zscore = float((do - de) / SE) data = [] data.append("Observed mean distance: " + unicode(do)) data.append("Expected mean distance: " + unicode(de)) data.append("Nearest neighbour index: " + unicode(d)) data.append("Number of points: " + unicode(count)) data.append("Z-Score: " + unicode(zscore)) self.createHTML(output, data) self.setOutputValue(self.OBSERVED_MD, float( data[ 0 ].split( ": " )[ 1 ] ) ) self.setOutputValue(self.EXPECTED_MD, float( data[ 1 ].split( ": " )[ 1 ] ) ) self.setOutputValue(self.NN_INDEX, float( data[ 2 ].split( ": " )[ 1 ] ) ) self.setOutputValue(self.POINT_COUNT, float( data[ 3 ].split( ": " )[ 1 ] ) ) self.setOutputValue(self.Z_SCORE, float( data[ 4 ].split( ": " )[ 1 ] ) )
def processAlgorithm(self, progress): layer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT_LAYER)) fieldName = self.getParameterValue(self.FIELD_NAME) outputFile = self.getOutputValue(self.OUTPUT_HTML_FILE) index = layer.fieldNameIndex(fieldName) layer.select([index], QgsRectangle(), False) cvValue = 0 minValue = 0 maxValue = 0 sumValue = 0 meanValue = 0 medianValue = 0 stdDevValue = 0 isFirst = True values = [] features = QGisLayers.features(layer) count = len(features) total = 100.0 / float(count) current = 0 for ft in features: value = float(ft.attributeMap()[index].toDouble()[0]) if isFirst: minValue = value maxValue = value isFirst = False else: if value < minValue: minValue = value if value > maxValue: maxValue = value values.append( value ) sumValue += value current += 1 progress.setPercentage(int(current * total)) # calculate additional values rValue = maxValue - minValue uniqueValue = utils.getUniqueValuesCount(layer, index) if count > 0: meanValue = sumValue / count if meanValue != 0.00: for v in values: stdDevValue += ((v - meanValue) * (v - meanValue)) stdDevValue = math.sqrt(stdDevValue / count) cvValue = stdDevValue / meanValue if count > 1: tmp = values tmp.sort() # calculate median if (count % 2) == 0: medianValue = 0.5 * (tmp[(count - 1) / 2] + tmp[count / 2]) else: medianValue = tmp[(count + 1) / 2 - 1] data = [] data.append("Count: " + unicode(count)) data.append("Unique values: " + unicode(uniqueValue)) data.append("Minimum value: " + unicode(minValue)) data.append("Maximum value: " + unicode(maxValue)) data.append("Range: " + unicode(rValue)) data.append("Sum: " + unicode(sumValue)) data.append("Mean value: " + unicode(meanValue)) data.append("Median value: " + unicode(medianValue)) data.append("Standard deviation: " + unicode(stdDevValue)) data.append("Coefficient of Variation: " + unicode(cvValue)) self.createHTML(outputFile, data) self.setOutputValue(self.COUNT, count) self.setOutputValue(self.UNIQUE, uniqueValue) self.setOutputValue(self.MIN, minValue) self.setOutputValue(self.MAX, maxValue) self.setOutputValue(self.RANGE, rValue) self.setOutputValue(self.SUM, sumValue) self.setOutputValue(self.MEAN, meanValue) self.setOutputValue(self.MEDIAN, medianValue) self.setOutputValue(self.STD_DEV, stdDevValue) self.setOutputValue(self.CV, cvValue)
def processAlgorithm(self, progress): polyLayer = QGisLayers.getObjectFromUri( self.getParameterValue(self.POLYGONS)) pointLayer = QGisLayers.getObjectFromUri( self.getParameterValue(self.POINTS)) fieldName = self.getParameterValue(self.FIELD) fieldidx = pointLayer.dataProvider().fieldNameIndex( self.getParameterValue(self.WEIGHT)) polyProvider = polyLayer.dataProvider() pointProvider = pointLayer.dataProvider() if polyProvider.crs() != pointProvider.crs(): SextanteLog.addToLog( SextanteLog.LOG_WARNING, "CRS warning: Input layers have non-matching CRS. This may cause unexpected results." ) idxCount, fieldList = utils.findOrCreateField( polyLayer, polyLayer.pendingFields(), fieldName) writer = self.getOutputFromName(self.OUTPUT).getVectorWriter( fieldList, polyProvider.geometryType(), polyProvider.crs()) spatialIndex = utils.createSpatialIndex(pointLayer) pointProvider.rewind() pointProvider.select() allAttrs = polyLayer.pendingAllAttributesList() polyLayer.select(allAttrs) ftPoint = QgsFeature() outFeat = QgsFeature() geom = QgsGeometry() current = 0 hasIntersections = False features = QGisLayers.features(polyLayer) total = 100.0 / float(len(features)) for ftPoly in features: geom = ftPoly.geometry() atMap = ftPoly.attributeMap() count = 0 hasIntersections = False points = spatialIndex.intersects(geom.boundingBox()) if len(points) > 0: hasIntersections = True if hasIntersections: for i in points: pointLayer.featureAtId(int(i), ftPoint, True, True) tmpGeom = QgsGeometry(ftPoint.geometry()) if geom.contains(tmpGeom): try: weight = float(ftPoint.attributeMap()[fieldidx]) except: weight = 1 count += weight outFeat.setGeometry(geom) outFeat.setAttributeMap(atMap) outFeat.addAttribute(idxCount, QVariant(count)) writer.addFeature(outFeat) current += 1 progress.setPercentage(int(current * total)) del writer
def processAlgorithm(self, progress): layer = QGisLayers.getObjectFromUri( self.getParameterValue(self.INPUT_LAYER)) fieldName = self.getParameterValue(self.FIELD_NAME) outputFile = self.getOutputValue(self.OUTPUT_HTML_FILE) index = layer.fieldNameIndex(fieldName) layer.select([index], QgsRectangle(), False) sumValue = 0 minValue = 0 maxValue = 0 meanValue = 0 countEmpty = 0 countFilled = 0 isFirst = True values = [] features = QGisLayers.features(layer) count = len(features) total = 100.0 / float(count) current = 0 for ft in features: length = float(len(ft.attributeMap()[index].toString())) if isFirst: minValue = length maxValue = length isFirst = False else: if length < minValue: minValue = length if length > maxValue: maxValue = length if length != 0.00: countFilled += 1 else: countEmpty += 1 values.append(length) sumValue += length current += 1 progress.setPercentage(int(current * total)) n = float(len(values)) if n > 0: meanValue = sumValue / n uniqueValues = utils.getUniqueValuesCount(layer, index) data = [] data.append("Minimum length: " + unicode(minValue)) data.append("Maximum length: " + unicode(maxValue)) data.append("Mean length: " + unicode(meanValue)) data.append("Filled: " + unicode(countFilled)) data.append("Empty: " + unicode(countEmpty)) data.append("Count: " + unicode(count)) data.append("Unique: " + unicode(uniqueValues)) self.createHTML(outputFile, data) self.setOutputValue(self.MIN_LEN, minValue) self.setOutputValue(self.MAX_LEN, maxValue) self.setOutputValue(self.MEAN_LEN, meanValue) self.setOutputValue(self.FILLED, countFilled) self.setOutputValue(self.EMPTY, countEmpty) self.setOutputValue(self.COUNT, count) self.setOutputValue(self.UNIQUE, uniqueValues)
def processAlgorithm(self, progress): layerA = QGisLayers.getObjectFromUri( self.getParameterValue(self.INPUT_A)) layerB = QGisLayers.getObjectFromUri( self.getParameterValue(self.INPUT_B)) fieldA = self.getParameterValue(self.FIELD_A) fieldB = self.getParameterValue(self.FIELD_B) providerA = layerA.dataProvider() providerB = layerB.dataProvider() idxA = layerA.fieldNameIndex(fieldA) idxB = layerB.fieldNameIndex(fieldB) fieldList = { 0: layerA.pendingFields()[idxA], 1: layerB.pendingFields()[idxB] } writer = self.getOutputFromName(self.OUTPUT).getVectorWriter( fieldList, QGis.WKBPoint, providerA.crs()) spatialIndex = utils.createSpatialIndex(layerB) providerA.rewind() layerA.select([idxA]) providerB.rewind() layerB.select([idxB]) inFeatA = QgsFeature() inFeatB = QgsFeature() outFeat = QgsFeature() inGeom = QgsGeometry() tmpGeom = QgsGeometry() current = 0 total = 100.0 / float(providerA.featureCount()) hasIntersections = False while layerA.nextFeature(inFeatA): inGeom = inFeatA.geometry() hasIntersections = False lines = spatialIndex.intersects(inGeom.boundingBox()) if len(lines) > 0: hasIntersections = True if hasIntersections: layerB.select([idxB]) for i in lines: layerB.featureAtId(int(i), inFeatB) tmpGeom = QgsGeometry(inFeatB.geometry()) points = [] atMapA = inFeatA.attributeMap() atMapB = inFeatB.attributeMap() if inGeom.intersects(tmpGeom): tempGeom = inGeom.intersection(tmpGeom) if tempGeom.type() == QGis.Point: if tempGeom.isMultipart(): points = tempGeom.asMultiPoint() else: points.append(tempGeom.asPoint()) for j in points: outFeat.setGeometry(tempGeom.fromPoint(j)) outFeat.addAttribute(0, atMapA[idxA]) outFeat.addAttribute(1, atMapB[idxB]) writer.addFeature(outFeat) current += 1 progress.setPercentage(int(current * total)) del writer
def processAlgorithm(self, progress): lineLayer = QGisLayers.getObjectFromUri( self.getParameterValue(self.LINES)) polyLayer = QGisLayers.getObjectFromUri( self.getParameterValue(self.POLYGONS)) lengthFieldName = self.getParameterValue(self.LEN_FIELD) countFieldName = self.getParameterValue(self.COUNT_FIELD) polyProvider = polyLayer.dataProvider() lineProvider = lineLayer.dataProvider() if polyProvider.crs() != lineProvider.crs(): SextanteLog.addToLog( SextanteLog.LOG_WARNING, "CRS warning: Input layers have non-matching CRS. This may cause unexpected results." ) idxLength, fieldList = utils.findOrCreateField( polyLayer, polyLayer.pendingFields(), lengthFieldName) idxCount, fieldList = utils.findOrCreateField(polyLayer, fieldList, countFieldName) writer = self.getOutputFromName(self.OUTPUT).getVectorWriter( fieldList, polyProvider.geometryType(), polyProvider.crs()) spatialIndex = utils.createSpatialIndex(lineLayer) lineProvider.rewind() lineProvider.select() allAttrs = polyLayer.pendingAllAttributesList() polyLayer.select(allAttrs) ftLine = QgsFeature() ftPoly = QgsFeature() outFeat = QgsFeature() inGeom = QgsGeometry() outGeom = QgsGeometry() distArea = QgsDistanceArea() current = 0 features = QGisLayers.features(polyLayer) total = 100.0 / float(len(features)) hasIntersections = False for ftPoly in features: inGeom = QgsGeometry(ftPoly.geometry()) atMap = ftPoly.attributeMap() count = 0 length = 0 hasIntersections = False lines = spatialIndex.intersects(inGeom.boundingBox()) if len(lines) > 0: hasIntersections = True if hasIntersections: for i in lines: lineProvider.featureAtId(int(i), ftLine) tmpGeom = QgsGeometry(ftLine.geometry()) if inGeom.intersects(tmpGeom): outGeom = inGeom.intersection(tmpGeom) length += distArea.measure(outGeom) count += 1 outFeat.setGeometry(inGeom) outFeat.setAttributeMap(atMap) outFeat.addAttribute(idxLength, QVariant(length)) outFeat.addAttribute(idxCount, QVariant(count)) writer.addFeature(outFeat) current += 1 progress.setPercentage(int(current * total)) del writer
def processAlgorithm(self, progress): layer = QGisLayers.getObjectFromUri( self.getParameterValue(self.INPUT_LAYER)) fieldName = self.getParameterValue(self.FIELD_NAME) outputFile = self.getOutputValue(self.OUTPUT_HTML_FILE) index = layer.fieldNameIndex(fieldName) layer.select([index], QgsRectangle(), False) cvValue = 0 minValue = 0 maxValue = 0 sumValue = 0 meanValue = 0 medianValue = 0 stdDevValue = 0 isFirst = True values = [] features = QGisLayers.features(layer) count = len(features) total = 100.0 / float(count) current = 0 for ft in features: value = float(ft.attributeMap()[index].toDouble()[0]) if isFirst: minValue = value maxValue = value isFirst = False else: if value < minValue: minValue = value if value > maxValue: maxValue = value values.append(value) sumValue += value current += 1 progress.setPercentage(int(current * total)) # calculate additional values rValue = maxValue - minValue uniqueValue = utils.getUniqueValuesCount(layer, index) if count > 0: meanValue = sumValue / count if meanValue != 0.00: for v in values: stdDevValue += ((v - meanValue) * (v - meanValue)) stdDevValue = math.sqrt(stdDevValue / count) cvValue = stdDevValue / meanValue if count > 1: tmp = values tmp.sort() # calculate median if (count % 2) == 0: medianValue = 0.5 * (tmp[(count - 1) / 2] + tmp[count / 2]) else: medianValue = tmp[(count + 1) / 2 - 1] data = [] data.append("Count: " + unicode(count)) data.append("Unique values: " + unicode(uniqueValue)) data.append("Minimum value: " + unicode(minValue)) data.append("Maximum value: " + unicode(maxValue)) data.append("Range: " + unicode(rValue)) data.append("Sum: " + unicode(sumValue)) data.append("Mean value: " + unicode(meanValue)) data.append("Median value: " + unicode(medianValue)) data.append("Standard deviation: " + unicode(stdDevValue)) data.append("Coefficient of Variation: " + unicode(cvValue)) self.createHTML(outputFile, data) self.setOutputValue(self.COUNT, count) self.setOutputValue(self.UNIQUE, uniqueValue) self.setOutputValue(self.MIN, minValue) self.setOutputValue(self.MAX, maxValue) self.setOutputValue(self.RANGE, rValue) self.setOutputValue(self.SUM, sumValue) self.setOutputValue(self.MEAN, meanValue) self.setOutputValue(self.MEDIAN, medianValue) self.setOutputValue(self.STD_DEV, stdDevValue) self.setOutputValue(self.CV, cvValue)
def processAlgorithm(self, progress): layerA = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT_A)) layerB = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT_B)) fieldA = self.getParameterValue(self.FIELD_A) fieldB = self.getParameterValue(self.FIELD_B) providerA = layerA.dataProvider() providerB = layerB.dataProvider() idxA = layerA.fieldNameIndex(fieldA) idxB = layerB.fieldNameIndex(fieldB) fieldList = {0 : layerA.pendingFields()[idxA], 1 : layerB.pendingFields()[idxB] } writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fieldList, QGis.WKBPoint, providerA.crs()) spatialIndex = utils.createSpatialIndex(layerB) providerA.rewind() layerA.select([idxA]) providerB.rewind() layerB.select([idxB]) inFeatA = QgsFeature() inFeatB = QgsFeature() outFeat = QgsFeature() inGeom = QgsGeometry() tmpGeom = QgsGeometry() current = 0 total = 100.0 / float(providerA.featureCount()) hasIntersections = False while layerA.nextFeature(inFeatA): inGeom = inFeatA.geometry() hasIntersections = False lines = spatialIndex.intersects(inGeom.boundingBox()) if len(lines) > 0: hasIntersections = True if hasIntersections: layerB.select([idxB]) for i in lines: layerB.featureAtId(int(i), inFeatB) tmpGeom = QgsGeometry(inFeatB.geometry()) points = [] atMapA = inFeatA.attributeMap() atMapB = inFeatB.attributeMap() if inGeom.intersects(tmpGeom): tempGeom = inGeom.intersection(tmpGeom) if tempGeom.type() == QGis.Point: if tempGeom.isMultipart(): points = tempGeom.asMultiPoint() else: points.append(tempGeom.asPoint()) for j in points: outFeat.setGeometry(tempGeom.fromPoint(j)) outFeat.addAttribute(0, atMapA[idxA]) outFeat.addAttribute(1, atMapB[idxB]) writer.addFeature(outFeat) current += 1 progress.setPercentage(int(current * total)) del writer
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 = 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
def processAlgorithm(self, progress): layer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT)) method = self.getParameterValue(self.METHOD) provider = layer.dataProvider() geometryType = layer.geometryType() layer.select(layer.pendingAllAttributesList()) idx1 = -1 idx2 = -1 fields = layer.pendingFields() if geometryType == QGis.Polygon: idx1, fields = utils.findOrCreateField(layer, fields, "area", 21, 6) idx2, fields = utils.findOrCreateField(layer, fields, "perimeter", 21, 6) elif geometryType == QGis.Line: idx1, fields = utils.findOrCreateField(layer, fields, "length", 21, 6) idx2 = idx1 else: idx1, fields = utils.findOrCreateField(layer, fields, "xcoord", 21, 6) idx2, fields = utils.findOrCreateField(layer, fields, "ycoord", 21, 6) writer = self.getOutputFromName(self.OUTPUT).getVectorWriter( fields, provider.geometryType(), provider.crs()) ellips = None crs = None coordTransform = None # calculate with: # 0 - layer CRS # 1 - project CRS # 2 - ellipsoidal if method == 2: settings = QSettings() ellips = settings.value("/qgis/measure/ellipsoid", "WGS84").toString() crs = layer.crs().srsid() elif method == 1: mapCRS = QGisLayers.iface.mapCanvas().mapRenderer().destinationCrs( ) layCRS = layer.crs() coordTransform = QgsCoordinateTransform(layCRS, mapCRS) outFeat = QgsFeature() inGeom = QgsGeometry() current = 0 features = QGisLayers.features(layer) total = 100.0 / float(len(features)) for inFeat in features: inGeom = inFeat.geometry() if method == 1: inGeom.transform(coordTransform) (attr1, attr2) = self.simpleMeasure(inGeom, method, ellips, crs) outFeat.setGeometry(inGeom) atMap = inFeat.attributeMap() outFeat.setAttributeMap(atMap) outFeat.addAttribute(idx1, QVariant(attr1)) outFeat.addAttribute(idx2, QVariant(attr2)) writer.addFeature(outFeat) current += 1 progress.setPercentage(int(current * total)) del writer
def processAlgorithm(self, progress): lineLayer = QGisLayers.getObjectFromUri(self.getParameterValue(self.LINES)) polyLayer = QGisLayers.getObjectFromUri(self.getParameterValue(self.POLYGONS)) lengthFieldName = self.getParameterValue(self.LEN_FIELD) countFieldName = self.getParameterValue(self.COUNT_FIELD) polyProvider = polyLayer.dataProvider() lineProvider = lineLayer.dataProvider() if polyProvider.crs() != lineProvider.crs(): SextanteLog.addToLog(SextanteLog.LOG_WARNING, "CRS warning: Input layers have non-matching CRS. This may cause unexpected results.") idxLength, fieldList = utils.findOrCreateField(polyLayer, polyLayer.pendingFields(), lengthFieldName) idxCount, fieldList = utils.findOrCreateField(polyLayer, fieldList, countFieldName) writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fieldList, polyProvider.geometryType(), polyProvider.crs()) spatialIndex = utils.createSpatialIndex(lineLayer) lineProvider.rewind() lineProvider.select() allAttrs = polyLayer.pendingAllAttributesList() polyLayer.select(allAttrs) ftLine = QgsFeature() ftPoly = QgsFeature() outFeat = QgsFeature() inGeom = QgsGeometry() outGeom = QgsGeometry() distArea = QgsDistanceArea() current = 0 features = QGisLayers.features(polyLayer) total = 100.0 / float(len(features)) hasIntersections = False for ftPoly in features: inGeom = QgsGeometry(ftPoly.geometry()) atMap = ftPoly.attributeMap() count = 0 length = 0 hasIntersections = False lines = spatialIndex.intersects(inGeom.boundingBox()) if len(lines) > 0: hasIntersections = True if hasIntersections: for i in lines: lineProvider.featureAtId(int(i), ftLine) tmpGeom = QgsGeometry(ftLine.geometry()) if inGeom.intersects(tmpGeom): outGeom = inGeom.intersection(tmpGeom) length += distArea.measure(outGeom) count += 1 outFeat.setGeometry(inGeom) outFeat.setAttributeMap(atMap) outFeat.addAttribute(idxLength, QVariant(length)) outFeat.addAttribute(idxCount, QVariant(count)) writer.addFeature(outFeat) current += 1 progress.setPercentage(int(current * total)) del writer
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 = 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
def processAlgorithm(self, progress): layer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT)) method = self.getParameterValue(self.METHOD) provider = layer.dataProvider() geometryType = layer.geometryType() layer.select(layer.pendingAllAttributesList()) idx1 = -1 idx2 = -1 fields = layer.pendingFields() if geometryType == QGis.Polygon: idx1, fields = utils.findOrCreateField(layer, fields, "area", 21, 6) idx2, fields = utils.findOrCreateField(layer, fields, "perimeter", 21, 6) elif geometryType == QGis.Line: idx1, fields = utils.findOrCreateField(layer, fields, "length", 21, 6) idx2 = idx1 else: idx1, fields = utils.findOrCreateField(layer, fields, "xcoord", 21, 6) idx2, fields = utils.findOrCreateField(layer, fields, "ycoord", 21, 6) writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fields, provider.geometryType(), provider.crs()) ellips = None crs = None coordTransform = None # calculate with: # 0 - layer CRS # 1 - project CRS # 2 - ellipsoidal if method == 2: settings = QSettings() ellips = settings.value("/qgis/measure/ellipsoid", "WGS84").toString() crs = layer.crs().srsid() elif method == 1: mapCRS = QGisLayers.iface.mapCanvas().mapRenderer().destinationCrs() layCRS = layer.crs() coordTransform = QgsCoordinateTransform(layCRS, mapCRS) outFeat = QgsFeature() inGeom = QgsGeometry() current = 0 features = QGisLayers.features(layer) total = 100.0 / float(len(features)) for inFeat in features: inGeom = inFeat.geometry() if method == 1: inGeom.transform(coordTransform) (attr1, attr2) = self.simpleMeasure(inGeom, method, ellips, crs) outFeat.setGeometry(inGeom) atMap = inFeat.attributeMap() outFeat.setAttributeMap(atMap) outFeat.addAttribute(idx1, QVariant(attr1)) outFeat.addAttribute(idx2, QVariant(attr2)) writer.addFeature( outFeat ) current += 1 progress.setPercentage(int(current * total)) del writer
def processAlgorithm(self, progress): layer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT_LAYER)) fieldName = self.getParameterValue(self.FIELD_NAME) outputFile = self.getOutputValue(self.OUTPUT_HTML_FILE) index = layer.fieldNameIndex(fieldName) layer.select([index], QgsRectangle(), False) sumValue = 0 minValue = 0 maxValue = 0 meanValue = 0 countEmpty = 0 countFilled = 0 isFirst = True values = [] features = QGisLayers.features(layer) count = len(features) total = 100.0 / float(count) current = 0 for ft in features: length = float(len(ft.attributeMap()[index].toString())) if isFirst: minValue = length maxValue = length isFirst = False else: if length < minValue: minValue = length if length > maxValue: maxValue = length if length != 0.00: countFilled += 1 else: countEmpty += 1 values.append(length) sumValue += length current += 1 progress.setPercentage(int(current * total)) n = float(len(values)) if n > 0: meanValue = sumValue / n uniqueValues = utils.getUniqueValuesCount(layer, index) data = [] data.append("Minimum length: " + unicode(minValue)) data.append("Maximum length: " + unicode(maxValue)) data.append("Mean length: " + unicode(meanValue)) data.append("Filled: " + unicode(countFilled)) data.append("Empty: " + unicode(countEmpty)) data.append("Count: " + unicode(count)) data.append("Unique: " + unicode(uniqueValues)) self.createHTML(outputFile, data) self.setOutputValue(self.MIN_LEN, minValue) self.setOutputValue(self.MAX_LEN, maxValue) self.setOutputValue(self.MEAN_LEN, meanValue) self.setOutputValue(self.FILLED, countFilled) self.setOutputValue(self.EMPTY, countEmpty) self.setOutputValue(self.COUNT, count) self.setOutputValue(self.UNIQUE, uniqueValues)