Skip to content

Commit 91bc447

Browse files
author
cfarmer
committed
Much faster centroid computation, and simplified code. Also adds Alexander Bruy as fTools contributor.
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@13314 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent f193203 commit 91bc447

File tree

2 files changed

+15
-39
lines changed

2 files changed

+15
-39
lines changed

python/plugins/fTools/doAbout.py

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# -*- coding: utf-8 -*-
12
# licensed under the terms of GNU GPL 2
23
#
34
# This program is free software; you can redistribute it and/or modify
@@ -53,6 +54,11 @@ def getText(self):
5354
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
5455
You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
5556
57+
fTools DEVELOPERS:
58+
Carson J. Q. Farmer
59+
Alexander Bruy
60+
**If you have contributed code to fTools and I haven't mentioned your name here, please contact me and I will add your name.
61+
5662
ACKNOWLEDGEMENTS:
5763
The following individuals (whether they know it or not) have contributed ideas, help, testing, code, and guidence towards this project, and I thank them.
5864
Hawthorn Beyer

python/plugins/fTools/tools/doGeometry.py

+9-39
Original file line numberDiff line numberDiff line change
@@ -421,52 +421,22 @@ def polygon_centroids( self ):
421421
writer = QgsVectorFileWriter( self.myName, self.myEncoding,
422422
fields, QGis.WKBPoint, vprovider.crs() )
423423
inFeat = QgsFeature()
424-
outfeat = QgsFeature()
424+
outFeat = QgsFeature()
425425
nFeat = vprovider.featureCount()
426426
nElement = 0
427427
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), 0 )
428428
self.emit( SIGNAL( "runRange(PyQt_PyObject)" ), ( 0, nFeat ) )
429429
while vprovider.nextFeature( inFeat ):
430-
geom = inFeat.geometry()
431-
area = 0.00
432-
bounding = inFeat.geometry().boundingBox()
433-
xmin = bounding.xMinimum()
434-
ymin = bounding.yMinimum()
435-
if geom.type() == 2:
436-
cx = 0
437-
cy = 0
438-
factor = 0
439-
if geom.isMultipart():
440-
polygons = geom.asMultiPolygon()
441-
for polygon in polygons:
442-
for line in polygon:
443-
for i in range(0,len(line)-1):
444-
j = (i + 1) % len(line)
445-
factor=((line[i].x()-xmin)*(line[j].y()-ymin)-(line[j].x()-xmin)*(line[i].y()-ymin))
446-
cx+=((line[i].x()-xmin)+(line[j].x()-xmin))*factor
447-
cy+=((line[i].y()-ymin)+(line[j].y()-ymin))*factor
448-
area+=factor
449-
else:
450-
polygon = geom.asPolygon()
451-
for line in polygon:
452-
for i in range(0,len(line)-1):
453-
j = (i + 1) % len(line)
454-
factor=((line[i].x()-xmin)*(line[j].y()-ymin)-(line[j].x()-xmin)*(line[i].y()-ymin))
455-
cx+=((line[i].x()-xmin)+(line[j].x()-xmin))*factor
456-
cy+=((line[i].y()-ymin)+(line[j].y()-ymin))*factor
457-
area+=factor
458-
459-
if area==0:
460-
continue
461-
462-
cx/=area*3.00
463-
cy/=area*3.00
464-
outfeat.setGeometry( QgsGeometry.fromPoint( QgsPoint( cx+xmin, cy+ymin ) ) )
465-
atMap = inFeat.attributeMap()
466-
outfeat.setAttributeMap( atMap )
467-
writer.addFeature( outfeat )
468430
nElement += 1
469431
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), nElement )
432+
inGeom = inFeat.geometry()
433+
atMap = inFeat.attributeMap()
434+
outGeom = QgsGeometry(inGeom.centroid())
435+
if outGeom is None:
436+
return "math_error"
437+
outFeat.setAttributeMap( atMap )
438+
outFeat.setGeometry( outGeom )
439+
writer.addFeature( outFeat )
470440
del writer
471441
return True
472442

0 commit comments

Comments
 (0)