30
30
from qgis .PyQt .QtGui import QIcon
31
31
from qgis .PyQt .QtCore import QVariant
32
32
33
- from qgis .core import QgsProject , QgsCoordinateTransform , QgsFeature , QgsField , QgsWkbTypes , QgsFeatureSink , QgsProcessingUtils
34
- from qgis .utils import iface
33
+ from qgis .core import (QgsCoordinateTransform ,
34
+ QgsField ,
35
+ QgsWkbTypes ,
36
+ QgsFeatureSink ,
37
+ QgsProcessingUtils ,
38
+ QgsDistanceArea )
35
39
36
40
from processing .algs .qgis .QgisAlgorithm import QgisAlgorithm
37
41
from processing .core .parameters import ParameterVector
@@ -59,6 +63,9 @@ def group(self):
59
63
60
64
def __init__ (self ):
61
65
super ().__init__ ()
66
+ self .export_z = False
67
+ self .export_m = False
68
+ self .distance_area = None
62
69
63
70
def initAlgorithm (self , config = None ):
64
71
self .calc_methods = [self .tr ('Layer CRS' ),
@@ -85,8 +92,6 @@ def processAlgorithm(self, parameters, context, feedback):
85
92
geometryType = layer .geometryType ()
86
93
fields = layer .fields ()
87
94
88
- export_z = False
89
- export_m = False
90
95
if geometryType == QgsWkbTypes .PolygonGeometry :
91
96
areaName = vector .createUniqueFieldName ('area' , fields )
92
97
fields .append (QgsField (areaName , QVariant .Double ))
@@ -101,64 +106,77 @@ def processAlgorithm(self, parameters, context, feedback):
101
106
yName = vector .createUniqueFieldName ('ycoord' , fields )
102
107
fields .append (QgsField (yName , QVariant .Double ))
103
108
if QgsWkbTypes .hasZ (layer .wkbType ()):
104
- export_z = True
109
+ self . export_z = True
105
110
zName = vector .createUniqueFieldName ('zcoord' , fields )
106
111
fields .append (QgsField (zName , QVariant .Double ))
107
112
if QgsWkbTypes .hasM (layer .wkbType ()):
108
- export_m = True
113
+ self . export_m = True
109
114
zName = vector .createUniqueFieldName ('mvalue' , fields )
110
115
fields .append (QgsField (zName , QVariant .Double ))
111
116
112
117
writer = self .getOutputFromName (self .OUTPUT ).getVectorWriter (fields , layer .wkbType (), layer .crs (),
113
118
context )
114
119
115
- ellips = None
116
- crs = None
117
120
coordTransform = None
118
121
119
122
# Calculate with:
120
123
# 0 - layer CRS
121
124
# 1 - project CRS
122
125
# 2 - ellipsoidal
123
126
127
+ self .distance_area = QgsDistanceArea ()
124
128
if method == 2 :
125
- ellips = QgsProject . instance (). ellipsoid ( )
126
- crs = layer . crs (). srsid ( )
129
+ self . distance_area . setSourceCrs ( layer . crs () )
130
+ self . distance_area . setEllipsoid ( context . project (). ellipsoid () )
127
131
elif method == 1 :
128
- mapCRS = iface .mapCanvas ().mapSettings ().destinationCrs ()
129
- layCRS = layer .crs ()
130
- coordTransform = QgsCoordinateTransform (layCRS , mapCRS )
131
-
132
- outFeat = QgsFeature ()
133
-
134
- outFeat .initAttributes (len (fields ))
135
- outFeat .setFields (fields )
132
+ coordTransform = QgsCoordinateTransform (layer .crs (), context .project ().crs ())
136
133
137
134
features = QgsProcessingUtils .getFeatures (layer , context )
138
135
total = 100.0 / layer .featureCount () if layer .featureCount () else 0
139
136
for current , f in enumerate (features ):
140
- inGeom = f .geometry ()
141
-
142
- if method == 1 :
143
- inGeom .transform (coordTransform )
144
-
145
- (attr1 , attr2 ) = vector .simpleMeasure (inGeom , method , ellips , crs )
146
-
147
- outFeat .setGeometry (inGeom )
137
+ outFeat = f
148
138
attrs = f .attributes ()
149
- attrs .append (attr1 )
150
- if attr2 is not None :
151
- attrs .append (attr2 )
139
+ inGeom = f .geometry ()
140
+ if inGeom :
141
+ if coordTransform is not None :
142
+ inGeom .transform (coordTransform )
152
143
153
- # add point z/m
154
- if export_z :
155
- attrs .append (inGeom .geometry ().z ())
156
- if export_m :
157
- attrs .append (inGeom .geometry ().m ())
144
+ if inGeom .type () == QgsWkbTypes .PointGeometry :
145
+ attrs .extend (self .point_attributes (inGeom ))
146
+ elif inGeom .type () == QgsWkbTypes .PolygonGeometry :
147
+ attrs .extend (self .polygon_attributes (inGeom ))
148
+ else :
149
+ attrs .extend (self .line_attributes (inGeom ))
158
150
159
151
outFeat .setAttributes (attrs )
160
152
writer .addFeature (outFeat , QgsFeatureSink .FastInsert )
161
153
162
154
feedback .setProgress (int (current * total ))
163
155
164
156
del writer
157
+
158
+ def point_attributes (self , geometry ):
159
+ pt = None
160
+ if not geometry .isMultipart ():
161
+ pt = geometry .geometry ()
162
+ else :
163
+ if geometry .numGeometries () > 0 :
164
+ pt = geometry .geometryN (0 )
165
+ attrs = []
166
+ if pt :
167
+ attrs .append (pt .x ())
168
+ attrs .append (pt .y ())
169
+ # add point z/m
170
+ if self .export_z :
171
+ attrs .append (pt .z ())
172
+ if self .export_m :
173
+ attrs .append (pt .m ())
174
+ return attrs
175
+
176
+ def line_attributes (self , geometry ):
177
+ return [self .distance_area .measureLength (geometry )]
178
+
179
+ def polygon_attributes (self , geometry ):
180
+ area = self .distance_area .measureArea (geometry )
181
+ perimeter = self .distance_area .measurePerimeter (geometry )
182
+ return [area , perimeter ]
0 commit comments