QGIS API Documentation 3.35.0-Master (274c3da7543)
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 "qgscolorutils.h"
25
26QgsPointCloudCategory::QgsPointCloudCategory( const int value, const QColor &color, const QString &label, bool render, double pointSize )
27 : mValue( value )
28 , mColor( color )
29 , mPointSize( pointSize )
30 , mLabel( label )
31 , mRender( render )
32{
33}
34
36{
37 return mValue == other.value() &&
38 mColor == other.color() &&
39 mPointSize == other.pointSize() &&
40 mLabel == other.label() &&
41 mRender == other.renderState();
42}
43
44//
45// QgsPointCloudClassifiedRenderer
46//
47
49 : mAttribute( attributeName )
50 , mCategories( categories )
51{
52}
53
55{
56 return QStringLiteral( "classified" );
57}
58
60{
61 std::unique_ptr< QgsPointCloudClassifiedRenderer > res = std::make_unique< QgsPointCloudClassifiedRenderer >();
62 res->mAttribute = mAttribute;
63 res->mCategories = mCategories;
64
65 copyCommonProperties( res.get() );
66
67 return res.release();
68}
69
71{
72 QgsRectangle visibleExtent = context.renderContext().extent();
73 if ( renderAsTriangles() )
74 {
75 // we need to include also points slightly outside of the visible extent,
76 // otherwise the triangulation may be missing triangles near the edges and corners
77 visibleExtent.grow( std::max( visibleExtent.width(), visibleExtent.height() ) * 0.05 );
78 }
79
80 const char *ptr = block->data();
81 int count = block->pointCount();
82 const QgsPointCloudAttributeCollection request = block->attributes();
83
84 const std::size_t recordSize = request.pointRecordSize();
85 int attributeOffset = 0;
86 const QgsPointCloudAttribute *attribute = request.find( mAttribute, attributeOffset );
87 if ( !attribute )
88 return;
89 const QgsPointCloudAttribute::DataType attributeType = attribute->type();
90
91 const bool renderElevation = context.renderContext().elevationMap();
92 const QgsDoubleRange zRange = context.renderContext().zRange();
93 const bool considerZ = !zRange.isInfinite() || renderElevation;
94
95 int rendered = 0;
96 double x = 0;
97 double y = 0;
98 double z = 0;
100 const bool reproject = ct.isValid();
101
102 QHash< int, QColor > colors;
103 QHash< int, int > pointSizes;
104 for ( const QgsPointCloudCategory &category : std::as_const( mCategories ) )
105 {
106 if ( !category.renderState() )
107 continue;
108
109 colors.insert( category.value(), category.color() );
110
111 const double size = category.pointSize() > 0 ? category.pointSize() : pointSize();
112 pointSizes.insert( category.value(), context.renderContext().convertToPainterUnits( size, pointSizeUnit(), pointSizeMapUnitScale() ) );
113 }
114
115 for ( int i = 0; i < count; ++i )
116 {
117 if ( context.renderContext().renderingStopped() )
118 {
119 break;
120 }
121
122 // z value filtering is cheapest, if we're doing it...
123 if ( considerZ )
124 {
125 z = pointZ( context, ptr, i );
126 if ( !zRange.contains( z ) )
127 continue;
128 }
129
130 int attributeValue = 0;
131 context.getAttribute( ptr, i * recordSize + attributeOffset, attributeType, attributeValue );
132 const QColor color = colors.value( attributeValue );
133 if ( !color.isValid() )
134 continue;
135
136 pointXY( context, ptr, i, x, y );
137 if ( visibleExtent.contains( x, y ) )
138 {
139 if ( reproject )
140 {
141 try
142 {
143 ct.transformInPlace( x, y, z );
144 }
145 catch ( QgsCsException & )
146 {
147 continue;
148 }
149 }
150
151 if ( renderAsTriangles() )
152 {
153 addPointToTriangulation( x, y, z, color, context );
154 }
155 else
156 {
157 const double size = pointSizes.value( attributeValue );
158 drawPoint( x, y, color, size, context );
159 if ( renderElevation )
160 drawPointToElevationMap( x, y, z, size, context );
161 }
162 rendered++;
163 }
164 }
165 context.incrementPointsRendered( rendered );
166}
167
168bool QgsPointCloudClassifiedRenderer::willRenderPoint( const QVariantMap &pointAttributes )
169{
170 if ( !pointAttributes.contains( mAttribute ) )
171 return false;
172 bool parsedCorrectly;
173 int attributeInt = pointAttributes[ mAttribute ].toInt( &parsedCorrectly );
174 if ( !parsedCorrectly )
175 return false;
176 for ( const QgsPointCloudCategory &category : std::as_const( mCategories ) )
177 {
178 if ( category.value() == attributeInt )
179 return category.renderState();
180 }
181 return false;
182}
183
185{
186 std::unique_ptr< QgsPointCloudClassifiedRenderer > r = std::make_unique< QgsPointCloudClassifiedRenderer >();
187
188 r->setAttribute( element.attribute( QStringLiteral( "attribute" ), QStringLiteral( "Classification" ) ) );
189
191 const QDomElement catsElem = element.firstChildElement( QStringLiteral( "categories" ) );
192 if ( !catsElem.isNull() )
193 {
194 QDomElement catElem = catsElem.firstChildElement();
195 while ( !catElem.isNull() )
196 {
197 if ( catElem.tagName() == QLatin1String( "category" ) )
198 {
199 const int value = catElem.attribute( QStringLiteral( "value" ) ).toInt();
200 const double size = catElem.attribute( QStringLiteral( "pointSize" ), QStringLiteral( "0" ) ).toDouble();
201 const QString label = catElem.attribute( QStringLiteral( "label" ) );
202 const bool render = catElem.attribute( QStringLiteral( "render" ) ) != QLatin1String( "false" );
203 const QColor color = QgsColorUtils::colorFromString( catElem.attribute( QStringLiteral( "color" ) ) );
204 categories.append( QgsPointCloudCategory( value, color, label, render, size ) );
205 }
206 catElem = catElem.nextSiblingElement();
207 }
208 r->setCategories( categories );
209 }
210
211 r->restoreCommonProperties( element, context );
212
213 return r.release();
214}
215
217{
237}
238
239QDomElement QgsPointCloudClassifiedRenderer::save( QDomDocument &doc, const QgsReadWriteContext &context ) const
240{
241 QDomElement rendererElem = doc.createElement( QStringLiteral( "renderer" ) );
242
243 rendererElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "classified" ) );
244 rendererElem.setAttribute( QStringLiteral( "attribute" ), mAttribute );
245
246 // categories
247 QDomElement catsElem = doc.createElement( QStringLiteral( "categories" ) );
248 for ( const QgsPointCloudCategory &category : mCategories )
249 {
250 QDomElement catElem = doc.createElement( QStringLiteral( "category" ) );
251 catElem.setAttribute( QStringLiteral( "value" ), QString::number( category.value() ) );
252 catElem.setAttribute( QStringLiteral( "pointSize" ), QString::number( category.pointSize() ) );
253 catElem.setAttribute( QStringLiteral( "label" ), category.label() );
254 catElem.setAttribute( QStringLiteral( "color" ), QgsColorUtils::colorToString( category.color() ) );
255 catElem.setAttribute( QStringLiteral( "render" ), category.renderState() ? "true" : "false" );
256 catsElem.appendChild( catElem );
257 }
258 rendererElem.appendChild( catsElem );
259
260 saveCommonProperties( rendererElem, context );
261
262 return rendererElem;
263}
264
266{
267 QSet<QString> res;
268 res << mAttribute;
269 return res;
270}
271
272QList<QgsLayerTreeModelLegendNode *> QgsPointCloudClassifiedRenderer::createLegendNodes( QgsLayerTreeLayer *nodeLayer )
273{
274 QList<QgsLayerTreeModelLegendNode *> nodes;
275
276 for ( const QgsPointCloudCategory &category : std::as_const( mCategories ) )
277 {
278 nodes << new QgsRasterSymbolLegendNode( nodeLayer, category.color(), category.label(), nullptr, true, QString::number( category.value() ) );
279 }
280
281 return nodes;
282}
283
285{
286 QStringList res;
287 for ( const QgsPointCloudCategory &category : std::as_const( mCategories ) )
288 {
289 res << QString::number( category.value() );
290 }
291 return res;
292}
293
295{
296 bool ok = false;
297 const int value = key.toInt( &ok );
298 if ( !ok )
299 return false;
300
301 for ( const QgsPointCloudCategory &category : std::as_const( mCategories ) )
302 {
303 if ( category.value() == value )
304 return category.renderState();
305 }
306 return false;
307}
308
309void QgsPointCloudClassifiedRenderer::checkLegendItem( const QString &key, bool state )
310{
311 bool ok = false;
312 const int value = key.toInt( &ok );
313 if ( !ok )
314 return;
315
316 for ( auto it = mCategories.begin(); it != mCategories.end(); ++it )
317 {
318 if ( it->value() == value )
319 {
320 it->setRenderState( state );
321 return;
322 }
323 }
324}
325
327{
328 return mAttribute;
329}
330
331void QgsPointCloudClassifiedRenderer::setAttribute( const QString &attribute )
332{
333 mAttribute = attribute;
334}
335
340
342{
343 mCategories = categories;
344}
345
347{
348 mCategories.append( category );
349}
350
351std::unique_ptr<QgsPreparedPointCloudRendererData> QgsPointCloudClassifiedRenderer::prepare()
352{
353 std::unique_ptr< QgsPointCloudClassifiedRendererPreparedData > data = std::make_unique< QgsPointCloudClassifiedRendererPreparedData >();
354 data->attributeName = mAttribute;
355
356 for ( const QgsPointCloudCategory &category : std::as_const( mCategories ) )
357 {
358 if ( !category.renderState() )
359 continue;
360
361 data->colors.insert( category.value(), category.color() );
362 }
363
364 return data;
365}
366
371
373{
374 const QgsPointCloudAttributeCollection attributes = block->attributes();
375 const QgsPointCloudAttribute *attribute = attributes.find( attributeName, attributeOffset );
376 if ( !attribute )
377 return false;
378
379 attributeType = attribute->type();
380 return true;
381}
382
384{
385 int attributeValue = 0;
387 return colors.value( attributeValue );
388}
389
390
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.
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.
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...
Abstract base class for 2d point cloud renderers.
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: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.
double width() const
Returns the width of the rectangle.
void grow(double delta)
Grows the rectangle in place by the specified amount.
double height() const
Returns the height of the rectangle.
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...
QgsCoordinateTransform coordinateTransform() const
Returns the current coordinate transform for the context.
QList< QgsPointCloudCategory > QgsPointCloudCategoryList