Skip to content

Commit c5ae3a0

Browse files
authored
Merge pull request #5199 from nyalldawson/extract_by_extent2
[processing] Extent handling improvements
2 parents 8d088c0 + 8605be0 commit c5ae3a0

22 files changed

+633
-19
lines changed

python/core/processing/qgsprocessingalgorithm.sip

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -662,12 +662,40 @@ class QgsProcessingAlgorithm
662662
:rtype: QgsCoordinateReferenceSystem
663663
%End
664664

665-
QgsRectangle parameterAsExtent( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
665+
QgsRectangle parameterAsExtent( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context,
666+
const QgsCoordinateReferenceSystem &crs = QgsCoordinateReferenceSystem() ) const;
666667
%Docstring
667668
Evaluates the parameter with matching ``name`` to a rectangular extent.
669+
670+
If ``crs`` is set, and the original coordinate reference system of the parameter can be determined, then the extent will be automatically
671+
reprojected so that it is in the specified ``crs``. In this case the extent of the reproject rectangle will be returned.
672+
673+
.. seealso:: parameterAsExtentGeometry()
668674
:rtype: QgsRectangle
669675
%End
670676

677+
QgsGeometry parameterAsExtentGeometry( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context,
678+
const QgsCoordinateReferenceSystem &crs = QgsCoordinateReferenceSystem() );
679+
%Docstring
680+
Evaluates the parameter with matching ``name`` to a rectangular extent, and returns a geometry covering this extent.
681+
682+
If ``crs`` is set, and the original coordinate reference system of the parameter can be determined, then the extent will be automatically
683+
reprojected so that it is in the specified ``crs``. Unlike parameterAsExtent(), the reprojected rectangle returned by this function
684+
will no longer be a rectangle itself (i.e. this method returns the geometry of the actual reprojected rectangle, while parameterAsExtent() returns
685+
just the extent of the reprojected rectangle).
686+
687+
.. seealso:: parameterAsExtent()
688+
:rtype: QgsGeometry
689+
%End
690+
691+
QgsCoordinateReferenceSystem parameterAsExtentCrs( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context );
692+
%Docstring
693+
Returns the coordinate reference system associated with an extent parameter value.
694+
695+
.. seealso:: parameterAsExtent()
696+
:rtype: QgsCoordinateReferenceSystem
697+
%End
698+
671699
QgsPointXY parameterAsPoint( const QVariantMap &parameters, const QString &name, QgsProcessingContext &context ) const;
672700
%Docstring
673701
Evaluates the parameter with matching ``name`` to a point.

python/core/processing/qgsprocessingparameters.sip

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,12 +534,42 @@ class QgsProcessingParameters
534534
:rtype: QgsCoordinateReferenceSystem
535535
%End
536536

537-
static QgsRectangle parameterAsExtent( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context );
537+
static QgsRectangle parameterAsExtent( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context,
538+
const QgsCoordinateReferenceSystem &crs = QgsCoordinateReferenceSystem() );
538539
%Docstring
539540
Evaluates the parameter with matching ``definition`` to a rectangular extent.
541+
542+
If ``crs`` is set, and the original coordinate reference system of the parameter can be determined, then the extent will be automatically
543+
reprojected so that it is in the specified ``crs``. In this case the extent of the reproject rectangle will be returned.
544+
545+
.. seealso:: parameterAsExtentGeometry()
546+
.. seealso:: parameterAsExtentCrs()
540547
:rtype: QgsRectangle
541548
%End
542549

550+
static QgsGeometry parameterAsExtentGeometry( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context,
551+
const QgsCoordinateReferenceSystem &crs = QgsCoordinateReferenceSystem() );
552+
%Docstring
553+
Evaluates the parameter with matching ``definition`` to a rectangular extent, and returns a geometry covering this extent.
554+
555+
If ``crs`` is set, and the original coordinate reference system of the parameter can be determined, then the extent will be automatically
556+
reprojected so that it is in the specified ``crs``. Unlike parameterAsExtent(), the reprojected rectangle returned by this function
557+
will no longer be a rectangle itself (i.e. this method returns the geometry of the actual reprojected rectangle, while parameterAsExtent() returns
558+
just the extent of the reprojected rectangle).
559+
560+
.. seealso:: parameterAsExtent()
561+
.. seealso:: parameterAsExtentCrs()
562+
:rtype: QgsGeometry
563+
%End
564+
565+
static QgsCoordinateReferenceSystem parameterAsExtentCrs( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context );
566+
%Docstring
567+
Returns the coordinate reference system associated with an extent parameter value.
568+
569+
.. seealso:: parameterAsExtent()
570+
:rtype: QgsCoordinateReferenceSystem
571+
%End
572+
543573
static QgsPointXY parameterAsPoint( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, QgsProcessingContext &context );
544574
%Docstring
545575
Evaluates the parameter with matching ``definition`` to a point.

python/plugins/processing/algs/qgis/CreateConstantRaster.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ def displayName(self):
7676
return self.tr('Create constant raster layer')
7777

7878
def processAlgorithm(self, parameters, context, feedback):
79-
extent = self.parameterAsExtent(parameters, self.EXTENT, context)
8079
crs = self.parameterAsCrs(parameters, self.TARGET_CRS, context)
80+
extent = self.parameterAsExtent(parameters, self.EXTENT, context, crs)
8181
value = self.parameterAsDouble(parameters, self.NUMBER, context)
8282
pixelSize = self.parameterAsDouble(parameters, self.PIXEL_SIZE, context)
8383

python/plugins/processing/algs/qgis/GridLine.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ def processAlgorithm(self, parameters, context, feedback):
105105
hOverlay = self.parameterAsDouble(parameters, self.HOVERLAY, context)
106106
vOverlay = self.parameterAsDouble(parameters, self.VOVERLAY, context)
107107

108-
bbox = self.parameterAsExtent(parameters, self.EXTENT, context)
109108
crs = self.parameterAsCrs(parameters, self.CRS, context)
109+
bbox = self.parameterAsExtent(parameters, self.EXTENT, context, crs)
110110

111111
width = bbox.width()
112112
height = bbox.height()

python/plugins/processing/algs/qgis/GridPolygon.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ def processAlgorithm(self, parameters, context, feedback):
113113
hOverlay = self.parameterAsDouble(parameters, self.HOVERLAY, context)
114114
vOverlay = self.parameterAsDouble(parameters, self.VOVERLAY, context)
115115

116-
bbox = self.parameterAsExtent(parameters, self.EXTENT, context)
117116
crs = self.parameterAsCrs(parameters, self.CRS, context)
117+
bbox = self.parameterAsExtent(parameters, self.EXTENT, context, crs)
118118

119119
width = bbox.width()
120120
height = bbox.height()

python/plugins/processing/algs/qgis/RandomPointsExtent.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,10 @@ def displayName(self):
9494
return self.tr('Random points in extent')
9595

9696
def processAlgorithm(self, parameters, context, feedback):
97-
bbox = self.parameterAsExtent(parameters, self.EXTENT, context)
9897
pointCount = self.parameterAsDouble(parameters, self.POINTS_NUMBER, context)
9998
minDistance = self.parameterAsDouble(parameters, self.MIN_DISTANCE, context)
10099
crs = self.parameterAsCrs(parameters, self.TARGET_CRS, context)
100+
bbox = self.parameterAsExtent(parameters, self.EXTENT, context, crs)
101101

102102
extent = QgsGeometry().fromRect(bbox)
103103

python/plugins/processing/algs/qgis/RegularPoints.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,12 @@ def displayName(self):
9393
return self.tr('Regular points')
9494

9595
def processAlgorithm(self, parameters, context, feedback):
96-
extent = self.parameterAsExtent(parameters, self.EXTENT, context)
97-
9896
spacing = self.parameterAsDouble(parameters, self.SPACING, context)
9997
inset = self.parameterAsDouble(parameters, self.INSET, context)
10098
randomize = self.parameterAsBool(parameters, self.RANDOMIZE, context)
10199
isSpacing = self.parameterAsBool(parameters, self.IS_SPACING, context)
102100
crs = self.parameterAsCrs(parameters, self.CRS, context)
101+
extent = self.parameterAsExtent(parameters, self.EXTENT, context, crs)
103102

104103
fields = QgsFields()
105104
fields.append(QgsField('id', QVariant.Int, '', 10, 0))

python/plugins/processing/gui/AlgorithmDialog.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,10 @@ def accept(self):
216216
self.tr('<b>Algorithm \'{0}\' starting...</b>').format(self.alg.displayName()), escape_html=False)
217217

218218
feedback.pushInfo(self.tr('Input parameters:'))
219-
feedback.pushCommandInfo(pformat(parameters))
219+
display_params = []
220+
for k, v in parameters.items():
221+
display_params.append("'" + k + "' : " + self.alg.parameterDefinition(k).valueAsPythonString(v, context))
222+
feedback.pushCommandInfo('{ ' + ', '.join(display_params) + ' }')
220223
feedback.pushInfo('')
221224
start_time = time.time()
222225

python/plugins/processing/gui/ExtentSelectionPanel.py

100644100755
Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@
3636
from qgis.utils import iface
3737
from qgis.core import (QgsProcessingUtils,
3838
QgsProcessingParameterDefinition,
39-
QgsProject)
39+
QgsProcessingParameters,
40+
QgsProject,
41+
QgsCoordinateReferenceSystem,
42+
QgsRectangle,
43+
QgsReferencedRectangle)
4044
from processing.gui.RectangleMapTool import RectangleMapTool
4145
from processing.core.ProcessingConfig import ProcessingConfig
4246
from processing.tools.dataobjects import createContext
@@ -54,6 +58,8 @@ def __init__(self, dialog, param):
5458

5559
self.dialog = dialog
5660
self.param = param
61+
self.crs = QgsProject.instance().crs()
62+
5763
if self.param.flags() & QgsProcessingParameterDefinition.FlagOptional:
5864
if hasattr(self.leText, 'setPlaceholderText'):
5965
self.leText.setPlaceholderText(
@@ -126,7 +132,7 @@ def useLayerExtent(self):
126132
(item, ok) = QInputDialog.getItem(self, self.tr('Select extent'),
127133
self.tr('Use extent from'), extents, False)
128134
if ok:
129-
self.setValueFromRect(extentsDict[item]["extent"])
135+
self.setValueFromRect(QgsReferencedRectangle(extentsDict[item]["extent"], QgsCoordinateReferenceSystem(extentsDict[item]["authid"])))
130136
if extentsDict[item]["authid"] != iface.mapCanvas().mapSettings().destinationCrs().authid():
131137
iface.messageBar().pushMessage(self.tr("Warning"),
132138
self.tr("The projection of the chosen layer is not the same as canvas projection! The selected extent might not be what was intended."),
@@ -146,6 +152,10 @@ def setValueFromRect(self, r):
146152
r.xMinimum(), r.xMaximum(), r.yMinimum(), r.yMaximum())
147153

148154
self.leText.setText(s)
155+
try:
156+
self.crs = r.crs()
157+
except:
158+
self.crs = QgsProject.instance().crs()
149159
self.tool.reset()
150160
canvas = iface.mapCanvas()
151161
canvas.setMapTool(self.prevMapTool)
@@ -155,7 +165,13 @@ def setValueFromRect(self, r):
155165

156166
def getValue(self):
157167
if str(self.leText.text()).strip() != '':
158-
return str(self.leText.text())
168+
try:
169+
parts = self.leText.text().split(',')
170+
parts = [float(p) for p in parts]
171+
r = QgsReferencedRectangle(QgsRectangle(parts[0], parts[2], parts[1], parts[3]), self.crs)
172+
return r
173+
except:
174+
return str(self.leText.text())
159175
else:
160176
return None
161177

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<GMLFeatureClassList>
2+
<GMLFeatureClass>
3+
<Name>extract_by_extent</Name>
4+
<ElementPath>extract_by_extent</ElementPath>
5+
<!--POLYGON-->
6+
<GeometryType>3</GeometryType>
7+
<SRSName>EPSG:4326</SRSName>
8+
<DatasetSpecificInfo>
9+
<FeatureCount>2</FeatureCount>
10+
<ExtentXMin>-1.00000</ExtentXMin>
11+
<ExtentXMax>6.00000</ExtentXMax>
12+
<ExtentYMin>-3.00000</ExtentYMin>
13+
<ExtentYMax>3.00000</ExtentYMax>
14+
</DatasetSpecificInfo>
15+
<PropertyDefn>
16+
<Name>name</Name>
17+
<ElementPath>name</ElementPath>
18+
<Type>String</Type>
19+
<Width>5</Width>
20+
</PropertyDefn>
21+
<PropertyDefn>
22+
<Name>intval</Name>
23+
<ElementPath>intval</ElementPath>
24+
<Type>Integer</Type>
25+
</PropertyDefn>
26+
<PropertyDefn>
27+
<Name>floatval</Name>
28+
<ElementPath>floatval</ElementPath>
29+
<Type>Real</Type>
30+
</PropertyDefn>
31+
</GMLFeatureClass>
32+
</GMLFeatureClassList>

0 commit comments

Comments
 (0)