Skip to content
Permalink
Browse files

Much faster centroid computation, and simplified code. Also adds Alex…

…ander Bruy as fTools contributor.

git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@13314 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
cfarmer
cfarmer committed Apr 15, 2010
1 parent f193203 commit 91bc447fbff079925190a924657c63f521f40e20
Showing with 15 additions and 39 deletions.
  1. +6 −0 python/plugins/fTools/doAbout.py
  2. +9 −39 python/plugins/fTools/tools/doGeometry.py
@@ -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
@@ -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
@@ -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

0 comments on commit 91bc447

Please sign in to comment.
You can’t perform that action at this time.