Skip to content

Commit

Permalink
New files added
Browse files Browse the repository at this point in the history
  • Loading branch information
homann committed Sep 10, 2012
1 parent 82a1591 commit e3e74bb
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 0 deletions.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
89 changes: 89 additions & 0 deletions src/plugins/georeferencer/qgsrasterchangecoords.cpp
@@ -0,0 +1,89 @@
/***************************************************************************
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>

#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
@@ -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

0 comments on commit e3e74bb

Please sign in to comment.