QGIS API Documentation 4.3.0-Master (0cfde48c85b)
Loading...
Searching...
No Matches
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
20#include "qgscolorramp.h"
23#include "qgspointcloudblock.h"
24#include "qgsstyle.h"
25#include "qgssymbollayerutils.h"
26
27#include <QString>
28
29using namespace Qt::StringLiterals;
30
32{
33 mColorRampShader.setSourceColorRamp( QgsStyle::defaultStyle()->colorRamp( u"Viridis"_s ).release() );
34 mColorRampShader.classifyColorRamp( 5, -1, QgsRectangle(), nullptr );
35}
36
38{
39 return u"ramp"_s;
40}
41
43{
44 auto res = std::make_unique< QgsPointCloudAttributeByRampRenderer >();
45 res->mAttribute = mAttribute;
46 res->mColorRampShader = mColorRampShader;
47 res->mMin = mMin;
48 res->mMax = mMax;
49
50 copyCommonProperties( res.get() );
51
52 return res.release();
53}
54
56{
57 QgsRectangle visibleExtent = context.renderContext().extent();
58 if ( renderAsTriangles() )
59 {
60 // we need to include also points slightly outside of the visible extent,
61 // otherwise the triangulation may be missing triangles near the edges and corners
62 visibleExtent.grow( std::max( visibleExtent.width(), visibleExtent.height() ) * 0.05 );
63 }
64
65 const char *ptr = block->data();
66 int count = block->pointCount();
67 const QgsPointCloudAttributeCollection request = block->attributes();
68
69 const std::size_t recordSize = request.pointRecordSize();
70 int attributeOffset = 0;
71 const QgsPointCloudAttribute *attribute = request.find( mAttribute, attributeOffset );
72 if ( !attribute )
73 return;
74 const QgsPointCloudAttribute::DataType attributeType = attribute->type();
75
76 const bool renderElevation = context.renderContext().elevationMap();
77 const QgsDoubleRange zRange = context.renderContext().zRange();
78 const bool considerZ = !zRange.isInfinite() || renderElevation || !context.mapCrsZFilter().isInfinite();
79
80 const bool applyZOffset = attribute->name() == "Z"_L1;
81 const bool applyXOffset = attribute->name() == "X"_L1;
82 const bool applyYOffset = attribute->name() == "Y"_L1;
83
84 int rendered = 0;
85 double x = 0;
86 double y = 0;
87 double z = 0;
89 const bool reproject = ct.isValid();
90
91 int red = 0;
92 int green = 0;
93 int blue = 0;
94 int alpha = 0;
95
96 bool dataDefinedPropertiesActive = dataDefinedProperties().isActive( QgsPointCloudRenderer::Property::Color );
97 for ( int i = 0; i < count; ++i )
98 {
99 if ( context.renderContext().renderingStopped() )
100 {
101 break;
102 }
103
104 if ( considerZ )
105 {
106 // z value filtering is cheapest, if we're doing it...
107 z = pointZ( context, ptr, i );
108 if ( !zRange.contains( z ) )
109 continue;
110 }
111
112 pointXY( context, ptr, i, x, y );
113 if ( visibleExtent.contains( x, y ) )
114 {
115 if ( reproject )
116 {
117 try
118 {
119 ct.transformInPlace( x, y, z );
120 }
121 catch ( QgsCsException & )
122 {
123 continue;
124 }
125 }
126
127 if ( !context.mapCrsZFilter().contains( z ) )
128 continue;
129
130 double attributeValue = 0;
131 context.getAttribute( ptr, i * recordSize + attributeOffset, attributeType, attributeValue );
132
133 if ( applyXOffset )
134 attributeValue = context.offset().x() + context.scale().x() * attributeValue;
135 if ( applyYOffset )
136 attributeValue = context.offset().y() + context.scale().y() * attributeValue;
137 if ( applyZOffset )
138 attributeValue = ( context.offset().z() + context.scale().z() * attributeValue ) * context.zValueScale() + context.zValueFixedOffset();
139
140 mColorRampShader.shade( attributeValue, &red, &green, &blue, &alpha );
141
142 QColor color( red, green, blue, alpha );
143 if ( dataDefinedPropertiesActive )
144 color = colorFromExpression( block, i, color, context );
145
146 if ( renderAsTriangles() )
147 {
148 addPointToTriangulation( x, y, z, color, context );
149
150 // We don't want to render any points if we're rendering triangles and there is no preview painter
151 if ( !context.renderContext().previewRenderPainter() )
152 continue;
153 }
154
155 drawPoint( x, y, color, context );
156 if ( renderElevation )
157 drawPointToElevationMap( x, y, z, context );
158
159 rendered++;
160 }
161 }
162 context.incrementPointsRendered( rendered );
163}
164
165
167{
168 auto r = std::make_unique< QgsPointCloudAttributeByRampRenderer >();
169
170 r->setAttribute( element.attribute( u"attribute"_s, u"Intensity"_s ) );
171
172 QDomElement elemShader = element.firstChildElement( u"colorrampshader"_s );
173 r->mColorRampShader.readXml( elemShader, context );
174
175 r->setMinimum( element.attribute( u"min"_s, u"0"_s ).toDouble() );
176 r->setMaximum( element.attribute( u"max"_s, u"100"_s ).toDouble() );
177
178 r->restoreCommonProperties( element, context );
179
180 return r.release();
181}
182
183QDomElement QgsPointCloudAttributeByRampRenderer::save( QDomDocument &doc, const QgsReadWriteContext &context ) const
184{
185 QDomElement rendererElem = doc.createElement( u"renderer"_s );
186
187 rendererElem.setAttribute( u"type"_s, u"ramp"_s );
188 rendererElem.setAttribute( u"min"_s, mMin );
189 rendererElem.setAttribute( u"max"_s, mMax );
190
191 rendererElem.setAttribute( u"attribute"_s, mAttribute );
192
193 QDomElement elemShader = mColorRampShader.writeXml( doc, context );
194 rendererElem.appendChild( elemShader );
195
196 saveCommonProperties( rendererElem, context );
197
198 return rendererElem;
199}
200
202{
203 QSet<QString> res = QgsPointCloudRenderer::usedAttributes( context );
204 res << mAttribute;
205 return res;
206}
207
208QList<QgsLayerTreeModelLegendNode *> QgsPointCloudAttributeByRampRenderer::createLegendNodes( QgsLayerTreeLayer *nodeLayer )
209{
210 QList<QgsLayerTreeModelLegendNode *> res;
211 res << new QgsSimpleLegendNode( nodeLayer, mAttribute );
212
213 switch ( mColorRampShader.colorRampType() )
214 {
216 // for interpolated shaders we use a ramp legend node unless the settings flag
217 // to use the continuous legend is not set, in that case we fall through
218 if ( mColorRampShader.sourceColorRamp() && ( !mColorRampShader.legendSettings() || mColorRampShader.legendSettings()->useContinuousLegend() ) )
219 {
220 res << new QgsColorRampLegendNode(
221 nodeLayer,
222 mColorRampShader.sourceColorRamp()->clone(),
223 mColorRampShader.legendSettings() ? *mColorRampShader.legendSettings() : QgsColorRampLegendNodeSettings(),
224 mColorRampShader.minimumValue(),
225 mColorRampShader.maximumValue()
226 );
227 break;
228 }
229 [[fallthrough]];
232 {
233 // for all others we use itemised lists
234 QList< QPair< QString, QColor > > items;
235 mColorRampShader.legendSymbologyItems( items );
236 res.reserve( items.size() );
237 for ( const QPair< QString, QColor > &item : std::as_const( items ) )
238 {
239 res << new QgsRasterSymbolLegendNode( nodeLayer, item.second, item.first );
240 }
241 break;
242 }
243 }
244 return res;
245}
246
248{
249 return mAttribute;
250}
251
253{
254 mAttribute = attribute;
255}
256
261
263{
264 mColorRampShader = shader;
265}
266
268{
269 return mMin;
270}
271
276
278{
279 return mMax;
280}
281
283{
284 mMax = value;
285}
286
287std::unique_ptr<QgsPreparedPointCloudRendererData> QgsPointCloudAttributeByRampRenderer::prepare()
288{
289 auto data = std::make_unique< QgsPointCloudAttributeByRampRendererPreparedData >();
290 data->attributeName = mAttribute;
291 data->colorRampShader = mColorRampShader;
292
293 data->attributeIsX = mAttribute == "X"_L1;
294 data->attributeIsY = mAttribute == "Y"_L1;
295 data->attributeIsZ = mAttribute == "Z"_L1;
296 return data;
297}
298
300{
301 double attributeValue = 0;
302 if ( attributeIsZ )
303 attributeValue = z;
304 else
306
307 if ( attributeIsX )
308 attributeValue = block->offset().x() + block->scale().x() * attributeValue;
309 else if ( attributeIsY )
310 attributeValue = block->offset().y() + block->scale().y() * attributeValue;
311
312 int red = 0;
313 int green = 0;
314 int blue = 0;
315 int alpha = 0;
316 colorRampShader.shade( attributeValue, &red, &green, &blue, &alpha );
317 return QColor( red, green, blue, alpha );
318}
319
324
326{
327 const QgsPointCloudAttributeCollection attributes = block->attributes();
328 const QgsPointCloudAttribute *attribute = attributes.find( attributeName, attributeOffset );
329 if ( !attribute )
330 return false;
331
332 attributeType = attribute->type();
333 return true;
334}
@ Exact
Assigns the color of the exact matching value in the color ramp item list.
Definition qgis.h:1597
@ Linear
Interpolates the color between two class breaks linearly.
Definition qgis.h:1595
@ Discrete
Assigns the color of the higher class for every pixel between two class breaks.
Definition qgis.h:1596
Settings for a color ramp legend node.
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.
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:217
bool isInfinite() const
Returns true if the range consists of all possible values.
Definition qgsrange.h:266
Layer tree node points to a map layer.
QgsLayerTreeLayer * clone() const override
Create a copy of the node. Returns new instance.
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.
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.
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...
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.
QgsDoubleRange mapCrsZFilter() const
Returns the allowable range of z values in the map (destination) CRS.
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.
QColor colorFromExpression(const QgsPointCloudBlock *block, int pointIndex, const QColor &rendererColor, QgsPointCloudRenderContext &context)
Computes color from the expression set, uses expression referenced variables and renderer base color.
virtual QSet< QString > usedAttributes(const QgsPointCloudRenderContext &context) const
Returns a list of attributes required by this renderer.
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).
const QgsPropertyCollection & dataDefinedProperties() const
Returns the renderer's property collection, used for data defined overrides.
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 isActive(int key) const final
Returns true if the collection contains an active property with the specified key.
bool contains(const QgsRange< T > &other) const
Returns true if this range contains another range.
Definition qgsrange.h:138
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.
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.
Implementation of legend node interface for displaying arbitrary labels with icons.
static QgsStyle * defaultStyle(bool initialize=true)
Returns the default application-wide style.
Definition qgsstyle.cpp:164
double y() const
Returns Y coordinate.
Definition qgsvector3d.h:60
double z() const
Returns Z coordinate.
Definition qgsvector3d.h:62
double x() const
Returns X coordinate.
Definition qgsvector3d.h:58