Skip to content

Commit

Permalink
GPC point fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
roya0045 authored and nyalldawson committed Feb 9, 2021
1 parent 3e7ea83 commit 07c23ab
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 26 deletions.
28 changes: 24 additions & 4 deletions src/app/georeferencer/qgsgcpcanvasitem.cpp
Expand Up @@ -18,8 +18,10 @@
#include "qgsproject.h"
#include "qgsrasterlayer.h"
#include "qgssettings.h"
#include "qgscoordinatereferencesystem.h"
#include "qgscoordinatetransform.h"

QgsGCPCanvasItem::QgsGCPCanvasItem( QgsMapCanvas *mapCanvas, const QgsGeorefDataPoint *dataPoint, bool isGCPSource )
QgsGCPCanvasItem::QgsGCPCanvasItem( QgsMapCanvas *mapCanvas, QgsGeorefDataPoint *dataPoint, bool isGCPSource )
: QgsMapCanvasItem( mapCanvas )
, mDataPoint( dataPoint )
, mPointBrush( Qt::red )
Expand All @@ -46,14 +48,14 @@ void QgsGCPCanvasItem::paint( QPainter *p )
bool enabled = true;
QgsPointXY worldCoords;
int id = -1;
QgsCoordinateReferenceSystem mapCrs = mMapCanvas->mapSettings().destinationCrs();

if ( mDataPoint )
{
enabled = mDataPoint->isEnabled();
worldCoords = mDataPoint->transCoords();
worldCoords = mDataPoint->canvasCoords();
id = mDataPoint->id();
}

p->setOpacity( enabled ? 1.0 : 0.3 );

// draw the point
Expand Down Expand Up @@ -158,7 +160,25 @@ void QgsGCPCanvasItem::updatePosition()
return;
}

setPos( toCanvasCoordinates( mIsGCPSource ? mDataPoint->pixelCoords() : mDataPoint->mapCoords() ) );
if ( mIsGCPSource )
{
setPos( toCanvasCoordinates( mDataPoint->pixelCoords() ) );
return;
}
if ( mDataPoint->canvasCoords().isEmpty() )
{
QgsCoordinateReferenceSystem mapCrs = mMapCanvas->mapSettings().destinationCrs();
QgsPointXY mapCoords;
if ( mapCrs.toWkt() == mDataPoint->crs().toWkt() )
mapCoords = mDataPoint->mapCoords();
else
{
QgsCoordinateTransform transf( mDataPoint->crs(), mapCrs, QgsProject::instance() );
mapCoords = transf.transform( mDataPoint->mapCoords() );
}
mDataPoint->setCanvasCoords( mapCoords );
}
setPos( toCanvasCoordinates( mDataPoint->canvasCoords() ) );
}

void QgsGCPCanvasItem::drawResidualArrow( QPainter *p, const QgsRenderContext &context )
Expand Down
4 changes: 2 additions & 2 deletions src/app/georeferencer/qgsgcpcanvasitem.h
Expand Up @@ -24,7 +24,7 @@ class QgsGeorefDataPoint;
class QgsGCPCanvasItem : public QgsMapCanvasItem
{
public:
QgsGCPCanvasItem( QgsMapCanvas *mapCanvas, const QgsGeorefDataPoint *dataPoint, bool isGCPSource/* = true*/ );
QgsGCPCanvasItem( QgsMapCanvas *mapCanvas, QgsGeorefDataPoint *dataPoint, bool isGCPSource/* = true*/ );

//! draws point information
void paint( QPainter *p ) override;
Expand All @@ -43,7 +43,7 @@ class QgsGCPCanvasItem : public QgsMapCanvasItem

private:

const QgsGeorefDataPoint *mDataPoint = nullptr;
QgsGeorefDataPoint *mDataPoint = nullptr;
QSizeF mTextBounds;
QBrush mPointBrush;
QBrush mLabelBrush;
Expand Down
20 changes: 18 additions & 2 deletions src/app/georeferencer/qgsgeorefdatapoint.cpp
Expand Up @@ -33,6 +33,7 @@ QgsGeorefDataPoint::QgsGeorefDataPoint( QgsMapCanvas *srcCanvas, QgsMapCanvas *d
, mEnabled( enable )
{
mTransCoords = QgsPointXY( mapCoords );
mCanvasCoords = QgsPointXY();
mGCPSourceItem = new QgsGCPCanvasItem( srcCanvas, this, true );
mGCPDestinationItem = new QgsGCPCanvasItem( dstCanvas, this, false );
mGCPSourceItem->setEnabled( enable );
Expand All @@ -52,6 +53,7 @@ QgsGeorefDataPoint::QgsGeorefDataPoint( const QgsGeorefDataPoint &p )
mTransCoords = p.transCoords();
mEnabled = p.isEnabled();
mResidual = p.residual();
mCanvasCoords = p.canvasCoords();
mCrs = p.crs();
mId = p.id();
}
Expand Down Expand Up @@ -100,6 +102,17 @@ QgsPointXY QgsGeorefDataPoint::transCoords() const
return mTransCoords.isEmpty() ? mMapCoords : mTransCoords;
}


void QgsGeorefDataPoint::setCanvasCoords( const QgsPointXY &p )
{
mCanvasCoords = p;
}

QgsPointXY QgsGeorefDataPoint::canvasCoords() const
{
return mCanvasCoords;
}

void QgsGeorefDataPoint::setEnabled( bool enabled )
{
mEnabled = enabled;
Expand Down Expand Up @@ -169,12 +182,15 @@ void QgsGeorefDataPoint::moveTo( QPoint p, bool isMapPlugin )
else
{
QgsPointXY pnt = mGCPDestinationItem->toMapCoordinates( p );
setCanvasCoords( pnt );
mMapCoords = pnt;
if ( mSrcCanvas && !mSrcCanvas->mapSettings().destinationCrs().isValid() )
if ( mSrcCanvas && mSrcCanvas->mapSettings().destinationCrs().isValid() )
mCrs = mSrcCanvas->mapSettings().destinationCrs();
else
mCrs = mGCPDestinationItem->canvas()->mapSettings().destinationCrs() ;
mCrs = mGCPDestinationItem->canvas()->mapSettings().destinationCrs();
}
if ( !mCrs.isValid() )
mCrs = QgsProject::instance()->crs();
mGCPSourceItem->update();
mGCPDestinationItem->update();
updateCoords();
Expand Down
4 changes: 4 additions & 0 deletions src/app/georeferencer/qgsgeorefdatapoint.h
Expand Up @@ -43,6 +43,9 @@ class QgsGeorefDataPoint : public QObject
QgsPointXY transCoords() const;
void setTransCoords( const QgsPointXY &p );

QgsPointXY canvasCoords() const;
void setCanvasCoords( const QgsPointXY &p );

bool isEnabled() const { return mEnabled; }
void setEnabled( bool enabled );

Expand Down Expand Up @@ -71,6 +74,7 @@ class QgsGeorefDataPoint : public QObject
QgsPointXY mPixelCoords;
QgsPointXY mMapCoords;
QgsPointXY mTransCoords;
QgsPointXY mCanvasCoords;

int mId;
QgsCoordinateReferenceSystem mCrs;
Expand Down
54 changes: 36 additions & 18 deletions src/app/georeferencer/qgsgeorefmainwindow.cpp
Expand Up @@ -545,6 +545,8 @@ void QgsGeoreferencerMainWindow::addPoint( const QgsPointXY &pixelCoords, const
{
QgsGeorefDataPoint *pnt = new QgsGeorefDataPoint( mCanvas, QgisApp::instance()->mapCanvas(), pixelCoords, mapCoords, crs, enable );
mPoints.append( pnt );
if ( !mLastGCPProjection.isValid() || mLastGCPProjection.toWkt() != crs.toWkt() )
mLastGCPProjection = QgsCoordinateReferenceSystem( crs );
mGCPsDirty = true;
if ( finalize )
{
Expand Down Expand Up @@ -632,9 +634,11 @@ void QgsGeoreferencerMainWindow::releasePoint( QPoint p )

void QgsGeoreferencerMainWindow::showCoordDialog( const QgsPointXY &pixelCoords )
{

QgsCoordinateReferenceSystem lastProjection = mLastGCPProjection.isValid() ? mLastGCPProjection : mProjection;
if ( mLayer && !mMapCoordsDialog )
{
mMapCoordsDialog = new QgsMapCoordsDialog( QgisApp::instance()->mapCanvas(), pixelCoords, mProjection, this );
mMapCoordsDialog = new QgsMapCoordsDialog( QgisApp::instance()->mapCanvas(), pixelCoords, lastProjection, this );
connect( mMapCoordsDialog, &QgsMapCoordsDialog::pointAdded, this, [ = ]( const QgsPointXY & a, const QgsPointXY & b, const QgsCoordinateReferenceSystem & crs )
{
addPoint( a, b, crs );
Expand Down Expand Up @@ -1180,6 +1184,7 @@ void QgsGeoreferencerMainWindow::setupConnections()

// Connect mapCanvas rotation widget
connect( mRotationEdit, static_cast < void ( QgsDoubleSpinBox::* )( double ) > ( &QgsDoubleSpinBox::valueChanged ), this, &QgsGeoreferencerMainWindow::updateCanvasRotation );
connect( mCanvas, &QgsMapCanvas::destinationCrsChanged, this, &QgsGeoreferencerMainWindow::invalidateCanvasCoords );
}

void QgsGeoreferencerMainWindow::removeOldLayer()
Expand Down Expand Up @@ -1273,30 +1278,31 @@ bool QgsGeoreferencerMainWindow::loadGCPs( /*bool verbose*/ )
QTextStream points( &pointFile );
QString line = points.readLine();
int i = 0;
QgsCoordinateReferenceSystem proj;
if ( line.contains( "#CRS: " ) )
{
proj = QgsCoordinateReferenceSystem( line.remove( "#CRS: " ) );
line = points.readLine();
}
else
proj = QgsProject::instance()->crs();

while ( !points.atEnd() )
{
line = points.readLine();
QStringList ls;
if ( line.contains( ',' ) ) // in previous format "\t" is delimiter of points in new - ","
{
// points from new georeferencer
ls = line.split( ',' );
}
ls = line.split( ',' ); // points from new georeferencer
else
{
// points from prev georeferencer
ls = line.split( '\t' );
}
ls = line.split( '\t' ); // points from prev georeferencer

if ( ls.count() < 4 )
{
return false;
}

QgsPointXY mapCoords( ls.at( 0 ).toDouble(), ls.at( 1 ).toDouble() ); // map x,y
QgsPointXY pixelCoords( ls.at( 2 ).toDouble(), ls.at( 3 ).toDouble() ); // pixel x,y
QgsCoordinateReferenceSystem proj( ls.at( 8 ) );
if ( ls.count() == 5 || ls.count() == 9 )

if ( ls.count() == 5 )
{
bool enable = ls.at( 4 ).toInt();
addPoint( pixelCoords, mapCoords, proj, enable, false );
Expand Down Expand Up @@ -1326,19 +1332,19 @@ void QgsGeoreferencerMainWindow::saveGCPs()
if ( pointFile.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
{
QTextStream points( &pointFile );
points << QStringLiteral( "#CRS: %1" ).arg( mProjection.toWkt( QgsCoordinateReferenceSystem::WKT_PREFERRED ) ) << endl;
points << "mapX,mapY,pixelX,pixelY,enable,dX,dY,residual" << endl;
for ( QgsGeorefDataPoint *pt : qgis::as_const( mPoints ) )
{
points << QStringLiteral( "%1,%2,%3,%4,%5,%6,%7,%8,%9" )
.arg( qgsDoubleToString( pt->mapCoords().x() ),
qgsDoubleToString( pt->mapCoords().y() ),
points << QStringLiteral( "%1,%2,%3,%4,%5,%6,%7,%8" )
.arg( qgsDoubleToString( pt->transCoords().x() ),
qgsDoubleToString( pt->transCoords().y() ),
qgsDoubleToString( pt->pixelCoords().x() ),
qgsDoubleToString( pt->pixelCoords().y() ) )
.arg( pt->isEnabled() )
.arg( qgsDoubleToString( pt->residual().x() ),
qgsDoubleToString( pt->residual().y() ),
qgsDoubleToString( std::sqrt( pt->residual().x() * pt->residual().x() + pt->residual().y() * pt->residual().y() ) ),
pt->crs().toWkt( QgsCoordinateReferenceSystem::WKT_PREFERRED ) )
qgsDoubleToString( std::sqrt( pt->residual().x() * pt->residual().x() + pt->residual().y() * pt->residual().y() ) ) )
<< endl;
}

Expand Down Expand Up @@ -2226,3 +2232,15 @@ void QgsGeoreferencerMainWindow::clearGCPData()

QgisApp::instance()->mapCanvas()->refresh();
}

void QgsGeoreferencerMainWindow::invalidateCanvasCoords()
{
int count = mPoints.count();
int j = 0;
for ( int i = 0; i < count; ++i, ++j )
{
QgsGeorefDataPoint *p = mPoints.at( i );
p->setCanvasCoords( QgsPointXY() );
p->updateCoords();
}
}
2 changes: 2 additions & 0 deletions src/app/georeferencer/qgsgeorefmainwindow.h
Expand Up @@ -124,6 +124,7 @@ class QgsGeoreferencerMainWindow : public QMainWindow, private Ui::QgsGeorefPlug
void extentsChanged(); // Use for need add again Raster (case above)

bool updateGeorefTransform();
void invalidateCanvasCoords();

private:
enum SaveGCPs
Expand Down Expand Up @@ -225,6 +226,7 @@ class QgsGeoreferencerMainWindow : public QMainWindow, private Ui::QgsGeorefPlug
QString mTranslatedRasterFileName;
QString mGCPpointsFileName;
QgsCoordinateReferenceSystem mProjection;
QgsCoordinateReferenceSystem mLastGCPProjection;
QString mPdfOutputFile;
QString mPdfOutputMapFile;
QString mSaveGcp;
Expand Down

0 comments on commit 07c23ab

Please sign in to comment.