Skip to content
Permalink
Browse files

Remove vector.simpleMeasure

It's quite a confusing function - instead use optimised versions
of the measurement calculations in its place
  • Loading branch information
nyalldawson committed Jul 27, 2017
1 parent 3e3f1d4 commit 18dd09762b01e793c365fbe66a638cbe901b51f6
@@ -37,7 +37,8 @@
QgsGeometry,
QgsWkbTypes,
QgsProcessingUtils,
QgsFields)
QgsFields,
NULL)

from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
@@ -145,7 +146,12 @@ def processAlgorithm(self, parameters, context, feedback):
tmpGeom = QgsGeometry(outGeom.fromMultiPoint(hull))
try:
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.setAttributes([fid, val, area, perim])
writer.addFeature(outFeat, QgsFeatureSink.FastInsert)
@@ -166,7 +172,12 @@ def processAlgorithm(self, parameters, context, feedback):
tmpGeom = QgsGeometry(outGeom.fromMultiPoint(hull))
try:
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.setAttributes([0, 'all', area, perim])
writer.addFeature(outFeat, QgsFeatureSink.FastInsert)
@@ -30,8 +30,12 @@
from qgis.PyQt.QtGui import QIcon
from qgis.PyQt.QtCore import QVariant

from qgis.core import QgsProject, QgsCoordinateTransform, QgsFeature, QgsField, QgsWkbTypes, QgsFeatureSink, QgsProcessingUtils
from qgis.utils import iface
from qgis.core import (QgsCoordinateTransform,
QgsField,
QgsWkbTypes,
QgsFeatureSink,
QgsProcessingUtils,
QgsDistanceArea)

from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.core.parameters import ParameterVector
@@ -59,6 +63,9 @@ def group(self):

def __init__(self):
super().__init__()
self.export_z = False
self.export_m = False
self.distance_area = None

def initAlgorithm(self, config=None):
self.calc_methods = [self.tr('Layer CRS'),
@@ -85,8 +92,6 @@ def processAlgorithm(self, parameters, context, feedback):
geometryType = layer.geometryType()
fields = layer.fields()

export_z = False
export_m = False
if geometryType == QgsWkbTypes.PolygonGeometry:
areaName = vector.createUniqueFieldName('area', fields)
fields.append(QgsField(areaName, QVariant.Double))
@@ -101,64 +106,77 @@ def processAlgorithm(self, parameters, context, feedback):
yName = vector.createUniqueFieldName('ycoord', fields)
fields.append(QgsField(yName, QVariant.Double))
if QgsWkbTypes.hasZ(layer.wkbType()):
export_z = True
self.export_z = True
zName = vector.createUniqueFieldName('zcoord', fields)
fields.append(QgsField(zName, QVariant.Double))
if QgsWkbTypes.hasM(layer.wkbType()):
export_m = True
self.export_m = True
zName = vector.createUniqueFieldName('mvalue', fields)
fields.append(QgsField(zName, QVariant.Double))

writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fields, layer.wkbType(), layer.crs(),
context)

ellips = None
crs = None
coordTransform = None

# Calculate with:
# 0 - layer CRS
# 1 - project CRS
# 2 - ellipsoidal

self.distance_area = QgsDistanceArea()
if method == 2:
ellips = QgsProject.instance().ellipsoid()
crs = layer.crs().srsid()
self.distance_area.setSourceCrs(layer.crs())
self.distance_area.setEllipsoid(context.project().ellipsoid())
elif method == 1:
mapCRS = iface.mapCanvas().mapSettings().destinationCrs()
layCRS = layer.crs()
coordTransform = QgsCoordinateTransform(layCRS, mapCRS)

outFeat = QgsFeature()

outFeat.initAttributes(len(fields))
outFeat.setFields(fields)
coordTransform = QgsCoordinateTransform(layer.crs(), context.project().crs())

features = QgsProcessingUtils.getFeatures(layer, context)
total = 100.0 / layer.featureCount() if layer.featureCount() else 0
for current, f in enumerate(features):
inGeom = f.geometry()

if method == 1:
inGeom.transform(coordTransform)

(attr1, attr2) = vector.simpleMeasure(inGeom, method, ellips, crs)

outFeat.setGeometry(inGeom)
outFeat = f
attrs = f.attributes()
attrs.append(attr1)
if attr2 is not None:
attrs.append(attr2)
inGeom = f.geometry()
if inGeom:
if coordTransform is not None:
inGeom.transform(coordTransform)

# add point z/m
if export_z:
attrs.append(inGeom.geometry().z())
if export_m:
attrs.append(inGeom.geometry().m())
if inGeom.type() == QgsWkbTypes.PointGeometry:
attrs.extend(self.point_attributes(inGeom))
elif inGeom.type() == QgsWkbTypes.PolygonGeometry:
attrs.extend(self.polygon_attributes(inGeom))
else:
attrs.extend(self.line_attributes(inGeom))

outFeat.setAttributes(attrs)
writer.addFeature(outFeat, QgsFeatureSink.FastInsert)

feedback.setProgress(int(current * total))

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


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):
"""Create single field map from two input field maps.
"""

0 comments on commit 18dd097

Please sign in to comment.
You can’t perform that action at this time.