Skip to content

Commit

Permalink
[processing]Fix "float division by zero" in polygonize algorithm if no
Browse files Browse the repository at this point in the history
polygons were created.
For shapely 1.2.16+ and GEOS 3.2+ unary_union will be used for noding
lines - union method sometimes failed to create properly noded polyline.
  • Loading branch information
Piotr Pociask committed Feb 8, 2014
1 parent 1d09f43 commit 02822d4
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion python/plugins/processing/algs/Polygonize.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ def processAlgorithm(self, progress):
allLinesList = []
features = vector.features(vlayer)
current = 0
progress.setInfo('Processing lines...')
total = 40.0 / float(len(features))
for inFeat in features:
inGeom = inFeat.geometry()
Expand All @@ -77,10 +78,19 @@ def processAlgorithm(self, progress):
progress.setPercentage(int(current * total))
progress.setPercentage(40)
allLines = MultiLineString(allLinesList)
allLines = allLines.union(Point(0, 0))
progress.setInfo('Noding lines...')
try:
from shapely.ops import unary_union
allLines = unary_union(allLines)
except ImportError:
allLines = allLines.union(Point(0, 0))
progress.setPercentage(45)
progress.setInfo('Polygonizing...')
polygons = list(polygonize([allLines]))
if not polygons:
raise GeoAlgorithmExecutionException('No polygons were created!')
progress.setPercentage(50)
progress.setInfo('Saving polygons...')
writer = output.getVectorWriter(fields, QGis.WKBPolygon, vlayer.crs())
outFeat = QgsFeature()
current = 0
Expand All @@ -93,6 +103,7 @@ def processAlgorithm(self, progress):
writer.addFeature(outFeat)
current += 1
progress.setPercentage(50 + int(current * total))
progress.setInfo('Finished')
del writer

def defineCharacteristics(self):
Expand Down

0 comments on commit 02822d4

Please sign in to comment.