21
21
22
22
// /@cond PRIVATE
23
23
24
- QString QgsVectorizeAlgorithm::name () const
25
- {
26
- return QStringLiteral ( " pixelstopolygons" );
27
- }
28
-
29
- QString QgsVectorizeAlgorithm::displayName () const
30
- {
31
- return QObject::tr ( " Raster pixels to polygons" );
32
- }
33
-
34
- QStringList QgsVectorizeAlgorithm::tags () const
35
- {
36
- return QObject::tr ( " vectorize,polygonize,raster,convert,pixels" ).split ( ' ,' );
37
- }
38
-
39
- QString QgsVectorizeAlgorithm::shortHelpString () const
40
- {
41
- return QObject::tr ( " This algorithm converts a raster layer to a vector layer, by creating polygon features for each individual pixel in the raster layer." );
42
- }
43
-
44
- QgsVectorizeAlgorithm *QgsVectorizeAlgorithm::createInstance () const
45
- {
46
- return new QgsVectorizeAlgorithm ();
47
- }
48
-
49
- QString QgsVectorizeAlgorithm::group () const
24
+ QString QgsVectorizeAlgorithmBase::group () const
50
25
{
51
26
return QObject::tr ( " Vector creation" );
52
27
}
53
28
54
- QString QgsVectorizeAlgorithm ::groupId () const
29
+ QString QgsVectorizeAlgorithmBase ::groupId () const
55
30
{
56
31
return QStringLiteral ( " vectorcreation" );
57
32
}
58
33
59
- void QgsVectorizeAlgorithm ::initAlgorithm ( const QVariantMap & )
34
+ void QgsVectorizeAlgorithmBase ::initAlgorithm ( const QVariantMap & )
60
35
{
61
36
addParameter ( new QgsProcessingParameterRasterLayer ( QStringLiteral ( " INPUT_RASTER" ),
62
37
QObject::tr ( " Raster layer" ) ) );
@@ -65,10 +40,10 @@ void QgsVectorizeAlgorithm::initAlgorithm( const QVariantMap & )
65
40
addParameter ( new QgsProcessingParameterString ( QStringLiteral ( " FIELD_NAME" ),
66
41
QObject::tr ( " Field name" ), QStringLiteral ( " VALUE" ) ) );
67
42
68
- addParameter ( new QgsProcessingParameterFeatureSink ( QStringLiteral ( " OUTPUT" ), QObject::tr ( " Vectorized layer " ), QgsProcessing::TypeVectorPolygon ) );
43
+ addParameter ( new QgsProcessingParameterFeatureSink ( QStringLiteral ( " OUTPUT" ), outputName ( ), outputType () ) );
69
44
}
70
45
71
- bool QgsVectorizeAlgorithm ::prepareAlgorithm ( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback * )
46
+ bool QgsVectorizeAlgorithmBase ::prepareAlgorithm ( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback * )
72
47
{
73
48
QgsRasterLayer *layer = parameterAsRasterLayer ( parameters, QStringLiteral ( " INPUT_RASTER" ), context );
74
49
@@ -90,14 +65,14 @@ bool QgsVectorizeAlgorithm::prepareAlgorithm( const QVariantMap ¶meters, Qgs
90
65
return true ;
91
66
}
92
67
93
- QVariantMap QgsVectorizeAlgorithm ::processAlgorithm ( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
68
+ QVariantMap QgsVectorizeAlgorithmBase ::processAlgorithm ( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
94
69
{
95
70
const QString fieldName = parameterAsString ( parameters, QStringLiteral ( " FIELD_NAME" ), context );
96
71
QgsFields fields;
97
72
fields.append ( QgsField ( fieldName, QVariant::Double, QString (), 20 , 8 ) );
98
73
99
74
QString dest;
100
- std::unique_ptr< QgsFeatureSink > sink ( parameterAsSink ( parameters, QStringLiteral ( " OUTPUT" ), context, dest, fields, QgsWkbTypes::Polygon , mCrs ) );
75
+ std::unique_ptr< QgsFeatureSink > sink ( parameterAsSink ( parameters, QStringLiteral ( " OUTPUT" ), context, dest, fields, sinkType () , mCrs ) );
101
76
if ( !sink )
102
77
throw QgsProcessingException ( invalidSinkError ( parameters, QStringLiteral ( " OUTPUT" ) ) );
103
78
@@ -112,9 +87,6 @@ QVariantMap QgsVectorizeAlgorithm::processAlgorithm( const QVariantMap ¶mete
112
87
int nbBlocksHeight = static_cast < int >( std::ceil ( 1.0 * mNbCellsYProvider / maxHeight ) );
113
88
int nbBlocks = nbBlocksWidth * nbBlocksHeight;
114
89
115
- double hCellSizeX = mRasterUnitsPerPixelX / 2.0 ;
116
- double hCellSizeY = mRasterUnitsPerPixelY / 2.0 ;
117
-
118
90
int iterLeft = 0 ;
119
91
int iterTop = 0 ;
120
92
int iterCols = 0 ;
@@ -141,9 +113,7 @@ QVariantMap QgsVectorizeAlgorithm::processAlgorithm( const QVariantMap ¶mete
141
113
{
142
114
if ( !rasterBlock->isNoData ( row, column ) )
143
115
{
144
-
145
- QgsGeometry pixelRectGeometry = QgsGeometry::fromRect ( QgsRectangle ( currentX - hCellSizeX, currentY - hCellSizeY, currentX + hCellSizeX, currentY + hCellSizeY ) );
146
-
116
+ QgsGeometry pixelRectGeometry = createGeometryForPixel ( currentX, currentY, mRasterUnitsPerPixelX , mRasterUnitsPerPixelY );
147
117
double value = rasterBlock->value ( row, column );
148
118
149
119
QgsFeature f;
@@ -162,6 +132,121 @@ QVariantMap QgsVectorizeAlgorithm::processAlgorithm( const QVariantMap ¶mete
162
132
return outputs;
163
133
}
164
134
135
+ //
136
+ // QgsRasterPixelsToPolygonsAlgorithm
137
+ //
138
+
139
+ QString QgsRasterPixelsToPolygonsAlgorithm::name () const
140
+ {
141
+ return QStringLiteral ( " pixelstopolygons" );
142
+ }
143
+
144
+ QString QgsRasterPixelsToPolygonsAlgorithm::displayName () const
145
+ {
146
+ return QObject::tr ( " Raster pixels to polygons" );
147
+ }
148
+
149
+ QStringList QgsRasterPixelsToPolygonsAlgorithm::tags () const
150
+ {
151
+ return QObject::tr ( " vectorize,polygonize,raster,convert,pixels" ).split ( ' ,' );
152
+ }
153
+
154
+ QString QgsRasterPixelsToPolygonsAlgorithm::shortHelpString () const
155
+ {
156
+ return QObject::tr ( " This algorithm converts a raster layer to a vector layer, by creating polygon features "
157
+ " for each individual pixel's extent in the raster layer.\n\n "
158
+ " Any nodata pixels are skipped in the output." );
159
+ }
160
+
161
+ QString QgsRasterPixelsToPolygonsAlgorithm::shortDescription () const
162
+ {
163
+ return QObject::tr ( " Creates a vector layer of polygons corresponding to each pixel in a raster layer." );
164
+ }
165
+
166
+ QgsRasterPixelsToPolygonsAlgorithm *QgsRasterPixelsToPolygonsAlgorithm::createInstance () const
167
+ {
168
+ return new QgsRasterPixelsToPolygonsAlgorithm ();
169
+ }
170
+
171
+ QString QgsRasterPixelsToPolygonsAlgorithm::outputName () const
172
+ {
173
+ return QObject::tr ( " Vector polygons" );
174
+ }
175
+
176
+ QgsProcessing::SourceType QgsRasterPixelsToPolygonsAlgorithm::outputType () const
177
+ {
178
+ return QgsProcessing::TypeVectorPolygon;
179
+ }
180
+
181
+ QgsWkbTypes::Type QgsRasterPixelsToPolygonsAlgorithm::sinkType () const
182
+ {
183
+ return QgsWkbTypes::Polygon;
184
+ }
185
+
186
+ QgsGeometry QgsRasterPixelsToPolygonsAlgorithm::createGeometryForPixel ( double centerX, double centerY, double pixelWidthX, double pixelWidthY ) const
187
+ {
188
+ const double hCellSizeX = pixelWidthX / 2.0 ;
189
+ const double hCellSizeY = pixelWidthY / 2.0 ;
190
+ return QgsGeometry::fromRect ( QgsRectangle ( centerX - hCellSizeX, centerY - hCellSizeY, centerX + hCellSizeX, centerY + hCellSizeY ) );
191
+ }
192
+
193
+
194
+ //
195
+ // QgsRasterPixelsToPointsAlgorithm
196
+ //
197
+
198
+ QString QgsRasterPixelsToPointsAlgorithm::name () const
199
+ {
200
+ return QStringLiteral ( " pixelstopoints" );
201
+ }
202
+
203
+ QString QgsRasterPixelsToPointsAlgorithm::displayName () const
204
+ {
205
+ return QObject::tr ( " Raster pixels to points" );
206
+ }
207
+
208
+ QStringList QgsRasterPixelsToPointsAlgorithm::tags () const
209
+ {
210
+ return QObject::tr ( " vectorize,polygonize,raster,convert,pixels,centers" ).split ( ' ,' );
211
+ }
212
+
213
+ QString QgsRasterPixelsToPointsAlgorithm::shortHelpString () const
214
+ {
215
+ return QObject::tr ( " This algorithm converts a raster layer to a vector layer, by creating point features "
216
+ " for each individual pixel's center in the raster layer.\n\n "
217
+ " Any nodata pixels are skipped in the output." );
218
+ }
219
+
220
+ QString QgsRasterPixelsToPointsAlgorithm::shortDescription () const
221
+ {
222
+ return QObject::tr ( " Creates a vector layer of points corresponding to each pixel in a raster layer." );
223
+ }
224
+
225
+ QgsRasterPixelsToPointsAlgorithm *QgsRasterPixelsToPointsAlgorithm::createInstance () const
226
+ {
227
+ return new QgsRasterPixelsToPointsAlgorithm ();
228
+ }
229
+
230
+ QString QgsRasterPixelsToPointsAlgorithm::outputName () const
231
+ {
232
+ return QObject::tr ( " Vector points" );
233
+ }
234
+
235
+ QgsProcessing::SourceType QgsRasterPixelsToPointsAlgorithm::outputType () const
236
+ {
237
+ return QgsProcessing::TypeVectorPoint;
238
+ }
239
+
240
+ QgsWkbTypes::Type QgsRasterPixelsToPointsAlgorithm::sinkType () const
241
+ {
242
+ return QgsWkbTypes::Point;
243
+ }
244
+
245
+ QgsGeometry QgsRasterPixelsToPointsAlgorithm::createGeometryForPixel ( double centerX, double centerY, double , double ) const
246
+ {
247
+ return QgsGeometry ( new QgsPoint ( centerX, centerY ) );
248
+ }
249
+
165
250
// /@endcond
166
251
167
252
0 commit comments