2012-10-04 19:33:47 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
ExportGeometryInfo.py
|
|
|
|
---------------------
|
|
|
|
Date : August 2012
|
|
|
|
Copyright : (C) 2012 by Victor Olaya
|
|
|
|
Email : volayaf at gmail dot com
|
|
|
|
***************************************************************************
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
***************************************************************************
|
|
|
|
"""
|
|
|
|
|
|
|
|
__author__ = 'Victor Olaya'
|
|
|
|
__date__ = 'August 2012'
|
|
|
|
__copyright__ = '(C) 2012, Victor Olaya'
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2012-10-04 19:33:47 +02:00
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2012-10-04 19:33:47 +02:00
|
|
|
__revision__ = '$Format:%H$'
|
|
|
|
|
2016-01-25 15:42:11 +02:00
|
|
|
import os
|
2018-03-11 09:10:21 +10:00
|
|
|
import math
|
2016-01-25 15:42:11 +02:00
|
|
|
|
2016-04-22 10:38:48 +02:00
|
|
|
from qgis.PyQt.QtGui import QIcon
|
|
|
|
from qgis.PyQt.QtCore import QVariant
|
2016-01-25 15:42:11 +02:00
|
|
|
|
2018-02-15 13:02:23 +10:00
|
|
|
from qgis.core import (NULL,
|
2018-05-15 11:24:29 +07:00
|
|
|
QgsApplication,
|
2018-02-15 13:02:23 +10:00
|
|
|
QgsCoordinateTransform,
|
2017-07-27 10:46:53 +10:00
|
|
|
QgsField,
|
2017-09-23 08:41:54 +10:00
|
|
|
QgsFields,
|
2017-07-27 10:46:53 +10:00
|
|
|
QgsWkbTypes,
|
2018-03-11 09:10:21 +10:00
|
|
|
QgsPointXY,
|
2017-07-27 10:46:53 +10:00
|
|
|
QgsFeatureSink,
|
2017-07-27 11:21:24 +10:00
|
|
|
QgsDistanceArea,
|
2017-09-23 08:41:54 +10:00
|
|
|
QgsProcessingUtils,
|
2018-04-27 12:31:56 +10:00
|
|
|
QgsProcessingException,
|
2017-07-27 11:21:24 +10:00
|
|
|
QgsProcessingParameterFeatureSource,
|
|
|
|
QgsProcessingParameterEnum,
|
|
|
|
QgsProcessingParameterFeatureSink)
|
2016-01-25 15:42:11 +02:00
|
|
|
|
2017-06-06 13:41:42 +10:00
|
|
|
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
|
2017-05-02 14:47:58 +10:00
|
|
|
from processing.tools import vector
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2016-01-25 15:42:11 +02:00
|
|
|
pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
|
|
|
|
|
2013-02-27 15:07:57 +04:00
|
|
|
|
2017-05-19 11:27:16 +10:00
|
|
|
class ExportGeometryInfo(QgisAlgorithm):
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
INPUT = 'INPUT'
|
|
|
|
METHOD = 'CALC_METHOD'
|
|
|
|
OUTPUT = 'OUTPUT'
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2017-03-29 10:42:42 +10:00
|
|
|
def icon(self):
|
2018-05-15 11:24:29 +07:00
|
|
|
return QgsApplication.getThemeIcon("/algorithms/mAlgorithmAddGeometryAttributes.svg")
|
|
|
|
|
|
|
|
def svgIconPath(self):
|
|
|
|
return QgsApplication.iconPath("/algorithms/mAlgorithmAddGeometryAttributes.svg")
|
2016-01-25 15:42:11 +02:00
|
|
|
|
2017-03-29 09:51:13 +10:00
|
|
|
def tags(self):
|
2018-05-15 11:26:17 +07:00
|
|
|
return self.tr('export,add,information,measurements,areas,lengths,perimeters,latitudes,longitudes,x,y,z,extract,points,lines,polygons,sinuosity,fields').split(',')
|
2017-03-29 09:51:13 +10:00
|
|
|
|
2017-03-29 12:04:09 +10:00
|
|
|
def group(self):
|
2017-08-22 23:36:42 +10:00
|
|
|
return self.tr('Vector geometry')
|
2017-03-29 12:04:09 +10:00
|
|
|
|
2017-12-14 14:03:56 +02:00
|
|
|
def groupId(self):
|
|
|
|
return 'vectorgeometry'
|
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
2017-07-27 10:46:53 +10:00
|
|
|
self.export_z = False
|
|
|
|
self.export_m = False
|
|
|
|
self.distance_area = None
|
2015-08-31 16:59:11 +02:00
|
|
|
self.calc_methods = [self.tr('Layer CRS'),
|
|
|
|
self.tr('Project CRS'),
|
|
|
|
self.tr('Ellipsoidal')]
|
|
|
|
|
2017-07-27 11:21:24 +10:00
|
|
|
def initAlgorithm(self, config=None):
|
|
|
|
self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT,
|
|
|
|
self.tr('Input layer')))
|
|
|
|
self.addParameter(QgsProcessingParameterEnum(self.METHOD,
|
|
|
|
self.tr('Calculate using'), options=self.calc_methods, defaultValue=0))
|
|
|
|
self.addParameter(QgsProcessingParameterFeatureSink(self.OUTPUT, self.tr('Added geom info')))
|
2012-10-01 20:48:06 +03:00
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def name(self):
|
|
|
|
return 'exportaddgeometrycolumns'
|
|
|
|
|
|
|
|
def displayName(self):
|
2018-05-15 11:26:17 +07:00
|
|
|
return self.tr('Add geometry attributes')
|
2017-05-15 13:40:38 +10:00
|
|
|
|
2017-05-15 16:19:46 +10:00
|
|
|
def processAlgorithm(self, parameters, context, feedback):
|
2017-07-27 11:21:24 +10:00
|
|
|
source = self.parameterAsSource(parameters, self.INPUT, context)
|
2018-04-27 12:31:56 +10:00
|
|
|
if source is None:
|
|
|
|
raise QgsProcessingException(self.invalidSourceError(parameters, self.INPUT))
|
|
|
|
|
2017-07-27 11:21:24 +10:00
|
|
|
method = self.parameterAsEnum(parameters, self.METHOD, context)
|
2012-10-01 20:48:06 +03:00
|
|
|
|
2017-07-27 11:21:24 +10:00
|
|
|
wkb_type = source.wkbType()
|
|
|
|
fields = source.fields()
|
2012-10-01 20:48:06 +03:00
|
|
|
|
2017-09-23 08:41:54 +10:00
|
|
|
new_fields = QgsFields()
|
2017-07-27 11:21:24 +10:00
|
|
|
if QgsWkbTypes.geometryType(wkb_type) == QgsWkbTypes.PolygonGeometry:
|
2017-09-23 08:41:54 +10:00
|
|
|
new_fields.append(QgsField('area', QVariant.Double))
|
|
|
|
new_fields.append(QgsField('perimeter', QVariant.Double))
|
2017-07-27 11:21:24 +10:00
|
|
|
elif QgsWkbTypes.geometryType(wkb_type) == QgsWkbTypes.LineGeometry:
|
2017-09-23 08:41:54 +10:00
|
|
|
new_fields.append(QgsField('length', QVariant.Double))
|
2018-03-11 09:10:21 +10:00
|
|
|
if not QgsWkbTypes.isMultiType(source.wkbType()):
|
|
|
|
new_fields.append(QgsField('straightdis', QVariant.Double))
|
|
|
|
new_fields.append(QgsField('sinuosity', QVariant.Double))
|
2012-10-01 20:48:06 +03:00
|
|
|
else:
|
2019-02-24 09:48:03 +10:00
|
|
|
if QgsWkbTypes.isMultiType(source.wkbType()):
|
|
|
|
new_fields.append(QgsField('numparts', QVariant.Int))
|
|
|
|
else:
|
|
|
|
new_fields.append(QgsField('xcoord', QVariant.Double))
|
|
|
|
new_fields.append(QgsField('ycoord', QVariant.Double))
|
|
|
|
if QgsWkbTypes.hasZ(source.wkbType()):
|
|
|
|
self.export_z = True
|
|
|
|
new_fields.append(QgsField('zcoord', QVariant.Double))
|
|
|
|
if QgsWkbTypes.hasM(source.wkbType()):
|
|
|
|
self.export_m = True
|
|
|
|
new_fields.append(QgsField('mvalue', QVariant.Double))
|
2013-02-28 21:43:59 +04:00
|
|
|
|
2017-09-23 08:41:54 +10:00
|
|
|
fields = QgsProcessingUtils.combineFields(fields, new_fields)
|
2017-07-27 11:21:24 +10:00
|
|
|
(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
|
|
|
|
fields, wkb_type, source.sourceCrs())
|
2018-04-27 12:52:02 +10:00
|
|
|
if sink is None:
|
|
|
|
raise QgsProcessingException(self.invalidSinkError(parameters, self.OUTPUT))
|
2012-10-01 20:48:06 +03:00
|
|
|
|
|
|
|
coordTransform = None
|
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
# Calculate with:
|
2012-10-01 20:48:06 +03:00
|
|
|
# 0 - layer CRS
|
|
|
|
# 1 - project CRS
|
|
|
|
# 2 - ellipsoidal
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2017-07-27 10:46:53 +10:00
|
|
|
self.distance_area = QgsDistanceArea()
|
2013-06-20 23:40:51 +02:00
|
|
|
if method == 2:
|
2017-12-20 15:08:14 +10:00
|
|
|
self.distance_area.setSourceCrs(source.sourceCrs(), context.transformContext())
|
2017-07-27 10:46:53 +10:00
|
|
|
self.distance_area.setEllipsoid(context.project().ellipsoid())
|
2012-10-01 20:48:06 +03:00
|
|
|
elif method == 1:
|
2017-11-05 21:32:50 +10:00
|
|
|
coordTransform = QgsCoordinateTransform(source.sourceCrs(), context.project().crs(), context.project())
|
2013-02-28 21:43:59 +04:00
|
|
|
|
2017-07-27 11:21:24 +10:00
|
|
|
features = source.getFeatures()
|
|
|
|
total = 100.0 / source.featureCount() if source.featureCount() else 0
|
2016-02-17 09:36:59 +02:00
|
|
|
for current, f in enumerate(features):
|
2017-07-27 11:21:24 +10:00
|
|
|
if feedback.isCanceled():
|
|
|
|
break
|
|
|
|
|
2017-07-27 10:46:53 +10:00
|
|
|
outFeat = f
|
2013-02-28 21:43:59 +04:00
|
|
|
attrs = f.attributes()
|
2017-07-27 10:46:53 +10:00
|
|
|
inGeom = f.geometry()
|
|
|
|
if inGeom:
|
|
|
|
if coordTransform is not None:
|
|
|
|
inGeom.transform(coordTransform)
|
2016-10-11 08:46:38 +10:00
|
|
|
|
2017-07-27 10:46:53 +10:00
|
|
|
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))
|
2016-10-11 08:46:38 +10:00
|
|
|
|
2018-02-15 13:02:23 +10:00
|
|
|
# ensure consistent count of attributes - otherwise null
|
|
|
|
# geometry features will have incorrect attribute length
|
|
|
|
# and provider may reject them
|
|
|
|
if len(attrs) < len(fields):
|
|
|
|
attrs += [NULL] * (len(fields) - len(attrs))
|
|
|
|
|
2013-02-28 21:43:59 +04:00
|
|
|
outFeat.setAttributes(attrs)
|
2017-07-27 11:21:24 +10:00
|
|
|
sink.addFeature(outFeat, QgsFeatureSink.FastInsert)
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2017-01-06 20:04:00 +10:00
|
|
|
feedback.setProgress(int(current * total))
|
2012-10-01 20:48:06 +03:00
|
|
|
|
2017-07-27 11:21:24 +10:00
|
|
|
return {self.OUTPUT: dest_id}
|
2017-07-27 10:46:53 +10:00
|
|
|
|
|
|
|
def point_attributes(self, geometry):
|
2019-02-24 09:48:03 +10:00
|
|
|
attrs = []
|
2017-07-27 10:46:53 +10:00
|
|
|
if not geometry.isMultipart():
|
2017-10-25 17:48:19 +10:00
|
|
|
pt = geometry.constGet()
|
2017-07-27 10:46:53 +10:00
|
|
|
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())
|
2019-02-24 09:48:03 +10:00
|
|
|
else:
|
|
|
|
attrs = [geometry.constGet().numGeometries()]
|
2017-07-27 10:46:53 +10:00
|
|
|
return attrs
|
|
|
|
|
|
|
|
def line_attributes(self, geometry):
|
2018-03-11 09:10:21 +10:00
|
|
|
if geometry.isMultipart():
|
|
|
|
return [self.distance_area.measureLength(geometry)]
|
|
|
|
else:
|
|
|
|
curve = geometry.constGet()
|
|
|
|
p1 = curve.startPoint()
|
|
|
|
p2 = curve.endPoint()
|
|
|
|
straight_distance = self.distance_area.measureLine(QgsPointXY(p1), QgsPointXY(p2))
|
|
|
|
sinuosity = curve.sinuosity()
|
|
|
|
if math.isnan(sinuosity):
|
|
|
|
sinuosity = NULL
|
|
|
|
return [self.distance_area.measureLength(geometry), straight_distance, sinuosity]
|
2017-07-27 10:46:53 +10:00
|
|
|
|
|
|
|
def polygon_attributes(self, geometry):
|
|
|
|
area = self.distance_area.measureArea(geometry)
|
|
|
|
perimeter = self.distance_area.measurePerimeter(geometry)
|
|
|
|
return [area, perimeter]
|