Skip to content
Permalink
Browse files

[GPS] Fix error when vector layer has Z

add elevation from GPS to point3D. Fixes #32016
  • Loading branch information
lbartoletti committed May 20, 2020
1 parent de79972 commit 600f93aa56874ec3c54a79a38238a76de33a3267
@@ -46,6 +46,7 @@
#include "qgslocaldefaultsettings.h"
#include "qgsprojectdisplaysettings.h"
#include "qgsbearingnumericformat.h"
#include "qgspolygon.h"

// QWT Charting widget

@@ -833,17 +834,20 @@ void QgsGpsInformationWidget::displayGPSInformation( const QgsGpsInformation &in
QgsPointXY myNewCenter;
nmeaPOS newNmeaPosition;
nmeaTIME newNmeaTime;
double newAlt = 0.0;
if ( validFlag )
{
myNewCenter = QgsPointXY( info.longitude, info.latitude );
newNmeaPosition.lat = nmea_degree2radian( info.latitude );
newNmeaPosition.lon = nmea_degree2radian( info.longitude );
newAlt = info.elevation;
nmea_time_now( &newNmeaTime );
}
else
{
myNewCenter = mLastGpsPosition;
newNmeaPosition = mLastNmeaPosition;
newAlt = mLastElevation;
}
if ( !mAcquisitionEnabled || ( nmea_distance( &newNmeaPosition, &mLastNmeaPosition ) < mDistanceThreshold ) )
{
@@ -925,6 +929,7 @@ void QgsGpsInformationWidget::displayGPSInformation( const QgsGpsInformation &in
mLastGpsPosition = myNewCenter;
mLastNmeaPosition = newNmeaPosition;
mLastNmeaTime = newNmeaTime;
mLastElevation = newAlt;
// Pan based on user specified behavior
if ( radRecenterMap->isChecked() || radRecenterWhenNeeded->isChecked() )
{
@@ -1042,7 +1047,9 @@ void QgsGpsInformationWidget::addVertex()

// we store the capture list in wgs84 and then transform to layer crs when
// calling close feature
mCaptureList.push_back( mLastGpsPosition );
QgsPoint point = QgsPoint( mLastGpsPosition.x(), mLastGpsPosition.y(), mLastElevation );
mCaptureList.push_back( point );


// we store the rubber band points in map canvas CRS so transform to map crs
// potential problem with transform errors and wrong coordinates if map CRS is changed after points are stored - SLM
@@ -1100,14 +1107,22 @@ void QgsGpsInformationWidget::mBtnCloseFeature_clicked()
}

QgsCoordinateTransform t( mWgs84CRS, vlayer->crs(), QgsProject::instance() );
bool is3D = QgsWkbTypes::hasZ( vlayer->wkbType() );
switch ( vlayer->geometryType() )
{
case QgsWkbTypes::PointGeometry:
{
QgsFeature f;
try
{
QgsGeometry g = QgsGeometry::fromPointXY( t.transform( mLastGpsPosition ) );
QgsPointXY pointXY = t.transform( mLastGpsPosition );

QgsGeometry g;
if ( is3D )
g = QgsGeometry( new QgsPoint( pointXY.x(), pointXY.y(), mLastElevation ) );
else
g = QgsGeometry::fromPointXY( pointXY );

if ( QgsWkbTypes::isMultiType( vlayer->wkbType() ) )
g.convertToMultiType();

@@ -1151,9 +1166,14 @@ void QgsGpsInformationWidget::mBtnCloseFeature_clicked()
QgsFeature f;
QgsGeometry g;

std::unique_ptr<QgsLineString> ring( new QgsLineString( mCaptureList ) );
if ( ! is3D )
ring->dropZValue();

if ( vlayer->geometryType() == QgsWkbTypes::LineGeometry )
{
g = QgsGeometry::fromPolylineXY( mCaptureList );

g = QgsGeometry( ring.release() );
try
{
g.transform( t );
@@ -1169,13 +1189,11 @@ void QgsGpsInformationWidget::mBtnCloseFeature_clicked()
}
else if ( vlayer->geometryType() == QgsWkbTypes::PolygonGeometry )
{
QVector< QgsPointXY > line = mCaptureList;

// close ring if required
if ( line.constFirst() != line.constLast() )
line << line.constFirst();
ring->close();
std::unique_ptr<QgsPolygon> polygon( new QgsPolygon() );
polygon->setExteriorRing( ring.release() );

g = QgsGeometry::fromPolygonXY( QVector< QgsPolylineXY > () << line );
g = QgsGeometry( polygon.release() );
try
{
g.transform( t );
@@ -133,7 +133,8 @@ class APP_EXPORT QgsGpsInformationWidget: public QgsPanelWidget, private Ui::Qgs
// not used QPointF gpsToPixelPosition( const QgsPoint& point );
QgsRubberBand *mRubberBand = nullptr;
QgsPointXY mLastGpsPosition;
QVector<QgsPointXY> mCaptureList;
QVector<QgsPoint> mCaptureList;
double mLastElevation = 0.0;
FixStatus mLastFixStatus;
QString mDateTimeFormat; // user specified format string in registry (no UI presented)
QPointer< QgsVectorLayer > mLastLayer;
@@ -270,14 +270,14 @@ void TestQgsGpsInformationWidget::testTimestampWrite()
QCOMPARE( _testWrite( tempLayerString, widget.get(), QStringLiteral( "stringf" ), Qt::TimeSpec::TimeZone ).toString( Qt::DateFormat::ISODate ), tzTime.toString( Qt::DateFormat::ISODate ) );

// Test write on line string field
widget->mCaptureList.push_back( QgsPointXY( 1, 2 ) );
widget->mCaptureList.push_back( QgsPointXY( 3, 4 ) );
widget->mCaptureList.push_back( QgsPoint( 1, 2 ) );
widget->mCaptureList.push_back( QgsPoint( 3, 4 ) );
QCOMPARE( _testWrite( tempLayerLineString, widget.get(), QStringLiteral( "stringf" ), Qt::TimeSpec::UTC ).toString( Qt::DateFormat::ISODate ), dateTime.toString( Qt::DateFormat::ISODate ) );
widget->mCaptureList.push_back( QgsPointXY( 1, 2 ) );
widget->mCaptureList.push_back( QgsPointXY( 3, 4 ) );
widget->mCaptureList.push_back( QgsPoint( 1, 2 ) );
widget->mCaptureList.push_back( QgsPoint( 3, 4 ) );
QCOMPARE( _testWrite( tempLayerLineString, widget.get(), QStringLiteral( "stringf" ), Qt::TimeSpec::LocalTime ).toString( Qt::DateFormat::ISODate ), localTime.toString( Qt::DateFormat::ISODate ) );
widget->mCaptureList.push_back( QgsPointXY( 1, 2 ) );
widget->mCaptureList.push_back( QgsPointXY( 3, 4 ) );
widget->mCaptureList.push_back( QgsPoint( 1, 2 ) );
widget->mCaptureList.push_back( QgsPoint( 3, 4 ) );
QCOMPARE( _testWrite( tempLayerLineString, widget.get(), QStringLiteral( "stringf" ), Qt::TimeSpec::TimeZone ).toString( Qt::DateFormat::ISODate ), tzTime.toString( Qt::DateFormat::ISODate ) );

// Write on GPKG

0 comments on commit 600f93a

Please sign in to comment.
You can’t perform that action at this time.