23 changes: 20 additions & 3 deletions src/plugins/georeferencer/qgsgeoreftransform.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include <vector>
#include <stdexcept>

#include "qgsrasterchangecoords.h"

class QgsGeorefTransformInterface
{
public:
Expand Down Expand Up @@ -79,6 +81,20 @@ class QgsGeorefTransform : public QgsGeorefTransformInterface
*/
void selectTransformParametrisation( TransformParametrisation parametrisation );

/**
* Setting the mRasterChangeCoords for change type coordinate(map for pixel).
*/
void setRasterChangeCoords( const QString &fileRaster );

//! \returns Whether has Coordinate Reference Systems in image
bool hasCrs() const { return mRasterChangeCoords.hasCrs(); }

//! \returns Coordinates of image
QgsPoint toColumnLine(const QgsPoint &pntMap) { return mRasterChangeCoords.toColumnLine( pntMap ); }

//! \returns Bounding box of image(transform to coordinate of Map or Image )
QgsRectangle getBoundingBox(const QgsRectangle &rect, bool toPixel) { return mRasterChangeCoords.getBoundingBox( rect, toPixel); }

//! \brief The transform parametrisation currently in use.
TransformParametrisation transformParametrisation() const;

Expand Down Expand Up @@ -113,22 +129,22 @@ class QgsGeorefTransform : public QgsGeorefTransformInterface
*
* \note Negative y-axis points down in raster CS.
*/
bool transformRasterToWorld( const QgsPoint &raster, QgsPoint &world ) const;
bool transformRasterToWorld( const QgsPoint &raster, QgsPoint &world );

/**
* \brief Transform from referenced coordinates to raster coordinates.
*
* \note Negative y-axis points down in raster CS.
*/
bool transformWorldToRaster( const QgsPoint &world, QgsPoint &raster ) const;
bool transformWorldToRaster( const QgsPoint &world, QgsPoint &raster );

/**
* \brief Transforms from raster to world if rasterToWorld is true,
* \brief or from world to raster when rasterToWorld is false.
*
* \note Negative y-axis points down in raster CS.
*/
bool transform( const QgsPoint &src, QgsPoint &dst, bool rasterToWorld ) const;
bool transform( const QgsPoint &src, QgsPoint &dst, bool rasterToWorld );

//! \brief Returns origin and scale if this is a linear transform, fails otherwise.
bool getLinearOriginScale( QgsPoint &origin, double &scaleX, double &scaleY ) const;
Expand All @@ -149,6 +165,7 @@ class QgsGeorefTransform : public QgsGeorefTransformInterface
QgsGeorefTransformInterface *mGeorefTransformImplementation;
TransformParametrisation mTransformParametrisation;
bool mParametersInitialized;
QgsRasterChangeCoords mRasterChangeCoords;
};

#endif
8 changes: 4 additions & 4 deletions src/plugins/georeferencer/qgsmapcoordsdialogbase.ui
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>362</width>
<height>171</height>
<width>497</width>
<height>204</height>
</rect>
</property>
<property name="windowTitle">
Expand All @@ -32,7 +32,7 @@
<item row="0" column="0">
<widget class="QLabel" name="textLabel1">
<property name="text">
<string>X:</string>
<string>X / East:</string>
</property>
</widget>
</item>
Expand All @@ -46,7 +46,7 @@
<item row="0" column="0">
<widget class="QLabel" name="textLabel2">
<property name="text">
<string>Y:</string>
<string>Y / North:</string>
</property>
</widget>
</item>
Expand Down
91 changes: 91 additions & 0 deletions src/plugins/georeferencer/qgsrasterchangecoords.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/***************************************************************************
qgsrasterchangecoords.cpp
--------------------------------------
Date : 25-June-2011
Copyright : (C) 2011 by Luiz Motta
Email : motta.luiz at gmail.com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgsrasterchangecoords.h"

#include <qgspoint.h>
#include <gdal.h>

#include <QFile>

#if defined(GDAL_VERSION_NUM) && GDAL_VERSION_NUM >= 1800
#define TO8F(x) (x).toUtf8().constData()
#else
#define TO8F(x) QFile::encodeName( x ).constData()
#endif

QgsRasterChangeCoords::QgsRasterChangeCoords()
{
mHasCrs = false;
}

void QgsRasterChangeCoords::setRaster( const QString &fileRaster )
{
GDALAllRegister();
GDALDatasetH hDS = GDALOpen( TO8F( fileRaster ), GA_ReadOnly );
double adfGeoTransform[6];
if( GDALGetProjectionRef( hDS ) != NULL && GDALGetGeoTransform(hDS, adfGeoTransform) == CE_None)
//if ( false )
{
mHasCrs = true;
mUL_X = adfGeoTransform[0];
mUL_Y = adfGeoTransform[3];
mResX = adfGeoTransform[1];
mResY = adfGeoTransform[5];
}
else
{
mHasCrs = false;
}
GDALClose( hDS );
}

std::vector<QgsPoint> QgsRasterChangeCoords::getPixelCoords(const std::vector<QgsPoint> &mapCoords)
{
const int size = mapCoords.size();
std::vector<QgsPoint> pixelCoords( size );
for ( int i = 0; i < size; i++ )
{
pixelCoords[i] = toColumnLine( mapCoords.at( i ) );
}
return pixelCoords;
}

QgsRectangle QgsRasterChangeCoords::getBoundingBox(const QgsRectangle &rect, bool toPixel)
{
QgsRectangle rectReturn;
QgsPoint p1( rect.xMinimum(), rect.yMinimum() );
QgsPoint p2( rect.xMaximum(), rect.yMaximum() );
QgsPoint ( QgsRasterChangeCoords::* func )( const QgsPoint & );

func = toPixel ? &QgsRasterChangeCoords::toColumnLine : &QgsRasterChangeCoords::toXY;
rectReturn.set( ( this->*func ) (p1), ( this->*func )(p2) );

return rectReturn;
}

QgsPoint QgsRasterChangeCoords::toColumnLine(const QgsPoint &pntMap)
{
double col = ( pntMap.x() - mUL_X ) / mResX;
double line = ( mUL_Y - pntMap.y() ) / mResY;
return QgsPoint(col, line);
}

QgsPoint QgsRasterChangeCoords::toXY(const QgsPoint &pntPixel)
{
double x = mUL_X + ( pntPixel.x() * mResX );
double y = mUL_Y + ( pntPixel.y() * -mResY );
return QgsPoint(x, y);
}
43 changes: 43 additions & 0 deletions src/plugins/georeferencer/qgsrasterchangecoords.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/***************************************************************************
qgsrasterchangecoords.h
--------------------------------------
Date : 25-June-2011
Copyright : (C) 2011 by Luiz Motta
Email : motta.luiz at gmail.com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#ifndef QGSRASTERCHANGECOORDS_H
#define QGSRASTERCHANGECOORDS_H

#include <vector>

#include "qgspoint.h"
#include "qgsrectangle.h"

class QgsRasterChangeCoords
{
public:
QgsRasterChangeCoords( );
void setRaster( const QString &fileRaster );
bool hasCrs() const { return mHasCrs; }
std::vector<QgsPoint> getPixelCoords(const std::vector<QgsPoint> &mapCoords);
QgsRectangle getBoundingBox(const QgsRectangle &rect, bool toPixel);
QgsPoint toColumnLine(const QgsPoint &pntMap);
QgsPoint toXY(const QgsPoint &pntPixel);

private:
bool mHasCrs;
double mUL_X;
double mUL_Y;
double mResX;
double mResY;
};

#endif // QGSRASTERCHANGECOORDS_H