mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-18 00:03:05 -04:00
Port grid lines to new API
This commit is contained in:
parent
7ab82444f1
commit
54be720485
@ -39,14 +39,15 @@ from qgis.core import (QgsRectangle,
|
|||||||
QgsPoint,
|
QgsPoint,
|
||||||
QgsLineString,
|
QgsLineString,
|
||||||
QgsWkbTypes,
|
QgsWkbTypes,
|
||||||
|
QgsProcessing,
|
||||||
|
QgsProcessingException,
|
||||||
|
QgsProcessingParameterEnum,
|
||||||
|
QgsProcessingParameterExtent,
|
||||||
|
QgsProcessingParameterNumber,
|
||||||
|
QgsProcessingParameterCrs,
|
||||||
|
QgsProcessingParameterFeatureSink,
|
||||||
QgsFields)
|
QgsFields)
|
||||||
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
|
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
|
||||||
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
|
|
||||||
from processing.core.parameters import ParameterExtent
|
|
||||||
from processing.core.parameters import ParameterNumber
|
|
||||||
from processing.core.parameters import ParameterCrs
|
|
||||||
from processing.core.outputs import OutputVector
|
|
||||||
from processing.tools import dataobjects
|
|
||||||
|
|
||||||
pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
|
pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
|
||||||
|
|
||||||
@ -73,19 +74,24 @@ class GridLine(QgisAlgorithm):
|
|||||||
super().__init__()
|
super().__init__()
|
||||||
|
|
||||||
def initAlgorithm(self, config=None):
|
def initAlgorithm(self, config=None):
|
||||||
self.addParameter(ParameterExtent(self.EXTENT,
|
self.addParameter(QgsProcessingParameterExtent(self.EXTENT, self.tr('Grid extent')))
|
||||||
self.tr('Grid extent'), optional=False))
|
|
||||||
self.addParameter(ParameterNumber(self.HSPACING,
|
|
||||||
self.tr('Horizontal spacing'), 0.0, 1000000000.0, default=0.0001))
|
|
||||||
self.addParameter(ParameterNumber(self.VSPACING,
|
|
||||||
self.tr('Vertical spacing'), 0.0, 1000000000.0, default=0.0001))
|
|
||||||
self.addParameter(ParameterNumber(self.HOVERLAY,
|
|
||||||
self.tr('Horizontal overlay'), 0.0, 1000000000.0, default=0.0))
|
|
||||||
self.addParameter(ParameterNumber(self.VOVERLAY,
|
|
||||||
self.tr('Vertical overlay'), 0.0, 1000000000.0, default=0.0))
|
|
||||||
self.addParameter(ParameterCrs(self.CRS, 'Grid CRS', 'EPSG:4326'))
|
|
||||||
|
|
||||||
self.addOutput(OutputVector(self.OUTPUT, self.tr('Grid'), datatype=[dataobjects.TYPE_VECTOR_LINE]))
|
self.addParameter(QgsProcessingParameterNumber(self.HSPACING,
|
||||||
|
self.tr('Horizontal spacing'), QgsProcessingParameterNumber.Double,
|
||||||
|
0.0001, False, 0, 1000000000.0))
|
||||||
|
self.addParameter(QgsProcessingParameterNumber(self.VSPACING,
|
||||||
|
self.tr('Vertical spacing'), QgsProcessingParameterNumber.Double,
|
||||||
|
0.0001, False, 0, 1000000000.0))
|
||||||
|
self.addParameter(QgsProcessingParameterNumber(self.HOVERLAY,
|
||||||
|
self.tr('Horizontal overlay'), QgsProcessingParameterNumber.Double,
|
||||||
|
0.0, False, 0, 1000000000.0))
|
||||||
|
self.addParameter(QgsProcessingParameterNumber(self.VOVERLAY,
|
||||||
|
self.tr('Vertical overlay'), QgsProcessingParameterNumber.Double,
|
||||||
|
0.0, False, 0, 1000000000.0))
|
||||||
|
|
||||||
|
self.addParameter(QgsProcessingParameterCrs(self.CRS, 'Grid CRS', 'ProjectCrs'))
|
||||||
|
|
||||||
|
self.addParameter(QgsProcessingParameterFeatureSink(self.OUTPUT, self.tr('Grid'), type=QgsProcessing.TypeVectorLine))
|
||||||
|
|
||||||
def name(self):
|
def name(self):
|
||||||
return 'creategridlines'
|
return 'creategridlines'
|
||||||
@ -94,33 +100,31 @@ class GridLine(QgisAlgorithm):
|
|||||||
return self.tr('Create grid (lines)')
|
return self.tr('Create grid (lines)')
|
||||||
|
|
||||||
def processAlgorithm(self, parameters, context, feedback):
|
def processAlgorithm(self, parameters, context, feedback):
|
||||||
extent = self.getParameterValue(self.EXTENT).split(',')
|
hSpacing = self.parameterAsDouble(parameters, self.HSPACING, context)
|
||||||
hSpacing = self.getParameterValue(self.HSPACING)
|
vSpacing = self.parameterAsDouble(parameters, self.VSPACING, context)
|
||||||
vSpacing = self.getParameterValue(self.VSPACING)
|
hOverlay = self.parameterAsDouble(parameters, self.HOVERLAY, context)
|
||||||
hOverlay = self.getParameterValue(self.HOVERLAY)
|
vOverlay = self.parameterAsDouble(parameters, self.VOVERLAY, context)
|
||||||
vOverlay = self.getParameterValue(self.VOVERLAY)
|
|
||||||
crs = QgsCoordinateReferenceSystem(self.getParameterValue(self.CRS))
|
|
||||||
|
|
||||||
bbox = QgsRectangle(float(extent[0]), float(extent[2]),
|
bbox = self.parameterAsExtent(parameters, self.EXTENT, context)
|
||||||
float(extent[1]), float(extent[3]))
|
crs = self.parameterAsCrs(parameters, self.CRS, context)
|
||||||
|
|
||||||
width = bbox.width()
|
width = bbox.width()
|
||||||
height = bbox.height()
|
height = bbox.height()
|
||||||
|
|
||||||
if hSpacing <= 0 or vSpacing <= 0:
|
if hSpacing <= 0 or vSpacing <= 0:
|
||||||
raise GeoAlgorithmExecutionException(
|
raise QgsProcessingException(
|
||||||
self.tr('Invalid grid spacing: {0}/{1}').format(hSpacing, vSpacing))
|
self.tr('Invalid grid spacing: {0}/{1}').format(hSpacing, vSpacing))
|
||||||
|
|
||||||
if hSpacing <= hOverlay or vSpacing <= vOverlay:
|
if hSpacing <= hOverlay or vSpacing <= vOverlay:
|
||||||
raise GeoAlgorithmExecutionException(
|
raise QgsProcessingException(
|
||||||
self.tr('Invalid overlay: {0}/{1}').format(hOverlay, vOverlay))
|
self.tr('Invalid overlay: {0}/{1}').format(hOverlay, vOverlay))
|
||||||
|
|
||||||
if width < hSpacing:
|
if width < hSpacing:
|
||||||
raise GeoAlgorithmExecutionException(
|
raise QgsProcessingException(
|
||||||
self.tr('Horizontal spacing is too small for the covered area'))
|
self.tr('Horizontal spacing is too small for the covered area'))
|
||||||
|
|
||||||
if height < vSpacing:
|
if height < vSpacing:
|
||||||
raise GeoAlgorithmExecutionException(
|
raise QgsProcessingException(
|
||||||
self.tr('Vertical spacing is too small for the covered area'))
|
self.tr('Vertical spacing is too small for the covered area'))
|
||||||
|
|
||||||
fields = QgsFields()
|
fields = QgsFields()
|
||||||
@ -131,7 +135,8 @@ class GridLine(QgisAlgorithm):
|
|||||||
fields.append(QgsField('id', QVariant.Int, '', 10, 0))
|
fields.append(QgsField('id', QVariant.Int, '', 10, 0))
|
||||||
fields.append(QgsField('coord', QVariant.Double, '', 24, 15))
|
fields.append(QgsField('coord', QVariant.Double, '', 24, 15))
|
||||||
|
|
||||||
writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fields, QgsWkbTypes.LineString, crs, context)
|
(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
|
||||||
|
fields, QgsWkbTypes.LineString, crs)
|
||||||
|
|
||||||
if hOverlay > 0:
|
if hOverlay > 0:
|
||||||
hSpace = [hSpacing - hOverlay, hOverlay]
|
hSpace = [hSpacing - hOverlay, hOverlay]
|
||||||
@ -154,6 +159,9 @@ class GridLine(QgisAlgorithm):
|
|||||||
count_update = count_max * 0.10
|
count_update = count_max * 0.10
|
||||||
y = bbox.yMaximum()
|
y = bbox.yMaximum()
|
||||||
while y >= bbox.yMinimum():
|
while y >= bbox.yMinimum():
|
||||||
|
if feedback.isCanceled():
|
||||||
|
break
|
||||||
|
|
||||||
pt1 = QgsPoint(bbox.xMinimum(), y)
|
pt1 = QgsPoint(bbox.xMinimum(), y)
|
||||||
pt2 = QgsPoint(bbox.xMaximum(), y)
|
pt2 = QgsPoint(bbox.xMaximum(), y)
|
||||||
line = QgsLineString()
|
line = QgsLineString()
|
||||||
@ -165,7 +173,7 @@ class GridLine(QgisAlgorithm):
|
|||||||
y,
|
y,
|
||||||
id,
|
id,
|
||||||
y])
|
y])
|
||||||
writer.addFeature(feat, QgsFeatureSink.FastInsert)
|
sink.addFeature(feat, QgsFeatureSink.FastInsert)
|
||||||
y = y - vSpace[count % 2]
|
y = y - vSpace[count % 2]
|
||||||
id += 1
|
id += 1
|
||||||
count += 1
|
count += 1
|
||||||
@ -181,6 +189,9 @@ class GridLine(QgisAlgorithm):
|
|||||||
count_update = count_max * 0.10
|
count_update = count_max * 0.10
|
||||||
x = bbox.xMinimum()
|
x = bbox.xMinimum()
|
||||||
while x <= bbox.xMaximum():
|
while x <= bbox.xMaximum():
|
||||||
|
if feedback.isCanceled():
|
||||||
|
break
|
||||||
|
|
||||||
pt1 = QgsPoint(x, bbox.yMaximum())
|
pt1 = QgsPoint(x, bbox.yMaximum())
|
||||||
pt2 = QgsPoint(x, bbox.yMinimum())
|
pt2 = QgsPoint(x, bbox.yMinimum())
|
||||||
line = QgsLineString()
|
line = QgsLineString()
|
||||||
@ -192,11 +203,11 @@ class GridLine(QgisAlgorithm):
|
|||||||
bbox.yMinimum(),
|
bbox.yMinimum(),
|
||||||
id,
|
id,
|
||||||
x])
|
x])
|
||||||
writer.addFeature(feat, QgsFeatureSink.FastInsert)
|
sink.addFeature(feat, QgsFeatureSink.FastInsert)
|
||||||
x = x + hSpace[count % 2]
|
x = x + hSpace[count % 2]
|
||||||
id += 1
|
id += 1
|
||||||
count += 1
|
count += 1
|
||||||
if int(math.fmod(count, count_update)) == 0:
|
if int(math.fmod(count, count_update)) == 0:
|
||||||
feedback.setProgress(50 + int(count / count_max * 50))
|
feedback.setProgress(50 + int(count / count_max * 50))
|
||||||
|
|
||||||
del writer
|
return {self.OUTPUT: dest_id}
|
||||||
|
@ -70,6 +70,7 @@ from .ExtractSpecificNodes import ExtractSpecificNodes
|
|||||||
from .FixedDistanceBuffer import FixedDistanceBuffer
|
from .FixedDistanceBuffer import FixedDistanceBuffer
|
||||||
from .FixGeometry import FixGeometry
|
from .FixGeometry import FixGeometry
|
||||||
from .GeometryByExpression import GeometryByExpression
|
from .GeometryByExpression import GeometryByExpression
|
||||||
|
from .GridLine import GridLine
|
||||||
from .GridPolygon import GridPolygon
|
from .GridPolygon import GridPolygon
|
||||||
from .Heatmap import Heatmap
|
from .Heatmap import Heatmap
|
||||||
from .Hillshade import Hillshade
|
from .Hillshade import Hillshade
|
||||||
@ -139,7 +140,6 @@ from .ZonalStatistics import ZonalStatistics
|
|||||||
# from .ExtractByLocation import ExtractByLocation
|
# from .ExtractByLocation import ExtractByLocation
|
||||||
# from .SelectByLocation import SelectByLocation
|
# from .SelectByLocation import SelectByLocation
|
||||||
# from .SpatialJoin import SpatialJoin
|
# from .SpatialJoin import SpatialJoin
|
||||||
# from .GridLine import GridLine
|
|
||||||
# from .Gridify import Gridify
|
# from .Gridify import Gridify
|
||||||
# from .HubDistancePoints import HubDistancePoints
|
# from .HubDistancePoints import HubDistancePoints
|
||||||
# from .HubDistanceLines import HubDistanceLines
|
# from .HubDistanceLines import HubDistanceLines
|
||||||
@ -188,7 +188,7 @@ class QGISAlgorithmProvider(QgsProcessingProvider):
|
|||||||
# SelectByLocation(),
|
# SelectByLocation(),
|
||||||
# ExtractByLocation(),
|
# ExtractByLocation(),
|
||||||
# SpatialJoin(),
|
# SpatialJoin(),
|
||||||
# GridLine(), Gridify(), HubDistancePoints(),
|
# Gridify(), HubDistancePoints(),
|
||||||
# HubDistanceLines(), HubLines(),
|
# HubDistanceLines(), HubLines(),
|
||||||
# GeometryConvert(), FieldsCalculator(),
|
# GeometryConvert(), FieldsCalculator(),
|
||||||
# JoinAttributes(),
|
# JoinAttributes(),
|
||||||
@ -240,6 +240,7 @@ class QGISAlgorithmProvider(QgsProcessingProvider):
|
|||||||
FixedDistanceBuffer(),
|
FixedDistanceBuffer(),
|
||||||
FixGeometry(),
|
FixGeometry(),
|
||||||
GeometryByExpression(),
|
GeometryByExpression(),
|
||||||
|
GridLine(),
|
||||||
GridPolygon(),
|
GridPolygon(),
|
||||||
Heatmap(),
|
Heatmap(),
|
||||||
Hillshade(),
|
Hillshade(),
|
||||||
|
@ -1795,21 +1795,21 @@ tests:
|
|||||||
name: expected/dropped_geometry.csv
|
name: expected/dropped_geometry.csv
|
||||||
type: vector
|
type: vector
|
||||||
|
|
||||||
# - algorithm: qgis:creategridlines
|
- algorithm: qgis:creategridlines
|
||||||
# name: Create grid (lines)
|
name: Create grid (lines)
|
||||||
# params:
|
params:
|
||||||
# CRS: EPSG:4326
|
CRS: EPSG:4326
|
||||||
# EXTENT: -1,11.2,-4,6.5
|
EXTENT: -1,11.2,-4,6.5
|
||||||
# HSPACING: 5.0
|
HSPACING: 5.0
|
||||||
# VSPACING: 3.0
|
VSPACING: 3.0
|
||||||
# results:
|
results:
|
||||||
# OUTPUT:
|
OUTPUT:
|
||||||
# name: expected/grid_lines.gml
|
name: expected/grid_lines.gml
|
||||||
# type: vector
|
type: vector
|
||||||
# compare:
|
compare:
|
||||||
# geometry:
|
geometry:
|
||||||
# precision: 7
|
precision: 7
|
||||||
#
|
|
||||||
- algorithm: qgis:creategridpolygon
|
- algorithm: qgis:creategridpolygon
|
||||||
name: Create grid (rectangles)
|
name: Create grid (rectangles)
|
||||||
params:
|
params:
|
||||||
@ -1858,20 +1858,20 @@ tests:
|
|||||||
geometry:
|
geometry:
|
||||||
precision: 7
|
precision: 7
|
||||||
|
|
||||||
# - algorithm: qgis:creategridlines
|
- algorithm: qgis:creategridlines
|
||||||
# name: Create grid (lines with overlay)
|
name: Create grid (lines with overlay)
|
||||||
# params:
|
params:
|
||||||
# CRS: EPSG:4326
|
CRS: EPSG:4326
|
||||||
# EXTENT: -1,11.2,-4,6.5
|
EXTENT: -1,11.2,-4,6.5
|
||||||
# HOVERLAY: 2.0
|
HOVERLAY: 2.0
|
||||||
# HSPACING: 5.0
|
HSPACING: 5.0
|
||||||
# VOVERLAY: 1.0
|
VOVERLAY: 1.0
|
||||||
# VSPACING: 3.0
|
VSPACING: 3.0
|
||||||
# results:
|
results:
|
||||||
# OUTPUT:
|
OUTPUT:
|
||||||
# name: expected/grid_lines_overlay.gml
|
name: expected/grid_lines_overlay.gml
|
||||||
# type: vector
|
type: vector
|
||||||
#
|
|
||||||
- algorithm: qgis:creategridpolygon
|
- algorithm: qgis:creategridpolygon
|
||||||
name: Create grid (rectangle with overlay)
|
name: Create grid (rectangle with overlay)
|
||||||
params:
|
params:
|
||||||
@ -2586,21 +2586,21 @@ tests:
|
|||||||
# OUTPUT_LAYER:
|
# OUTPUT_LAYER:
|
||||||
# name: expected/buffer_ovals.gml
|
# name: expected/buffer_ovals.gml
|
||||||
# type: vector
|
# type: vector
|
||||||
#
|
|
||||||
# - algorithm: qgis:creategridlines
|
- algorithm: qgis:creategridlines
|
||||||
# name: Lines grid 0.1 degree spacing
|
name: Lines grid 0.1 degree spacing
|
||||||
# params:
|
params:
|
||||||
# CRS: EPSG:4326
|
CRS: EPSG:4326
|
||||||
# EXTENT: -0.10453905405405395,8.808021567567568,-2.5010055337837844,4.058021763513514
|
EXTENT: -0.10453905405405395,8.808021567567568,-2.5010055337837844,4.058021763513514
|
||||||
# HOVERLAY: 0.0
|
HOVERLAY: 0.0
|
||||||
# HSPACING: 0.1
|
HSPACING: 0.1
|
||||||
# VOVERLAY: 0.0
|
VOVERLAY: 0.0
|
||||||
# VSPACING: 0.1
|
VSPACING: 0.1
|
||||||
# results:
|
results:
|
||||||
# OUTPUT:
|
OUTPUT:
|
||||||
# name: expected/create_grid_lines.gml
|
name: expected/create_grid_lines.gml
|
||||||
# type: vector
|
type: vector
|
||||||
#
|
|
||||||
# - algorithm: qgis:convertgeometrytype
|
# - algorithm: qgis:convertgeometrytype
|
||||||
# name: polygon to centroid
|
# name: polygon to centroid
|
||||||
# params:
|
# params:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user