Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #731 from ddanielvaz/bugfix-6063
Preserve attributes in Convex Hull (fix #6063)
  • Loading branch information
alexbruy committed Sep 12, 2013
2 parents 885c8b4 + 8a50ddb commit 033440f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
43 changes: 28 additions & 15 deletions python/plugins/fTools/tools/doGeoprocessing.py
Expand Up @@ -467,13 +467,17 @@ def convex_hull(self, useField ):
GEOS_EXCEPT = True
FEATURE_EXCEPT = True
vproviderA = self.vlayerA.dataProvider()
# creating fields
idField = QgsField("outID", QVariant.String)
#
outFeatFields = QgsFields()
if useField:
importedField = vproviderA.fields().at( self.myParam )
importedFieldName = importedField.name( )
#
outFeatFields.extend( vproviderA.fields() )
# creating area and perimeter fields
areaField = QgsField("area", QVariant.Double)
perimField = QgsField("perim", QVariant.Double)
# appending fields
outFeatFields = QgsFields()
outFeatFields.append(idField)
outFeatFields.append(areaField)
outFeatFields.append(perimField)
#
Expand Down Expand Up @@ -501,15 +505,14 @@ def convex_hull(self, useField ):
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), 0 )
self.emit( SIGNAL( "runRange(PyQt_PyObject)" ), ( 0, nFeat ) )
for i in unique:
hull = []
first = True
outID = 0
hull = []
for inFeat in selectionA:
atMap = inFeat.attributes()
idVar = atMap[ self.myParam ]
if idVar == i:
if first:
outID = idVar
firstFeature = QgsFeature( inFeat )
first = False
inGeom = QgsGeometry( inFeat.geometry() )
points = ftools_utils.extractPoints( inGeom )
Expand All @@ -522,7 +525,8 @@ def convex_hull(self, useField ):
outGeom = tmpGeom.convexHull()
outFeat.setGeometry( outGeom )
(area, perim) = self.simpleMeasure( outGeom )
outFeat.setAttribute( "outID", outID )
for f in firstFeature.fields():
outFeat.setAttribute( f.name( ), firstFeature.attribute( f.name( ) ) )
outFeat.setAttribute( "area", area )
outFeat.setAttribute( "perim", perim )
writer.addFeature( outFeat )
Expand All @@ -543,6 +547,11 @@ def convex_hull(self, useField ):
try:
outGeom = tmpGeom.convexHull()
outFeat.setGeometry( outGeom )
(area, perim) = self.simpleMeasure( outGeom )
for f in inFeat.fields():
outFeat.setAttribute( f.name( ), inFeat.attribute( f.name( ) ) )
outFeat.setAttribute( "area", area )
outFeat.setAttribute( "perim", perim )
writer.addFeature( outFeat )
except:
GEOS_EXCEPT = False
Expand All @@ -554,18 +563,16 @@ def convex_hull(self, useField ):
nFeat = nFeat * len( unique )
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), 0 )
self.emit( SIGNAL( "runRange(PyQt_PyObject)" ), ( 0, nFeat ) )

for i in unique:
hull = []
first = True
outID = 0

hull = []
fitA = vproviderA.getFeatures()
while fitA.nextFeature( inFeat ):
atMap = inFeat.attributes()
idVar = atMap[ self.myParam ]
idVar = inFeat.attribute( importedFieldName )
if idVar == i:
if first:
outID = idVar
firstFeature = QgsFeature( inFeat )
first = False
inGeom = QgsGeometry( inFeat.geometry() )
points = ftools_utils.extractPoints( inGeom )
Expand All @@ -578,7 +585,8 @@ def convex_hull(self, useField ):
outGeom = tmpGeom.convexHull()
outFeat.setGeometry( outGeom )
(area, perim) = self.simpleMeasure( outGeom )
outFeat.setAttribute( "outID", outID )
for f in firstFeature.fields():
outFeat.setAttribute( f.name( ), firstFeature.attribute( f.name( ) ) )
outFeat.setAttribute( "area", area )
outFeat.setAttribute( "perim", perim )
writer.addFeature( outFeat )
Expand All @@ -600,6 +608,11 @@ def convex_hull(self, useField ):
try:
outGeom = tmpGeom.convexHull()
outFeat.setGeometry( outGeom )
(area, perim) = self.simpleMeasure( outGeom )
for f in inFeat.fields():
outFeat.setAttribute( f.name( ), inFeat.attribute( f.name( ) ) )
outFeat.setAttribute( "area", area )
outFeat.setAttribute( "perim", perim )
writer.addFeature( outFeat )
except:
GEOS_EXCEPT = False
Expand Down
3 changes: 2 additions & 1 deletion python/plugins/fTools/tools/ftools_utils.py
Expand Up @@ -132,7 +132,7 @@ def extractPoints( geom ):
temp_geom = geom.asMultiPoint()
else:
temp_geom.append(geom.asPoint())
if geom.type() == 1: # it's a line
elif geom.type() == 1: # it's a line
if geom.isMultipart():
multi_geom = geom.asMultiPolyline() #multi_geog is a multiline
for i in multi_geom: #i is a line
Expand All @@ -149,6 +149,7 @@ def extractPoints( geom ):
multi_geom = geom.asPolygon() #multi_geom is a polygon
for i in multi_geom: #i is a line
temp_geom.extend( i )
# FIXME - if there is none of know geoms (point, line, polygon) show an warning message
return temp_geom

# Check if two input field maps are unique, and resolve name issues if they aren't
Expand Down

0 comments on commit 033440f

Please sign in to comment.