Skip to content
Permalink
Browse files

Remove dependance on shapely library

Port processing polygonize alg to use native QgsGeometry methods
  • Loading branch information
nyalldawson committed Jan 31, 2017
1 parent 4cebb46 commit 14bd79f844244b54b011006de60d73765f83fa4b
@@ -319,7 +319,6 @@ Depends:
gdal-bin,
python3-gdal,
python3-matplotlib,
python3-shapely,
libqgis-customwidgets,
${misc:Depends}
XB-Python-Version: ${python:Versions}
@@ -348,7 +348,6 @@ Depends:
gdal-bin,
python3-gdal,
python3-matplotlib,
python3-shapely,
libqgis-customwidgets,
${misc:Depends}
XB-Python-Version: ${python:Versions}
@@ -25,12 +25,14 @@

__revision__ = '$Format:%H$'

from shapely.ops import polygonize
from shapely.ops import unary_union
from shapely.geometry import MultiLineString

from qgis.PyQt.QtCore import QVariant
from qgis.core import Qgis, QgsFields, QgsField, QgsFeature, QgsGeometry, QgsWkbTypes
from qgis.core import (Qgis,
QgsFields,
QgsField,
QgsFeature,
QgsGeometry,
QgsWkbTypes,
QgsFeatureRequest)
from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from processing.core.parameters import ParameterVector
@@ -70,39 +72,36 @@ def processAlgorithm(self, feedback):
fields.append(QgsField('perimeter', QVariant.Double,
'double', 16, 2))
allLinesList = []
features = vector.features(vlayer)
features = vector.features(vlayer, QgsFeatureRequest().setSubsetOfAttributes([]))
feedback.pushInfo(self.tr('Processing lines...'))
total = 40.0 / len(features)
for current, inFeat in enumerate(features):
inGeom = inFeat.geometry()
if inGeom.isMultipart():
allLinesList.extend(inGeom.asMultiPolyline())
else:
allLinesList.append(inGeom.asPolyline())
if inFeat.geometry():
allLinesList.append(inFeat.geometry())
feedback.setProgress(int(current * total))

feedback.setProgress(40)
allLines = MultiLineString(allLinesList)

feedback.pushInfo(self.tr('Noding lines...'))
allLines = unary_union(allLines)
allLines = QgsGeometry.unaryUnion(allLinesList)

feedback.setProgress(45)
feedback.pushInfo(self.tr('Polygonizing...'))
polygons = list(polygonize([allLines]))
if not polygons:
polygons = QgsGeometry.polygonize([allLines])
if polygons.isEmpty():
raise GeoAlgorithmExecutionException(self.tr('No polygons were created!'))
feedback.setProgress(50)

feedback.pushInfo('Saving polygons...')
writer = output.getVectorWriter(fields, QgsWkbTypes.Polygon, vlayer.crs())
outFeat = QgsFeature()
total = 50.0 / len(polygons)
for current, polygon in enumerate(polygons):
outFeat.setGeometry(QgsGeometry.fromWkt(polygon.wkt))
total = 50.0 / polygons.geometry().numGeometries()
for i in range(polygons.geometry().numGeometries()):
outFeat = QgsFeature()
geom = QgsGeometry(polygons.geometry().geometryN(i).clone())
outFeat.setGeometry(geom)
if self.getParameterValue(self.GEOMETRY):
outFeat.setAttributes([None] * fieldsCount + [polygon.area,
polygon.length])
outFeat.setAttributes([None] * fieldsCount + [geom.geometry().area(),
geom.geometry().perimeter()])
writer.addFeature(outFeat)
feedback.setProgress(50 + int(current * total))
del writer
@@ -34,13 +34,6 @@
except:
hasMatplotlib = False

try:
import shapely
assert shapely # silence pyflakes
hasShapely = True
except:
hasShapely = False

from qgis.PyQt.QtGui import QIcon

from qgis.core import (Qgis,
@@ -190,6 +183,7 @@
from .ServiceAreaFromPoint import ServiceAreaFromPoint
from .ServiceAreaFromLayer import ServiceAreaFromLayer
from .TruncateTable import TruncateTable
from .Polygonize import Polygonize

pluginPath = os.path.normpath(os.path.join(
os.path.split(os.path.dirname(__file__))[0], os.pardir))
@@ -258,7 +252,7 @@ def __init__(self):
RasterCalculator(), Heatmap(), Orthogonalize(),
ShortestPathPointToPoint(), ShortestPathPointToLayer(),
ShortestPathLayerToPoint(), ServiceAreaFromPoint(),
ServiceAreaFromLayer(), TruncateTable()
ServiceAreaFromLayer(), TruncateTable(), Polygonize()
]

if hasMatplotlib:
@@ -275,10 +269,6 @@ def __init__(self):
PolarPlot(),
])

if hasShapely:
from .Polygonize import Polygonize
self.alglist.extend([Polygonize()])

if Qgis.QGIS_VERSION_INT >= 21300:
from .ExecuteSQL import ExecuteSQL
self.alglist.extend([ExecuteSQL()])

0 comments on commit 14bd79f

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