Skip to content

Commit

Permalink
Much faster centroid computation, and simplified code. Also adds Alex…
Browse files Browse the repository at this point in the history
…ander Bruy as fTools contributor.

git-svn-id: http://svn.osgeo.org/qgis/trunk@13314 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
cfarmer committed Apr 15, 2010
1 parent 71a9f81 commit 21a8a9e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 39 deletions.
6 changes: 6 additions & 0 deletions python/plugins/fTools/doAbout.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
# licensed under the terms of GNU GPL 2
#
# This program is free software; you can redistribute it and/or modify
Expand Down Expand Up @@ -53,6 +54,11 @@ def getText(self):
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.
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.
fTools DEVELOPERS:
Carson J. Q. Farmer
Alexander Bruy
**If you have contributed code to fTools and I haven't mentioned your name here, please contact me and I will add your name.
ACKNOWLEDGEMENTS:
The following individuals (whether they know it or not) have contributed ideas, help, testing, code, and guidence towards this project, and I thank them.
Hawthorn Beyer
Expand Down
48 changes: 9 additions & 39 deletions python/plugins/fTools/tools/doGeometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,52 +421,22 @@ def polygon_centroids( self ):
writer = QgsVectorFileWriter( self.myName, self.myEncoding,
fields, QGis.WKBPoint, vprovider.crs() )
inFeat = QgsFeature()
outfeat = QgsFeature()
outFeat = QgsFeature()
nFeat = vprovider.featureCount()
nElement = 0
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), 0 )
self.emit( SIGNAL( "runRange(PyQt_PyObject)" ), ( 0, nFeat ) )
while vprovider.nextFeature( inFeat ):
geom = inFeat.geometry()
area = 0.00
bounding = inFeat.geometry().boundingBox()
xmin = bounding.xMinimum()
ymin = bounding.yMinimum()
if geom.type() == 2:
cx = 0
cy = 0
factor = 0
if geom.isMultipart():
polygons = geom.asMultiPolygon()
for polygon in polygons:
for line in polygon:
for i in range(0,len(line)-1):
j = (i + 1) % len(line)
factor=((line[i].x()-xmin)*(line[j].y()-ymin)-(line[j].x()-xmin)*(line[i].y()-ymin))
cx+=((line[i].x()-xmin)+(line[j].x()-xmin))*factor
cy+=((line[i].y()-ymin)+(line[j].y()-ymin))*factor
area+=factor
else:
polygon = geom.asPolygon()
for line in polygon:
for i in range(0,len(line)-1):
j = (i + 1) % len(line)
factor=((line[i].x()-xmin)*(line[j].y()-ymin)-(line[j].x()-xmin)*(line[i].y()-ymin))
cx+=((line[i].x()-xmin)+(line[j].x()-xmin))*factor
cy+=((line[i].y()-ymin)+(line[j].y()-ymin))*factor
area+=factor

if area==0:
continue

cx/=area*3.00
cy/=area*3.00
outfeat.setGeometry( QgsGeometry.fromPoint( QgsPoint( cx+xmin, cy+ymin ) ) )
atMap = inFeat.attributeMap()
outfeat.setAttributeMap( atMap )
writer.addFeature( outfeat )
nElement += 1
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), nElement )
inGeom = inFeat.geometry()
atMap = inFeat.attributeMap()
outGeom = QgsGeometry(inGeom.centroid())
if outGeom is None:
return "math_error"
outFeat.setAttributeMap( atMap )
outFeat.setGeometry( outGeom )
writer.addFeature( outFeat )
del writer
return True

Expand Down

0 comments on commit 21a8a9e

Please sign in to comment.