mirror of
https://github.com/qgis/QGIS.git
synced 2025-03-03 00:02:25 -05:00
[FEATURE][processing] New algorithm "Contour Polygons"
With similar functionality to the existing GDAL contour algorithm, but exporting polygon representations of the contours
This commit is contained in:
parent
0f1f43f77a
commit
b73bd58cfb
@ -37,7 +37,7 @@ from .buildvrt import buildvrt
|
||||
from .ClipRasterByExtent import ClipRasterByExtent
|
||||
from .ClipRasterByMask import ClipRasterByMask
|
||||
from .ColorRelief import ColorRelief
|
||||
from .contour import contour
|
||||
from .contour import contour, contour_polygon
|
||||
from .Datasources2Vrt import Datasources2Vrt
|
||||
from .fillnodata import fillnodata
|
||||
from .gdalinfo import gdalinfo
|
||||
@ -147,6 +147,7 @@ class GdalAlgorithmProvider(QgsProcessingProvider):
|
||||
ClipRasterByMask(),
|
||||
ColorRelief(),
|
||||
contour(),
|
||||
contour_polygon(),
|
||||
Datasources2Vrt(),
|
||||
fillnodata(),
|
||||
gdalinfo(),
|
||||
|
@ -139,7 +139,7 @@ class contour(GdalAlgorithm):
|
||||
def commandName(self):
|
||||
return 'gdal_contour'
|
||||
|
||||
def getConsoleCommands(self, parameters, context, feedback, executing=True):
|
||||
def _buildArgsList(self, parameters, context, feedback, executing):
|
||||
inLayer = self.parameterAsRasterLayer(parameters, self.INPUT, context)
|
||||
if inLayer is None:
|
||||
raise QgsProcessingException(self.invalidRasterError(parameters, self.INPUT))
|
||||
@ -192,5 +192,61 @@ class contour(GdalAlgorithm):
|
||||
|
||||
arguments.append(inLayer.source())
|
||||
arguments.append(output)
|
||||
return arguments
|
||||
|
||||
def getConsoleCommands(self, parameters, context, feedback, executing=True):
|
||||
arguments = self._buildArgsList(parameters, context, feedback, executing)
|
||||
return [self.commandName(), GdalUtils.escapeAndJoin(arguments)]
|
||||
|
||||
|
||||
class contour_polygon(contour):
|
||||
|
||||
FIELD_NAME_MIN = 'FIELD_NAME_MIN'
|
||||
FIELD_NAME_MAX = 'FIELD_NAME_MAX'
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
|
||||
def initAlgorithm(self, config=None):
|
||||
super().initAlgorithm(config)
|
||||
# FIELD_NAME isn't used in polygon mode
|
||||
self.removeParameter(contour.FIELD_NAME)
|
||||
|
||||
self.addParameter(QgsProcessingParameterString(self.FIELD_NAME_MIN,
|
||||
self.tr('Attribute name for minimum elevation of contour polygon'),
|
||||
defaultValue='ELEV_MIN',
|
||||
optional=True))
|
||||
|
||||
self.addParameter(QgsProcessingParameterString(self.FIELD_NAME_MAX,
|
||||
self.tr('Attribute name for maximum elevation of contour polygon'),
|
||||
defaultValue='ELEV_MAX',
|
||||
optional=True))
|
||||
|
||||
# Need to replace the output parameter, as we are producing a different type of output
|
||||
self.removeParameter(contour.OUTPUT)
|
||||
self.addParameter(QgsProcessingParameterVectorDestination(
|
||||
contour.OUTPUT, self.tr('Contours'), QgsProcessing.TypeVectorPolygon))
|
||||
|
||||
def name(self):
|
||||
return 'contour_polygon'
|
||||
|
||||
def displayName(self):
|
||||
return self.tr('Contour Polygons')
|
||||
|
||||
def getConsoleCommands(self, parameters, context, feedback, executing=True):
|
||||
arguments = self._buildArgsList(parameters, context, feedback, executing)
|
||||
|
||||
fieldNameMin = self.parameterAsString(parameters, self.FIELD_NAME_MIN, context)
|
||||
fieldNameMax = self.parameterAsString(parameters, self.FIELD_NAME_MAX, context)
|
||||
|
||||
if fieldNameMin:
|
||||
arguments.insert(0, fieldNameMin)
|
||||
arguments.insert(0, '-amin')
|
||||
|
||||
if fieldNameMax:
|
||||
arguments.insert(0, fieldNameMax)
|
||||
arguments.insert(0, '-amax')
|
||||
|
||||
arguments.insert(0, "-p")
|
||||
|
||||
return [self.commandName(), GdalUtils.escapeAndJoin(arguments)]
|
||||
|
@ -47,7 +47,7 @@ from processing.algs.gdal.GridNearestNeighbor import GridNearestNeighbor
|
||||
from processing.algs.gdal.gdal2tiles import gdal2tiles
|
||||
from processing.algs.gdal.gdalcalc import gdalcalc
|
||||
from processing.algs.gdal.gdaltindex import gdaltindex
|
||||
from processing.algs.gdal.contour import contour
|
||||
from processing.algs.gdal.contour import contour, contour_polygon
|
||||
from processing.algs.gdal.gdalinfo import gdalinfo
|
||||
from processing.algs.gdal.hillshade import hillshade
|
||||
from processing.algs.gdal.aspect import aspect
|
||||
@ -404,6 +404,25 @@ class TestGdalRasterAlgorithms(unittest.TestCase, AlgorithmsTestBase.AlgorithmsT
|
||||
source + ' ' +
|
||||
outdir + '/check.jpg'])
|
||||
|
||||
def testContourPolygon(self):
|
||||
context = QgsProcessingContext()
|
||||
feedback = QgsProcessingFeedback()
|
||||
source = os.path.join(testDataPath, 'dem.tif')
|
||||
alg = contour_polygon()
|
||||
alg.initAlgorithm()
|
||||
with tempfile.TemporaryDirectory() as outdir:
|
||||
self.assertEqual(
|
||||
alg.getConsoleCommands({'INPUT': source,
|
||||
'BAND': 1,
|
||||
'FIELD_NAME_MIN': 'min',
|
||||
'FIELD_NAME_MAX': 'max',
|
||||
'INTERVAL': 5,
|
||||
'OUTPUT': outdir + '/check.shp'}, context, feedback),
|
||||
['gdal_contour',
|
||||
'-p -amax max -amin min -b 1 -i 5.0 -f "ESRI Shapefile" ' +
|
||||
source + ' ' +
|
||||
outdir + '/check.shp'])
|
||||
|
||||
def testContour(self):
|
||||
context = QgsProcessingContext()
|
||||
feedback = QgsProcessingFeedback()
|
||||
|
Loading…
x
Reference in New Issue
Block a user