QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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
35 addRenderer( new QgsPointCloudRendererMetadata( u"extent"_s, QObject::tr( "Extent Only" ), QgsPointCloudExtentRenderer::create ) );
36 addRenderer( new QgsPointCloudRendererMetadata( u"ramp"_s, QObject::tr( "Attribute by Ramp" ), QgsPointCloudAttributeByRampRenderer::create ) );
37 addRenderer( new QgsPointCloudRendererMetadata( u"rgb"_s, QObject::tr( "RGB" ), QgsPointCloudRgbRenderer::create ) );
38 addRenderer( new QgsPointCloudRendererMetadata( u"classified"_s, QObject::tr( "Classification" ), QgsPointCloudClassifiedRenderer::create ) );
39}
40
45
47{
48 if ( !metadata || mRenderers.contains( metadata->name() ) )
49 return false;
50
51 mRenderers[metadata->name()] = metadata;
52 mRenderersOrder << metadata->name();
53 return true;
54}
55
56bool QgsPointCloudRendererRegistry::removeRenderer( const QString &rendererName )
57{
58 if ( !mRenderers.contains( rendererName ) )
59 return false;
60
61 delete mRenderers[rendererName];
62 mRenderers.remove( rendererName );
63 mRenderersOrder.removeAll( rendererName );
64 return true;
65}
66
68{
69 return mRenderers.value( rendererName );
70}
71
73{
74 QStringList renderers;
75 for ( const QString &renderer : mRenderersOrder )
76 {
77 QgsPointCloudRendererAbstractMetadata *r = mRenderers.value( renderer );
78 if ( r )
79 renderers << renderer;
80 }
81 return renderers;
82}
83
85{
86 const QgsPointCloudDataProvider *provider = layer->dataProvider();
87 if ( !provider )
89
90 const QgsPointCloudStatistics stats = layer->statistics();
91
92 if ( ( provider->name() == "pdal"_L1 ) && ( !provider->hasValidIndex() ) )
93 {
94 // for now, default to extent renderer only for las/laz files
95 return new QgsPointCloudExtentRenderer();
96 }
97
98 // If we are calculating statistics, we default to the extent renderer until the statistics calculation finishes
100 {
101 return new QgsPointCloudExtentRenderer();
102 }
103
104 const QgsPointCloudAttributeCollection attributes = provider->attributes();
105
106 //if red/green/blue attributes are present, then default to a RGB renderer
107 if ( attributes.indexOf( "Red"_L1 ) >= 0 && attributes.indexOf( "Green"_L1 ) >= 0 && attributes.indexOf( "Blue"_L1 ) >= 0 )
108 {
109 auto renderer = std::make_unique< QgsPointCloudRgbRenderer >();
110
111 // set initial guess for rgb ranges
112 const double redMax = stats.maximum( u"Red"_s );
113 const double greenMax = stats.maximum( u"Red"_s );
114 const double blueMax = stats.maximum( u"Red"_s );
115 if ( !std::isnan( redMax ) && !std::isnan( greenMax ) && !std::isnan( blueMax ) )
116 {
117 const int maxValue = std::max( blueMax, std::max( redMax, greenMax ) );
118
119 if ( maxValue == 0 )
120 {
121 // r/g/b max value is 0 -- likely these attributes have been created by some process, but are empty.
122 // in any case they won't result in a useful render, so don't use RGB renderer for this dataset.
123 renderer.reset();
124 }
125 else
126 {
127 // 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
128 // likely to be 8 bit or 16 bit color values
129 const int rangeGuess = maxValue > 255 ? 65535 : 255;
130
131 if ( rangeGuess > 255 )
132 {
133 // looks like 16 bit colors, so default to a stretch contrast enhancement
135 contrast.setMinimumValue( 0 );
136 contrast.setMaximumValue( rangeGuess );
138 renderer->setRedContrastEnhancement( new QgsContrastEnhancement( contrast ) );
139 renderer->setGreenContrastEnhancement( new QgsContrastEnhancement( contrast ) );
140 renderer->setBlueContrastEnhancement( new QgsContrastEnhancement( contrast ) );
141 }
142 }
143 }
144 else
145 {
147 contrast.setMinimumValue( std::numeric_limits<uint16_t>::lowest() );
148 contrast.setMaximumValue( std::numeric_limits<uint16_t>::max() );
150 renderer->setRedContrastEnhancement( new QgsContrastEnhancement( contrast ) );
151 renderer->setGreenContrastEnhancement( new QgsContrastEnhancement( contrast ) );
152 renderer->setBlueContrastEnhancement( new QgsContrastEnhancement( contrast ) );
153 }
154
155 if ( renderer )
156 return renderer.release();
157 }
158
159 // otherwise try a classified renderer...
160 if ( attributes.indexOf( "Classification"_L1 ) >= 0 )
161 {
162 // are any classifications present?
163 QList<int> classes = stats.classesOf( u"Classification"_s );
164 // ignore "not classified" classes, and see if any are left...
165 classes.removeAll( 0 );
166 classes.removeAll( 1 );
167 if ( !classes.empty() )
168 {
170 auto renderer = std::make_unique< QgsPointCloudClassifiedRenderer >( "Classification"_L1, categories );
171 return renderer.release();
172 }
173 }
174
175 // fallback to shading by Z
176 auto renderer = std::make_unique< QgsPointCloudAttributeByRampRenderer >();
177 renderer->setAttribute( u"Z"_s );
178
179 // set initial range for z values if possible
180 const double zMin = stats.minimum( u"Z"_s );
181 const double zMax = stats.maximum( u"Z"_s );
182 if ( !std::isnan( zMin ) && !std::isnan( zMax ) )
183 {
184 renderer->setMinimum( zMin );
185 renderer->setMaximum( zMax );
186
187 QgsColorRampShader shader = renderer->colorRampShader();
188 shader.setMinimumValue( zMin );
189 shader.setMaximumValue( zMax );
190 shader.classifyColorRamp( 5, -1, QgsRectangle(), nullptr );
191 renderer->setColorRampShader( shader );
192 }
193 return renderer.release();
194}
195
197{
198 if ( !layer )
200
201 const QgsPointCloudStatistics stats = layer->statistics();
202 const QList<int> layerClasses = stats.classesOf( u"Classification"_s );
204
205 if ( layerClasses.isEmpty() )
206 return defaultCategories;
207
208 QgsPointCloudCategoryList categories;
209 for ( const int &layerClass : layerClasses )
210 {
211 const bool isDefaultCategory = layerClass >= 0 && layerClass < defaultCategories.size();
212 const QColor color = isDefaultCategory ? defaultCategories.at( layerClass ).color() : QgsApplication::colorSchemeRegistry()->fetchRandomStyleColor();
213 const QString label = isDefaultCategory ? QgsPointCloudDataProvider::translatedLasClassificationCodes().value( layerClass, QString::number( layerClass ) ) : QString::number( layerClass );
214 categories.append( QgsPointCloudCategory( layerClass, color, label ) );
215 }
216 return categories;
217}
@ UInt16
Sixteen bit unsigned integer (quint16).
Definition qgis.h:397
@ UnknownDataType
Unknown or unspecified type.
Definition qgis.h:394
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