Skip to content

Commit

Permalink
Remove dependance on shapely library
Browse files Browse the repository at this point in the history
Port processing polygonize alg to use native QgsGeometry methods
  • Loading branch information
nyalldawson committed Jan 31, 2017
1 parent 4cebb46 commit 14bd79f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 35 deletions.
1 change: 0 additions & 1 deletion debian/control
Expand Up @@ -319,7 +319,6 @@ Depends:
gdal-bin,
python3-gdal,
python3-matplotlib,
python3-shapely,
libqgis-customwidgets,
${misc:Depends}
XB-Python-Version: ${python:Versions}
Expand Down
1 change: 0 additions & 1 deletion debian/control.in
Expand Up @@ -348,7 +348,6 @@ Depends:
gdal-bin,
python3-gdal,
python3-matplotlib,
python3-shapely,
libqgis-customwidgets,
${misc:Depends}
XB-Python-Version: ${python:Versions}
Expand Down
41 changes: 20 additions & 21 deletions python/plugins/processing/algs/qgis/Polygonize.py
Expand Up @@ -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
Expand Down Expand Up @@ -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
14 changes: 2 additions & 12 deletions python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py
Expand Up @@ -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,
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -258,7 +252,7 @@ def __init__(self):
RasterCalculator(), Heatmap(), Orthogonalize(),
ShortestPathPointToPoint(), ShortestPathPointToLayer(),
ShortestPathLayerToPoint(), ServiceAreaFromPoint(),
ServiceAreaFromLayer(), TruncateTable()
ServiceAreaFromLayer(), TruncateTable(), Polygonize()
]

if hasMatplotlib:
Expand All @@ -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()])
Expand Down

0 comments on commit 14bd79f

Please sign in to comment.