23 QString QgsRasterSamplingAlgorithm::name()
const
25 return QStringLiteral(
"rastersampling" );
28 QString QgsRasterSamplingAlgorithm::displayName()
const
30 return QObject::tr(
"Sample raster values" );
33 QStringList QgsRasterSamplingAlgorithm::tags()
const
35 return QObject::tr(
"extract,point,pixel,value" ).split(
',' );
38 QString QgsRasterSamplingAlgorithm::group()
const
40 return QObject::tr(
"Raster analysis" );
43 QString QgsRasterSamplingAlgorithm::groupId()
const
45 return QStringLiteral(
"rasteranalysis" );
48 QString QgsRasterSamplingAlgorithm::shortDescription()
const
50 return QObject::tr(
"Samples raster values under a set of points." );
53 QString 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." );
59 QgsRasterSamplingAlgorithm *QgsRasterSamplingAlgorithm::createInstance()
const
61 return new QgsRasterSamplingAlgorithm();
64 void 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 QString fieldPrefix = parameterAsString( parameters, QStringLiteral(
"COLUMN_PREFIX" ), context );
95 for (
int band = 1; band <= mBandCount; band++ )
101 newFields.
append(
QgsField( QStringLiteral(
"%1%2" ).arg( fieldPrefix, QString::number( band ) ), intSafe ? QVariant::Int : QVariant::Double ) );
102 emptySampleAttributes += QVariant();
107 std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral(
"OUTPUT" ), context, dest, fields,
108 source->wkbType(), source->sourceCrs() ) );
112 const long count = source->featureCount();
113 const double step = count > 0 ? 100.0 / count : 1;
132 attributes += emptySampleAttributes;
133 outputFeature.setAttributes( attributes );
135 feedback->
reportError( QObject::tr(
"No geometry attached to feature %1." ).arg( feature.
id() ) );
142 attributes += emptySampleAttributes;
143 outputFeature.setAttributes( attributes );
145 feedback->
reportError( QObject::tr(
"Impossible to sample data of multipart feature %1." ).arg( feature.
id() ) );
148 QgsPointXY point( *( geometry.
isMultipart() ? qgsgeometry_cast< const QgsPoint * >( qgsgeometry_cast< const QgsMultiPoint * >( geometry.
constGet() )->geometryN( 0 ) ) :
149 qgsgeometry_cast< const QgsPoint * >( geometry.
constGet() ) ) );
152 point = ct.transform( point );
156 attributes += emptySampleAttributes;
157 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 double value = mDataProvider->sample( point, band, &ok );
167 attributes += ok ? value : QVariant();
169 outputFeature.setAttributes( attributes );
174 outputs.insert( QStringLiteral(
"OUTPUT" ), dest );