25QString QgsLineDensityAlgorithm::name()
const
27 return QStringLiteral(
"linedensity" );
30QString QgsLineDensityAlgorithm::displayName()
const
32 return QObject::tr(
"Line density" );
35QStringList QgsLineDensityAlgorithm::tags()
const
37 return QObject::tr(
"density,kernel,line,line density,interpolation,weight" ).split(
',' );
40QString QgsLineDensityAlgorithm::group()
const
42 return QObject::tr(
"Interpolation" );
45QString QgsLineDensityAlgorithm::groupId()
const
47 return QStringLiteral(
"interpolation" );
50void QgsLineDensityAlgorithm::initAlgorithm(
const QVariantMap & )
54 addParameter(
new QgsProcessingParameterDistance( QStringLiteral(
"RADIUS" ), QObject::tr(
"Search radius" ), 10, QStringLiteral(
"INPUT" ),
false, 0 ) );
55 addParameter(
new QgsProcessingParameterDistance( QStringLiteral(
"PIXEL_SIZE" ), QObject::tr(
"Pixel size" ), 10, QStringLiteral(
"INPUT" ),
false ) );
59 auto createOptsParam = std::make_unique<QgsProcessingParameterString>( QStringLiteral(
"CREATE_OPTIONS" ), QObject::tr(
"Creation options" ), QVariant(),
false,
true );
60 createOptsParam->setMetadata( QVariantMap( { { QStringLiteral(
"widget_wrapper" ), QVariantMap( { { QStringLiteral(
"widget_type" ), QStringLiteral(
"rasteroptions" ) } } ) } } ) );
62 addParameter( createOptsParam.release() );
64 auto creationOptsParam = std::make_unique<QgsProcessingParameterString>( QStringLiteral(
"CREATION_OPTIONS" ), QObject::tr(
"Creation options" ), QVariant(),
false,
true );
65 creationOptsParam->setMetadata( QVariantMap( { { QStringLiteral(
"widget_wrapper" ), QVariantMap( { { QStringLiteral(
"widget_type" ), QStringLiteral(
"rasteroptions" ) } } ) } } ) );
67 addParameter( creationOptsParam.release() );
72QString QgsLineDensityAlgorithm::shortHelpString()
const
74 return QObject::tr(
"The line density interpolation algorithm calculates a density measure of linear features "
75 "which is obtained in a circular neighborhood within each raster cell. "
76 "First, the length of the segment of each line that is intersected by the circular neighborhood "
77 "is multiplied with the lines weight factor. In a second step, all length values are summed and "
78 "divided by the area of the circular neighborhood. This process is repeated for all raster cells."
82QgsLineDensityAlgorithm *QgsLineDensityAlgorithm::createInstance()
const
84 return new QgsLineDensityAlgorithm();
90 mSource.reset( parameterAsSource( parameters, QStringLiteral(
"INPUT" ), context ) );
94 mWeightField = parameterAsString( parameters, QStringLiteral(
"WEIGHT" ), context );
96 mPixelSize = parameterAsDouble( parameters, QStringLiteral(
"PIXEL_SIZE" ), context );
98 mSearchRadius = parameterAsDouble( parameters, QStringLiteral(
"RADIUS" ), context );
99 if ( mSearchRadius < 0.5 * mPixelSize * std::sqrt( 2 ) )
100 throw QgsProcessingException( QObject::tr(
"Raster cells must be fully contained by the search circle. Therefore, "
101 "the search radius must not be smaller than half of the pixel diagonal." ) );
103 mExtent = mSource->sourceExtent();
104 mCrs = mSource->sourceCrs();
110 const QgsPoint firstCellMidpoint =
QgsPoint( mExtent.xMinimum() + ( mPixelSize / 2 ), mExtent.yMaximum() - ( mPixelSize / 2 ) );
121 const QStringList weightName = QStringList( mWeightField );
122 const QgsFields attrFields = mSource->fields();
134 if ( !mWeightField.isEmpty() )
136 const double analysisWeight = f.
attribute( mWeightField ).toDouble();
137 mFeatureWeights.insert( f.
id(), analysisWeight );
141 QString creationOptions = parameterAsString( parameters, QStringLiteral(
"CREATION_OPTIONS" ), context ).trimmed();
143 const QString optionsString = parameterAsString( parameters, QStringLiteral(
"CREATE_OPTIONS" ), context );
144 if ( !optionsString.isEmpty() )
145 creationOptions = optionsString;
147 const QString outputFile = parameterAsOutputLayer( parameters, QStringLiteral(
"OUTPUT" ), context );
148 const QFileInfo fi( outputFile );
153 const int rows =
static_cast<int>( 0.5 + mExtent.height() / mPixelSize );
154 const int cols =
static_cast<int>( 0.5 + mExtent.width() / mPixelSize );
158 const QgsRectangle rasterExtent =
QgsRectangle( mExtent.xMinimum(), mExtent.yMaximum() - ( rows * mPixelSize ), mExtent.xMinimum() + ( cols * mPixelSize ), mExtent.yMaximum() );
163 if ( !creationOptions.isEmpty() )
171 if ( !provider->isValid() )
174 provider->setNoDataValue( 1, -9999 );
181 for (
int row = 0; row < rows; row++ )
183 for (
int col = 0; col < cols; col++ )
191 mSearchGeometry.translate( mPixelSize, 0 );
193 const QList<QgsFeatureId> fids = mIndex.intersects( mSearchGeometry.boundingBox() );
195 if ( !fids.isEmpty() )
198 engine->prepareGeometry();
200 double absDensity = 0;
203 const QgsGeometry lineGeom = mIndex.geometry(
id );
205 if ( engine->intersects( lineGeom.
constGet() ) )
207 double analysisLineLength = 0;
210 analysisLineLength = mDa.measureLength(
QgsGeometry( engine->intersection( mIndex.geometry(
id ).constGet() ) ) );
219 if ( !mWeightField.isEmpty() )
221 weight = mFeatureWeights.value(
id );
224 absDensity += ( analysisLineLength * weight );
228 double lineDensity = 0;
229 if ( absDensity > 0 )
232 double analysisSearchGeometryArea = 0;
235 analysisSearchGeometryArea = mDa.measureArea( mSearchGeometry );
242 lineDensity = absDensity / analysisSearchGeometryArea;
244 rasterDataLine->setValue( 0, col, lineDensity );
249 rasterDataLine->setValue( 0, col, 0.0 );
252 feedback->
setProgress(
static_cast<double>( cellcnt ) /
static_cast<double>( totalCellcnt ) * 100 );
255 if ( !provider->writeBlock( rasterDataLine.get(), 1, 0, row ) )
257 throw QgsProcessingException( QObject::tr(
"Could not write raster block: %1" ).arg( provider->error().summary() ) );
261 mSearchGeometry.translate( ( cols - 1 ) * -mPixelSize, -mPixelSize );
265 outputs.insert( QStringLiteral(
"OUTPUT" ), outputFile );
@ VectorLine
Vector line layers.
@ Numeric
Accepts numeric fields.
@ Float32
Thirty two bit floating point (float)
@ Hidden
Parameter is hidden and should not be shown to users.
@ Advanced
Parameter is an advanced parameter which should be hidden from users by default.
Custom exception class for Coordinate Reference System related exceptions.
A general purpose distance and area calculator, capable of performing ellipsoid based calculations.
virtual QgsPolygon * toPolygon(unsigned int segments=36) const
Returns a segmented polygon.
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.
Wraps a request for features to a vector layer (or directly its vector data provider).
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
@ 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...
Q_INVOKABLE QVariant attribute(const QString &name) const
Lookup attribute value by attribute name.
bool isCanceled() const
Tells whether the operation has been canceled already.
void setProgress(double progress)
Sets the current progress for the feedback object.
Container of fields for a vector layer.
A geometry is the spatial representation of a feature.
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
static QgsGeometryEngine * createGeometryEngine(const QgsAbstractGeometry *geometry, double precision=0.0, Qgis::GeosCreationFlags flags=Qgis::GeosCreationFlag::SkipEmptyInteriorRings)
Creates and returns a new geometry engine representing the specified geometry using precision on a gr...
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.
QString ellipsoid() const
Returns the ellipsoid to use for distance and area calculations.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
A double numeric parameter for distance values.
An input feature source (such as vector layers) parameter for processing algorithms.
A vector layer or feature source field parameter for processing algorithms.
A raster layer destination parameter, for specifying the destination path for a raster layer created ...
The raster file writer which allows you to save a raster to a new file.
static QString driverForExtension(const QString &extension)
Returns the GDAL driver name for a specified file extension.
void setOutputProviderKey(const QString &key)
Sets the name of the data provider for the raster output.
void setCreationOptions(const QStringList &options)
Sets a list of data source creation options to use when creating the output raster file.
void setOutputFormat(const QString &format)
Sets the output format.
QgsRasterDataProvider * createOneBandRaster(Qgis::DataType dataType, int width, int height, const QgsRectangle &extent, const QgsCoordinateReferenceSystem &crs) SIP_FACTORY
Create a raster file with one band without initializing the pixel data.
A rectangle specified with double values.
A spatial index for QgsFeature objects.
@ FlagStoreFeatureGeometries
Indicates that the spatial index should also store feature geometries. This requires more memory,...
unsigned long long qgssize
Qgssize is used instead of size_t, because size_t is stdlib type, unknown by SIP, and it would be har...
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features