38
38
import voronoi
39
39
from sets import Set
40
40
41
- class GeometryDialog (QDialog , Ui_Dialog ):
42
-
43
- def __init__ (self , iface , function ):
44
- QDialog .__init__ (self , iface .mainWindow ())
41
+ class GeometryDialog ( QDialog , Ui_Dialog ):
42
+ def __init__ ( self , iface , function ):
43
+ QDialog .__init__ ( self , iface .mainWindow () )
45
44
self .iface = iface
46
- self .setupUi (self )
45
+ self .setupUi ( self )
47
46
self .myFunction = function
48
47
self .buttonOk = self .buttonBox_2 .button ( QDialogButtonBox .Ok )
49
- QObject .connect (self .toolOut , SIGNAL ("clicked()" ), self .outFile )
48
+ QObject .connect ( self .toolOut , SIGNAL ( "clicked()" ), self .outFile )
50
49
if self .myFunction == 1 :
51
- QObject .connect (self .inShape , SIGNAL ("currentIndexChanged(QString)" ), self .update )
50
+ QObject .connect ( self .inShape , SIGNAL ( "currentIndexChanged( QString )" ), self .update )
52
51
self .manageGui ()
53
52
self .success = False
54
53
self .cancel_close = self .buttonBox_2 .button ( QDialogButtonBox .Close )
55
- self .progressBar .setValue (0 )
54
+ self .progressBar .setValue ( 0 )
56
55
57
- def update (self ):
56
+ def update ( self ):
58
57
self .cmbField .clear ()
59
- inputLayer = unicode (self .inShape .currentText ())
58
+ inputLayer = unicode ( self .inShape .currentText () )
60
59
if inputLayer != "" :
61
- changedLayer = ftools_utils .getVectorLayerByName (inputLayer )
62
- changedField = ftools_utils .getFieldList (changedLayer )
60
+ changedLayer = ftools_utils .getVectorLayerByName ( inputLayer )
61
+ changedField = ftools_utils .getFieldList ( changedLayer )
63
62
for i in changedField :
64
- self .cmbField .addItem (unicode (changedField [i ].name ()) )
65
- self .cmbField .addItem ("--- " + self .tr ( "Merge all" ) + " ---" )
63
+ self .cmbField .addItem ( unicode ( changedField [ i ].name () ) )
64
+ self .cmbField .addItem ( "--- " + self .tr ( "Merge all" ) + " ---" )
66
65
67
- def accept (self ):
66
+ def accept ( self ):
68
67
if self .inShape .currentText () == "" :
69
- QMessageBox .information (self , self .tr ("Geometry" ), self .tr ( "Please specify input vector layer" ) )
68
+ QMessageBox .information ( self , self .tr ( "Geometry" ),
69
+ self .tr ( "Please specify input vector layer" ) )
70
70
elif self .outShape .text () == "" :
71
- QMessageBox .information (self , self .tr ("Geometry" ), self .tr ( "Please specify output shapefile" ) )
71
+ QMessageBox .information ( self , self .tr ( "Geometry" ),
72
+ self .tr ( "Please specify output shapefile" ) )
72
73
elif self .lineEdit .isVisible () and self .lineEdit .value () < 0.00 :
73
- QMessageBox .information (self , self .tr ("Geometry" ), self .tr ( "Please specify valid tolerance value" ) )
74
+ QMessageBox .information ( self , self .tr ( "Geometry" ),
75
+ self .tr ( "Please specify valid tolerance value" ) )
74
76
elif self .cmbField .isVisible () and self .cmbField .currentText () == "" :
75
- QMessageBox .information (self , self .tr ("Geometry" ), self .tr ( "Please specify valid UID field" ) )
77
+ QMessageBox .information ( self , self .tr ( "Geometry" ),
78
+ self .tr ( "Please specify valid UID field" ) )
76
79
else :
77
80
self .outShape .clear ()
78
81
self .geometry ( self .inShape .currentText (), self .lineEdit .value (), self .cmbField .currentText () )
79
82
80
83
def outFile (self ):
81
84
self .outShape .clear ()
82
- (self .shapefileName , self .encoding ) = ftools_utils .saveDialog (self )
85
+ (self .shapefileName , self .encoding ) = ftools_utils .saveDialog ( self )
83
86
if self .shapefileName is None or self .encoding is None :
84
87
return
85
- self .outShape .setText (QString (self .shapefileName ))
88
+ self .outShape .setText ( QString ( self .shapefileName ) )
89
+
90
+ def manageGui ( self ):
91
+ self .lblField .setVisible ( False )
92
+ self .cmbField .setVisible ( False )
86
93
87
- def manageGui (self ):
94
+ self .lblCalcType .setVisible ( False )
95
+ self .cmbCalcType .setVisible ( False )
88
96
if self .myFunction == 1 : # Singleparts to multipart
89
97
self .setWindowTitle ( self .tr ( "Singleparts to multipart" ) )
90
- self .lineEdit .setVisible (False )
91
- self .label .setVisible (False )
98
+ self .lineEdit .setVisible ( False )
99
+ self .label .setVisible ( False )
92
100
self .label_2 .setText ( self .tr ( "Output shapefile" ) )
93
- self .cmbField .setVisible (True )
94
- self .field_label .setVisible (True )
101
+ self .cmbField .setVisible ( True )
102
+ self .lblField .setVisible ( True )
95
103
elif self .myFunction == 2 : # Multipart to singleparts
96
104
self .setWindowTitle ( self .tr ( "Multipart to singleparts" ) )
97
- self .lineEdit .setVisible (False )
98
- self .label .setVisible (False )
99
- self .label_2 .setText (self .tr ( "Output shapefile" ) )
100
- self .cmbField .setVisible (False )
101
- self .field_label .setVisible (False )
105
+ self .lineEdit .setVisible ( False )
106
+ self .label .setVisible ( False )
107
+ self .label_2 .setText ( self .tr ( "Output shapefile" ) )
102
108
elif self .myFunction == 3 : # Extract nodes
103
109
self .setWindowTitle ( self .tr ( "Extract nodes" ) )
104
- self .lineEdit .setVisible (False )
105
- self .label .setVisible (False )
106
- self .cmbField .setVisible (False )
107
- self .field_label .setVisible (False )
110
+ self .lineEdit .setVisible ( False )
111
+ self .label .setVisible ( False )
108
112
elif self .myFunction == 4 : # Polygons to lines
109
113
self .setWindowTitle ( self .tr ( "Polygons to lines" ) )
110
114
self .label_2 .setText ( self .tr ( "Output shapefile" ) )
111
115
self .label_3 .setText ( self .tr ( "Input polygon vector layer" ) )
112
- self .label .setVisible (False )
113
- self .lineEdit .setVisible (False )
114
- self .cmbField .setVisible (False )
115
- self .field_label .setVisible (False )
116
+ self .label .setVisible ( False )
117
+ self .lineEdit .setVisible ( False )
116
118
elif self .myFunction == 5 : # Export/Add geometry columns
117
119
self .setWindowTitle ( self .tr ( "Export/Add geometry columns" ) )
118
120
self .label_2 .setText ( self .tr ( "Output shapefile" ) )
119
121
self .label_3 .setText ( self .tr ( "Input vector layer" ) )
120
- self .label .setVisible (False )
121
- self .lineEdit .setVisible (False )
122
- self .cmbField .setVisible (False )
123
- self .field_label .setVisible (False )
122
+ self .label .setVisible ( False )
123
+ self .lineEdit .setVisible ( False )
124
+ # populate calculation types
125
+ self .lblCalcType .setVisible ( True )
126
+ self .cmbCalcType .setVisible ( True )
127
+ self .cmbCalcType .addItem ( self .tr ( "Layer CRS" ) )
128
+ self .cmbCalcType .addItem ( self .tr ( "Project CRS" ) )
129
+ self .cmbCalcType .addItem ( self .tr ( "Ellipsoid" ) )
124
130
elif self .myFunction == 7 : # Polygon centroids
125
131
self .setWindowTitle ( self .tr ( "Polygon centroids" ) )
126
132
self .label_2 .setText ( self .tr ( "Output point shapefile" ) )
127
133
self .label_3 .setText ( self .tr ( "Input polygon vector layer" ) )
128
134
self .label .setVisible ( False )
129
135
self .lineEdit .setVisible ( False )
130
- self .cmbField .setVisible ( False )
131
- self .field_label .setVisible ( False )
132
136
else :
133
137
if self .myFunction == 8 : # Delaunay triangulation
134
138
self .setWindowTitle ( self .tr ( "Delaunay triangulation" ) )
@@ -139,27 +143,22 @@ def manageGui(self):
139
143
self .setWindowTitle ( self .tr ( "Voronoi polygon" ) )
140
144
self .label_3 .setText ( self .tr ( "Input point vector layer" ) )
141
145
self .label .setText ( self .tr ( "Buffer region" ) )
142
- self .lineEdit .setSuffix (" %" )
143
- self .lineEdit .setRange (0 , 100 )
144
- self .lineEdit .setSingleStep (5 )
145
- self .lineEdit .setValue (0 )
146
+ self .lineEdit .setSuffix ( " %" )
147
+ self .lineEdit .setRange ( 0 , 100 )
148
+ self .lineEdit .setSingleStep ( 5 )
149
+ self .lineEdit .setValue ( 0 )
146
150
elif self .myFunction == 11 : #Lines to polygons
147
151
self .setWindowTitle ( self .tr ( "Lines to polygons" ) )
148
152
self .label_2 .setText ( self .tr ( "Output shapefile" ) )
149
153
self .label_3 .setText ( self .tr ( "Input line vector layer" ) )
150
- self .label .setVisible (False )
151
- self .lineEdit .setVisible (False )
152
- self .cmbField .setVisible (False )
153
- self .field_label .setVisible (False )
154
-
154
+ self .label .setVisible ( False )
155
+ self .lineEdit .setVisible ( False )
155
156
else : # Polygon from layer extent
156
157
self .setWindowTitle ( self .tr ( "Polygon from layer extent" ) )
157
158
self .label_3 .setText ( self .tr ( "Input layer" ) )
158
159
self .label .setVisible ( False )
159
160
self .lineEdit .setVisible ( False )
160
161
self .label_2 .setText ( self .tr ( "Output polygon shapefile" ) )
161
- self .cmbField .setVisible ( False )
162
- self .field_label .setVisible ( False )
163
162
self .resize ( 381 , 100 )
164
163
self .populateLayers ()
165
164
@@ -186,9 +185,9 @@ def populateLayers( self ):
186
185
#5: Export/Add geometry columns
187
186
#6: Simplify geometries (disabled)
188
187
#7: Polygon centroids
189
- #8: Delaunay triangulation
190
- #9: Polygon from layer extent
191
- #10:Voronoi polygons
188
+ #8: Delaunay triangulation
189
+ #9: Polygon from layer extent
190
+ #10: Voronoi polygons
192
191
#11: Lines to polygons
193
192
194
193
def geometry ( self , myLayer , myParam , myField ):
@@ -200,15 +199,16 @@ def geometry( self, myLayer, myParam, myField ):
200
199
check = QFile ( self .shapefileName )
201
200
if check .exists ():
202
201
if not QgsVectorFileWriter .deleteShapeFile ( self .shapefileName ):
203
- QMessageBox .warning ( self , self .tr ("Geoprocessing" ), self .tr ( "Unable to delete existing shapefile." ) )
202
+ QMessageBox .warning ( self , self .tr ( "Geometry" ),
203
+ self .tr ( "Unable to delete existing shapefile." ) )
204
204
return
205
205
self .buttonOk .setEnabled ( False )
206
206
self .testThread = geometryThread ( self .iface .mainWindow (), self , self .myFunction , vlayer , myParam ,
207
- myField , self .shapefileName , self .encoding )
208
- QObject .connect ( self .testThread , SIGNAL ( "runFinished(PyQt_PyObject)" ), self .runFinishedFromThread )
209
- QObject .connect ( self .testThread , SIGNAL ( "runStatus(PyQt_PyObject)" ), self .runStatusFromThread )
210
- QObject .connect ( self .testThread , SIGNAL ( "runRange(PyQt_PyObject)" ), self .runRangeFromThread )
211
- self .cancel_close .setText ( self .tr ("Cancel" ) )
207
+ myField , self .shapefileName , self .encoding , self . cmbCalcType . currentIndex () )
208
+ QObject .connect ( self .testThread , SIGNAL ( "runFinished( PyQt_PyObject )" ), self .runFinishedFromThread )
209
+ QObject .connect ( self .testThread , SIGNAL ( "runStatus( PyQt_PyObject )" ), self .runStatusFromThread )
210
+ QObject .connect ( self .testThread , SIGNAL ( "runRange( PyQt_PyObject )" ), self .runRangeFromThread )
211
+ self .cancel_close .setText ( self .tr ( "Cancel" ) )
212
212
QObject .connect ( self .cancel_close , SIGNAL ( "clicked()" ), self .cancelThread )
213
213
self .testThread .start ()
214
214
@@ -221,13 +221,17 @@ def runFinishedFromThread( self, success ):
221
221
self .buttonOk .setEnabled ( True )
222
222
extra = ""
223
223
if success == "math_error" :
224
- QMessageBox .warning ( self , self .tr ("Geometry" ), self .tr ("Error processing specified tolerance!\n Please choose larger tolerance..." ) )
224
+ QMessageBox .warning ( self , self .tr ( "Geometry" ),
225
+ self .tr ( "Error processing specified tolerance!\n Please choose larger tolerance..." ) )
225
226
if not QgsVectorFileWriter .deleteShapeFile ( self .shapefileName ):
226
- QMessageBox .warning ( self , self .tr ("Geometry" ), self .tr ( "Unable to delete incomplete shapefile." ) )
227
+ QMessageBox .warning ( self , self .tr ( "Geometry" ),
228
+ self .tr ( "Unable to delete incomplete shapefile." ) )
227
229
elif success == "attr_error" :
228
- QMessageBox .warning ( self , self .tr ("Geometry" ), self .tr ("At least two features must have same attribute value!\n Please choose another field..." ) )
230
+ QMessageBox .warning ( self , self .tr ( "Geometry" ),
231
+ self .tr ( "At least two features must have same attribute value!\n Please choose another field..." ) )
229
232
if not QgsVectorFileWriter .deleteShapeFile ( self .shapefileName ):
230
- QMessageBox .warning ( self , self .tr ("Geometry" ), self .tr ( "Unable to delete incomplete shapefile." ) )
233
+ QMessageBox .warning ( self , self .tr ( "Geometry" ),
234
+ self .tr ( "Unable to delete incomplete shapefile." ) )
231
235
else :
232
236
if success == "valid_error" :
233
237
extra = self .tr ("One or more features in the output layer may have invalid "
@@ -237,14 +241,15 @@ def runFinishedFromThread( self, success ):
237
241
QObject .disconnect ( self .cancel_close , SIGNAL ( "clicked()" ), self .cancelThread )
238
242
if success :
239
243
addToTOC = QMessageBox .question ( self , self .tr ("Geometry" ),
240
- self .tr ( "Created output shapefile:\n %1\n %2\n \n Would you like to add the new layer to the TOC?" ).arg ( unicode ( self .shapefileName ) ).arg ( extra ),
241
- QMessageBox .Yes , QMessageBox .No , QMessageBox .NoButton )
244
+ self .tr ( "Created output shapefile:\n %1\n %2\n \n Would you like to add the new layer to the TOC?" ).arg ( unicode ( self .shapefileName ) ).arg ( extra ),
245
+ QMessageBox .Yes , QMessageBox .No , QMessageBox .NoButton )
242
246
if addToTOC == QMessageBox .Yes :
243
247
if not ftools_utils .addShapeToCanvas ( unicode ( self .shapefileName ) ):
244
- QMessageBox .warning ( self , self .tr ("Geoprocessing" ), self .tr ( "Error loading output shapefile:\n %1" ).arg ( unicode ( self .shapefileName ) ))
248
+ QMessageBox .warning ( self , self .tr ( "Geometry" ),
249
+ self .tr ( "Error loading output shapefile:\n %1" ).arg ( unicode ( self .shapefileName ) ) )
245
250
self .populateLayers ()
246
251
else :
247
- QMessageBox .warning ( self , self .tr ("Geometry" ), self .tr ( "Error writing output shapefile." ) )
252
+ QMessageBox .warning ( self , self .tr ( "Geometry" ), self .tr ( "Error writing output shapefile." ) )
248
253
249
254
def runStatusFromThread ( self , status ):
250
255
self .progressBar .setValue ( status )
@@ -253,7 +258,7 @@ def runRangeFromThread( self, range_vals ):
253
258
self .progressBar .setRange ( range_vals [ 0 ], range_vals [ 1 ] )
254
259
255
260
class geometryThread ( QThread ):
256
- def __init__ ( self , parentThread , parentObject , function , vlayer , myParam , myField , myName , myEncoding ):
261
+ def __init__ ( self , parentThread , parentObject , function , vlayer , myParam , myField , myName , myEncoding , myCalcType ):
257
262
QThread .__init__ ( self , parentThread )
258
263
self .parent = parentObject
259
264
self .running = False
@@ -263,6 +268,7 @@ def __init__( self, parentThread, parentObject, function, vlayer, myParam, myFie
263
268
self .myField = myField
264
269
self .myName = myName
265
270
self .myEncoding = myEncoding
271
+ self .myCalcType = myCalcType
266
272
267
273
def run ( self ):
268
274
self .running = True
@@ -287,10 +293,10 @@ def run( self ):
287
293
success = self .voronoi_polygons ()
288
294
elif self .myFunction == 11 : # Lines to polygons
289
295
success = self .lines_to_polygons ()
290
- self .emit ( SIGNAL ( "runFinished(PyQt_PyObject)" ), success )
291
- self .emit ( SIGNAL ( "runStatus(PyQt_PyObject)" ), 0 )
296
+ self .emit ( SIGNAL ( "runFinished( PyQt_PyObject )" ), success )
297
+ self .emit ( SIGNAL ( "runStatus( PyQt_PyObject )" ), 0 )
292
298
293
- def stop (self ):
299
+ def stop ( self ):
294
300
self .running = False
295
301
296
302
def single_to_multi ( self ):
@@ -299,9 +305,9 @@ def single_to_multi( self ):
299
305
vprovider .select ( allAttrs )
300
306
fields = vprovider .fields ()
301
307
allValid = True
302
- geomType = self .singleToMultiGeom (vprovider .geometryType ())
303
- writer = QgsVectorFileWriter ( self .myName , self .myEncoding ,
304
- fields , geomType , vprovider .crs () )
308
+ geomType = self .singleToMultiGeom ( vprovider .geometryType () )
309
+ writer = QgsVectorFileWriter ( self .myName , self .myEncoding , fields ,
310
+ geomType , vprovider .crs () )
305
311
inFeat = QgsFeature ()
306
312
outFeat = QgsFeature ()
307
313
inGeom = QgsGeometry ()
@@ -310,27 +316,25 @@ def single_to_multi( self ):
310
316
if not index == - 1 :
311
317
unique = ftools_utils .getUniqueValues ( vprovider , int ( index ) )
312
318
else :
313
- unique = [QVariant (QString ()) ]
319
+ unique = [ QVariant ( QString () ) ]
314
320
nFeat = vprovider .featureCount () * len ( unique )
315
321
nElement = 0
316
- self .emit ( SIGNAL ( "runStatus(PyQt_PyObject)" ), 0 )
317
- self .emit ( SIGNAL ( "runRange(PyQt_PyObject)" ), ( 0 , nFeat ) )
318
- merge_all = self .myField == QString ("--- " + self .tr ( "Merge all" ) + " ---" )
319
- if not len ( unique ) == self .vlayer .featureCount () \
320
- or merge_all :
322
+ self .emit ( SIGNAL ( "runStatus( PyQt_PyObject )" ), 0 )
323
+ self .emit ( SIGNAL ( "runRange( PyQt_PyObject )" ), ( 0 , nFeat ) )
324
+ merge_all = self .myField == QString ( "--- " + self .tr ( "Merge all" ) + " ---" )
325
+ if not len ( unique ) == self .vlayer .featureCount () or merge_all :
321
326
for i in unique :
322
327
vprovider .rewind ()
323
328
multi_feature = []
324
329
first = True
325
- vprovider .select (allAttrs )
330
+ vprovider .select ( allAttrs )
326
331
while vprovider .nextFeature ( inFeat ):
327
332
atMap = inFeat .attributeMap ()
328
333
if not merge_all :
329
334
idVar = atMap [ index ]
330
335
else :
331
- idVar = QVariant (QString ())
332
- if idVar .toString ().trimmed () == i .toString ().trimmed () \
333
- or merge_all :
336
+ idVar = QVariant ( QString () )
337
+ if idVar .toString ().trimmed () == i .toString ().trimmed () or merge_all :
334
338
if first :
335
339
atts = atMap
336
340
first = False
@@ -339,13 +343,13 @@ def single_to_multi( self ):
339
343
feature_list = self .extractAsMulti ( inGeom )
340
344
multi_feature .extend ( feature_list )
341
345
nElement += 1
342
- self .emit ( SIGNAL ( "runStatus(PyQt_PyObject)" ), nElement )
346
+ self .emit ( SIGNAL ( "runStatus( PyQt_PyObject )" ), nElement )
343
347
outFeat .setAttributeMap ( atts )
344
- outGeom = QgsGeometry ( self .convertGeometry (multi_feature , vType ) )
348
+ outGeom = QgsGeometry ( self .convertGeometry ( multi_feature , vType ) )
345
349
if not outGeom .isGeosValid ():
346
350
allValid = "valid_error"
347
- outFeat .setGeometry (outGeom )
348
- writer .addFeature (outFeat )
351
+ outFeat .setGeometry ( outGeom )
352
+ writer .addFeature ( outFeat )
349
353
del writer
350
354
else :
351
355
return "attr_error"
@@ -356,20 +360,20 @@ def multi_to_single( self ):
356
360
allAttrs = vprovider .attributeIndexes ()
357
361
vprovider .select ( allAttrs )
358
362
fields = vprovider .fields ()
359
- geomType = self .multiToSingleGeom (vprovider .geometryType ())
360
- writer = QgsVectorFileWriter ( self .myName , self .myEncoding ,
361
- fields , geomType , vprovider .crs () )
363
+ geomType = self .multiToSingleGeom ( vprovider .geometryType () )
364
+ writer = QgsVectorFileWriter ( self .myName , self .myEncoding , fields ,
365
+ geomType , vprovider .crs () )
362
366
inFeat = QgsFeature ()
363
367
outFeat = QgsFeature ()
364
368
inGeom = QgsGeometry ()
365
369
outGeom = QgsGeometry ()
366
370
nFeat = vprovider .featureCount ()
367
371
nElement = 0
368
- self .emit ( SIGNAL ( "runStatus(PyQt_PyObject)" ), 0 )
369
- self .emit ( SIGNAL ( "runRange(PyQt_PyObject)" ), ( 0 , nFeat ) )
372
+ self .emit ( SIGNAL ( "runStatus( PyQt_PyObject )" ), 0 )
373
+ self .emit ( SIGNAL ( "runRange( PyQt_PyObject )" ), ( 0 , nFeat ) )
370
374
while vprovider .nextFeature ( inFeat ):
371
375
nElement += 1
372
- self .emit ( SIGNAL ( "runStatus(PyQt_PyObject)" ), nElement )
376
+ self .emit ( SIGNAL ( "runStatus( PyQt_PyObject )" ), nElement )
373
377
inGeom = inFeat .geometry ()
374
378
atMap = inFeat .attributeMap ()
375
379
featList = self .extractAsSingle ( inGeom )
@@ -385,19 +389,19 @@ def extract_nodes( self ):
385
389
allAttrs = vprovider .attributeIndexes ()
386
390
vprovider .select ( allAttrs )
387
391
fields = vprovider .fields ()
388
- writer = QgsVectorFileWriter ( self .myName , self .myEncoding ,
389
- fields , QGis .WKBPoint , vprovider .crs () )
392
+ writer = QgsVectorFileWriter ( self .myName , self .myEncoding , fields ,
393
+ QGis .WKBPoint , vprovider .crs () )
390
394
inFeat = QgsFeature ()
391
395
outFeat = QgsFeature ()
392
396
inGeom = QgsGeometry ()
393
397
outGeom = QgsGeometry ()
394
398
nFeat = vprovider .featureCount ()
395
399
nElement = 0
396
- self .emit ( SIGNAL ( "runStatus(PyQt_PyObject)" ), 0 )
397
- self .emit ( SIGNAL ( "runRange(PyQt_PyObject)" ), ( 0 , nFeat ) )
400
+ self .emit ( SIGNAL ( "runStatus( PyQt_PyObject )" ), 0 )
401
+ self .emit ( SIGNAL ( "runRange( PyQt_PyObject )" ), ( 0 , nFeat ) )
398
402
while vprovider .nextFeature ( inFeat ):
399
403
nElement += 1
400
- self .emit ( SIGNAL ( "runStatus(PyQt_PyObject)" ), nElement )
404
+ self .emit ( SIGNAL ( "runStatus( PyQt_PyObject )" ), nElement )
401
405
inGeom = inFeat .geometry ()
402
406
atMap = inFeat .attributeMap ()
403
407
pointList = ftools_utils .extractPoints ( inGeom )
@@ -413,20 +417,20 @@ def polygons_to_lines( self ):
413
417
allAttrs = vprovider .attributeIndexes ()
414
418
vprovider .select ( allAttrs )
415
419
fields = vprovider .fields ()
416
- writer = QgsVectorFileWriter ( self .myName , self .myEncoding ,
417
- fields , QGis .WKBLineString , vprovider .crs () )
420
+ writer = QgsVectorFileWriter ( self .myName , self .myEncoding , fields ,
421
+ QGis .WKBLineString , vprovider .crs () )
418
422
inFeat = QgsFeature ()
419
423
outFeat = QgsFeature ()
420
424
inGeom = QgsGeometry ()
421
425
outGeom = QgsGeometry ()
422
426
nFeat = vprovider .featureCount ()
423
427
nElement = 0
424
- self .emit ( SIGNAL ( "runStatus(PyQt_PyObject)" ), 0 )
425
- self .emit ( SIGNAL ( "runRange(PyQt_PyObject)" ), ( 0 , nFeat ) )
426
- while vprovider .nextFeature (inFeat ):
428
+ self .emit ( SIGNAL ( "runStatus( PyQt_PyObject )" ), 0 )
429
+ self .emit ( SIGNAL ( "runRange( PyQt_PyObject )" ), ( 0 , nFeat ) )
430
+ while vprovider .nextFeature ( inFeat ):
427
431
multi = False
428
432
nElement += 1
429
- self .emit ( SIGNAL ( "runStatus(PyQt_PyObject)" ), nElement )
433
+ self .emit ( SIGNAL ( "runStatus( PyQt_PyObject )" ), nElement )
430
434
inGeom = inFeat .geometry ()
431
435
if inGeom .isMultipart ():
432
436
multi = True
@@ -444,27 +448,27 @@ def lines_to_polygons( self ):
444
448
allAttrs = vprovider .attributeIndexes ()
445
449
vprovider .select ( allAttrs )
446
450
fields = vprovider .fields ()
447
- writer = QgsVectorFileWriter ( self .myName , self .myEncoding ,
448
- fields , QGis .WKBPolygon , vprovider .crs () )
451
+ writer = QgsVectorFileWriter ( self .myName , self .myEncoding , fields ,
452
+ QGis .WKBPolygon , vprovider .crs () )
449
453
inFeat = QgsFeature ()
450
454
outFeat = QgsFeature ()
451
455
inGeom = QgsGeometry ()
452
456
nFeat = vprovider .featureCount ()
453
457
nElement = 0
454
- self .emit ( SIGNAL ( "runStatus(PyQt_PyObject)" ), 0 )
455
- self .emit ( SIGNAL ( "runRange(PyQt_PyObject)" ), ( 0 , nFeat ) )
456
- while vprovider .nextFeature (inFeat ):
458
+ self .emit ( SIGNAL ( "runStatus( PyQt_PyObject )" ), 0 )
459
+ self .emit ( SIGNAL ( "runRange( PyQt_PyObject )" ), ( 0 , nFeat ) )
460
+ while vprovider .nextFeature ( inFeat ):
457
461
outGeomList = []
458
462
multi = False
459
463
nElement += 1
460
- self .emit ( SIGNAL ( "runStatus(PyQt_PyObject)" ), nElement )
464
+ self .emit ( SIGNAL ( "runStatus( PyQt_PyObject )" ), nElement )
461
465
if inFeat .geometry ().isMultipart ():
462
466
outGeomList = inFeat .geometry ().asMultiPolyline ()
463
467
multi = True
464
468
else :
465
469
outGeomList .append ( inFeat .geometry ().asPolyline () )
466
470
polyGeom = self .remove_bad_lines ( outGeomList )
467
- if len (polyGeom ) <> 0 :
471
+ if len ( polyGeom ) <> 0 :
468
472
outFeat .setGeometry ( QgsGeometry .fromPolygon ( polyGeom ) )
469
473
atMap = inFeat .attributeMap ()
470
474
outFeat .setAttributeMap ( atMap )
@@ -477,20 +481,41 @@ def export_geometry_info( self ):
477
481
allAttrs = vprovider .attributeIndexes ()
478
482
vprovider .select ( allAttrs )
479
483
( fields , index1 , index2 ) = self .checkGeometryFields ( self .vlayer )
480
- writer = QgsVectorFileWriter ( self .myName , self .myEncoding ,
481
- fields , vprovider .geometryType (), vprovider .crs () )
484
+ writer = QgsVectorFileWriter ( self .myName , self .myEncoding , fields ,
485
+ vprovider .geometryType (), vprovider .crs () )
482
486
inFeat = QgsFeature ()
483
487
outFeat = QgsFeature ()
484
488
inGeom = QgsGeometry ()
485
489
nFeat = vprovider .featureCount ()
486
490
nElement = 0
487
- self .emit ( SIGNAL ( "runStatus(PyQt_PyObject)" ), 0 )
488
- self .emit ( SIGNAL ( "runRange(PyQt_PyObject)" ), ( 0 , nFeat ) )
491
+
492
+ # calculate with:
493
+ # 0 - layer CRS
494
+ # 1 - project CRS
495
+ # 2 - ellipsoidal
496
+ ellips = None
497
+ crs = None
498
+ coordTransform = None
499
+ if self .myCalcType == 2 :
500
+ settings = QSettings ()
501
+ ellips = settings .value ( "/qgis/measure/ellipsoid" , "WGS84" ).toString ()
502
+ crs = self .parent .iface .mapCanvas ().mapRenderer ().destinationCrs ().srsid ()
503
+ elif self .myCalcType == 1 :
504
+ mapCRS = self .parent .iface .mapCanvas ().mapRenderer ().destinationCrs ()
505
+ layCRS = self .vlayer .crs ()
506
+ coordTransform = QgsCoordinateTransform ( layCRS , mapCRS )
507
+
508
+ self .emit ( SIGNAL ( "runStatus( PyQt_PyObject )" ), 0 )
509
+ self .emit ( SIGNAL ( "runRange( PyQt_PyObject )" ), ( 0 , nFeat ) )
489
510
while vprovider .nextFeature (inFeat ):
490
- self .emit ( SIGNAL ( "runStatus(PyQt_PyObject)" ), nElement )
511
+ self .emit ( SIGNAL ( "runStatus( PyQt_PyObject )" ), nElement )
491
512
nElement += 1
492
513
inGeom = inFeat .geometry ()
493
- ( attr1 , attr2 ) = self .simpleMeasure ( inGeom )
514
+
515
+ if self .myCalcType == 1 :
516
+ inGeom .transform ( coordTransform )
517
+ ( attr1 , attr2 ) = self .simpleMeasure ( inGeom , self .myCalcType , ellips , crs )
518
+
494
519
outFeat .setGeometry ( inGeom )
495
520
atMap = inFeat .attributeMap ()
496
521
outFeat .setAttributeMap ( atMap )
@@ -505,20 +530,20 @@ def polygon_centroids( self ):
505
530
allAttrs = vprovider .attributeIndexes ()
506
531
vprovider .select ( allAttrs )
507
532
fields = vprovider .fields ()
508
- writer = QgsVectorFileWriter ( self .myName , self .myEncoding ,
509
- fields , QGis .WKBPoint , vprovider .crs () )
533
+ writer = QgsVectorFileWriter ( self .myName , self .myEncoding , fields ,
534
+ QGis .WKBPoint , vprovider .crs () )
510
535
inFeat = QgsFeature ()
511
536
outFeat = QgsFeature ()
512
537
nFeat = vprovider .featureCount ()
513
538
nElement = 0
514
- self .emit ( SIGNAL ( "runStatus(PyQt_PyObject)" ), 0 )
515
- self .emit ( SIGNAL ( "runRange(PyQt_PyObject)" ), ( 0 , nFeat ) )
539
+ self .emit ( SIGNAL ( "runStatus( PyQt_PyObject )" ), 0 )
540
+ self .emit ( SIGNAL ( "runRange( PyQt_PyObject )" ), ( 0 , nFeat ) )
516
541
while vprovider .nextFeature ( inFeat ):
517
542
nElement += 1
518
- self .emit ( SIGNAL ( "runStatus(PyQt_PyObject)" ), nElement )
543
+ self .emit ( SIGNAL ( "runStatus( PyQt_PyObject )" ), nElement )
519
544
inGeom = inFeat .geometry ()
520
545
atMap = inFeat .attributeMap ()
521
- outGeom = QgsGeometry (inGeom .centroid ())
546
+ outGeom = QgsGeometry ( inGeom .centroid () )
522
547
if outGeom is None :
523
548
return "math_error"
524
549
outFeat .setAttributeMap ( atMap )
@@ -533,219 +558,216 @@ def delaunay_triangulation( self ):
533
558
vprovider = self .vlayer .dataProvider ()
534
559
allAttrs = vprovider .attributeIndexes ()
535
560
vprovider .select ( allAttrs )
536
- fields = {
537
- 0 : QgsField ( "POINTA" , QVariant .Double ),
538
- 1 : QgsField ( "POINTB" , QVariant .Double ),
539
- 2 : QgsField ( "POINTC" , QVariant .Double ) }
540
- writer = QgsVectorFileWriter ( self .myName , self .myEncoding ,
541
- fields , QGis .WKBPolygon , vprovider .crs () )
561
+ fields = { 0 : QgsField ( "POINTA" , QVariant .Double ),
562
+ 1 : QgsField ( "POINTB" , QVariant .Double ),
563
+ 2 : QgsField ( "POINTC" , QVariant .Double ) }
564
+ writer = QgsVectorFileWriter ( self .myName , self .myEncoding , fields ,
565
+ QGis .WKBPolygon , vprovider .crs () )
542
566
inFeat = QgsFeature ()
543
567
c = voronoi .Context ()
544
568
pts = []
545
569
ptDict = {}
546
570
ptNdx = - 1
547
- while vprovider .nextFeature (inFeat ):
548
- geom = QgsGeometry (inFeat .geometry ())
571
+ while vprovider .nextFeature ( inFeat ):
572
+ geom = QgsGeometry ( inFeat .geometry () )
549
573
point = geom .asPoint ()
550
574
x = point .x ()
551
575
y = point .y ()
552
- pts .append (( x , y ) )
576
+ pts .append ( ( x , y ) )
553
577
ptNdx += 1
554
578
ptDict [ptNdx ] = inFeat .id ()
555
579
if len (pts ) < 3 :
556
580
return False
557
- uniqueSet = Set (item for item in pts )
558
- ids = [pts .index (item ) for item in uniqueSet ]
559
- sl = voronoi .SiteList ([ voronoi .Site (* i ) for i in uniqueSet ] )
581
+ uniqueSet = Set ( item for item in pts )
582
+ ids = [ pts .index ( item ) for item in uniqueSet ]
583
+ sl = voronoi .SiteList ( [ voronoi .Site ( * i ) for i in uniqueSet ] )
560
584
c .triangulate = True
561
- voronoi .voronoi (sl , c )
585
+ voronoi .voronoi ( sl , c )
562
586
triangles = c .triangles
563
587
feat = QgsFeature ()
564
588
nFeat = len ( triangles )
565
589
nElement = 0
566
- self .emit ( SIGNAL ( "runStatus(PyQt_PyObject)" ), 0 )
567
- self .emit ( SIGNAL ( "runRange(PyQt_PyObject)" ), ( 0 , nFeat ) )
590
+ self .emit ( SIGNAL ( "runStatus( PyQt_PyObject )" ), 0 )
591
+ self .emit ( SIGNAL ( "runRange( PyQt_PyObject )" ), ( 0 , nFeat ) )
568
592
for triangle in triangles :
569
- indicies = list (triangle )
570
- indicies .append (indicies [0 ] )
593
+ indicies = list ( triangle )
594
+ indicies .append ( indicies [ 0 ] )
571
595
polygon = []
572
596
step = 0
573
597
for index in indicies :
574
- vprovider .featureAtId (ptDict [ids [index ] ], inFeat , True , allAttrs )
575
- geom = QgsGeometry (inFeat .geometry ())
576
- point = QgsPoint (geom .asPoint ())
577
- polygon .append (point )
578
- if step <= 3 : feat .addAttribute (step , QVariant (ids [index ]) )
598
+ vprovider .featureAtId ( ptDict [ ids [ index ] ], inFeat , True , allAttrs )
599
+ geom = QgsGeometry ( inFeat .geometry () )
600
+ point = QgsPoint ( geom .asPoint () )
601
+ polygon .append ( point )
602
+ if step <= 3 : feat .addAttribute ( step , QVariant ( ids [ index ] ) )
579
603
step += 1
580
- geometry = QgsGeometry ().fromPolygon ([ polygon ] )
581
- feat .setGeometry (geometry )
582
- writer .addFeature (feat )
604
+ geometry = QgsGeometry ().fromPolygon ( [ polygon ] )
605
+ feat .setGeometry ( geometry )
606
+ writer .addFeature ( feat )
583
607
nElement += 1
584
- self .emit ( SIGNAL ( "runStatus(PyQt_PyObject)" ), nElement )
608
+ self .emit ( SIGNAL ( "runStatus( PyQt_PyObject )" ), nElement )
585
609
del writer
586
610
return True
587
611
588
612
def voronoi_polygons ( self ):
589
613
vprovider = self .vlayer .dataProvider ()
590
614
allAttrs = vprovider .attributeIndexes ()
591
615
vprovider .select ( allAttrs )
592
- writer = QgsVectorFileWriter ( self .myName , self .myEncoding ,
593
- vprovider . fields (), QGis .WKBPolygon , vprovider .crs () )
616
+ writer = QgsVectorFileWriter ( self .myName , self .myEncoding , vprovider . fields (),
617
+ QGis .WKBPolygon , vprovider .crs () )
594
618
inFeat = QgsFeature ()
595
619
outFeat = QgsFeature ()
596
620
extent = self .vlayer .extent ()
597
- extraX = extent .height ()* ( self .myParam / 100.00 )
598
- extraY = extent .width ()* ( self .myParam / 100.00 )
621
+ extraX = extent .height () * ( self .myParam / 100.00 )
622
+ extraY = extent .width () * ( self .myParam / 100.00 )
599
623
height = extent .height ()
600
624
width = extent .width ()
601
625
c = voronoi .Context ()
602
626
pts = []
603
627
ptDict = {}
604
628
ptNdx = - 1
605
- while vprovider .nextFeature (inFeat ):
606
- geom = QgsGeometry (inFeat .geometry ())
629
+ while vprovider .nextFeature ( inFeat ):
630
+ geom = QgsGeometry ( inFeat .geometry () )
607
631
point = geom .asPoint ()
608
- x = point .x ()- extent .xMinimum ()
609
- y = point .y ()- extent .yMinimum ()
610
- pts .append (( x , y ) )
632
+ x = point .x () - extent .xMinimum ()
633
+ y = point .y () - extent .yMinimum ()
634
+ pts .append ( ( x , y ) )
611
635
ptNdx += 1
612
- ptDict [ptNdx ] = inFeat .id ()
636
+ ptDict [ ptNdx ] = inFeat .id ()
613
637
self .vlayer = None
614
- if len (pts ) < 3 :
638
+ if len ( pts ) < 3 :
615
639
return False
616
- uniqueSet = Set (item for item in pts )
617
- ids = [pts .index (item ) for item in uniqueSet ]
618
- sl = voronoi .SiteList ([ voronoi .Site (i [ 0 ], i [1 ], sitenum = j ) for j , i in enumerate (uniqueSet )] )
619
- voronoi .voronoi (sl , c )
640
+ uniqueSet = Set ( item for item in pts )
641
+ ids = [ pts .index ( item ) for item in uniqueSet ]
642
+ sl = voronoi .SiteList ( [ voronoi .Site ( i [ 0 ], i [ 1 ], sitenum = j ) for j , i in enumerate ( uniqueSet ) ] )
643
+ voronoi .voronoi ( sl , c )
620
644
inFeat = QgsFeature ()
621
- nFeat = len (c .polygons )
645
+ nFeat = len ( c .polygons )
622
646
nElement = 0
623
- self .emit ( SIGNAL ( "runStatus(PyQt_PyObject)" ), 0 )
624
- self .emit ( SIGNAL ( "runRange(PyQt_PyObject)" ), ( 0 , nFeat ) )
647
+ self .emit ( SIGNAL ( "runStatus( PyQt_PyObject )" ), 0 )
648
+ self .emit ( SIGNAL ( "runRange( PyQt_PyObject )" ), ( 0 , nFeat ) )
625
649
for site , edges in c .polygons .iteritems ():
626
- vprovider .featureAtId (ptDict [ids [site ] ], inFeat , True , allAttrs )
627
- lines = self .clip_voronoi (edges , c , width , height , extent , extraX , extraY )
628
- geom = QgsGeometry .fromMultiPoint (lines )
629
- geom = QgsGeometry (geom .convexHull ())
630
- outFeat .setGeometry (geom )
631
- outFeat .setAttributeMap (inFeat .attributeMap ())
632
- writer .addFeature (outFeat )
650
+ vprovider .featureAtId ( ptDict [ ids [ site ] ], inFeat , True , allAttrs )
651
+ lines = self .clip_voronoi ( edges , c , width , height , extent , extraX , extraY )
652
+ geom = QgsGeometry .fromMultiPoint ( lines )
653
+ geom = QgsGeometry ( geom .convexHull () )
654
+ outFeat .setGeometry ( geom )
655
+ outFeat .setAttributeMap ( inFeat .attributeMap () )
656
+ writer .addFeature ( outFeat )
633
657
nElement += 1
634
- self .emit (SIGNAL ("runStatus(PyQt_PyObject)" ), nElement )
658
+ self .emit ( SIGNAL ( "runStatus( PyQt_PyObject )" ), nElement )
635
659
del writer
636
660
return True
637
661
638
-
639
- def clip_voronoi (self , edges , c , width , height , extent , exX , exY ):
662
+ def clip_voronoi ( self , edges , c , width , height , extent , exX , exY ):
640
663
""" Clip voronoi function based on code written for Inkscape
641
664
Copyright (C) 2010 Alvin Penner, penner@vaxxine.com
642
665
"""
643
- def clip_line (x1 , y1 , x2 , y2 , w , h , x , y ):
644
- if x1 < 0 - x and x2 < 0 - x :
645
- return [0 , 0 , 0 , 0 ]
646
- if x1 > w + x and x2 > w + x :
647
- return [0 , 0 , 0 , 0 ]
648
- if x1 < 0 - x :
649
- y1 = (y1 * x2 - y2 * x1 ) / ( x2 - x1 )
650
- x1 = 0 - x
651
- if x2 < 0 - x :
652
- y2 = (y1 * x2 - y2 * x1 ) / ( x2 - x1 )
653
- x2 = 0 - x
654
- if x1 > w + x :
655
- y1 = y1 + (w + x - x1 ) * ( y2 - y1 ) / ( x2 - x1 )
656
- x1 = w + x
657
- if x2 > w + x :
658
- y2 = y1 + (w + x - x1 ) * ( y2 - y1 ) / ( x2 - x1 )
659
- x2 = w + x
660
- if y1 < 0 - y and y2 < 0 - y :
661
- return [0 , 0 , 0 , 0 ]
662
- if y1 > h + y and y2 > h + y :
663
- return [0 , 0 , 0 , 0 ]
666
+ def clip_line ( x1 , y1 , x2 , y2 , w , h , x , y ):
667
+ if x1 < 0 - x and x2 < 0 - x :
668
+ return [ 0 , 0 , 0 , 0 ]
669
+ if x1 > w + x and x2 > w + x :
670
+ return [ 0 , 0 , 0 , 0 ]
671
+ if x1 < 0 - x :
672
+ y1 = ( y1 * x2 - y2 * x1 ) / ( x2 - x1 )
673
+ x1 = 0 - x
674
+ if x2 < 0 - x :
675
+ y2 = ( y1 * x2 - y2 * x1 ) / ( x2 - x1 )
676
+ x2 = 0 - x
677
+ if x1 > w + x :
678
+ y1 = y1 + ( w + x - x1 ) * ( y2 - y1 ) / ( x2 - x1 )
679
+ x1 = w + x
680
+ if x2 > w + x :
681
+ y2 = y1 + ( w + x - x1 ) * ( y2 - y1 ) / ( x2 - x1 )
682
+ x2 = w + x
683
+ if y1 < 0 - y and y2 < 0 - y :
684
+ return [ 0 , 0 , 0 , 0 ]
685
+ if y1 > h + y and y2 > h + y :
686
+ return [ 0 , 0 , 0 , 0 ]
664
687
if x1 == x2 and y1 == y2 :
665
- return [0 , 0 , 0 , 0 ]
666
- if y1 < 0 - y :
667
- x1 = (x1 * y2 - x2 * y1 ) / ( y2 - y1 )
668
- y1 = 0 - y
669
- if y2 < 0 - y :
670
- x2 = (x1 * y2 - x2 * y1 ) / ( y2 - y1 )
671
- y2 = 0 - y
672
- if y1 > h + y :
673
- x1 = x1 + (h + y - y1 ) * ( x2 - x1 ) / ( y2 - y1 )
674
- y1 = h + y
675
- if y2 > h + y :
676
- x2 = x1 + (h + y - y1 )* ( x2 - x1 ) / ( y2 - y1 )
677
- y2 = h + y
678
- return [x1 , y1 , x2 , y2 ]
688
+ return [ 0 , 0 , 0 , 0 ]
689
+ if y1 < 0 - y :
690
+ x1 = ( x1 * y2 - x2 * y1 ) / ( y2 - y1 )
691
+ y1 = 0 - y
692
+ if y2 < 0 - y :
693
+ x2 = ( x1 * y2 - x2 * y1 ) / ( y2 - y1 )
694
+ y2 = 0 - y
695
+ if y1 > h + y :
696
+ x1 = x1 + ( h + y - y1 ) * ( x2 - x1 ) / ( y2 - y1 )
697
+ y1 = h + y
698
+ if y2 > h + y :
699
+ x2 = x1 + ( h + y - y1 ) * ( x2 - x1 ) / ( y2 - y1 )
700
+ y2 = h + y
701
+ return [ x1 , y1 , x2 , y2 ]
679
702
lines = []
680
703
hasXMin = False
681
704
hasYMin = False
682
705
hasXMax = False
683
706
hasYMax = False
684
707
for edge in edges :
685
- if edge [1 ] >= 0 and edge [2 ] >= 0 : # two vertices
686
- [x1 , y1 , x2 , y2 ] = clip_line (c .vertices [edge [1 ]][ 0 ], c .vertices [edge [1 ]][ 1 ], c .vertices [edge [2 ]][ 0 ], c .vertices [edge [2 ]][ 1 ], width , height , exX , exY )
687
- elif edge [1 ] >= 0 : # only one vertex
688
- if c .lines [edge [0 ]][ 1 ] == 0 : # vertical line
689
- xtemp = c .lines [edge [0 ]][ 2 ] / c .lines [edge [0 ]][ 0 ]
690
- if c .vertices [edge [1 ]][ 1 ] > (height + exY ) / 2 :
691
- ytemp = height + exY
708
+ if edge [ 1 ] >= 0 and edge [ 2 ] >= 0 : # two vertices
709
+ [ x1 , y1 , x2 , y2 ] = clip_line ( c .vertices [ edge [ 1 ] ][ 0 ], c .vertices [ edge [ 1 ] ][ 1 ], c .vertices [ edge [ 2 ] ][ 0 ], c .vertices [ edge [ 2 ] ][ 1 ], width , height , exX , exY )
710
+ elif edge [ 1 ] >= 0 : # only one vertex
711
+ if c .lines [ edge [ 0 ] ][ 1 ] == 0 : # vertical line
712
+ xtemp = c .lines [ edge [ 0 ] ][ 2 ] / c .lines [ edge [ 0 ] ][ 0 ]
713
+ if c .vertices [ edge [ 1 ] ][ 1 ] > ( height + exY ) / 2 :
714
+ ytemp = height + exY
692
715
else :
693
- ytemp = 0 - exX
716
+ ytemp = 0 - exX
694
717
else :
695
- xtemp = width + exX
696
- ytemp = (c .lines [edge [0 ]][ 2 ] - (width + exX ) * c .lines [edge [0 ]][ 0 ]) / c .lines [edge [0 ]][ 1 ]
697
- [x1 , y1 , x2 , y2 ] = clip_line (c .vertices [edge [1 ]][ 0 ], c .vertices [edge [1 ]][ 1 ], xtemp , ytemp , width , height , exX , exY )
698
- elif edge [2 ] >= 0 : # only one vertex
699
- if c .lines [edge [0 ]][ 1 ] == 0 : # vertical line
700
- xtemp = c .lines [edge [0 ]][ 2 ] / c .lines [edge [0 ]][ 0 ]
701
- if c .vertices [edge [2 ]][ 1 ] > (height + exY ) / 2 :
702
- ytemp = height + exY
718
+ xtemp = width + exX
719
+ ytemp = ( c .lines [ edge [ 0 ] ][ 2 ] - ( width + exX ) * c .lines [ edge [ 0 ] ][ 0 ] ) / c .lines [ edge [ 0 ] ][ 1 ]
720
+ [ x1 , y1 , x2 , y2 ] = clip_line ( c .vertices [ edge [ 1 ] ][ 0 ], c .vertices [ edge [ 1 ] ][ 1 ], xtemp , ytemp , width , height , exX , exY )
721
+ elif edge [ 2 ] >= 0 : # only one vertex
722
+ if c .lines [ edge [ 0 ] ][ 1 ] == 0 : # vertical line
723
+ xtemp = c .lines [ edge [ 0 ] ][ 2 ] / c .lines [ edge [ 0 ] ][ 0 ]
724
+ if c .vertices [ edge [ 2 ] ][ 1 ] > ( height + exY ) / 2 :
725
+ ytemp = height + exY
703
726
else :
704
- ytemp = 0.0 - exY
727
+ ytemp = 0.0 - exY
705
728
else :
706
- xtemp = 0.0 - exX
707
- ytemp = c .lines [edge [0 ]][ 2 ] / c .lines [edge [0 ]][ 1 ]
708
- [x1 , y1 , x2 , y2 ] = clip_line (xtemp , ytemp , c .vertices [edge [2 ]][ 0 ], c .vertices [edge [2 ]][ 1 ], width , height , exX , exY )
729
+ xtemp = 0.0 - exX
730
+ ytemp = c .lines [ edge [ 0 ] ][ 2 ] / c .lines [ edge [ 0 ] ][ 1 ]
731
+ [ x1 , y1 , x2 , y2 ] = clip_line ( xtemp , ytemp , c .vertices [ edge [ 2 ] ][ 0 ], c .vertices [ edge [ 2 ] ][ 1 ], width , height , exX , exY )
709
732
if x1 or x2 or y1 or y2 :
710
- lines .append (QgsPoint (x1 + extent .xMinimum (),y1 + extent .yMinimum ()) )
711
- lines .append (QgsPoint (x2 + extent .xMinimum (),y2 + extent .yMinimum ()) )
712
- if 0 - exX in (x1 , x2 ):
733
+ lines .append ( QgsPoint ( x1 + extent .xMinimum (), y1 + extent .yMinimum () ) )
734
+ lines .append ( QgsPoint ( x2 + extent .xMinimum (), y2 + extent .yMinimum () ) )
735
+ if 0 - exX in ( x1 , x2 ):
713
736
hasXMin = True
714
- if 0 - exY in (y1 , y2 ):
737
+ if 0 - exY in ( y1 , y2 ):
715
738
hasYMin = True
716
- if height + exY in (y1 , y2 ):
739
+ if height + exY in ( y1 , y2 ):
717
740
hasYMax = True
718
- if width + exX in (x1 , x2 ):
741
+ if width + exX in ( x1 , x2 ):
719
742
hasXMax = True
720
743
if hasXMin :
721
744
if hasYMax :
722
- lines .append (QgsPoint (extent .xMinimum ()- exX , height + extent .yMinimum ()+ exY ) )
745
+ lines .append ( QgsPoint ( extent .xMinimum () - exX , height + extent .yMinimum () + exY ) )
723
746
if hasYMin :
724
- lines .append (QgsPoint (extent .xMinimum ()- exX , extent .yMinimum ()- exY ) )
747
+ lines .append ( QgsPoint ( extent .xMinimum () - exX , extent .yMinimum () - exY ) )
725
748
if hasXMax :
726
749
if hasYMax :
727
- lines .append (QgsPoint (width + extent .xMinimum ()+ exX , height + extent .yMinimum ()+ exY ) )
750
+ lines .append ( QgsPoint ( width + extent .xMinimum () + exX , height + extent .yMinimum () + exY ) )
728
751
if hasYMin :
729
- lines .append (QgsPoint (width + extent .xMinimum ()+ exX , extent .yMinimum ()- exY ) )
752
+ lines .append ( QgsPoint ( width + extent .xMinimum () + exX , extent .yMinimum () - exY ) )
730
753
return lines
731
754
732
755
def layer_extent ( self ):
733
- self .emit ( SIGNAL ( "runStatus(PyQt_PyObject)" ), 0 )
734
- self .emit ( SIGNAL ( "runRange(PyQt_PyObject)" ), ( 0 , 0 ) )
735
- fields = {
736
- 0 : QgsField ( "MINX" , QVariant .Double ),
737
- 1 : QgsField ( "MINY" , QVariant .Double ),
738
- 2 : QgsField ( "MAXX" , QVariant .Double ),
739
- 3 : QgsField ( "MAXY" , QVariant .Double ),
740
- 4 : QgsField ( "CNTX" , QVariant .Double ),
741
- 5 : QgsField ( "CNTY" , QVariant .Double ),
742
- 6 : QgsField ( "AREA" , QVariant .Double ),
743
- 7 : QgsField ( "PERIM" , QVariant .Double ),
744
- 8 : QgsField ( "HEIGHT" , QVariant .Double ),
745
- 9 : QgsField ( "WIDTH" , QVariant .Double ) }
746
-
747
- writer = QgsVectorFileWriter ( self .myName , self .myEncoding ,
748
- fields , QGis .WKBPolygon , self .vlayer .crs () )
756
+ self .emit ( SIGNAL ( "runStatus( PyQt_PyObject )" ), 0 )
757
+ self .emit ( SIGNAL ( "runRange( PyQt_PyObject )" ), ( 0 , 0 ) )
758
+ fields = { 0 : QgsField ( "MINX" , QVariant .Double ),
759
+ 1 : QgsField ( "MINY" , QVariant .Double ),
760
+ 2 : QgsField ( "MAXX" , QVariant .Double ),
761
+ 3 : QgsField ( "MAXY" , QVariant .Double ),
762
+ 4 : QgsField ( "CNTX" , QVariant .Double ),
763
+ 5 : QgsField ( "CNTY" , QVariant .Double ),
764
+ 6 : QgsField ( "AREA" , QVariant .Double ),
765
+ 7 : QgsField ( "PERIM" , QVariant .Double ),
766
+ 8 : QgsField ( "HEIGHT" , QVariant .Double ),
767
+ 9 : QgsField ( "WIDTH" , QVariant .Double ) }
768
+
769
+ writer = QgsVectorFileWriter ( self .myName , self .myEncoding , fields ,
770
+ QGis .WKBPolygon , self .vlayer .crs () )
749
771
rect = self .vlayer .extent ()
750
772
minx = rect .xMinimum ()
751
773
miny = rect .yMinimum ()
@@ -757,42 +779,46 @@ def layer_extent( self ):
757
779
cnty = miny + ( height / 2.0 )
758
780
area = width * height
759
781
perim = ( 2 * width ) + (2 * height )
760
- rect = [
761
- QgsPoint ( minx , miny ),
762
- QgsPoint ( minx , maxy ),
763
- QgsPoint ( maxx , maxy ),
764
- QgsPoint ( maxx , miny ),
765
- QgsPoint ( minx , miny ) ]
782
+ rect = [ QgsPoint ( minx , miny ),
783
+ QgsPoint ( minx , maxy ),
784
+ QgsPoint ( maxx , maxy ),
785
+ QgsPoint ( maxx , miny ),
786
+ QgsPoint ( minx , miny ) ]
766
787
geometry = QgsGeometry ().fromPolygon ( [ rect ] )
767
788
feat = QgsFeature ()
768
789
feat .setGeometry ( geometry )
769
- feat .setAttributeMap ( {
770
- 0 : QVariant ( minx ),
771
- 1 : QVariant ( miny ),
772
- 2 : QVariant ( maxx ),
773
- 3 : QVariant ( maxy ),
774
- 4 : QVariant ( cntx ),
775
- 5 : QVariant ( cnty ),
776
- 6 : QVariant ( area ),
777
- 7 : QVariant ( perim ),
778
- 8 : QVariant ( height ),
779
- 9 : QVariant ( width ) } )
790
+ feat .setAttributeMap ( { 0 : QVariant ( minx ),
791
+ 1 : QVariant ( miny ),
792
+ 2 : QVariant ( maxx ),
793
+ 3 : QVariant ( maxy ),
794
+ 4 : QVariant ( cntx ),
795
+ 5 : QVariant ( cnty ),
796
+ 6 : QVariant ( area ),
797
+ 7 : QVariant ( perim ),
798
+ 8 : QVariant ( height ),
799
+ 9 : QVariant ( width ) } )
780
800
writer .addFeature ( feat )
781
- self .emit ( SIGNAL ( "runRange(PyQt_PyObject)" ), ( 0 , 100 ) )
782
- self .emit ( SIGNAL ( "runStatus(PyQt_PyObject)" ), 0 )
801
+ self .emit ( SIGNAL ( "runRange( PyQt_PyObject )" ), ( 0 , 100 ) )
802
+ self .emit ( SIGNAL ( "runStatus( PyQt_PyObject )" ), 0 )
783
803
del writer
784
804
785
805
return True
786
806
787
- def simpleMeasure ( self , inGeom ):
788
- if inGeom .wkbType () in (QGis .WKBPoint , QGis .WKBPoint25D ):
807
+ def simpleMeasure ( self , inGeom , calcType , ellips , crs ):
808
+ if inGeom .wkbType () in ( QGis .WKBPoint , QGis .WKBPoint25D ):
789
809
pt = QgsPoint ()
790
810
pt = inGeom .asPoint ()
791
811
attr1 = pt .x ()
792
812
attr2 = pt .y ()
793
813
else :
794
814
measure = QgsDistanceArea ()
795
- attr1 = measure .measure (inGeom )
815
+
816
+ if calcType == 2 :
817
+ measure .setSourceCrs ( crs )
818
+ measure .setEllipsoid ( ellips )
819
+ measure .setProjectionsEnabled ( True )
820
+
821
+ attr1 = measure .measure ( inGeom )
796
822
if inGeom .type () == QGis .Polygon :
797
823
attr2 = self .perimMeasure ( inGeom , measure )
798
824
else :
@@ -814,7 +840,7 @@ def perimMeasure( self, inGeom, measure ):
814
840
815
841
def checkForField ( self , L , e ):
816
842
e = QString ( e ).toLower ()
817
- fieldRange = range ( 0 ,len ( L ) )
843
+ fieldRange = range ( 0 , len ( L ) )
818
844
for item in fieldRange :
819
845
if L [ item ].toLower () == e :
820
846
return True , item
@@ -830,37 +856,38 @@ def checkGeometryFields( self, vlayer ):
830
856
if geomType == QGis .Polygon :
831
857
plp = "Poly"
832
858
( found , index1 ) = self .checkForField ( nameList , "AREA" )
859
+
833
860
if not found :
834
- field = QgsField ( "AREA" , QVariant .Double , "double" , 21 , 6 , self .tr ("Polygon area" ) )
861
+ field = QgsField ( "AREA" , QVariant .Double , "double" , 21 , 6 , self .tr ( "Polygon area" ) )
835
862
index1 = len ( fieldList .keys () )
836
863
fieldList [ index1 ] = field
837
864
( found , index2 ) = self .checkForField ( nameList , "PERIMETER" )
838
865
839
866
if not found :
840
- field = QgsField ( "PERIMETER" , QVariant .Double , "double" , 21 , 6 , self .tr ("Polygon perimeter" ) )
867
+ field = QgsField ( "PERIMETER" , QVariant .Double , "double" , 21 , 6 , self .tr ( "Polygon perimeter" ) )
841
868
index2 = len ( fieldList .keys () )
842
869
fieldList [ index2 ] = field
843
870
elif geomType == QGis .Line :
844
871
plp = "Line"
845
- (found , index1 ) = self .checkForField (nameList , "LENGTH" )
872
+ ( found , index1 ) = self .checkForField ( nameList , "LENGTH" )
846
873
if not found :
847
- field = QgsField ("LENGTH" , QVariant .Double , "double" , 21 , 6 , self .tr ("Line length" ) )
848
- index1 = len (fieldList .keys ())
849
- fieldList [index1 ] = field
874
+ field = QgsField ( "LENGTH" , QVariant .Double , "double" , 21 , 6 , self .tr ( "Line length" ) )
875
+ index1 = len ( fieldList .keys () )
876
+ fieldList [ index1 ] = field
850
877
index2 = index1
851
878
else :
852
879
plp = "Point"
853
- (found , index1 ) = self .checkForField (nameList , "XCOORD" )
880
+ ( found , index1 ) = self .checkForField ( nameList , "XCOORD" )
854
881
if not found :
855
- field = QgsField ("XCOORD" , QVariant .Double , "double" , 21 , 6 , self .tr ("Point x coordinate" ) )
856
- index1 = len (fieldList .keys ())
857
- fieldList [index1 ] = field
858
- (found , index2 ) = self .checkForField (nameList , "YCOORD" )
882
+ field = QgsField ( "XCOORD" , QVariant .Double , "double" , 21 , 6 , self .tr ( "Point x coordinate" ) )
883
+ index1 = len ( fieldList .keys () )
884
+ fieldList [ index1 ] = field
885
+ ( found , index2 ) = self .checkForField ( nameList , "YCOORD" )
859
886
if not found :
860
- field = QgsField ("YCOORD" , QVariant .Double , "double" , 21 , 6 , self .tr ("Point y coordinate" ) )
861
- index2 = len (fieldList .keys ())
862
- fieldList [index2 ] = field
863
- return (fieldList , index1 , index2 )
887
+ field = QgsField ( "YCOORD" , QVariant .Double , "double" , 21 , 6 , self .tr ( "Point y coordinate" ) )
888
+ index2 = len ( fieldList .keys () )
889
+ fieldList [ index2 ] = field
890
+ return ( fieldList , index1 , index2 )
864
891
865
892
def extractAsLine ( self , geom ):
866
893
multi_geom = QgsGeometry ()
@@ -869,7 +896,7 @@ def extractAsLine( self, geom ):
869
896
if geom .isMultipart ():
870
897
multi_geom = geom .asMultiPolygon ()
871
898
for i in multi_geom :
872
- temp_geom .extend (i )
899
+ temp_geom .extend ( i )
873
900
else :
874
901
multi_geom = geom .asPolygon ()
875
902
temp_geom = multi_geom
@@ -879,46 +906,46 @@ def extractAsLine( self, geom ):
879
906
880
907
def remove_bad_lines ( self , lines ):
881
908
temp_geom = []
882
- if len (lines ) == 1 :
883
- if len (lines [0 ] ) > 2 :
909
+ if len ( lines ) == 1 :
910
+ if len ( lines [ 0 ] ) > 2 :
884
911
temp_geom = lines
885
912
else :
886
913
temp_geom = []
887
914
else :
888
- temp_geom = [elem for elem in lines if len (elem ) > 2 ]
915
+ temp_geom = [ elem for elem in lines if len ( elem ) > 2 ]
889
916
return temp_geom
890
917
891
- def singleToMultiGeom (self , wkbType ):
892
- try :
893
- if wkbType in (QGis .WKBPoint , QGis .WKBMultiPoint ,
894
- QGis .WKBPoint25D , QGis .WKBMultiPoint25D ):
895
- return QGis .WKBMultiPoint
896
- elif wkbType in (QGis .WKBLineString , QGis .WKBMultiLineString ,
897
- QGis .WKBMultiLineString25D , QGis .WKBLineString25D ):
898
- return QGis .WKBMultiLineString
899
- elif wkbType in (QGis .WKBPolygon , QGis .WKBMultiPolygon ,
900
- QGis .WKBMultiPolygon25D , QGis .WKBPolygon25D ):
901
- return QGis .WKBMultiPolygon
902
- else :
903
- return QGis .WKBUnknown
904
- except Exception , err :
905
- print str (err )
906
-
907
- def multiToSingleGeom (self , wkbType ):
908
- try :
909
- if wkbType in (QGis .WKBPoint , QGis .WKBMultiPoint ,
910
- QGis .WKBPoint25D , QGis .WKBMultiPoint25D ):
911
- return QGis .WKBPoint
912
- elif wkbType in (QGis .WKBLineString , QGis .WKBMultiLineString ,
913
- QGis .WKBMultiLineString25D , QGis .WKBLineString25D ):
914
- return QGis .WKBLineString
915
- elif wkbType in (QGis .WKBPolygon , QGis .WKBMultiPolygon ,
916
- QGis .WKBMultiPolygon25D , QGis .WKBPolygon25D ):
917
- return QGis .WKBPolygon
918
- else :
919
- return QGis .WKBUnknown
920
- except Exception , err :
921
- print str (err )
918
+ def singleToMultiGeom ( self , wkbType ):
919
+ try :
920
+ if wkbType in ( QGis .WKBPoint , QGis .WKBMultiPoint ,
921
+ QGis .WKBPoint25D , QGis .WKBMultiPoint25D ):
922
+ return QGis .WKBMultiPoint
923
+ elif wkbType in ( QGis .WKBLineString , QGis .WKBMultiLineString ,
924
+ QGis .WKBMultiLineString25D , QGis .WKBLineString25D ):
925
+ return QGis .WKBMultiLineString
926
+ elif wkbType in ( QGis .WKBPolygon , QGis .WKBMultiPolygon ,
927
+ QGis .WKBMultiPolygon25D , QGis .WKBPolygon25D ):
928
+ return QGis .WKBMultiPolygon
929
+ else :
930
+ return QGis .WKBUnknown
931
+ except Exception , err :
932
+ print str ( err )
933
+
934
+ def multiToSingleGeom ( self , wkbType ):
935
+ try :
936
+ if wkbType in ( QGis .WKBPoint , QGis .WKBMultiPoint ,
937
+ QGis .WKBPoint25D , QGis .WKBMultiPoint25D ):
938
+ return QGis .WKBPoint
939
+ elif wkbType in ( QGis .WKBLineString , QGis .WKBMultiLineString ,
940
+ QGis .WKBMultiLineString25D , QGis .WKBLineString25D ):
941
+ return QGis .WKBLineString
942
+ elif wkbType in ( QGis .WKBPolygon , QGis .WKBMultiPolygon ,
943
+ QGis .WKBMultiPolygon25D , QGis .WKBPolygon25D ):
944
+ return QGis .WKBPolygon
945
+ else :
946
+ return QGis .WKBUnknown
947
+ except Exception , err :
948
+ print str ( err )
922
949
923
950
def extractAsSingle ( self , geom ):
924
951
multi_geom = QgsGeometry ()
@@ -966,8 +993,8 @@ def extractAsMulti( self, geom ):
966
993
967
994
def convertGeometry ( self , geom_list , vType ):
968
995
if vType == 0 :
969
- return QgsGeometry ().fromMultiPoint (geom_list )
996
+ return QgsGeometry ().fromMultiPoint ( geom_list )
970
997
elif vType == 1 :
971
- return QgsGeometry ().fromMultiPolyline (geom_list )
998
+ return QgsGeometry ().fromMultiPolyline ( geom_list )
972
999
else :
973
- return QgsGeometry ().fromMultiPolygon (geom_list )
1000
+ return QgsGeometry ().fromMultiPolygon ( geom_list )
0 commit comments