mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-15 00:04:00 -04:00
Remove vector.simpleMeasure
It's quite a confusing function - instead use optimised versions of the measurement calculations in its place
This commit is contained in:
parent
3e3f1d42d9
commit
18dd09762b
@ -37,7 +37,8 @@ from qgis.core import (QgsField,
|
|||||||
QgsGeometry,
|
QgsGeometry,
|
||||||
QgsWkbTypes,
|
QgsWkbTypes,
|
||||||
QgsProcessingUtils,
|
QgsProcessingUtils,
|
||||||
QgsFields)
|
QgsFields,
|
||||||
|
NULL)
|
||||||
|
|
||||||
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
|
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
|
||||||
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
|
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
|
||||||
@ -145,7 +146,12 @@ class ConvexHull(QgisAlgorithm):
|
|||||||
tmpGeom = QgsGeometry(outGeom.fromMultiPoint(hull))
|
tmpGeom = QgsGeometry(outGeom.fromMultiPoint(hull))
|
||||||
try:
|
try:
|
||||||
outGeom = tmpGeom.convexHull()
|
outGeom = tmpGeom.convexHull()
|
||||||
(area, perim) = vector.simpleMeasure(outGeom)
|
if outGeom:
|
||||||
|
area = outGeom.geometry().area()
|
||||||
|
perim = outGeom.geometry().perimeter()
|
||||||
|
else:
|
||||||
|
area = NULL
|
||||||
|
perim = NULL
|
||||||
outFeat.setGeometry(outGeom)
|
outFeat.setGeometry(outGeom)
|
||||||
outFeat.setAttributes([fid, val, area, perim])
|
outFeat.setAttributes([fid, val, area, perim])
|
||||||
writer.addFeature(outFeat, QgsFeatureSink.FastInsert)
|
writer.addFeature(outFeat, QgsFeatureSink.FastInsert)
|
||||||
@ -166,7 +172,12 @@ class ConvexHull(QgisAlgorithm):
|
|||||||
tmpGeom = QgsGeometry(outGeom.fromMultiPoint(hull))
|
tmpGeom = QgsGeometry(outGeom.fromMultiPoint(hull))
|
||||||
try:
|
try:
|
||||||
outGeom = tmpGeom.convexHull()
|
outGeom = tmpGeom.convexHull()
|
||||||
(area, perim) = vector.simpleMeasure(outGeom)
|
if outGeom:
|
||||||
|
area = outGeom.geometry().area()
|
||||||
|
perim = outGeom.geometry().perimeter()
|
||||||
|
else:
|
||||||
|
area = NULL
|
||||||
|
perim = NULL
|
||||||
outFeat.setGeometry(outGeom)
|
outFeat.setGeometry(outGeom)
|
||||||
outFeat.setAttributes([0, 'all', area, perim])
|
outFeat.setAttributes([0, 'all', area, perim])
|
||||||
writer.addFeature(outFeat, QgsFeatureSink.FastInsert)
|
writer.addFeature(outFeat, QgsFeatureSink.FastInsert)
|
||||||
|
@ -30,8 +30,12 @@ import os
|
|||||||
from qgis.PyQt.QtGui import QIcon
|
from qgis.PyQt.QtGui import QIcon
|
||||||
from qgis.PyQt.QtCore import QVariant
|
from qgis.PyQt.QtCore import QVariant
|
||||||
|
|
||||||
from qgis.core import QgsProject, QgsCoordinateTransform, QgsFeature, QgsField, QgsWkbTypes, QgsFeatureSink, QgsProcessingUtils
|
from qgis.core import (QgsCoordinateTransform,
|
||||||
from qgis.utils import iface
|
QgsField,
|
||||||
|
QgsWkbTypes,
|
||||||
|
QgsFeatureSink,
|
||||||
|
QgsProcessingUtils,
|
||||||
|
QgsDistanceArea)
|
||||||
|
|
||||||
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
|
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
|
||||||
from processing.core.parameters import ParameterVector
|
from processing.core.parameters import ParameterVector
|
||||||
@ -59,6 +63,9 @@ class ExportGeometryInfo(QgisAlgorithm):
|
|||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
self.export_z = False
|
||||||
|
self.export_m = False
|
||||||
|
self.distance_area = None
|
||||||
|
|
||||||
def initAlgorithm(self, config=None):
|
def initAlgorithm(self, config=None):
|
||||||
self.calc_methods = [self.tr('Layer CRS'),
|
self.calc_methods = [self.tr('Layer CRS'),
|
||||||
@ -85,8 +92,6 @@ class ExportGeometryInfo(QgisAlgorithm):
|
|||||||
geometryType = layer.geometryType()
|
geometryType = layer.geometryType()
|
||||||
fields = layer.fields()
|
fields = layer.fields()
|
||||||
|
|
||||||
export_z = False
|
|
||||||
export_m = False
|
|
||||||
if geometryType == QgsWkbTypes.PolygonGeometry:
|
if geometryType == QgsWkbTypes.PolygonGeometry:
|
||||||
areaName = vector.createUniqueFieldName('area', fields)
|
areaName = vector.createUniqueFieldName('area', fields)
|
||||||
fields.append(QgsField(areaName, QVariant.Double))
|
fields.append(QgsField(areaName, QVariant.Double))
|
||||||
@ -101,19 +106,17 @@ class ExportGeometryInfo(QgisAlgorithm):
|
|||||||
yName = vector.createUniqueFieldName('ycoord', fields)
|
yName = vector.createUniqueFieldName('ycoord', fields)
|
||||||
fields.append(QgsField(yName, QVariant.Double))
|
fields.append(QgsField(yName, QVariant.Double))
|
||||||
if QgsWkbTypes.hasZ(layer.wkbType()):
|
if QgsWkbTypes.hasZ(layer.wkbType()):
|
||||||
export_z = True
|
self.export_z = True
|
||||||
zName = vector.createUniqueFieldName('zcoord', fields)
|
zName = vector.createUniqueFieldName('zcoord', fields)
|
||||||
fields.append(QgsField(zName, QVariant.Double))
|
fields.append(QgsField(zName, QVariant.Double))
|
||||||
if QgsWkbTypes.hasM(layer.wkbType()):
|
if QgsWkbTypes.hasM(layer.wkbType()):
|
||||||
export_m = True
|
self.export_m = True
|
||||||
zName = vector.createUniqueFieldName('mvalue', fields)
|
zName = vector.createUniqueFieldName('mvalue', fields)
|
||||||
fields.append(QgsField(zName, QVariant.Double))
|
fields.append(QgsField(zName, QVariant.Double))
|
||||||
|
|
||||||
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fields, layer.wkbType(), layer.crs(),
|
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fields, layer.wkbType(), layer.crs(),
|
||||||
context)
|
context)
|
||||||
|
|
||||||
ellips = None
|
|
||||||
crs = None
|
|
||||||
coordTransform = None
|
coordTransform = None
|
||||||
|
|
||||||
# Calculate with:
|
# Calculate with:
|
||||||
@ -121,40 +124,29 @@ class ExportGeometryInfo(QgisAlgorithm):
|
|||||||
# 1 - project CRS
|
# 1 - project CRS
|
||||||
# 2 - ellipsoidal
|
# 2 - ellipsoidal
|
||||||
|
|
||||||
|
self.distance_area = QgsDistanceArea()
|
||||||
if method == 2:
|
if method == 2:
|
||||||
ellips = QgsProject.instance().ellipsoid()
|
self.distance_area.setSourceCrs(layer.crs())
|
||||||
crs = layer.crs().srsid()
|
self.distance_area.setEllipsoid(context.project().ellipsoid())
|
||||||
elif method == 1:
|
elif method == 1:
|
||||||
mapCRS = iface.mapCanvas().mapSettings().destinationCrs()
|
coordTransform = QgsCoordinateTransform(layer.crs(), context.project().crs())
|
||||||
layCRS = layer.crs()
|
|
||||||
coordTransform = QgsCoordinateTransform(layCRS, mapCRS)
|
|
||||||
|
|
||||||
outFeat = QgsFeature()
|
|
||||||
|
|
||||||
outFeat.initAttributes(len(fields))
|
|
||||||
outFeat.setFields(fields)
|
|
||||||
|
|
||||||
features = QgsProcessingUtils.getFeatures(layer, context)
|
features = QgsProcessingUtils.getFeatures(layer, context)
|
||||||
total = 100.0 / layer.featureCount() if layer.featureCount() else 0
|
total = 100.0 / layer.featureCount() if layer.featureCount() else 0
|
||||||
for current, f in enumerate(features):
|
for current, f in enumerate(features):
|
||||||
inGeom = f.geometry()
|
outFeat = f
|
||||||
|
|
||||||
if method == 1:
|
|
||||||
inGeom.transform(coordTransform)
|
|
||||||
|
|
||||||
(attr1, attr2) = vector.simpleMeasure(inGeom, method, ellips, crs)
|
|
||||||
|
|
||||||
outFeat.setGeometry(inGeom)
|
|
||||||
attrs = f.attributes()
|
attrs = f.attributes()
|
||||||
attrs.append(attr1)
|
inGeom = f.geometry()
|
||||||
if attr2 is not None:
|
if inGeom:
|
||||||
attrs.append(attr2)
|
if coordTransform is not None:
|
||||||
|
inGeom.transform(coordTransform)
|
||||||
|
|
||||||
# add point z/m
|
if inGeom.type() == QgsWkbTypes.PointGeometry:
|
||||||
if export_z:
|
attrs.extend(self.point_attributes(inGeom))
|
||||||
attrs.append(inGeom.geometry().z())
|
elif inGeom.type() == QgsWkbTypes.PolygonGeometry:
|
||||||
if export_m:
|
attrs.extend(self.polygon_attributes(inGeom))
|
||||||
attrs.append(inGeom.geometry().m())
|
else:
|
||||||
|
attrs.extend(self.line_attributes(inGeom))
|
||||||
|
|
||||||
outFeat.setAttributes(attrs)
|
outFeat.setAttributes(attrs)
|
||||||
writer.addFeature(outFeat, QgsFeatureSink.FastInsert)
|
writer.addFeature(outFeat, QgsFeatureSink.FastInsert)
|
||||||
@ -162,3 +154,29 @@ class ExportGeometryInfo(QgisAlgorithm):
|
|||||||
feedback.setProgress(int(current * total))
|
feedback.setProgress(int(current * total))
|
||||||
|
|
||||||
del writer
|
del writer
|
||||||
|
|
||||||
|
def point_attributes(self, geometry):
|
||||||
|
pt = None
|
||||||
|
if not geometry.isMultipart():
|
||||||
|
pt = geometry.geometry()
|
||||||
|
else:
|
||||||
|
if geometry.numGeometries() > 0:
|
||||||
|
pt = geometry.geometryN(0)
|
||||||
|
attrs = []
|
||||||
|
if pt:
|
||||||
|
attrs.append(pt.x())
|
||||||
|
attrs.append(pt.y())
|
||||||
|
# add point z/m
|
||||||
|
if self.export_z:
|
||||||
|
attrs.append(pt.z())
|
||||||
|
if self.export_m:
|
||||||
|
attrs.append(pt.m())
|
||||||
|
return attrs
|
||||||
|
|
||||||
|
def line_attributes(self, geometry):
|
||||||
|
return [self.distance_area.measureLength(geometry)]
|
||||||
|
|
||||||
|
def polygon_attributes(self, geometry):
|
||||||
|
area = self.distance_area.measureArea(geometry)
|
||||||
|
perimeter = self.distance_area.measurePerimeter(geometry)
|
||||||
|
return [area, perimeter]
|
||||||
|
@ -196,38 +196,6 @@ def extractPoints(geom):
|
|||||||
return points
|
return points
|
||||||
|
|
||||||
|
|
||||||
def simpleMeasure(geom, method=0, ellips=None, crs=None):
|
|
||||||
# Method defines calculation type:
|
|
||||||
# 0 - layer CRS
|
|
||||||
# 1 - project CRS
|
|
||||||
# 2 - ellipsoidal
|
|
||||||
|
|
||||||
if geom.type() == QgsWkbTypes.PointGeometry:
|
|
||||||
if not geom.isMultipart():
|
|
||||||
pt = geom.geometry()
|
|
||||||
attr1 = pt.x()
|
|
||||||
attr2 = pt.y()
|
|
||||||
else:
|
|
||||||
pt = geom.asMultiPoint()
|
|
||||||
attr1 = pt[0].x()
|
|
||||||
attr2 = pt[0].y()
|
|
||||||
else:
|
|
||||||
measure = QgsDistanceArea()
|
|
||||||
|
|
||||||
if method == 2:
|
|
||||||
measure.setSourceCrs(crs)
|
|
||||||
measure.setEllipsoid(ellips)
|
|
||||||
|
|
||||||
if geom.type() == QgsWkbTypes.PolygonGeometry:
|
|
||||||
attr1 = measure.measureArea(geom)
|
|
||||||
attr2 = measure.measurePerimeter(geom)
|
|
||||||
else:
|
|
||||||
attr1 = measure.measureLength(geom)
|
|
||||||
attr2 = None
|
|
||||||
|
|
||||||
return (attr1, attr2)
|
|
||||||
|
|
||||||
|
|
||||||
def combineFields(fieldsA, fieldsB):
|
def combineFields(fieldsA, fieldsB):
|
||||||
"""Create single field map from two input field maps.
|
"""Create single field map from two input field maps.
|
||||||
"""
|
"""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user