QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
qgspointcloudrendererregistry.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgspointcloudrendererregistry.cpp
3 ---------------------
4 begin : November 2020
5 copyright : (C) 2020 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
16
17#include "qgsapplication.h"
20
21#include <QString>
22
23using namespace Qt::StringLiterals;
24
25// default renderers
29#include "qgspointcloudlayer.h"
31
33{
34 // add default renderers
36 QObject::tr( "Extent Only" ),
39 QObject::tr( "Attribute by Ramp" ),
42 QObject::tr( "RGB" ),
44 addRenderer( new QgsPointCloudRendererMetadata( u"classified"_s,
45 QObject::tr( "Classification" ),
47}
48
53
55{
56 if ( !metadata || mRenderers.contains( metadata->name() ) )
57 return false;
58
59 mRenderers[metadata->name()] = metadata;
60 mRenderersOrder << metadata->name();
61 return true;
62}
63
64bool QgsPointCloudRendererRegistry::removeRenderer( const QString &rendererName )
65{
66 if ( !mRenderers.contains( rendererName ) )
67 return false;
68
69 delete mRenderers[rendererName];
70 mRenderers.remove( rendererName );
71 mRenderersOrder.removeAll( rendererName );
72 return true;
73}
74
76{
77 return mRenderers.value( rendererName );
78}
79
81{
82 QStringList renderers;
83 for ( const QString &renderer : mRenderersOrder )
84 {
85 QgsPointCloudRendererAbstractMetadata *r = mRenderers.value( renderer );
86 if ( r )
87 renderers << renderer;
88 }
89 return renderers;
90}
91
93{
94 const QgsPointCloudDataProvider *provider = layer->dataProvider();
95 if ( !provider )
97
98 const QgsPointCloudStatistics stats = layer->statistics();
99
100 if ( ( provider->name() == "pdal"_L1 ) && ( !provider->hasValidIndex() ) )
101 {
102 // for now, default to extent renderer only for las/laz files
103 return new QgsPointCloudExtentRenderer();
104 }
105
106 // If we are calculating statistics, we default to the extent renderer until the statistics calculation finishes
108 {
109 return new QgsPointCloudExtentRenderer();
110 }
111
112 const QgsPointCloudAttributeCollection attributes = provider->attributes();
113
114 //if red/green/blue attributes are present, then default to a RGB renderer
115 if ( attributes.indexOf( "Red"_L1 ) >= 0 && attributes.indexOf( "Green"_L1 ) >= 0 && attributes.indexOf( "Blue"_L1 ) >= 0 )
116 {
117 auto renderer = std::make_unique< QgsPointCloudRgbRenderer >();
118
119 // set initial guess for rgb ranges
120 const double redMax = stats.maximum( u"Red"_s );
121 const double greenMax = stats.maximum( u"Red"_s );
122 const double blueMax = stats.maximum( u"Red"_s );
123 if ( !std::isnan( redMax ) && !std::isnan( greenMax ) && !std::isnan( blueMax ) )
124 {
125 const int maxValue = std::max( blueMax, std::max( redMax, greenMax ) );
126
127 if ( maxValue == 0 )
128 {
129 // r/g/b max value is 0 -- likely these attributes have been created by some process, but are empty.
130 // in any case they won't result in a useful render, so don't use RGB renderer for this dataset.
131 renderer.reset();
132 }
133 else
134 {
135 // try and guess suitable range from input max values -- we don't just take the provider max value directly here, but rather see if it's
136 // likely to be 8 bit or 16 bit color values
137 const int rangeGuess = maxValue > 255 ? 65535 : 255;
138
139 if ( rangeGuess > 255 )
140 {
141 // looks like 16 bit colors, so default to a stretch contrast enhancement
143 contrast.setMinimumValue( 0 );
144 contrast.setMaximumValue( rangeGuess );
146 renderer->setRedContrastEnhancement( new QgsContrastEnhancement( contrast ) );
147 renderer->setGreenContrastEnhancement( new QgsContrastEnhancement( contrast ) );
148 renderer->setBlueContrastEnhancement( new QgsContrastEnhancement( contrast ) );
149 }
150 }
151 }
152 else
153 {
155 contrast.setMinimumValue( std::numeric_limits<uint16_t>::lowest() );
156 contrast.setMaximumValue( std::numeric_limits<uint16_t>::max() );
158 renderer->setRedContrastEnhancement( new QgsContrastEnhancement( contrast ) );
159 renderer->setGreenContrastEnhancement( new QgsContrastEnhancement( contrast ) );
160 renderer->setBlueContrastEnhancement( new QgsContrastEnhancement( contrast ) );
161 }
162
163 if ( renderer )
164 return renderer.release();
165 }
166
167 // otherwise try a classified renderer...
168 if ( attributes.indexOf( "Classification"_L1 ) >= 0 )
169 {
170 // are any classifications present?
171 QList<int> classes = stats.classesOf( u"Classification"_s );
172 // ignore "not classified" classes, and see if any are left...
173 classes.removeAll( 0 );
174 classes.removeAll( 1 );
175 if ( !classes.empty() )
176 {
178 auto renderer = std::make_unique< QgsPointCloudClassifiedRenderer >( "Classification"_L1, categories );
179 return renderer.release();
180 }
181 }
182
183 // fallback to shading by Z
184 auto renderer = std::make_unique< QgsPointCloudAttributeByRampRenderer >();
185 renderer->setAttribute( u"Z"_s );
186
187 // set initial range for z values if possible
188 const double zMin = stats.minimum( u"Z"_s );
189 const double zMax = stats.maximum( u"Z"_s );
190 if ( !std::isnan( zMin ) && !std::isnan( zMax ) )
191 {
192 renderer->setMinimum( zMin );
193 renderer->setMaximum( zMax );
194
195 QgsColorRampShader shader = renderer->colorRampShader();
196 shader.setMinimumValue( zMin );
197 shader.setMaximumValue( zMax );
198 shader.classifyColorRamp( 5, -1, QgsRectangle(), nullptr );
199 renderer->setColorRampShader( shader );
200 }
201 return renderer.release();
202}
203
205{
206 if ( !layer )
208
209 const QgsPointCloudStatistics stats = layer->statistics();
210 const QList<int> layerClasses = stats.classesOf( u"Classification"_s );
212
213 if ( layerClasses.isEmpty() )
214 return defaultCategories;
215
216 QgsPointCloudCategoryList categories;
217 for ( const int &layerClass : layerClasses )
218 {
219 const bool isDefaultCategory = layerClass >= 0 && layerClass < defaultCategories.size();
220 const QColor color = isDefaultCategory ? defaultCategories.at( layerClass ).color() : QgsApplication::colorSchemeRegistry()->fetchRandomStyleColor();
221 const QString label = isDefaultCategory ? QgsPointCloudDataProvider::translatedLasClassificationCodes().value( layerClass, QString::number( layerClass ) ) : QString::number( layerClass );
222 categories.append( QgsPointCloudCategory( layerClass, color, label ) );
223 }
224 return categories;
225}
@ UInt16
Sixteen bit unsigned integer (quint16).
Definition qgis.h:383
@ UnknownDataType
Unknown or unspecified type.
Definition qgis.h:380
static QgsColorSchemeRegistry * colorSchemeRegistry()
Returns the application's color scheme registry, used for managing color schemes.
A ramp shader will color a raster pixel based on a list of values ranges in a ramp.
void classifyColorRamp(int classes=0, int band=-1, const QgsRectangle &extent=QgsRectangle(), QgsRasterInterface *input=nullptr)
Classify color ramp shader.
QColor fetchRandomStyleColor() const
Returns a random color for use with a new symbol style (e.g.
Handles contrast enhancement and clipping.
@ StretchToMinimumMaximum
Linear histogram.
void setMinimumValue(double value, bool generateTable=true)
Sets the minimum value for the contrast enhancement range.
void setContrastEnhancementAlgorithm(ContrastEnhancementAlgorithm algorithm, bool generateTable=true)
Sets the contrast enhancement algorithm.
void setMaximumValue(double value, bool generateTable=true)
Sets the maximum value for the contrast enhancement range.
virtual QString name() const =0
Returns a provider name.
An RGB renderer for 2d visualisation of point clouds using embedded red, green and blue attributes.
static QgsPointCloudRenderer * create(QDomElement &element, const QgsReadWriteContext &context)
Creates an RGB renderer from an XML element.
A collection of point cloud attributes.
int indexOf(const QString &name) const
Returns the index of the attribute with the specified name.
Represents an individual category (class) from a QgsPointCloudClassifiedRenderer.
static QgsPointCloudRenderer * create(QDomElement &element, const QgsReadWriteContext &context)
Creates an RGB renderer from an XML element.
static QgsPointCloudCategoryList defaultCategories()
Returns the default list of categories.
Base class for providing data for QgsPointCloudLayer.
virtual QgsPointCloudAttributeCollection attributes() const =0
Returns the attributes available from this data provider.
static QMap< int, QString > translatedLasClassificationCodes()
Returns the map of LAS classification code to translated string value, corresponding to the ASPRS Sta...
bool hasValidIndex() const
Returns whether provider has index which is valid.
A renderer for 2d visualisation of point clouds which shows the dataset's extents using a fill symbol...
static QgsPointCloudRenderer * create(QDomElement &element, const QgsReadWriteContext &context)
Creates an extent renderer from an XML element.
Represents a map layer supporting display of point clouds.
@ Calculating
The statistics calculation task is running.
PointCloudStatisticsCalculationState statisticsCalculationState() const
Returns the status of point cloud statistics calculation.
QgsPointCloudDataProvider * dataProvider() override
Returns the layer's data provider, it may be nullptr.
const QgsPointCloudStatistics statistics() const
Returns the object containing statistics.
Stores metadata about one point cloud renderer class.
QString name() const
Returns the unique name of the renderer.
Convenience metadata class that uses static functions to create point cloud renderer and its widget.
static QgsPointCloudCategoryList classificationAttributeCategories(const QgsPointCloudLayer *layer)
Returns a list of categories using the available Classification classes of a specified layer,...
QgsPointCloudRendererAbstractMetadata * rendererMetadata(const QString &rendererName)
Returns the metadata for a specified renderer.
static QgsPointCloudRenderer * defaultRenderer(const QgsPointCloudLayer *layer)
Returns a new default point cloud renderer for a specified layer.
bool addRenderer(QgsPointCloudRendererAbstractMetadata *metadata)
Adds a renderer to the registry.
bool removeRenderer(const QString &rendererName)
Removes a renderer from registry.
QStringList renderersList() const
Returns a list of available renderers.
Abstract base class for 2d point cloud renderers.
static QgsPointCloudRenderer * create(QDomElement &element, const QgsReadWriteContext &context)
Creates an RGB renderer from an XML element.
Used to store statistics of a point cloud dataset.
double maximum(const QString &attribute) const
Returns the maximum value for the attribute attribute If no matching statistic is available then NaN ...
QList< int > classesOf(const QString &attribute) const
Returns a list of existing classes which are present for the specified attribute.
double minimum(const QString &attribute) const
Returns the minimum value for the attribute attribute If no matching statistic is available then NaN ...
virtual void setMaximumValue(double value)
Sets the maximum value for the raster shader.
virtual void setMinimumValue(double value)
Sets the minimum value for the raster shader.
A rectangle specified with double values.
QList< QgsPointCloudCategory > QgsPointCloudCategoryList