Skip to content

Commit

Permalink
unify 2D and 3D identify results conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
NEDJIMAbelgacem authored and wonder-sk committed Jan 13, 2021
1 parent 59bb58f commit f363877
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 69 deletions.
11 changes: 11 additions & 0 deletions python/gui/auto_generated/qgsmaptoolidentify.sip.in
Expand Up @@ -128,6 +128,17 @@ Performs identification based on a geometry (in map coordinates)
%Docstring
Returns a pointer to the identify menu which will be used in layer selection mode
this menu can also be customized
%End

static void fromPointCloudIdentificationToIdentifyResults( QgsPointCloudLayer *layer, const QVector<QVariantMap> &identified, QList<QgsMapToolIdentify::IdentifyResult> &results );
%Docstring
Converts point cloud identification results from variant maps to QgsMapToolIdentify.IdentifyResult and apply some formatting

.. note::

: the converted variant maps are pushed at the back of ``results`` without cleaning what's in it previously

.. versionadded:: 3.18
%End

public slots:
Expand Down
6 changes: 2 additions & 4 deletions src/app/3d/qgs3dmapcanvas.cpp
Expand Up @@ -256,9 +256,7 @@ void Qgs3DMapCanvas::updateTemporalRange( const QgsDateTimeRange &temporalrange
mScene->updateTemporal();
}

void Qgs3DMapCanvas::identifyPointCloudOnMouseEvent( QVector<QPair<QgsMapLayer *, QVector<QVariantMap>>> &result, QMouseEvent *event )
QSize Qgs3DMapCanvas::windowSize() const
{
QgsRay3D ray = Qgs3DUtils::rayFromScreenPoint( event->pos(), mEngine->size(), mEngine->camera() );

mScene->identifyPointCloudOnRay( result, ray );
return mEngine->size();
}
4 changes: 2 additions & 2 deletions src/app/3d/qgs3dmapcanvas.h
Expand Up @@ -87,11 +87,11 @@ class Qgs3DMapCanvas : public QWidget
void setTemporalController( QgsTemporalController *temporalController );

/**
* Identifies point cloud using mouse event
* Returns the size of the 3D canvas window
*
* \since QGIS 3.18
*/
void identifyPointCloudOnMouseEvent( QVector<QPair<QgsMapLayer *, QVector<QVariantMap>>> &result, QMouseEvent *event );
QSize windowSize() const;

signals:
//! Emitted when the 3D map canvas was successfully saved as image
Expand Down
39 changes: 8 additions & 31 deletions src/app/3d/qgs3dmaptoolidentify.cpp
Expand Up @@ -34,6 +34,8 @@
#include "qgspointcloudlayerelevationproperties.h"

#include "qgs3dmapscenepickhandler.h"
#include "qgs3dutils.h"
#include "qgscameracontroller.h"

class Qgs3DMapToolIdentifyPickHandler : public Qgs3DMapScenePickHandler
{
Expand Down Expand Up @@ -88,41 +90,16 @@ void Qgs3DMapToolIdentify::mouseReleaseEvent( QMouseEvent *event )

// point cloud identification
QVector<QPair<QgsMapLayer *, QVector<QVariantMap>>> layerPoints;
canvas()->identifyPointCloudOnMouseEvent( layerPoints, event );
Qgs3DMapCanvas *canvas = this->canvas();

QgsRay3D ray = Qgs3DUtils::rayFromScreenPoint( event->pos(), canvas->windowSize(), canvas->cameraController()->camera() );
canvas->scene()->identifyPointCloudOnRay( layerPoints, ray );

QVector<QgsMapToolIdentify::IdentifyResult> identifyResults;
QList<QgsMapToolIdentify::IdentifyResult> identifyResults;
for ( int i = 0; i < layerPoints.size(); ++i )
{
QgsPointCloudLayer *pcLayer = dynamic_cast< QgsPointCloudLayer * >( layerPoints[i].first );
int id = 1;
const QgsPointCloudLayerElevationProperties *elevationProps = qobject_cast< const QgsPointCloudLayerElevationProperties *>( pcLayer->elevationProperties() );
for ( const QVariantMap &pt : layerPoints[i].second )
{
QMap<QString, QString> ptStr;
QString classification;
for ( auto attrIt = pt.constBegin(); attrIt != pt.constEnd(); ++attrIt )
{
if ( attrIt.key().compare( QLatin1String( "Z" ), Qt::CaseInsensitive ) == 0
&& ( !qgsDoubleNear( elevationProps->zScale(), 1 ) || !qgsDoubleNear( elevationProps->zOffset(), 0 ) ) )
{
// Apply elevation properties
ptStr[ tr( "Z (original)" ) ] = attrIt.value().toString();
ptStr[ tr( "Z (adjusted)" ) ] = QString::number( attrIt.value().toDouble() * elevationProps->zScale() + elevationProps->zOffset() );
}
else if ( attrIt.key().compare( QLatin1String( "Classification" ), Qt::CaseInsensitive ) == 0 )
{
classification = QgsPointCloudDataProvider::translatedLasClassificationCodes().value( attrIt.value().toInt() );
ptStr[ attrIt.key() ] = QStringLiteral( "%1_%2 (%3)" ).arg( pcLayer->name() ).arg( attrIt.value().toString(), classification );
}
else
{
ptStr[attrIt.key()] = attrIt.value().toString();
}
}
QgsMapToolIdentify::IdentifyResult res( pcLayer, classification.isEmpty() ? QString::number( id ) : QStringLiteral( "%1 (%2)" ).arg( id ).arg( classification ), ptStr, QMap<QString, QString>() );
identifyResults.append( res );
++id;
}
QgsMapToolIdentify::fromPointCloudIdentificationToIdentifyResults( pcLayer, layerPoints[i].second, identifyResults );
}

QgsMapToolIdentifyAction *identifyTool2D = QgisApp::instance()->identifyMapTool();
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsmaptoolidentifyaction.cpp
Expand Up @@ -273,7 +273,7 @@ void QgsMapToolIdentifyAction::keyReleaseEvent( QKeyEvent *e )
}


void QgsMapToolIdentifyAction::showIdentifyResults( const QVector<IdentifyResult> &identifyResults )
void QgsMapToolIdentifyAction::showIdentifyResults( const QList<IdentifyResult> &identifyResults )
{
for ( const IdentifyResult &res : identifyResults )
{
Expand Down
8 changes: 6 additions & 2 deletions src/app/qgsmaptoolidentifyaction.h
Expand Up @@ -68,8 +68,12 @@ class APP_EXPORT QgsMapToolIdentifyAction : public QgsMapToolIdentify
void clearResults();
//! Looks up feature by its ID and outputs the result in GUI
void showResultsForFeature( QgsVectorLayer *vlayer, QgsFeatureId fid, const QgsPoint &pt );
//! Shows identification results in the GUI
void showIdentifyResults( const QVector<IdentifyResult> &identifyResults );

/**
* Shows identification results in the GUI
* \since QGIS 3.18
*/
void showIdentifyResults( const QList<IdentifyResult> &identifyResults );
public slots:
void handleCopyToClipboard( QgsFeatureStore & );
void handleChangedRasterResults( QList<QgsMapToolIdentify::IdentifyResult> &results );
Expand Down
65 changes: 36 additions & 29 deletions src/gui/qgsmaptoolidentify.cpp
Expand Up @@ -516,35 +516,9 @@ bool QgsMapToolIdentify::identifyPointCloudLayer( QList<QgsMapToolIdentify::Iden
const double searchRadiusMapUnits = mOverrideCanvasSearchRadius < 0 ? searchRadiusMU( mCanvas ) : mOverrideCanvasSearchRadius;

const QVector<QVariantMap> points = renderer->identify( layer, context, geometry, searchRadiusMapUnits );
int id = 1;
const QgsPointCloudLayerElevationProperties *elevationProps = qobject_cast< const QgsPointCloudLayerElevationProperties *>( layer->elevationProperties() );
for ( const QVariantMap &pt : points )
{
QMap<QString, QString> ptStr;
QString classification;
for ( auto attrIt = pt.constBegin(); attrIt != pt.constEnd(); ++attrIt )
{
if ( attrIt.key().compare( QLatin1String( "Z" ), Qt::CaseInsensitive ) == 0
&& ( !qgsDoubleNear( elevationProps->zScale(), 1 ) || !qgsDoubleNear( elevationProps->zOffset(), 0 ) ) )
{
// Apply elevation properties
ptStr[ tr( "Z (original)" ) ] = attrIt.value().toString();
ptStr[ tr( "Z (adjusted)" ) ] = QString::number( attrIt.value().toDouble() * elevationProps->zScale() + elevationProps->zOffset() );
}
else if ( attrIt.key().compare( QLatin1String( "Classification" ), Qt::CaseInsensitive ) == 0 )
{
classification = QgsPointCloudDataProvider::translatedLasClassificationCodes().value( attrIt.value().toInt() );
ptStr[ attrIt.key() ] = QStringLiteral( "%1 (%2)" ).arg( attrIt.value().toString(), classification );
}
else
{
ptStr[attrIt.key()] = attrIt.value().toString();
}
}
QgsMapToolIdentify::IdentifyResult res( layer, classification.isEmpty() ? QString::number( id ) : QStringLiteral( "%1 (%2)" ).arg( id ).arg( classification ), ptStr, QMap<QString, QString>() );
results->append( res );
++id;
}

fromPointCloudIdentificationToIdentifyResults( layer, points, *results );

return true;
}

Expand Down Expand Up @@ -1197,3 +1171,36 @@ void QgsMapToolIdentify::formatChanged( QgsRasterLayer *layer )
emit changedRasterResults( results );
}
}

void QgsMapToolIdentify::fromPointCloudIdentificationToIdentifyResults( QgsPointCloudLayer *layer, const QVector<QVariantMap> &identified, QList<QgsMapToolIdentify::IdentifyResult> &results )
{
int id = 1;
const QgsPointCloudLayerElevationProperties *elevationProps = qobject_cast< const QgsPointCloudLayerElevationProperties *>( layer->elevationProperties() );
for ( const QVariantMap &pt : identified )
{
QMap<QString, QString> ptStr;
QString classification;
for ( auto attrIt = pt.constBegin(); attrIt != pt.constEnd(); ++attrIt )
{
if ( attrIt.key().compare( QLatin1String( "Z" ), Qt::CaseInsensitive ) == 0
&& ( !qgsDoubleNear( elevationProps->zScale(), 1 ) || !qgsDoubleNear( elevationProps->zOffset(), 0 ) ) )
{
// Apply elevation properties
ptStr[ tr( "Z (original)" ) ] = attrIt.value().toString();
ptStr[ tr( "Z (adjusted)" ) ] = QString::number( attrIt.value().toDouble() * elevationProps->zScale() + elevationProps->zOffset() );
}
else if ( attrIt.key().compare( QLatin1String( "Classification" ), Qt::CaseInsensitive ) == 0 )
{
classification = QgsPointCloudDataProvider::translatedLasClassificationCodes().value( attrIt.value().toInt() );
ptStr[ attrIt.key() ] = QStringLiteral( "%1 (%2)" ).arg( attrIt.value().toString(), classification );
}
else
{
ptStr[attrIt.key()] = attrIt.value().toString();
}
}
QgsMapToolIdentify::IdentifyResult res( layer, classification.isEmpty() ? QString::number( id ) : QStringLiteral( "%1 (%2)" ).arg( id ).arg( classification ), ptStr, QMap<QString, QString>() );
results.append( res );
++id;
}
}
8 changes: 8 additions & 0 deletions src/gui/qgsmaptoolidentify.h
Expand Up @@ -36,6 +36,7 @@ class QgsMeshLayer;
class QgsHighlight;
class QgsIdentifyMenu;
class QgsPointCloudLayer;
class QgsPointCloudLayerElevationProperties;

/**
* \ingroup gui
Expand Down Expand Up @@ -146,6 +147,13 @@ class GUI_EXPORT QgsMapToolIdentify : public QgsMapTool
*/
QgsIdentifyMenu *identifyMenu() { return mIdentifyMenu; }

/**
* Converts point cloud identification results from variant maps to QgsMapToolIdentify::IdentifyResult and apply some formatting
* \note : the converted variant maps are pushed at the back of \a results without cleaning what's in it previously
* \since QGIS 3.18
*/
static void fromPointCloudIdentificationToIdentifyResults( QgsPointCloudLayer *layer, const QVector<QVariantMap> &identified, QList<QgsMapToolIdentify::IdentifyResult> &results );

public slots:
void formatChanged( QgsRasterLayer *layer );

Expand Down

0 comments on commit f363877

Please sign in to comment.