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
|
|
|
|
|
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
|
|
|
|
2017-06-23 14:34:38 +10:00
|
|
|
from qgis.core import QgsProject, QgsCoordinateTransform, QgsFeature, QgsField, QgsWkbTypes, QgsFeatureSink, QgsProcessingUtils
|
2014-05-20 16:30:59 +02:00
|
|
|
from qgis.utils import iface
|
2016-01-25 15:42:11 +02:00
|
|
|
|
2017-06-06 13:41:42 +10:00
|
|
|
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
|
2014-07-14 14:19:09 +02:00
|
|
|
from processing.core.parameters import ParameterVector
|
|
|
|
from processing.core.parameters import ParameterSelection
|
|
|
|
from processing.core.outputs import OutputVector
|
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):
|
2016-01-25 15:42:11 +02:00
|
|
|
return QIcon(os.path.join(pluginPath, 'images', 'ftools', 'export_geometry.png'))
|
|
|
|
|
2017-03-29 09:51:13 +10:00
|
|
|
def tags(self):
|
|
|
|
return self.tr('export,measurements,areas,lengths,perimeters,latitudes,longitudes,x,y,z,extract,points,lines,polygons').split(',')
|
|
|
|
|
2017-03-29 12:04:09 +10:00
|
|
|
def group(self):
|
|
|
|
return self.tr('Vector table tools')
|
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
2015-08-31 16:59:11 +02:00
|
|
|
self.calc_methods = [self.tr('Layer CRS'),
|
|
|
|
self.tr('Project CRS'),
|
|
|
|
self.tr('Ellipsoidal')]
|
|
|
|
|
2015-01-15 20:41:15 +02:00
|
|
|
self.addParameter(ParameterVector(self.INPUT,
|
2016-08-23 19:33:42 +03:00
|
|
|
self.tr('Input layer')))
|
2015-01-15 20:41:15 +02:00
|
|
|
self.addParameter(ParameterSelection(self.METHOD,
|
2015-08-31 16:59:11 +02:00
|
|
|
self.tr('Calculate using'), self.calc_methods, 0))
|
2012-10-01 20:48:06 +03:00
|
|
|
|
2015-05-22 16:39:20 +02:00
|
|
|
self.addOutput(OutputVector(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):
|
|
|
|
return self.tr('Export/Add geometry columns')
|
|
|
|
|
2017-05-15 16:19:46 +10:00
|
|
|
def processAlgorithm(self, parameters, context, feedback):
|
2017-05-02 13:40:49 +10:00
|
|
|
layer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT), context)
|
2013-01-12 23:36:00 +01:00
|
|
|
method = self.getParameterValue(self.METHOD)
|
2012-10-01 20:48:06 +03:00
|
|
|
|
|
|
|
geometryType = layer.geometryType()
|
2016-08-04 07:37:22 +10:00
|
|
|
fields = layer.fields()
|
2012-10-01 20:48:06 +03:00
|
|
|
|
2016-10-11 08:46:38 +10:00
|
|
|
export_z = False
|
|
|
|
export_m = False
|
2016-08-04 09:10:08 +02:00
|
|
|
if geometryType == QgsWkbTypes.PolygonGeometry:
|
2015-06-26 09:36:53 +02:00
|
|
|
areaName = vector.createUniqueFieldName('area', fields)
|
|
|
|
fields.append(QgsField(areaName, QVariant.Double))
|
|
|
|
perimeterName = vector.createUniqueFieldName('perimeter', fields)
|
|
|
|
fields.append(QgsField(perimeterName, QVariant.Double))
|
2016-08-04 09:10:08 +02:00
|
|
|
elif geometryType == QgsWkbTypes.LineGeometry:
|
2015-06-26 09:36:53 +02:00
|
|
|
lengthName = vector.createUniqueFieldName('length', fields)
|
|
|
|
fields.append(QgsField(lengthName, QVariant.Double))
|
2012-10-01 20:48:06 +03:00
|
|
|
else:
|
2015-06-26 09:36:53 +02:00
|
|
|
xName = vector.createUniqueFieldName('xcoord', fields)
|
|
|
|
fields.append(QgsField(xName, QVariant.Double))
|
|
|
|
yName = vector.createUniqueFieldName('ycoord', fields)
|
|
|
|
fields.append(QgsField(yName, QVariant.Double))
|
2016-10-11 08:46:38 +10:00
|
|
|
if QgsWkbTypes.hasZ(layer.wkbType()):
|
|
|
|
export_z = True
|
|
|
|
zName = vector.createUniqueFieldName('zcoord', fields)
|
|
|
|
fields.append(QgsField(zName, QVariant.Double))
|
|
|
|
if QgsWkbTypes.hasM(layer.wkbType()):
|
|
|
|
export_m = True
|
|
|
|
zName = vector.createUniqueFieldName('mvalue', fields)
|
|
|
|
fields.append(QgsField(zName, QVariant.Double))
|
2013-02-28 21:43:59 +04:00
|
|
|
|
2017-05-02 21:15:03 +10:00
|
|
|
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fields, layer.wkbType(), layer.crs(),
|
2017-04-26 14:33:53 +10:00
|
|
|
context)
|
2012-10-01 20:48:06 +03:00
|
|
|
|
|
|
|
ellips = None
|
|
|
|
crs = None
|
|
|
|
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
|
|
|
|
2013-06-20 23:40:51 +02:00
|
|
|
if method == 2:
|
2017-03-14 19:14:56 +10:00
|
|
|
ellips = QgsProject.instance().ellipsoid()
|
2012-10-01 20:48:06 +03:00
|
|
|
crs = layer.crs().srsid()
|
|
|
|
elif method == 1:
|
2016-01-20 16:39:13 +02:00
|
|
|
mapCRS = iface.mapCanvas().mapSettings().destinationCrs()
|
2012-10-01 20:48:06 +03:00
|
|
|
layCRS = layer.crs()
|
|
|
|
coordTransform = QgsCoordinateTransform(layCRS, mapCRS)
|
2013-01-12 23:36:00 +01:00
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
outFeat = QgsFeature()
|
|
|
|
|
2013-02-28 21:43:59 +04:00
|
|
|
outFeat.initAttributes(len(fields))
|
|
|
|
outFeat.setFields(fields)
|
|
|
|
|
2017-04-25 17:59:35 +10:00
|
|
|
features = QgsProcessingUtils.getFeatures(layer, context)
|
2017-06-23 13:49:32 +10:00
|
|
|
total = 100.0 / layer.featureCount() if layer.featureCount() else 0
|
2016-02-17 09:36:59 +02:00
|
|
|
for current, f in enumerate(features):
|
2013-02-28 21:43:59 +04:00
|
|
|
inGeom = f.geometry()
|
2012-10-01 20:48:06 +03:00
|
|
|
|
|
|
|
if method == 1:
|
2013-01-01 23:52:00 +01:00
|
|
|
inGeom.transform(coordTransform)
|
2012-10-01 20:48:06 +03:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
(attr1, attr2) = vector.simpleMeasure(inGeom, method, ellips, crs)
|
2012-10-01 20:48:06 +03:00
|
|
|
|
|
|
|
outFeat.setGeometry(inGeom)
|
2013-02-28 21:43:59 +04:00
|
|
|
attrs = f.attributes()
|
2015-06-26 09:36:53 +02:00
|
|
|
attrs.append(attr1)
|
2013-02-04 23:29:55 +01:00
|
|
|
if attr2 is not None:
|
2015-06-26 09:36:53 +02:00
|
|
|
attrs.append(attr2)
|
2016-10-11 08:46:38 +10:00
|
|
|
|
|
|
|
# add point z/m
|
|
|
|
if export_z:
|
|
|
|
attrs.append(inGeom.geometry().z())
|
|
|
|
if export_m:
|
|
|
|
attrs.append(inGeom.geometry().m())
|
|
|
|
|
2013-02-28 21:43:59 +04:00
|
|
|
outFeat.setAttributes(attrs)
|
2017-06-23 14:34:38 +10:00
|
|
|
writer.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
|
|
|
|
|
|
|
del writer
|