Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mesh point add elevation #58877

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 103 additions & 4 deletions src/app/mesh/qgsmaptooleditmeshframe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
#include "qgsmaptooleditmeshframe.h"

#include <QMessageBox>
#include <QVBoxLayout>
#include <QPushButton>
#include <QCheckBox>

#include "qgis.h"
#include "qgisapp.h"
Expand Down Expand Up @@ -45,7 +48,9 @@
#include "qgsmeshselectbyexpressiondialog.h"
#include "qgsmaptoolidentify.h"
#include "qgsidentifymenu.h"

#include "qgsprojectelevationproperties.h"
#include "qgscoordinatetransform.h"
#include "qgsterrainprovider.h"

//
// QgsZValueWidget
Expand All @@ -54,9 +59,12 @@

QgsZValueWidget::QgsZValueWidget( const QString &label, QWidget *parent ): QWidget( parent )
{
QHBoxLayout *layout = new QHBoxLayout( this );
QVBoxLayout *mainLayout = new QVBoxLayout( this );
mainLayout->setContentsMargins( 0, 0, 0, 0 );
setLayout( mainLayout );

QHBoxLayout *layout = new QHBoxLayout();
layout->setContentsMargins( 0, 0, 0, 0 );
setLayout( layout );

if ( !label.isEmpty() )
{
Expand All @@ -76,6 +84,22 @@ QgsZValueWidget::QgsZValueWidget( const QString &label, QWidget *parent ): QWidg
mZValueSpinBox->clear();

mZValueSpinBox->setFocusPolicy( Qt::NoFocus );

mGetZValuesButton = new QPushButton( this );
mGetZValuesButton->setText( "Get Z value from project terrain" );

mGetZValuesFromProjectElevationByDefaultCheckBox = new QCheckBox( "Set Z value from project terrain for new points", this );
JanCaha marked this conversation as resolved.
Show resolved Hide resolved

mainLayout->addLayout( layout );
mainLayout->addWidget( mGetZValuesButton );
mainLayout->addWidget( mGetZValuesFromProjectElevationByDefaultCheckBox );

connect( mGetZValuesButton, &QPushButton::pressed, this, &QgsZValueWidget::applyZValuesFromProjectElevation );
}

bool QgsZValueWidget::getZFromProjectElevationEnabled()
{
return mGetZValuesFromProjectElevationByDefaultCheckBox->isChecked();
}

double QgsZValueWidget::zValue() const
Expand Down Expand Up @@ -893,6 +917,10 @@ void QgsMapToolEditMeshFrame::cadCanvasReleaseEvent( QgsMapMouseEvent *e )
{
addVertex( mFirstClickPoint, e->mapPointMatch() );
mCadDockWidget->setPoints( QList<QgsPointXY>() << mFirstClickPoint << mFirstClickPoint );

// after addition select the vertex for easier editing of Z value
mCurrentVertexIndex = closeVertex( mapPoint );
select( mapPoint, e->modifiers(), tolerance );
}
else if ( mNewFaceMarker->isVisible() &&
mapPoint.distance( mNewFaceMarker->center() ) < tolerance
Expand Down Expand Up @@ -2047,6 +2075,49 @@ void QgsMapToolEditMeshFrame::selectContainedByGeometry( const QgsGeometry &geom
setSelectedVertices( selectedVertices.values(), behavior );
}

void QgsMapToolEditMeshFrame::applyZValueFromProjectTerrainOnSelectedVertices()
{
if ( !mZValueWidget )
return;

if ( mSelectedVertices.isEmpty() )
return;

const QgsAbstractTerrainProvider *terrainProvider = QgsProject::instance()->elevationProperties()->terrainProvider();
const QgsCoordinateTransform transformation = QgsCoordinateTransform( mCurrentLayer->crs(), terrainProvider->crs(), QgsProject::instance() );

QMap<int, double> zValues;
QgsPointXY point;
bool vertexTransformed;
double elevation;

for ( QMap<int, SelectedVertexData>::iterator it = mSelectedVertices.begin(); it != mSelectedVertices.end(); ++it )
{
const QgsPoint vertex = mapVertex( it.key() );

try
{
point = transformation.transform( vertex.x(), vertex.y() );
vertexTransformed = true;
}
catch ( const QgsCsException & )
{
vertexTransformed = false;
}

if ( vertexTransformed )
{
elevation = terrainProvider->heightAt( point.x(), point.y() );
if ( !std::isnan( elevation ) )
{
zValues.insert( it.key(), elevation );
}
}
}

mCurrentEditor->changeZValues( zValues.keys(), zValues.values() );
}

void QgsMapToolEditMeshFrame::applyZValueOnSelectedVertices()
{
if ( !mZValueWidget )
Expand Down Expand Up @@ -2631,6 +2702,8 @@ void QgsMapToolEditMeshFrame::createZValueWidget()
mZValueWidget->setDefaultValue( defaultZValue() );
mZValueWidget->setZValue( mUserZValue );
QgisApp::instance()->addUserInputWidget( mZValueWidget );

connect( mZValueWidget, &QgsZValueWidget::applyZValuesFromProjectElevation, this, &QgsMapToolEditMeshFrame::applyZValueFromProjectTerrainOnSelectedVertices );
}

void QgsMapToolEditMeshFrame::deleteZValueWidget()
Expand Down Expand Up @@ -2717,7 +2790,33 @@ void QgsMapToolEditMeshFrame::addVertex(
zValue = QgsMeshLayerUtils::interpolateFromVerticesData( v1, v2, v3, v1.z(), v2.z(), v3.z(), mapPoint );
}
else
zValue = currentZValue();
{
if ( mZValueWidget->getZFromProjectElevationEnabled() )
{
const QgsAbstractTerrainProvider *terrainProvider = QgsProject::instance()->elevationProperties()->terrainProvider();
const QgsCoordinateTransform transformation = QgsCoordinateTransform( mCurrentLayer->crs(), terrainProvider->crs(), QgsProject::instance() );

try
{
const QgsPointXY point = transformation.transform( effectivePoint.x(), effectivePoint.y() );
zValue = terrainProvider->heightAt( point.x(), point.y() );
}
catch ( const QgsCsException & )
{
zValue = std::numeric_limits<double>::quiet_NaN();
}
Comment on lines +2793 to +2807
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@uclaros unlike what we discussed last week, this cannot be moved up in the hierarchy of those if else statements.

I believe that this settings should only affect newly added vertices that are isolated. We probably do not want to affect behavior when adding vertices on edges and in faces (those have Z interpolated from the MESH vertices itself) and that is what the previous if else statements do. We only what to affect the else branch which are the isolated vertices. So I believe that this should be correct.


// either outside of terrain or the point cannot be transformed to terrainProvider CRS, use currentZValue
if ( std::isnan( zValue ) )
{
zValue = currentZValue();
}
}
else
{
zValue = currentZValue();
}
}

const QVector<QgsMeshVertex> points( 1, QgsMeshVertex( effectivePoint.x(), effectivePoint.y(), zValue ) );
if ( mCurrentEditor )
Expand Down
11 changes: 11 additions & 0 deletions src/app/mesh/qgsmaptooleditmeshframe.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class QgsSnapIndicator;
class QgsMeshTransformCoordinatesDockWidget;
class QComboBox;
class QCheckBox;
class QPushButton;
class QCheckBox;
class QgsUnitSelectionWidget;
class QgsMapToolSelectionHandler;

Expand All @@ -58,6 +60,9 @@ class APP_EXPORT QgsZValueWidget : public QWidget
//! Sets the current value \a z of the widget
void setZValue( double z );

//! Should z value be extract from project elevation setting
bool getZFromProjectElevationEnabled();

/**
* Sets the current value of the widget and set it as the default one,
* that is the value that is retrieve if the z value spin box is cleared
Expand All @@ -66,8 +71,13 @@ class APP_EXPORT QgsZValueWidget : public QWidget

QWidget *keyboardEntryWidget() const;

signals:
void applyZValuesFromProjectElevation();
JanCaha marked this conversation as resolved.
Show resolved Hide resolved

private:
QgsDoubleSpinBox *mZValueSpinBox = nullptr;
QPushButton *mGetZValuesButton = nullptr;
QCheckBox *mGetZValuesFromProjectElevationByDefaultCheckBox = nullptr;
};

class QgsMeshEditForceByLineAction : public QWidgetAction
Expand Down Expand Up @@ -235,6 +245,7 @@ class APP_EXPORT QgsMapToolEditMeshFrame : public QgsMapToolAdvancedDigitizing
void selectTouchedByGeometry( const QgsGeometry &geometry, Qgis::SelectBehavior behavior );
void selectContainedByGeometry( const QgsGeometry &geometry, Qgis::SelectBehavior behavior );
void applyZValueOnSelectedVertices();
void applyZValueFromProjectTerrainOnSelectedVertices();
void prepareSelection();
void updateSelectecVerticesMarker();
void moveSelection( const QgsPointXY &destinationPoint );
Expand Down
Loading