|
| 1 | +/*************************************************************************** |
| 2 | + qgsalgorithmvectorize.cpp |
| 3 | + --------------------- |
| 4 | + begin : June, 2018 |
| 5 | + copyright : (C) 2018 by Nyall Dawson |
| 6 | + email : nyall dot dawson at gmail dot com |
| 7 | + ***************************************************************************/ |
| 8 | + |
| 9 | +/*************************************************************************** |
| 10 | + * * |
| 11 | + * This program is free software; you can redistribute it and/or modify * |
| 12 | + * it under the terms of the GNU General Public License as published by * |
| 13 | + * the Free Software Foundation; either version 2 of the License, or * |
| 14 | + * (at your option) any later version. * |
| 15 | + * * |
| 16 | + ***************************************************************************/ |
| 17 | + |
| 18 | +#include "qgsalgorithmvectorize.h" |
| 19 | +#include "qgis.h" |
| 20 | +#include "qgsprocessing.h" |
| 21 | + |
| 22 | +///@cond PRIVATE |
| 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 |
| 50 | +{ |
| 51 | + return QObject::tr( "Vector creation" ); |
| 52 | +} |
| 53 | + |
| 54 | +QString QgsVectorizeAlgorithm::groupId() const |
| 55 | +{ |
| 56 | + return QStringLiteral( "vectorcreation" ); |
| 57 | +} |
| 58 | + |
| 59 | +void QgsVectorizeAlgorithm::initAlgorithm( const QVariantMap & ) |
| 60 | +{ |
| 61 | + addParameter( new QgsProcessingParameterRasterLayer( QStringLiteral( "INPUT_RASTER" ), |
| 62 | + QObject::tr( "Raster layer" ) ) ); |
| 63 | + addParameter( new QgsProcessingParameterBand( QStringLiteral( "RASTER_BAND" ), |
| 64 | + QObject::tr( "Band number" ), 1, QStringLiteral( "INPUT_RASTER" ) ) ); |
| 65 | + addParameter( new QgsProcessingParameterString( QStringLiteral( "FIELD_NAME" ), |
| 66 | + QObject::tr( "Field name" ), QStringLiteral( "VALUE" ) ) ); |
| 67 | + |
| 68 | + addParameter( new QgsProcessingParameterFeatureSink( QStringLiteral( "OUTPUT" ), QObject::tr( "Vectorized layer" ), QgsProcessing::TypeVectorPolygon ) ); |
| 69 | +} |
| 70 | + |
| 71 | +bool QgsVectorizeAlgorithm::prepareAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback * ) |
| 72 | +{ |
| 73 | + QgsRasterLayer *layer = parameterAsRasterLayer( parameters, QStringLiteral( "INPUT_RASTER" ), context ); |
| 74 | + |
| 75 | + if ( !layer ) |
| 76 | + throw QgsProcessingException( invalidRasterError( parameters, QStringLiteral( "INPUT_RASTER" ) ) ); |
| 77 | + |
| 78 | + mBand = parameterAsInt( parameters, QStringLiteral( "RASTER_BAND" ), context ); |
| 79 | + if ( mBand < 1 || mBand > layer->bandCount() ) |
| 80 | + throw QgsProcessingException( QObject::tr( "Invalid band number for RASTER_BAND (%1): Valid values for input raster are 1 to %2" ).arg( mBand ) |
| 81 | + .arg( layer->bandCount() ) ); |
| 82 | + |
| 83 | + mInterface.reset( layer->dataProvider()->clone() ); |
| 84 | + mExtent = layer->extent(); |
| 85 | + mCrs = layer->crs(); |
| 86 | + mRasterUnitsPerPixelX = std::abs( layer->rasterUnitsPerPixelX() ); |
| 87 | + mRasterUnitsPerPixelY = std::abs( layer->rasterUnitsPerPixelY() ); |
| 88 | + mNbCellsXProvider = mInterface->xSize(); |
| 89 | + mNbCellsYProvider = mInterface->ySize(); |
| 90 | + return true; |
| 91 | +} |
| 92 | + |
| 93 | +QVariantMap QgsVectorizeAlgorithm::processAlgorithm( const QVariantMap ¶meters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) |
| 94 | +{ |
| 95 | + const QString fieldName = parameterAsString( parameters, QStringLiteral( "FIELD_NAME" ), context ); |
| 96 | + QgsFields fields; |
| 97 | + fields.append( QgsField( fieldName, QVariant::Double, QString(), 20, 8 ) ); |
| 98 | + |
| 99 | + QString dest; |
| 100 | + std::unique_ptr< QgsFeatureSink > sink( parameterAsSink( parameters, QStringLiteral( "OUTPUT" ), context, dest, fields, QgsWkbTypes::Polygon, mCrs ) ); |
| 101 | + if ( !sink ) |
| 102 | + throw QgsProcessingException( invalidSinkError( parameters, QStringLiteral( "OUTPUT" ) ) ); |
| 103 | + |
| 104 | + |
| 105 | + int maxWidth = QgsRasterIterator::DEFAULT_MAXIMUM_TILE_WIDTH; |
| 106 | + int maxHeight = QgsRasterIterator::DEFAULT_MAXIMUM_TILE_HEIGHT; |
| 107 | + |
| 108 | + QgsRasterIterator iter( mInterface.get() ); |
| 109 | + iter.startRasterRead( mBand, mNbCellsXProvider, mNbCellsYProvider, mExtent ); |
| 110 | + |
| 111 | + int nbBlocksWidth = static_cast< int >( std::ceil( 1.0 * mNbCellsXProvider / maxWidth ) ); |
| 112 | + int nbBlocksHeight = static_cast< int >( std::ceil( 1.0 * mNbCellsYProvider / maxHeight ) ); |
| 113 | + int nbBlocks = nbBlocksWidth * nbBlocksHeight; |
| 114 | + |
| 115 | + double hCellSizeX = mRasterUnitsPerPixelX / 2.0; |
| 116 | + double hCellSizeY = mRasterUnitsPerPixelY / 2.0; |
| 117 | + |
| 118 | + int iterLeft = 0; |
| 119 | + int iterTop = 0; |
| 120 | + int iterCols = 0; |
| 121 | + int iterRows = 0; |
| 122 | + std::unique_ptr< QgsRasterBlock > rasterBlock; |
| 123 | + QgsRectangle blockExtent; |
| 124 | + while ( iter.readNextRasterPart( mBand, iterCols, iterRows, rasterBlock, iterLeft, iterTop, &blockExtent ) ) |
| 125 | + { |
| 126 | + if ( feedback ) |
| 127 | + feedback->setProgress( 100 * ( ( iterTop / maxHeight * nbBlocksWidth ) + iterLeft / maxWidth ) / nbBlocks ); |
| 128 | + if ( feedback && feedback->isCanceled() ) |
| 129 | + break; |
| 130 | + |
| 131 | + double currentY = blockExtent.yMaximum() - 0.5 * mRasterUnitsPerPixelY; |
| 132 | + |
| 133 | + for ( int row = 0; row < iterRows; row++ ) |
| 134 | + { |
| 135 | + if ( feedback && feedback->isCanceled() ) |
| 136 | + break; |
| 137 | + |
| 138 | + double currentX = blockExtent.xMinimum() + 0.5 * mRasterUnitsPerPixelX; |
| 139 | + |
| 140 | + for ( int column = 0; column < iterCols; column++ ) |
| 141 | + { |
| 142 | + if ( !rasterBlock->isNoData( row, column ) ) |
| 143 | + { |
| 144 | + |
| 145 | + QgsGeometry pixelRectGeometry = QgsGeometry::fromRect( QgsRectangle( currentX - hCellSizeX, currentY - hCellSizeY, currentX + hCellSizeX, currentY + hCellSizeY ) ); |
| 146 | + |
| 147 | + double value = rasterBlock->value( row, column ); |
| 148 | + |
| 149 | + QgsFeature f; |
| 150 | + f.setGeometry( pixelRectGeometry ); |
| 151 | + f.setAttributes( QgsAttributes() << value ); |
| 152 | + sink->addFeature( f, QgsFeatureSink::FastInsert ); |
| 153 | + } |
| 154 | + currentX += mRasterUnitsPerPixelX; |
| 155 | + } |
| 156 | + currentY -= mRasterUnitsPerPixelY; |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + QVariantMap outputs; |
| 161 | + outputs.insert( QStringLiteral( "OUTPUT" ), dest ); |
| 162 | + return outputs; |
| 163 | +} |
| 164 | + |
| 165 | +///@endcond |
| 166 | + |
| 167 | + |
0 commit comments