Skip to content
Permalink
Browse files

Port gridify to new API

  • Loading branch information
nyalldawson committed Aug 2, 2017
1 parent 54be720 commit 591de92b071e72466c6b78225ae1652256c5e009
@@ -31,95 +31,87 @@
QgsPointXY,
QgsWkbTypes,
QgsApplication,
QgsMessageLog,
QgsProcessingUtils)
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from processing.core.parameters import ParameterVector
from processing.core.parameters import ParameterNumber
from processing.core.outputs import OutputVector
QgsProcessingException,
QgsProcessingParameterNumber)
from processing.algs.qgis.QgisAlgorithm import QgisFeatureBasedAlgorithm


class Gridify(QgisAlgorithm):
INPUT = 'INPUT'
class Gridify(QgisFeatureBasedAlgorithm):

HSPACING = 'HSPACING'
VSPACING = 'VSPACING'
OUTPUT = 'OUTPUT'

def group(self):
return self.tr('Vector general tools')

def __init__(self):
super().__init__()
self.h_spacing = None
self.v_spacing = None

def initAlgorithm(self, config=None):
self.addParameter(ParameterVector(self.INPUT,
self.tr('Input Layer')))
self.addParameter(ParameterNumber(self.HSPACING,
self.tr('Horizontal spacing'), default=0.1))
self.addParameter(ParameterNumber(self.VSPACING,
self.tr('Vertical spacing'), default=0.1))

self.addOutput(OutputVector(self.OUTPUT, self.tr('Snapped')))
def initParameters(self, config=None):
self.addParameter(QgsProcessingParameterNumber(self.HSPACING,
self.tr('Horizontal spacing'), type=QgsProcessingParameterNumber.Double, minValue=0.0, defaultValue=0.1))
self.addParameter(QgsProcessingParameterNumber(self.VSPACING,
self.tr('Vertical spacing'), type=QgsProcessingParameterNumber.Double, minValue=0.0, defaultValue=0.1))

def name(self):
return 'snappointstogrid'

def displayName(self):
return self.tr('Snap points to grid')

def processAlgorithm(self, parameters, context, feedback):
layer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT), context)
hSpacing = self.getParameterValue(self.HSPACING)
vSpacing = self.getParameterValue(self.VSPACING)

if hSpacing <= 0 or vSpacing <= 0:
raise GeoAlgorithmExecutionException(
self.tr('Invalid grid spacing: {0}/{1}').format(hSpacing, vSpacing))
def outputName(self):
return self.tr('Snapped')

writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(layer.fields(), layer.wkbType(), layer.crs(),
context)
def prepareAlgorithm(self, parameters, context, feedback):
self.h_spacing = self.parameterAsDouble(parameters, self.HSPACING, context)
self.v_spacing = self.parameterAsDouble(parameters, self.VSPACING, context)
if self.h_spacing <= 0 or self.v_spacing <= 0:
raise QgsProcessingException(
self.tr('Invalid grid spacing: {0}/{1}').format(self.h_spacing, self.v_spacing))

features = QgsProcessingUtils.getFeatures(layer, context)
total = 100.0 / layer.featureCount() if layer.featureCount() else 0
return True

for current, f in enumerate(features):
geom = f.geometry()
geomType = geom.wkbType()
def processFeature(self, feature, feedback):
if feature.hasGeometry():
geom = feature.geometry()
geomType = QgsWkbTypes.flatType(geom.wkbType())
newGeom = None

if geomType == QgsWkbTypes.Point:
points = self._gridify([geom.asPoint()], hSpacing, vSpacing)
points = self._gridify([geom.asPoint()], self.h_spacing, self.v_spacing)
newGeom = QgsGeometry.fromPoint(points[0])
elif geomType == QgsWkbTypes.MultiPoint:
points = self._gridify(geom.aMultiPoint(), hSpacing, vSpacing)
points = self._gridify(geom.aMultiPoint(), self.h_spacing, self.v_spacing)
newGeom = QgsGeometry.fromMultiPoint(points)
elif geomType == QgsWkbTypes.LineString:
points = self._gridify(geom.asPolyline(), hSpacing, vSpacing)
points = self._gridify(geom.asPolyline(), self.h_spacing, self.v_spacing)
if len(points) < 2:
QgsMessageLog.logMessage(self.tr('Failed to gridify feature with FID {0}').format(f.id()), self.tr('Processing'), QgsMessageLog.INFO)
feedback.reportError(self.tr('Failed to gridify feature with FID {0}').format(feature.id()))
newGeom = None
else:
newGeom = QgsGeometry.fromPolyline(points)
elif geomType == QgsWkbTypes.MultiLineString:
polyline = []
for line in geom.asMultiPolyline():
points = self._gridify(line, hSpacing, vSpacing)
points = self._gridify(line, self.h_spacing, self.v_spacing)
if len(points) > 1:
polyline.append(points)
if len(polyline) <= 0:
QgsMessageLog.logMessage(self.tr('Failed to gridify feature with FID {0}').format(f.id()), self.tr('Processing'), QgsMessageLog.INFO)
feedback.reportError(self.tr('Failed to gridify feature with FID {0}').format(feature.id()))
newGeom = None
else:
newGeom = QgsGeometry.fromMultiPolyline(polyline)

elif geomType == QgsWkbTypes.Polygon:
polygon = []
for line in geom.asPolygon():
points = self._gridify(line, hSpacing, vSpacing)
points = self._gridify(line, self.h_spacing, self.v_spacing)
if len(points) > 1:
polygon.append(points)
if len(polygon) <= 0:
QgsMessageLog.logMessage(self.tr('Failed to gridify feature with FID {0}').format(f.id()), self.tr('Processing'), QgsMessageLog.INFO)
feedback.reportError(self.tr('Failed to gridify feature with FID {0}').format(feature.id()))
newGeom = None
else:
newGeom = QgsGeometry.fromPolygon(polygon)
@@ -128,28 +120,24 @@ def processAlgorithm(self, parameters, context, feedback):
for polygon in geom.asMultiPolygon():
newPolygon = []
for line in polygon:
points = self._gridify(line, hSpacing, vSpacing)
points = self._gridify(line, self.h_spacing, self.v_spacing)
if len(points) > 2:
newPolygon.append(points)

if len(newPolygon) > 0:
multipolygon.append(newPolygon)

if len(multipolygon) <= 0:
QgsMessageLog.logMessage(self.tr('Failed to gridify feature with FID {0}').format(f.id()), self.tr('Processing'), QgsMessageLog.INFO)
feedback.reportError(self.tr('Failed to gridify feature with FID {0}').format(feature.id()))
newGeom = None
else:
newGeom = QgsGeometry.fromMultiPolygon(multipolygon)

if newGeom is not None:
feat = QgsFeature()
feat.setGeometry(newGeom)
feat.setAttributes(f.attributes())
writer.addFeature(feat, QgsFeatureSink.FastInsert)

feedback.setProgress(int(current * total))

del writer
feature.setGeometry(newGeom)
else:
feature.clearGeometry()
return feature

def _gridify(self, points, hSpacing, vSpacing):
nPoints = []
@@ -70,6 +70,7 @@
from .FixedDistanceBuffer import FixedDistanceBuffer
from .FixGeometry import FixGeometry
from .GeometryByExpression import GeometryByExpression
from .Gridify import Gridify
from .GridLine import GridLine
from .GridPolygon import GridPolygon
from .Heatmap import Heatmap
@@ -140,7 +141,6 @@
# from .ExtractByLocation import ExtractByLocation
# from .SelectByLocation import SelectByLocation
# from .SpatialJoin import SpatialJoin
# from .Gridify import Gridify
# from .HubDistancePoints import HubDistancePoints
# from .HubDistanceLines import HubDistanceLines
# from .HubLines import HubLines
@@ -188,7 +188,7 @@ def getAlgs(self):
# SelectByLocation(),
# ExtractByLocation(),
# SpatialJoin(),
# Gridify(), HubDistancePoints(),
# HubDistancePoints(),
# HubDistanceLines(), HubLines(),
# GeometryConvert(), FieldsCalculator(),
# JoinAttributes(),
@@ -240,6 +240,7 @@ def getAlgs(self):
FixedDistanceBuffer(),
FixGeometry(),
GeometryByExpression(),
Gridify(),
GridLine(),
GridPolygon(),
Heatmap(),

0 comments on commit 591de92

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