QGIS API Documentation 3.99.0-Master (21b3aa880ba)
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
20#include "qgscolorramp.h"
21#include "qgscolorutils.h"
23#include "qgspointcloudblock.h"
25#include "qgsstyle.h"
26
27QgsPointCloudCategory::QgsPointCloudCategory( const int value, const QColor &color, const QString &label, bool render, double pointSize )
28 : mValue( value )
29 , mColor( color )
31 , mLabel( label )
32 , mRender( render )
33{
34}
35
37{
38 return mValue == other.value() &&
39 mColor == other.color() &&
40 mPointSize == other.pointSize() &&
41 mLabel == other.label() &&
42 mRender == other.renderState();
43}
44
45//
46// QgsPointCloudClassifiedRenderer
47//
48
50 : mAttribute( attributeName )
51 , mCategories( categories )
52{
53}
54
56{
57 return QStringLiteral( "classified" );
58}
59
61{
62 auto res = std::make_unique< QgsPointCloudClassifiedRenderer >();
63 res->mAttribute = mAttribute;
64 res->mCategories = mCategories;
65
66 copyCommonProperties( res.get() );
67
68 return res.release();
69}
70
72{
73 QgsRectangle visibleExtent = context.renderContext().extent();
74 if ( renderAsTriangles() )
75 {
76 // we need to include also points slightly outside of the visible extent,
77 // otherwise the triangulation may be missing triangles near the edges and corners
78 visibleExtent.grow( std::max( visibleExtent.width(), visibleExtent.height() ) * 0.05 );
79 }
80
81 const char *ptr = block->data();
82 int count = block->pointCount();
83 const QgsPointCloudAttributeCollection request = block->attributes();
84
85 const std::size_t recordSize = request.pointRecordSize();
86 int attributeOffset = 0;
87 const QgsPointCloudAttribute *attribute = request.find( mAttribute, attributeOffset );
88 if ( !attribute )
89 return;
90 const QgsPointCloudAttribute::DataType attributeType = attribute->type();
91
92 const bool renderElevation = context.renderContext().elevationMap();
93 const QgsDoubleRange zRange = context.renderContext().zRange();
94 const bool considerZ = !zRange.isInfinite() || renderElevation;
95
96 int rendered = 0;
97 double x = 0;
98 double y = 0;
99 double z = 0;
101 const bool reproject = ct.isValid();
102
103 QHash< int, QColor > colors;
104 QHash< int, int > pointSizes;
105 for ( const QgsPointCloudCategory &category : std::as_const( mCategories ) )
106 {
107 if ( !category.renderState() )
108 continue;
109
110 colors.insert( category.value(), category.color() );
111
112 const double size = category.pointSize() > 0 ? category.pointSize() : pointSize();
113 pointSizes.insert( category.value(), context.renderContext().convertToPainterUnits( size, pointSizeUnit(), pointSizeMapUnitScale() ) );
114 }
115
116 for ( int i = 0; i < count; ++i )
117 {
118 if ( context.renderContext().renderingStopped() )
119 {
120 break;
121 }
122
123 // z value filtering is cheapest, if we're doing it...
124 if ( considerZ )
125 {
126 z = pointZ( context, ptr, i );
127 if ( !zRange.contains( z ) )
128 continue;
129 }
130
131 int attributeValue = 0;
132 context.getAttribute( ptr, i * recordSize + attributeOffset, attributeType, attributeValue );
133 const QColor color = colors.value( attributeValue );
134 if ( !color.isValid() )
135 continue;
136
137 pointXY( context, ptr, i, x, y );
138 if ( visibleExtent.contains( x, y ) )
139 {
140 if ( reproject )
141 {
142 try
143 {
144 ct.transformInPlace( x, y, z );
145 }
146 catch ( QgsCsException & )
147 {
148 continue;
149 }
150 }
151
152 if ( renderAsTriangles() )
153 {
154 addPointToTriangulation( x, y, z, color, context );
155
156 // We don't want to render any points if we're rendering triangles and there is no preview painter
157 if ( !context.renderContext().previewRenderPainter() )
158 continue;
159 }
160
161 const double size = pointSizes.value( attributeValue );
162 drawPoint( x, y, color, size, context );
163 if ( renderElevation )
164 drawPointToElevationMap( x, y, z, size, context );
165
166 rendered++;
167 }
168 }
169 context.incrementPointsRendered( rendered );
170}
171
172bool QgsPointCloudClassifiedRenderer::willRenderPoint( const QVariantMap &pointAttributes )
173{
174 if ( !pointAttributes.contains( mAttribute ) )
175 return false;
176 bool parsedCorrectly;
177 int attributeInt = pointAttributes[ mAttribute ].toInt( &parsedCorrectly );
178 if ( !parsedCorrectly )
179 return false;
180 for ( const QgsPointCloudCategory &category : std::as_const( mCategories ) )
181 {
182 if ( category.value() == attributeInt )
183 return category.renderState();
184 }
185 return false;
186}
187
189{
190 auto r = std::make_unique< QgsPointCloudClassifiedRenderer >();
191
192 r->setAttribute( element.attribute( QStringLiteral( "attribute" ), QStringLiteral( "Classification" ) ) );
193
195 const QDomElement catsElem = element.firstChildElement( QStringLiteral( "categories" ) );
196 if ( !catsElem.isNull() )
197 {
198 QDomElement catElem = catsElem.firstChildElement();
199 while ( !catElem.isNull() )
200 {
201 if ( catElem.tagName() == QLatin1String( "category" ) )
202 {
203 const int value = catElem.attribute( QStringLiteral( "value" ) ).toInt();
204 const double size = catElem.attribute( QStringLiteral( "pointSize" ), QStringLiteral( "0" ) ).toDouble();
205 const QString label = catElem.attribute( QStringLiteral( "label" ) );
206 const bool render = catElem.attribute( QStringLiteral( "render" ) ) != QLatin1String( "false" );
207 const QColor color = QgsColorUtils::colorFromString( catElem.attribute( QStringLiteral( "color" ) ) );
208 categories.append( QgsPointCloudCategory( value, color, label, render, size ) );
209 }
210 catElem = catElem.nextSiblingElement();
211 }
212 r->setCategories( categories );
213 }
214
215 r->restoreCommonProperties( element, context );
216
217 return r.release();
218}
219
221{
241}
242
243QDomElement QgsPointCloudClassifiedRenderer::save( QDomDocument &doc, const QgsReadWriteContext &context ) const
244{
245 QDomElement rendererElem = doc.createElement( QStringLiteral( "renderer" ) );
246
247 rendererElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "classified" ) );
248 rendererElem.setAttribute( QStringLiteral( "attribute" ), mAttribute );
249
250 // categories
251 QDomElement catsElem = doc.createElement( QStringLiteral( "categories" ) );
252 for ( const QgsPointCloudCategory &category : mCategories )
253 {
254 QDomElement catElem = doc.createElement( QStringLiteral( "category" ) );
255 catElem.setAttribute( QStringLiteral( "value" ), QString::number( category.value() ) );
256 catElem.setAttribute( QStringLiteral( "pointSize" ), QString::number( category.pointSize() ) );
257 catElem.setAttribute( QStringLiteral( "label" ), category.label() );
258 catElem.setAttribute( QStringLiteral( "color" ), QgsColorUtils::colorToString( category.color() ) );
259 catElem.setAttribute( QStringLiteral( "render" ), category.renderState() ? "true" : "false" );
260 catsElem.appendChild( catElem );
261 }
262 rendererElem.appendChild( catsElem );
263
264 saveCommonProperties( rendererElem, context );
265
266 return rendererElem;
267}
268
270{
271 QSet<QString> res;
272 res << mAttribute;
273 return res;
274}
275
276QList<QgsLayerTreeModelLegendNode *> QgsPointCloudClassifiedRenderer::createLegendNodes( QgsLayerTreeLayer *nodeLayer )
277{
278 QList<QgsLayerTreeModelLegendNode *> nodes;
279
280 for ( const QgsPointCloudCategory &category : std::as_const( mCategories ) )
281 {
282 nodes << new QgsRasterSymbolLegendNode( nodeLayer, category.color(), category.label(), nullptr, true, QString::number( category.value() ) );
283 }
284
285 return nodes;
286}
287
289{
290 QStringList res;
291 for ( const QgsPointCloudCategory &category : std::as_const( mCategories ) )
292 {
293 res << QString::number( category.value() );
294 }
295 return res;
296}
297
299{
300 bool ok = false;
301 const int value = key.toInt( &ok );
302 if ( !ok )
303 return false;
304
305 for ( const QgsPointCloudCategory &category : std::as_const( mCategories ) )
306 {
307 if ( category.value() == value )
308 return category.renderState();
309 }
310 return false;
311}
312
313void QgsPointCloudClassifiedRenderer::checkLegendItem( const QString &key, bool state )
314{
315 bool ok = false;
316 const int value = key.toInt( &ok );
317 if ( !ok )
318 return;
319
320 for ( auto it = mCategories.begin(); it != mCategories.end(); ++it )
321 {
322 if ( it->value() == value )
323 {
324 it->setRenderState( state );
325 return;
326 }
327 }
328}
329
331{
332 return mAttribute;
333}
334
336{
337 mAttribute = attribute;
338}
339
344
349
351{
352 mCategories.append( category );
353}
354
355std::unique_ptr<QgsPreparedPointCloudRendererData> QgsPointCloudClassifiedRenderer::prepare()
356{
357 auto data = std::make_unique< QgsPointCloudClassifiedRendererPreparedData >();
358 data->attributeName = mAttribute;
359
360 for ( const QgsPointCloudCategory &category : std::as_const( mCategories ) )
361 {
362 if ( !category.renderState() )
363 continue;
364
365 data->colors.insert( category.value(), category.color() );
366 }
367
368 return data;
369}
370
375
377{
378 const QgsPointCloudAttributeCollection attributes = block->attributes();
379 const QgsPointCloudAttribute *attribute = attributes.find( attributeName, attributeOffset );
380 if ( !attribute )
381 return false;
382
383 attributeType = attribute->type();
384 return true;
385}
386
388{
389 int attributeValue = 0;
391 return colors.value( attributeValue );
392}
393
394
static QColor colorFromString(const QString &string)
Decodes a string into a color value.
static QString colorToString(const QColor &color)
Encodes a color into a string value.
Handles coordinate transforms between two 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:233
bool isInfinite() const
Returns true if the range consists of all possible values.
Definition qgsrange.h:287
Layer tree node points to a map layer.
A 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
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.
double pointSize() const
Returns the point size for 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...
bool renderAsTriangles() const
Returns whether points are triangulated to render solid surface.
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.
const QgsMapUnitScale & pointSizeMapUnitScale() const
Returns the map unit scale used for the point size.
void addPointToTriangulation(double x, double y, double z, const QColor &color, QgsPointCloudRenderContext &context)
Adds a point to the list of points to be triangulated (only used when renderAsTriangles() is enabled)...
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).
Qgis::RenderUnit pointSizeUnit() const
Returns the units used for the point size.
double pointSize() const
Returns the point size.
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:137
Implementation of legend node interface for displaying raster legend entries.
A container for the context for various read/write operations on objects.
A rectangle specified with double values.
bool contains(const QgsRectangle &rect) const
Returns true when rectangle contains other rectangle.
void grow(double delta)
Grows the rectangle in place by the specified amount.
double convertToPainterUnits(double size, Qgis::RenderUnit unit, const QgsMapUnitScale &scale=QgsMapUnitScale(), Qgis::RenderSubcomponentProperty property=Qgis::RenderSubcomponentProperty::Generic) const
Converts a size from the specified units to painter units (pixels).
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...
QPainter * previewRenderPainter()
Returns the const destination QPainter for temporary in-progress preview renders.
QgsCoordinateTransform coordinateTransform() const
Returns the current coordinate transform for the context.
QList< QgsPointCloudCategory > QgsPointCloudCategoryList