QGIS API Documentation 3.34.0-Prizren (ffbdd678812)
Loading...
Searching...
No Matches
qgspointcloudclassifiedrenderer.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgspointcloudclassifiedrenderer.h
3 --------------------
4 begin : October 2020
5 copyright : (C) 2020 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
19#include "qgspointcloudblock.h"
20#include "qgsstyle.h"
21#include "qgscolorramp.h"
22#include "qgssymbollayerutils.h"
25
26QgsPointCloudCategory::QgsPointCloudCategory( const int value, const QColor &color, const QString &label, bool render )
27 : mValue( value )
28 , mColor( color )
29 , mLabel( label )
30 , mRender( render )
31{
32}
33
35{
36 return mValue == other.value() &&
37 mColor == other.color() &&
38 mLabel == other.label() &&
39 mRender == other.renderState();
40}
41
42//
43// QgsPointCloudClassifiedRenderer
44//
45
47 : mAttribute( attributeName )
48 , mCategories( categories )
49{
50}
51
53{
54 return QStringLiteral( "classified" );
55}
56
58{
59 std::unique_ptr< QgsPointCloudClassifiedRenderer > res = std::make_unique< QgsPointCloudClassifiedRenderer >();
60 res->mAttribute = mAttribute;
61 res->mCategories = mCategories;
62
63 copyCommonProperties( res.get() );
64
65 return res.release();
66}
67
69{
70 const QgsRectangle visibleExtent = context.renderContext().extent();
71
72 const char *ptr = block->data();
73 int count = block->pointCount();
74 const QgsPointCloudAttributeCollection request = block->attributes();
75
76 const std::size_t recordSize = request.pointRecordSize();
77 int attributeOffset = 0;
78 const QgsPointCloudAttribute *attribute = request.find( mAttribute, attributeOffset );
79 if ( !attribute )
80 return;
81 const QgsPointCloudAttribute::DataType attributeType = attribute->type();
82
83 const bool renderElevation = context.renderContext().elevationMap();
84 const QgsDoubleRange zRange = context.renderContext().zRange();
85 const bool considerZ = !zRange.isInfinite() || renderElevation;
86
87 int rendered = 0;
88 double x = 0;
89 double y = 0;
90 double z = 0;
92 const bool reproject = ct.isValid();
93
94 QHash< int, QColor > colors;
95 for ( const QgsPointCloudCategory &category : std::as_const( mCategories ) )
96 {
97 if ( !category.renderState() )
98 continue;
99
100 colors.insert( category.value(), category.color() );
101 }
102
103 for ( int i = 0; i < count; ++i )
104 {
105 if ( context.renderContext().renderingStopped() )
106 {
107 break;
108 }
109
110 // z value filtering is cheapest, if we're doing it...
111 if ( considerZ )
112 {
113 z = pointZ( context, ptr, i );
114 if ( !zRange.contains( z ) )
115 continue;
116 }
117
118 int attributeValue = 0;
119 context.getAttribute( ptr, i * recordSize + attributeOffset, attributeType, attributeValue );
120 const QColor color = colors.value( attributeValue );
121 if ( !color.isValid() )
122 continue;
123
124 pointXY( context, ptr, i, x, y );
125 if ( visibleExtent.contains( x, y ) )
126 {
127 if ( reproject )
128 {
129 try
130 {
131 ct.transformInPlace( x, y, z );
132 }
133 catch ( QgsCsException & )
134 {
135 continue;
136 }
137 }
138
139 drawPoint( x, y, color, context );
140 if ( renderElevation )
141 drawPointToElevationMap( x, y, z, context );
142 rendered++;
143 }
144 }
145 context.incrementPointsRendered( rendered );
146}
147
148bool QgsPointCloudClassifiedRenderer::willRenderPoint( const QVariantMap &pointAttributes )
149{
150 if ( !pointAttributes.contains( mAttribute ) )
151 return false;
152 bool parsedCorrectly;
153 int attributeInt = pointAttributes[ mAttribute ].toInt( &parsedCorrectly );
154 if ( !parsedCorrectly )
155 return false;
156 for ( const QgsPointCloudCategory &category : std::as_const( mCategories ) )
157 {
158 if ( category.value() == attributeInt )
159 return category.renderState();
160 }
161 return false;
162}
163
165{
166 std::unique_ptr< QgsPointCloudClassifiedRenderer > r = std::make_unique< QgsPointCloudClassifiedRenderer >();
167
168 r->setAttribute( element.attribute( QStringLiteral( "attribute" ), QStringLiteral( "Classification" ) ) );
169
171 const QDomElement catsElem = element.firstChildElement( QStringLiteral( "categories" ) );
172 if ( !catsElem.isNull() )
173 {
174 QDomElement catElem = catsElem.firstChildElement();
175 while ( !catElem.isNull() )
176 {
177 if ( catElem.tagName() == QLatin1String( "category" ) )
178 {
179 const int value = catElem.attribute( QStringLiteral( "value" ) ).toInt();
180 const QString label = catElem.attribute( QStringLiteral( "label" ) );
181 const bool render = catElem.attribute( QStringLiteral( "render" ) ) != QLatin1String( "false" );
182 const QColor color = QgsSymbolLayerUtils::decodeColor( catElem.attribute( QStringLiteral( "color" ) ) );
183 categories.append( QgsPointCloudCategory( value, color, label, render ) );
184 }
185 catElem = catElem.nextSiblingElement();
186 }
187 r->setCategories( categories );
188 }
189
190 r->restoreCommonProperties( element, context );
191
192 return r.release();
193}
194
196{
216}
217
218QDomElement QgsPointCloudClassifiedRenderer::save( QDomDocument &doc, const QgsReadWriteContext &context ) const
219{
220 QDomElement rendererElem = doc.createElement( QStringLiteral( "renderer" ) );
221
222 rendererElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "classified" ) );
223 rendererElem.setAttribute( QStringLiteral( "attribute" ), mAttribute );
224
225 // categories
226 QDomElement catsElem = doc.createElement( QStringLiteral( "categories" ) );
227 for ( const QgsPointCloudCategory &category : mCategories )
228 {
229 QDomElement catElem = doc.createElement( QStringLiteral( "category" ) );
230 catElem.setAttribute( QStringLiteral( "value" ), QString::number( category.value() ) );
231 catElem.setAttribute( QStringLiteral( "label" ), category.label() );
232 catElem.setAttribute( QStringLiteral( "color" ), QgsSymbolLayerUtils::encodeColor( category.color() ) );
233 catElem.setAttribute( QStringLiteral( "render" ), category.renderState() ? "true" : "false" );
234 catsElem.appendChild( catElem );
235 }
236 rendererElem.appendChild( catsElem );
237
238 saveCommonProperties( rendererElem, context );
239
240 return rendererElem;
241}
242
244{
245 QSet<QString> res;
246 res << mAttribute;
247 return res;
248}
249
250QList<QgsLayerTreeModelLegendNode *> QgsPointCloudClassifiedRenderer::createLegendNodes( QgsLayerTreeLayer *nodeLayer )
251{
252 QList<QgsLayerTreeModelLegendNode *> nodes;
253
254 for ( const QgsPointCloudCategory &category : std::as_const( mCategories ) )
255 {
256 nodes << new QgsRasterSymbolLegendNode( nodeLayer, category.color(), category.label(), nullptr, true, QString::number( category.value() ) );
257 }
258
259 return nodes;
260}
261
263{
264 QStringList res;
265 for ( const QgsPointCloudCategory &category : std::as_const( mCategories ) )
266 {
267 res << QString::number( category.value() );
268 }
269 return res;
270}
271
273{
274 bool ok = false;
275 const int value = key.toInt( &ok );
276 if ( !ok )
277 return false;
278
279 for ( const QgsPointCloudCategory &category : std::as_const( mCategories ) )
280 {
281 if ( category.value() == value )
282 return category.renderState();
283 }
284 return false;
285}
286
287void QgsPointCloudClassifiedRenderer::checkLegendItem( const QString &key, bool state )
288{
289 bool ok = false;
290 const int value = key.toInt( &ok );
291 if ( !ok )
292 return;
293
294 for ( auto it = mCategories.begin(); it != mCategories.end(); ++it )
295 {
296 if ( it->value() == value )
297 {
298 it->setRenderState( state );
299 return;
300 }
301 }
302}
303
305{
306 return mAttribute;
307}
308
309void QgsPointCloudClassifiedRenderer::setAttribute( const QString &attribute )
310{
311 mAttribute = attribute;
312}
313
318
320{
321 mCategories = categories;
322}
323
325{
326 mCategories.append( category );
327}
328
329std::unique_ptr<QgsPreparedPointCloudRendererData> QgsPointCloudClassifiedRenderer::prepare()
330{
331 std::unique_ptr< QgsPointCloudClassifiedRendererPreparedData > data = std::make_unique< QgsPointCloudClassifiedRendererPreparedData >();
332 data->attributeName = mAttribute;
333
334 for ( const QgsPointCloudCategory &category : std::as_const( mCategories ) )
335 {
336 if ( !category.renderState() )
337 continue;
338
339 data->colors.insert( category.value(), category.color() );
340 }
341
342 return data;
343}
344
349
351{
352 const QgsPointCloudAttributeCollection attributes = block->attributes();
353 const QgsPointCloudAttribute *attribute = attributes.find( attributeName, attributeOffset );
354 if ( !attribute )
355 return false;
356
357 attributeType = attribute->type();
358 return true;
359}
360
362{
363 int attributeValue = 0;
365 return colors.value( attributeValue );
366}
367
368
Class for doing transforms between two map coordinate systems.
void transformInPlace(double &x, double &y, double &z, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward) const
Transforms an array of x, y and z double coordinates in place, from the source CRS to the destination...
bool isValid() const
Returns true if the coordinate transform is valid, ie both the source and destination CRS have been s...
Custom exception class for Coordinate Reference System related exceptions.
QgsRange which stores a range of double values.
Definition qgsrange.h:203
bool isInfinite() const
Returns true if the range consists of all possible values.
Definition qgsrange.h:247
Layer tree node points to a map layer.
Collection of point cloud attributes.
int pointRecordSize() const
Returns total size of record.
const QgsPointCloudAttribute * find(const QString &attributeName, int &offset) const
Finds the attribute with the name.
Attribute for point cloud data pair of name and size in bytes.
DataType
Systems of unit measurement.
DataType type() const
Returns the data type.
Base class for storing raw data from point cloud nodes.
const char * data() const
Returns raw pointer to data.
QgsPointCloudAttributeCollection attributes() const
Returns the attributes that are stored in the data block, along with their size.
int pointCount() const
Returns number of points that are stored in the block.
int pointRecordSize() const
Returns the total size of each individual point record.
Represents an individual category (class) from a QgsPointCloudClassifiedRenderer.
QgsPointCloudCategory()=default
Constructor for QgsPointCloudCategory.
int value() const
Returns the value corresponding to this category.
bool renderState() const
Returns true if the category is currently enabled and should be rendered.
QColor color() const
Returns the color which will be used to render this category.
bool operator==(const QgsPointCloudCategory &other) const
Equality operator.
QString label() const
Returns the label for this category, which is used to represent the category within legends and the l...
QColor pointColor(const QgsPointCloudBlock *block, int i, double z) override
An optimised method of retrieving the color of a point from a point cloud block.
QSet< QString > usedAttributes() const override
Returns the set of attributes used by the prepared point cloud renderer.
bool prepareBlock(const QgsPointCloudBlock *block) override
Prepares the renderer for using the specified block.
void addCategory(const QgsPointCloudCategory &category)
Adds a category to the renderer.
QString attribute() const
Returns the attribute to use for the renderer.
QgsPointCloudClassifiedRenderer(const QString &attributeName=QString(), const QgsPointCloudCategoryList &categories=QgsPointCloudCategoryList())
Constructor for QgsPointCloudClassifiedRenderer.
bool willRenderPoint(const QVariantMap &pointAttributes) override
QDomElement save(QDomDocument &doc, const QgsReadWriteContext &context) const override
Saves the renderer configuration to an XML element.
void checkLegendItem(const QString &key, bool state=true) override
Called when the check state of the legend item with the specified key is changed.
QSet< QString > usedAttributes(const QgsPointCloudRenderContext &context) const override
Returns a list of attributes required by this renderer.
static QgsPointCloudRenderer * create(QDomElement &element, const QgsReadWriteContext &context)
Creates an RGB renderer from an XML element.
QgsPointCloudCategoryList categories() const
Returns the classification categories used for rendering.
static QgsPointCloudCategoryList defaultCategories()
Returns the default list of categories.
bool legendItemChecked(const QString &key) override
Returns true if the legend item with the specified key is checked.
QgsPointCloudRenderer * clone() const override
Create a deep copy of this renderer.
void setCategories(const QgsPointCloudCategoryList &categories)
Sets the classification categories used for rendering.
QList< QgsLayerTreeModelLegendNode * > createLegendNodes(QgsLayerTreeLayer *nodeLayer) override
Creates a set of legend nodes representing the renderer.
void renderBlock(const QgsPointCloudBlock *block, QgsPointCloudRenderContext &context) override
Renders a block of point cloud data using the specified render context.
std::unique_ptr< QgsPreparedPointCloudRendererData > prepare() override
Returns prepared data container for bulk point color retrieval.
QStringList legendRuleKeys() const override
Returns a list of all rule keys for legend nodes created by the renderer.
void setAttribute(const QString &attribute)
Sets the attribute to use for the renderer.
QString type() const override
Returns the identifier of the renderer type.
static QMap< int, QString > translatedLasClassificationCodes()
Returns the map of LAS classification code to translated string value, corresponding to the ASPRS Sta...
Encapsulates the render context for a 2D point cloud rendering operation.
QgsRenderContext & renderContext()
Returns a reference to the context's render context.
void incrementPointsRendered(long count)
Increments the count of points rendered by the specified amount.
static void getAttribute(const char *data, std::size_t offset, QgsPointCloudAttribute::DataType type, T &value)
Retrieves the attribute value from data at the specified offset, where type indicates the original da...
Abstract base class for 2d point cloud renderers.
void drawPointToElevationMap(double x, double y, double z, QgsPointCloudRenderContext &context) const
Draws a point at the elevation z using at the specified x and y (in map coordinates) on the elevation...
void saveCommonProperties(QDomElement &element, const QgsReadWriteContext &context) const
Saves common renderer properties (such as point size and screen error) to the specified DOM element.
void copyCommonProperties(QgsPointCloudRenderer *destination) const
Copies common point cloud properties (such as point size and screen error) to the destination rendere...
void drawPoint(double x, double y, const QColor &color, QgsPointCloudRenderContext &context) const
Draws a point using a color at the specified x and y (in map coordinates).
static double pointZ(QgsPointCloudRenderContext &context, const char *ptr, int i)
Retrieves the z value for the point at index i.
static void pointXY(QgsPointCloudRenderContext &context, const char *ptr, int i, double &x, double &y)
Retrieves the x and y coordinate for the point at index i.
bool contains(const QgsRange< T > &other) const
Returns true if this range contains another range.
Definition qgsrange.h:108
Implementation of legend node interface for displaying raster legend entries.
The class is used as a container of context for various read/write operations on other objects.
A rectangle specified with double values.
bool contains(const QgsRectangle &rect) const
Returns true when rectangle contains other rectangle.
QgsElevationMap * elevationMap() const
Returns the destination elevation map for the render operation.
const QgsRectangle & extent() const
When rendering a map layer, calling this method returns the "clipping" extent for the layer (in the l...
QgsDoubleRange zRange() const
Returns the range of z-values which should be rendered.
bool renderingStopped() const
Returns true if the rendering operation has been stopped and any ongoing rendering should be canceled...
QgsCoordinateTransform coordinateTransform() const
Returns the current coordinate transform for the context.
static QColor decodeColor(const QString &str)
static QString encodeColor(const QColor &color)
QList< QgsPointCloudCategory > QgsPointCloudCategoryList