25using namespace Qt::StringLiterals;
29QString QgsVectorizeAlgorithmBase::group()
const
31 return QObject::tr(
"Vector creation" );
34QString QgsVectorizeAlgorithmBase::groupId()
const
36 return u
"vectorcreation"_s;
39void QgsVectorizeAlgorithmBase::initAlgorithm(
const QVariantMap & )
50 QgsRasterLayer *layer = parameterAsRasterLayer( parameters, u
"INPUT_RASTER"_s, context );
55 mBand = parameterAsInt( parameters, u
"RASTER_BAND"_s, context );
56 if ( mBand < 1 || mBand > layer->
bandCount() )
57 throw QgsProcessingException( QObject::tr(
"Invalid band number for RASTER_BAND (%1): Valid values for input raster are 1 to %2" ).arg( mBand ).arg( layer->
bandCount() ) );
64 mNbCellsXProvider = mInterface->xSize();
65 mNbCellsYProvider = mInterface->ySize();
71 const QString fieldName = parameterAsString( parameters, u
"FIELD_NAME"_s, context );
73 fields.
append(
QgsField( fieldName, QMetaType::Type::Double, QString(), 20, 8 ) );
76 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, u
"OUTPUT"_s, context, dest, fields, sinkType(), mCrs ) );
81 iter.startRasterRead( mBand, mNbCellsXProvider, mNbCellsYProvider, mExtent );
87 std::unique_ptr<QgsRasterBlock> rasterBlock;
89 bool isNoData =
false;
90 while ( iter.readNextRasterPart( mBand, iterCols, iterRows, rasterBlock, iterLeft, iterTop, &blockExtent ) )
93 feedback->
setProgress( 100 * iter.progress( mBand ) );
97 double currentY = blockExtent.
yMaximum() - 0.5 * mRasterUnitsPerPixelY;
99 for (
int row = 0; row < iterRows; row++ )
104 double currentX = blockExtent.
xMinimum() + 0.5 * mRasterUnitsPerPixelX;
106 for (
int column = 0; column < iterCols; column++ )
108 const double value = rasterBlock->valueAndNoData( row, column, isNoData );
111 const QgsGeometry pixelRectGeometry = createGeometryForPixel( currentX, currentY, mRasterUnitsPerPixelX, mRasterUnitsPerPixelY );
119 currentX += mRasterUnitsPerPixelX;
121 currentY -= mRasterUnitsPerPixelY;
128 outputs.insert( u
"OUTPUT"_s, dest );
136QString QgsRasterPixelsToPolygonsAlgorithm::name()
const
138 return u
"pixelstopolygons"_s;
141QString QgsRasterPixelsToPolygonsAlgorithm::displayName()
const
143 return QObject::tr(
"Raster pixels to polygons" );
146QStringList QgsRasterPixelsToPolygonsAlgorithm::tags()
const
148 return QObject::tr(
"vectorize,polygonize,raster,convert,pixels" ).split(
',' );
151QString QgsRasterPixelsToPolygonsAlgorithm::shortHelpString()
const
153 return QObject::tr(
"This algorithm converts a raster layer to a vector layer, by creating polygon features "
154 "for each individual pixel's extent in the raster layer.\n\n"
155 "Any NoData pixels are skipped in the output." );
158QString QgsRasterPixelsToPolygonsAlgorithm::shortDescription()
const
160 return QObject::tr(
"Creates a vector layer of polygons corresponding to each pixel in a raster layer." );
163QgsRasterPixelsToPolygonsAlgorithm *QgsRasterPixelsToPolygonsAlgorithm::createInstance()
const
165 return new QgsRasterPixelsToPolygonsAlgorithm();
168QString QgsRasterPixelsToPolygonsAlgorithm::outputName()
const
170 return QObject::tr(
"Vector polygons" );
178Qgis::WkbType QgsRasterPixelsToPolygonsAlgorithm::sinkType()
const
183QgsGeometry QgsRasterPixelsToPolygonsAlgorithm::createGeometryForPixel(
double centerX,
double centerY,
double pixelWidthX,
double pixelWidthY )
const
185 const double hCellSizeX = pixelWidthX / 2.0;
186 const double hCellSizeY = pixelWidthY / 2.0;
195QString QgsRasterPixelsToPointsAlgorithm::name()
const
197 return u
"pixelstopoints"_s;
200QString QgsRasterPixelsToPointsAlgorithm::displayName()
const
202 return QObject::tr(
"Raster pixels to points" );
205QStringList QgsRasterPixelsToPointsAlgorithm::tags()
const
207 return QObject::tr(
"vectorize,polygonize,raster,convert,pixels,centers" ).split(
',' );
210QString QgsRasterPixelsToPointsAlgorithm::shortHelpString()
const
212 return QObject::tr(
"This algorithm converts a raster layer to a vector layer, by creating point features "
213 "for each individual pixel's center in the raster layer.\n\n"
214 "Any NoData pixels are skipped in the output." );
217QString QgsRasterPixelsToPointsAlgorithm::shortDescription()
const
219 return QObject::tr(
"Creates a vector layer of points corresponding to each pixel in a raster layer." );
222QgsRasterPixelsToPointsAlgorithm *QgsRasterPixelsToPointsAlgorithm::createInstance()
const
224 return new QgsRasterPixelsToPointsAlgorithm();
227QString QgsRasterPixelsToPointsAlgorithm::outputName()
const
229 return QObject::tr(
"Vector points" );
237Qgis::WkbType QgsRasterPixelsToPointsAlgorithm::sinkType()
const
242QgsGeometry QgsRasterPixelsToPointsAlgorithm::createGeometryForPixel(
double centerX,
double centerY,
double,
double )
const
ProcessingSourceType
Processing data source types.
@ VectorPoint
Vector point layers.
@ VectorPolygon
Vector polygon layers.
WkbType
The WKB type describes the number of dimensions a geometry has.
@ FastInsert
Use faster inserts, at the cost of updating the passed features to reflect changes made at the provid...
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
void setGeometry(const QgsGeometry &geometry)
Set the feature's geometry.
bool isCanceled() const
Tells whether the operation has been canceled already.
void setProgress(double progress)
Sets the current progress for the feedback object.
Encapsulate a field in an attribute table or data source.
Container of fields for a vector layer.
bool append(const QgsField &field, Qgis::FieldOrigin origin=Qgis::FieldOrigin::Provider, int originIndex=-1)
Appends a field.
A geometry is the spatial representation of a feature.
static QgsGeometry fromRect(const QgsRectangle &rect)
Creates a new geometry from a QgsRectangle.
virtual QgsRectangle extent() const
Returns the extent of the layer.
QgsCoordinateReferenceSystem crs
Point geometry type, with support for z-dimension and m-values.
Contains information about the context in which a processing algorithm is executed.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
A raster band parameter for Processing algorithms.
A feature sink output for processing algorithms.
A raster layer parameter for processing algorithms.
A string parameter for processing algorithms.
QgsRasterDataProvider * clone() const override=0
Clone itself, create deep copy.
Iterator for sequentially processing raster cells.
Represents a raster layer.
int bandCount() const
Returns the number of bands in this layer.
double rasterUnitsPerPixelX() const
Returns the number of raster units per each raster pixel in X axis.
QgsRasterDataProvider * dataProvider() override
Returns the source data provider.
double rasterUnitsPerPixelY() const
Returns the number of raster units per each raster pixel in Y axis.
A rectangle specified with double values.