26 void QgsRasterAnalysisUtils::cellInfoForBBox(
const QgsRectangle &rasterBBox,
const QgsRectangle &featureBBox,
double cellSizeX,
double cellSizeY,
27 int &nCellsX,
int &nCellsY,
int rasterWidth,
int rasterHeight,
QgsRectangle &rasterBlockExtent )
40 int offsetX =
static_cast< int >( std::floor( ( intersectBox.
xMinimum() - rasterBBox.
xMinimum() ) / cellSizeX ) );
41 int offsetY =
static_cast< int >( std::floor( ( rasterBBox.
yMaximum() - intersectBox.
yMaximum() ) / cellSizeY ) );
43 int maxColumn =
static_cast< int >( std::floor( ( intersectBox.
xMaximum() - rasterBBox.
xMinimum() ) / cellSizeX ) ) + 1;
44 int maxRow =
static_cast< int >( std::floor( ( rasterBBox.
yMaximum() - intersectBox.
yMinimum() ) / cellSizeY ) ) + 1;
46 nCellsX = maxColumn - offsetX;
47 nCellsY = maxRow - offsetY;
50 nCellsX = std::min( offsetX + nCellsX, rasterWidth ) - offsetX;
51 nCellsY = std::min( offsetY + nCellsY, rasterHeight ) - offsetY;
54 rasterBBox.
yMaximum() - offsetY * cellSizeY,
55 rasterBBox.
xMinimum() + ( nCellsX + offsetX ) * cellSizeX,
56 rasterBBox.
yMaximum() - ( nCellsY + offsetY ) * cellSizeY );
59 void QgsRasterAnalysisUtils::statisticsFromMiddlePointTest(
QgsRasterInterface *rasterInterface,
int rasterBand,
const QgsGeometry &poly,
int nCellsX,
int nCellsY,
double cellSizeX,
double cellSizeY,
const QgsRectangle &rasterBBox,
const std::function<
void(
double )> &addValue,
bool skipNodata )
66 polyEngine->prepareGeometry();
69 iter.startRasterRead( rasterBand, nCellsX, nCellsY, rasterBBox );
71 std::unique_ptr< QgsRasterBlock > block;
77 bool isNoData =
false;
78 while ( iter.readNextRasterPart( rasterBand, iterCols, iterRows, block, iterLeft, iterTop, &blockExtent ) )
80 double cellCenterY = blockExtent.
yMaximum() - 0.5 * cellSizeY;
82 for (
int row = 0; row < iterRows; ++row )
84 double cellCenterX = blockExtent.
xMinimum() + 0.5 * cellSizeX;
85 for (
int col = 0; col < iterCols; ++col )
87 const double pixelValue = block->valueAndNoData( row, col, isNoData );
88 if ( validPixel( pixelValue ) && ( !skipNodata || !isNoData ) )
90 QgsPoint cellCenter( cellCenterX, cellCenterY );
91 if ( polyEngine->contains( &cellCenter ) )
93 addValue( pixelValue );
96 cellCenterX += cellSizeX;
98 cellCenterY -= cellSizeY;
103 void QgsRasterAnalysisUtils::statisticsFromPreciseIntersection(
QgsRasterInterface *rasterInterface,
int rasterBand,
const QgsGeometry &poly,
int nCellsX,
int nCellsY,
double cellSizeX,
double cellSizeY,
const QgsRectangle &rasterBBox,
const std::function<
void(
double,
double )> &addValue,
bool skipNodata )
107 double hCellSizeX = cellSizeX / 2.0;
108 double hCellSizeY = cellSizeY / 2.0;
109 double pixelArea = cellSizeX * cellSizeY;
117 polyEngine->prepareGeometry();
120 iter.startRasterRead( rasterBand, nCellsX, nCellsY, rasterBBox );
122 std::unique_ptr< QgsRasterBlock > block;
128 bool isNoData =
false;
129 while ( iter.readNextRasterPart( rasterBand, iterCols, iterRows, block, iterLeft, iterTop, &blockExtent ) )
131 double currentY = blockExtent.
yMaximum() - 0.5 * cellSizeY;
132 for (
int row = 0; row < iterRows; ++row )
134 double currentX = blockExtent.
xMinimum() + 0.5 * cellSizeX;
135 for (
int col = 0; col < iterCols; ++col )
137 const double pixelValue = block->valueAndNoData( row, col, isNoData );
138 if ( validPixel( pixelValue ) && ( !skipNodata || !isNoData ) )
143 if ( !pixelRectGeometry.
isNull() && polyEngine->intersects( pixelRectGeometry.
constGet() ) )
147 if ( !intersectGeometry.
isEmpty() )
149 double intersectionArea = intersectGeometry.
area();
150 if ( intersectionArea > 0.0 )
152 weight = intersectionArea / pixelArea;
153 addValue( pixelValue, weight );
158 currentX += cellSizeX;
160 currentY -= cellSizeY;
165 bool QgsRasterAnalysisUtils::validPixel(
double value )
167 return !std::isnan( value );
170 static QVector< QPair< QString, Qgis::DataType > > sDataTypes;
172 void populateDataTypes()
174 if ( sDataTypes.empty() )
176 sDataTypes.append( qMakePair( QStringLiteral(
"Byte" ),
Qgis::Byte ) );
177 sDataTypes.append( qMakePair( QStringLiteral(
"Int16" ),
Qgis::Int16 ) );
178 sDataTypes.append( qMakePair( QStringLiteral(
"UInt16" ),
Qgis::UInt16 ) );
179 sDataTypes.append( qMakePair( QStringLiteral(
"Int32" ),
Qgis::Int32 ) );
180 sDataTypes.append( qMakePair( QStringLiteral(
"UInt32" ),
Qgis::UInt32 ) );
181 sDataTypes.append( qMakePair( QStringLiteral(
"Float32" ),
Qgis::Float32 ) );
182 sDataTypes.append( qMakePair( QStringLiteral(
"Float64" ),
Qgis::Float64 ) );
183 sDataTypes.append( qMakePair( QStringLiteral(
"CInt16" ),
Qgis::CInt16 ) );
184 sDataTypes.append( qMakePair( QStringLiteral(
"CInt32" ),
Qgis::CInt32 ) );
185 sDataTypes.append( qMakePair( QStringLiteral(
"CFloat32" ),
Qgis::CFloat32 ) );
186 sDataTypes.append( qMakePair( QStringLiteral(
"CFloat64" ),
Qgis::CFloat64 ) );
190 std::unique_ptr<QgsProcessingParameterDefinition> QgsRasterAnalysisUtils::createRasterTypeParameter(
const QString &name,
const QString &description,
Qgis::DataType defaultType )
195 int defaultChoice = 0;
197 for (
auto it = sDataTypes.constBegin(); it != sDataTypes.constEnd(); ++it )
199 names.append( it->first );
200 if ( it->second == defaultType )
205 return qgis::make_unique< QgsProcessingParameterEnum >( name, description, names,
false, defaultChoice );
208 Qgis::DataType QgsRasterAnalysisUtils::rasterTypeChoiceToDataType(
int choice )
210 if ( choice < 0 || choice >= sDataTypes.count() )
213 return sDataTypes.value( choice ).second;
A rectangle specified with double values.
Thirty two bit signed integer (qint32)
Iterator for sequentially processing raster cells.
Thirty two bit unsigned integer (quint32)
DataType
Raster data types.
A geometry is the spatial representation of a feature.
Thirty two bit floating point (float)
QgsGeometry intersection(const QgsGeometry &geometry) const
Returns a geometry representing the points shared by this geometry and other.
Sixteen bit signed integer (qint16)
Sixty four bit floating point (double)
static QgsGeometry fromRect(const QgsRectangle &rect)
Creates a new geometry from a QgsRectangle.
bool isEmpty() const
Returns true if the rectangle is empty.
QgsRectangle intersect(const QgsRectangle &rect) const
Returns the intersection with the given rectangle.
bool isEmpty() const
Returns true if the geometry is empty (eg a linestring with no vertices, or a collection with no geom...
Sixteen bit unsigned integer (quint16)
Base class for processing filters like renderers, reprojector, resampler etc.
Point geometry type, with support for z-dimension and m-values.
static QgsGeometryEngine * createGeometryEngine(const QgsAbstractGeometry *geometry)
Creates and returns a new geometry engine.
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
double yMinimum() const
Returns the y minimum value (bottom side of rectangle).
double xMaximum() const
Returns the x maximum value (right side of rectangle).
double xMinimum() const
Returns the x minimum value (left side of rectangle).
double yMaximum() const
Returns the y maximum value (top side of rectangle).
double area() const
Returns the area of the geometry using GEOS.
Eight bit unsigned integer (quint8)