QGIS API Documentation 3.29.0-Master (ade4f0cf0f)
qgspointcloudattributebyramprenderer.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgspointcloudattributebyramprenderer.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
27{
28 mColorRampShader.setSourceColorRamp( QgsStyle::defaultStyle()->colorRamp( QStringLiteral( "Viridis" ) ) );
29 mColorRampShader.classifyColorRamp( 5, -1, QgsRectangle(), nullptr );
30}
31
33{
34 return QStringLiteral( "ramp" );
35}
36
38{
39 std::unique_ptr< QgsPointCloudAttributeByRampRenderer > res = std::make_unique< QgsPointCloudAttributeByRampRenderer >();
40 res->mAttribute = mAttribute;
41 res->mColorRampShader = mColorRampShader;
42 res->mMin = mMin;
43 res->mMax = mMax;
44
45 copyCommonProperties( res.get() );
46
47 return res.release();
48}
49
51{
52 const QgsRectangle visibleExtent = context.renderContext().extent();
53
54 const char *ptr = block->data();
55 int count = block->pointCount();
56 const QgsPointCloudAttributeCollection request = block->attributes();
57
58 const std::size_t recordSize = request.pointRecordSize();
59 int attributeOffset = 0;
60 const QgsPointCloudAttribute *attribute = request.find( mAttribute, attributeOffset );
61 if ( !attribute )
62 return;
63 const QgsPointCloudAttribute::DataType attributeType = attribute->type();
64
65 const bool renderElevation = context.elevationMap();
66 const QgsDoubleRange zRange = context.renderContext().zRange();
67 const bool considerZ = !zRange.isInfinite() || renderElevation;
68
69 const bool applyZOffset = attribute->name() == QLatin1String( "Z" );
70 const bool applyXOffset = attribute->name() == QLatin1String( "X" );
71 const bool applyYOffset = attribute->name() == QLatin1String( "Y" );
72
73 int rendered = 0;
74 double x = 0;
75 double y = 0;
76 double z = 0;
78 const bool reproject = ct.isValid();
79
80 int red = 0;
81 int green = 0;
82 int blue = 0;
83 int alpha = 0;
84 for ( int i = 0; i < count; ++i )
85 {
86 if ( context.renderContext().renderingStopped() )
87 {
88 break;
89 }
90
91 if ( considerZ )
92 {
93 // z value filtering is cheapest, if we're doing it...
94 z = pointZ( context, ptr, i );
95 if ( !zRange.contains( z ) )
96 continue;
97 }
98
99 pointXY( context, ptr, i, x, y );
100 if ( visibleExtent.contains( x, y ) )
101 {
102 if ( reproject )
103 {
104 try
105 {
106 ct.transformInPlace( x, y, z );
107 }
108 catch ( QgsCsException & )
109 {
110 continue;
111 }
112 }
113
114 double attributeValue = 0;
115 context.getAttribute( ptr, i * recordSize + attributeOffset, attributeType, attributeValue );
116
117 if ( applyXOffset )
118 attributeValue = context.offset().x() + context.scale().x() * attributeValue;
119 if ( applyYOffset )
120 attributeValue = context.offset().y() + context.scale().y() * attributeValue;
121 if ( applyZOffset )
122 attributeValue = ( context.offset().z() + context.scale().z() * attributeValue ) * context.zValueScale() + context.zValueFixedOffset();
123
124 mColorRampShader.shade( attributeValue, &red, &green, &blue, &alpha );
125 drawPoint( x, y, QColor( red, green, blue, alpha ), context );
126 if ( renderElevation )
127 drawPointToElevationMap( x, y, z, context );
128
129 rendered++;
130 }
131 }
132 context.incrementPointsRendered( rendered );
133}
134
135
137{
138 std::unique_ptr< QgsPointCloudAttributeByRampRenderer > r = std::make_unique< QgsPointCloudAttributeByRampRenderer >();
139
140 r->setAttribute( element.attribute( QStringLiteral( "attribute" ), QStringLiteral( "Intensity" ) ) );
141
142 QDomElement elemShader = element.firstChildElement( QStringLiteral( "colorrampshader" ) );
143 r->mColorRampShader.readXml( elemShader, context );
144
145 r->setMinimum( element.attribute( QStringLiteral( "min" ), QStringLiteral( "0" ) ).toDouble() );
146 r->setMaximum( element.attribute( QStringLiteral( "max" ), QStringLiteral( "100" ) ).toDouble() );
147
148 r->restoreCommonProperties( element, context );
149
150 return r.release();
151}
152
153QDomElement QgsPointCloudAttributeByRampRenderer::save( QDomDocument &doc, const QgsReadWriteContext &context ) const
154{
155 QDomElement rendererElem = doc.createElement( QStringLiteral( "renderer" ) );
156
157 rendererElem.setAttribute( QStringLiteral( "type" ), QStringLiteral( "ramp" ) );
158 rendererElem.setAttribute( QStringLiteral( "min" ), mMin );
159 rendererElem.setAttribute( QStringLiteral( "max" ), mMax );
160
161 rendererElem.setAttribute( QStringLiteral( "attribute" ), mAttribute );
162
163 QDomElement elemShader = mColorRampShader.writeXml( doc, context );
164 rendererElem.appendChild( elemShader );
165
166 saveCommonProperties( rendererElem, context );
167
168 return rendererElem;
169}
170
172{
173 QSet<QString> res;
174 res << mAttribute;
175 return res;
176}
177
178QList<QgsLayerTreeModelLegendNode *> QgsPointCloudAttributeByRampRenderer::createLegendNodes( QgsLayerTreeLayer *nodeLayer )
179{
180 QList<QgsLayerTreeModelLegendNode *> res;
181 res << new QgsSimpleLegendNode( nodeLayer, mAttribute );
182
183 switch ( mColorRampShader.colorRampType() )
184 {
186 // for interpolated shaders we use a ramp legend node unless the settings flag
187 // to use the continuous legend is not set, in that case we fall through
188 if ( mColorRampShader.sourceColorRamp() && ( ! mColorRampShader.legendSettings() || mColorRampShader.legendSettings()->useContinuousLegend() ) )
189 {
190 res << new QgsColorRampLegendNode( nodeLayer, mColorRampShader.sourceColorRamp()->clone(),
191 mColorRampShader.legendSettings() ? *mColorRampShader.legendSettings() : QgsColorRampLegendNodeSettings(),
192 mColorRampShader.minimumValue(),
193 mColorRampShader.maximumValue() );
194 break;
195 }
196 Q_FALLTHROUGH();
199 {
200 // for all others we use itemised lists
201 QList< QPair< QString, QColor > > items;
202 mColorRampShader.legendSymbologyItems( items );
203 res.reserve( items.size() );
204 for ( const QPair< QString, QColor > &item : std::as_const( items ) )
205 {
206 res << new QgsRasterSymbolLegendNode( nodeLayer, item.second, item.first );
207 }
208 break;
209 }
210 }
211 return res;
212}
213
215{
216 return mAttribute;
217}
218
220{
221 mAttribute = attribute;
222}
223
225{
226 return mColorRampShader;
227}
228
230{
231 mColorRampShader = shader;
232}
233
235{
236 return mMin;
237}
238
240{
241 mMin = minimum;
242}
243
245{
246 return mMax;
247}
248
250{
251 mMax = value;
252}
253
254std::unique_ptr<QgsPreparedPointCloudRendererData> QgsPointCloudAttributeByRampRenderer::prepare()
255{
256 std::unique_ptr< QgsPointCloudAttributeByRampRendererPreparedData> data = std::make_unique< QgsPointCloudAttributeByRampRendererPreparedData >();
257 data->attributeName = mAttribute;
258 data->colorRampShader = mColorRampShader;
259
260 data->attributeIsX = mAttribute == QLatin1String( "X" );
261 data->attributeIsY = mAttribute == QLatin1String( "Y" );
262 data->attributeIsZ = mAttribute == QLatin1String( "Z" );
263 return data;
264}
265
267{
268 double attributeValue = 0;
269 if ( attributeIsZ )
270 attributeValue = z;
271 else
273
274 if ( attributeIsX )
275 attributeValue = block->offset().x() + block->scale().x() * attributeValue;
276 else if ( attributeIsY )
277 attributeValue = block->offset().y() + block->scale().y() * attributeValue;
278
279 int red = 0;
280 int green = 0;
281 int blue = 0;
282 int alpha = 0;
283 colorRampShader.shade( attributeValue, &red, &green, &blue, &alpha );
284 return QColor( red, green, blue, alpha );
285}
286
288{
289 return { attributeName };
290}
291
293{
294 const QgsPointCloudAttributeCollection attributes = block->attributes();
295 const QgsPointCloudAttribute *attribute = attributes.find( attributeName, attributeOffset );
296 if ( !attribute )
297 return false;
298
299 attributeType = attribute->type();
300 return true;
301}
Settings for a color ramp legend node.
bool useContinuousLegend() const
Returns true if a continuous gradient legend will be used.
A legend node which renders a color ramp.
A ramp shader will color a raster pixel based on a list of values ranges in a ramp.
void legendSymbologyItems(QList< QPair< QString, QColor > > &symbolItems) const override
Returns legend symbology items if provided by renderer.
const QgsColorRampLegendNodeSettings * legendSettings() const
Returns the color ramp shader legend settings.
Type colorRampType() const
Returns the color ramp type.
void setSourceColorRamp(QgsColorRamp *colorramp)
Set the source color ramp.
QDomElement writeXml(QDomDocument &doc, const QgsReadWriteContext &context=QgsReadWriteContext()) const
Writes configuration to a new DOM element.
bool shade(double value, int *returnRedValue, int *returnGreenValue, int *returnBlueValue, int *returnAlphaValue) const override
Generates and new RGB value based on one input value.
QgsColorRamp * sourceColorRamp() const
Returns the source color ramp.
@ Interpolated
Interpolates the color between two class breaks linearly.
@ Discrete
Assigns the color of the higher class for every pixel between two class breaks.
@ Exact
Assigns the color of the exact matching value in the color ramp item list.
void classifyColorRamp(int classes=0, int band=-1, const QgsRectangle &extent=QgsRectangle(), QgsRasterInterface *input=nullptr)
Classify color ramp shader.
virtual QgsColorRamp * clone() const =0
Creates a clone of the color ramp.
Class for doing transforms between two map coordinate systems.
bool isValid() const
Returns true if the coordinate transform is valid, ie both the source and destination CRS have been s...
void transformInPlace(double &x, double &y, double &z, Qgis::TransformDirection direction=Qgis::TransformDirection::Forward) const SIP_THROW(QgsCsException)
Transforms an array of x, y and z double coordinates in place, from the source CRS to the destination...
Custom exception class for Coordinate Reference System related exceptions.
Definition: qgsexception.h:66
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.
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.
bool prepareBlock(const QgsPointCloudBlock *block) override
Prepares the renderer for using the specified block.
QSet< QString > usedAttributes() const override
Returns the set of attributes used by the prepared point cloud renderer.
QString type() const override
Returns the identifier of the renderer type.
void setMaximum(double maximum)
Sets the maximum value for attributes which will be used by the color ramp shader.
QgsPointCloudRenderer * clone() const override
Create a deep copy of this renderer.
static QgsPointCloudRenderer * create(QDomElement &element, const QgsReadWriteContext &context)
Creates an RGB renderer from an XML element.
void renderBlock(const QgsPointCloudBlock *block, QgsPointCloudRenderContext &context) override
Renders a block of point cloud data using the specified render context.
void setMinimum(double minimum)
Sets the minimum value for attributes which will be used by the color ramp shader.
QgsPointCloudAttributeByRampRenderer()
Constructor for QgsPointCloudAttributeByRampRenderer.
QDomElement save(QDomDocument &doc, const QgsReadWriteContext &context) const override
Saves the renderer configuration to an XML element.
double maximum() const
Returns the maximum value for attributes which will be used by the color ramp shader.
QList< QgsLayerTreeModelLegendNode * > createLegendNodes(QgsLayerTreeLayer *nodeLayer) override
Creates a set of legend nodes representing the renderer.
void setColorRampShader(const QgsColorRampShader &shader)
Sets the color ramp shader function used to visualize the attribute.
QSet< QString > usedAttributes(const QgsPointCloudRenderContext &context) const override
Returns a list of attributes required by this renderer.
std::unique_ptr< QgsPreparedPointCloudRendererData > prepare() override
Returns prepared data container for bulk point color retrieval.
QgsColorRampShader colorRampShader() const
Returns the color ramp shader function used to visualize the attribute.
double minimum() const
Returns the minimum value for attributes which will be used by the color ramp shader.
QString attribute() const
Returns the attribute to use for the renderer.
void setAttribute(const QString &attribute)
Sets the attribute to use for the renderer.
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.
QgsVector3D scale() const
Returns the custom scale of the block.
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.
QgsVector3D offset() const
Returns the custom offset of the block.
Encapsulates the render context for a 2D point cloud rendering operation.
double zValueFixedOffset() const
Returns any constant offset which must be applied to z values taken from the point cloud index.
QgsVector3D offset() const
Returns the offset of the layer's int32 coordinates compared to CRS coords.
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...
QgsElevationMap * elevationMap()
Returns elevation map.
double zValueScale() const
Returns any constant scaling factor which must be applied to z values taken from the point cloud inde...
QgsVector3D scale() const
Returns the scale of the layer's int32 coordinates compared to CRS coords.
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
double maximumValue() const
Returns the minimum value for the raster shader.
double minimumValue() const
Returns the maximum value for the raster shader.
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.
Definition: qgsrectangle.h:42
bool contains(const QgsRectangle &rect) const SIP_HOLDGIL
Returns true when rectangle contains other rectangle.
Definition: qgsrectangle.h:363
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.
Implementation of legend node interface for displaying arbitrary label with icon.
static QgsStyle * defaultStyle()
Returns default application-wide style.
Definition: qgsstyle.cpp:145
double y() const
Returns Y coordinate.
Definition: qgsvector3d.h:51
double z() const
Returns Z coordinate.
Definition: qgsvector3d.h:53
double x() const
Returns X coordinate.
Definition: qgsvector3d.h:49