23QString QgsRasterSamplingAlgorithm::name()
const
25 return QStringLiteral(
"rastersampling" );
28QString QgsRasterSamplingAlgorithm::displayName()
const
30 return QObject::tr(
"Sample raster values" );
33QStringList QgsRasterSamplingAlgorithm::tags()
const
35 return QObject::tr(
"extract,point,pixel,value" ).split(
',' );
38QString QgsRasterSamplingAlgorithm::group()
const
40 return QObject::tr(
"Raster analysis" );
43QString QgsRasterSamplingAlgorithm::groupId()
const
45 return QStringLiteral(
"rasteranalysis" );
48QString QgsRasterSamplingAlgorithm::shortDescription()
const
50 return QObject::tr(
"Samples raster values under a set of points." );
53QString QgsRasterSamplingAlgorithm::shortHelpString()
const
55 return QObject::tr(
"This algorithm creates a new vector layer with the same attributes of the input layer and the raster values corresponding on the point location.\n\n"
56 "If the raster layer has more than one band, all the band values are sampled." );
59QgsRasterSamplingAlgorithm *QgsRasterSamplingAlgorithm::createInstance()
const
61 return new QgsRasterSamplingAlgorithm();
64void QgsRasterSamplingAlgorithm::initAlgorithm(
const QVariantMap & )
69 addParameter(
new QgsProcessingParameterString( QStringLiteral(
"COLUMN_PREFIX" ), QObject::tr(
"Output column prefix" ), QStringLiteral(
"SAMPLE_" ),
false,
true ) );
75 QgsRasterLayer *layer = parameterAsRasterLayer( parameters, QStringLiteral(
"RASTERCOPY" ), context );
88 std::unique_ptr<QgsFeatureSource> source( parameterAsSource( parameters, QStringLiteral(
"INPUT" ), context ) );
92 const QString fieldPrefix = parameterAsString( parameters, QStringLiteral(
"COLUMN_PREFIX" ), context );
95 for (
int band = 1; band <= mBandCount; band++ )
100 newFields.
append(
QgsField( QStringLiteral(
"%1%2" ).arg( fieldPrefix, QString::number( band ) ), intSafe ? QMetaType::Type::Int : QMetaType::Type::Double ) );
101 emptySampleAttributes += QVariant();
106 std::unique_ptr<QgsFeatureSink> sink( parameterAsSink( parameters, QStringLiteral(
"OUTPUT" ), context, dest, fields, source->wkbType(), source->sourceCrs() ) );
110 const long count = source->featureCount();
111 const double step = count > 0 ? 100.0 / count : 1;
130 attributes += emptySampleAttributes;
131 outputFeature.setAttributes( attributes );
134 feedback->
reportError( QObject::tr(
"No geometry attached to feature %1." ).arg( feature.
id() ) );
141 attributes += emptySampleAttributes;
142 outputFeature.setAttributes( attributes );
145 feedback->
reportError( QObject::tr(
"Impossible to sample data of multipart feature %1." ).arg( feature.
id() ) );
151 point = ct.transform( point );
155 attributes += emptySampleAttributes;
156 outputFeature.setAttributes( attributes );
159 feedback->
reportError( QObject::tr(
"Could not reproject feature %1 to raster CRS." ).arg( feature.
id() ) );
163 for (
int band = 1; band <= mBandCount; band++ )
166 const double value = mDataProvider->sample( point, band, &ok );
167 attributes += ok ? value : QVariant();
169 outputFeature.setAttributes( attributes );
177 outputs.insert( QStringLiteral(
"OUTPUT" ), dest );
@ VectorPoint
Vector point layers.
DataType
Raster data types.
@ Int16
Sixteen bit signed integer (qint16)
@ UInt16
Sixteen bit unsigned integer (quint16)
@ Byte
Eight bit unsigned integer (quint8)
@ Int32
Thirty two bit signed integer (qint32)
@ UInt32
Thirty two bit unsigned integer (quint32)
virtual int partCount() const =0
Returns count of parts contained in the geometry.
Defines a QGIS exception class.
Wrapper for iterator of features from vector data provider or vector layer.
bool nextFeature(QgsFeature &f)
Fetch next feature and stores in f, returns true on success.
This class wraps a request for features to a vector layer (or directly its vector data provider).
@ 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...
bool hasGeometry() const
Returns true if the feature has an associated 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.
QgsAbstractGeometry * get()
Returns a modifiable (non-const) reference to the underlying abstract geometry primitive.
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
bool isMultipart() const
Returns true if WKB of the geometry is of WKBMulti* type.
QgsCoordinateReferenceSystem crs
A class to represent a 2D point.
Point geometry type, with support for z-dimension and m-values.
Contains information about the context in which a processing algorithm is executed.
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
virtual void reportError(const QString &error, bool fatalError=false)
Reports that the algorithm encountered an error while executing.
A feature sink output for processing algorithms.
An input feature source (such as vector layers) parameter for processing algorithms.
A raster layer parameter for processing algorithms.
A string parameter for processing algorithms.
static QgsFields combineFields(const QgsFields &fieldsA, const QgsFields &fieldsB, const QString &fieldsBPrefix=QString())
Combines two field lists, avoiding duplicate field names (in a case-insensitive manner).
Base class for raster data providers.
QgsRasterDataProvider * clone() const override=0
Clone itself, create deep copy.
Represents a raster layer.
int bandCount() const
Returns the number of bands in this layer.
QgsRasterDataProvider * dataProvider() override
Returns the source data provider.
T qgsgeometry_cast(const QgsAbstractGeometry *geom)