Skip to content

Commit e3e74bb

Browse files
committedSep 10, 2012
New files added
1 parent 82a1591 commit e3e74bb

File tree

4 files changed

+132
-0
lines changed

4 files changed

+132
-0
lines changed
 
Loading
Loading
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/***************************************************************************
2+
qgsrasterchangecoords.cpp
3+
--------------------------------------
4+
Date : 25-June-2011
5+
Copyright : (C) 2011 by Luiz Motta
6+
Email : motta.luiz at gmail.com
7+
***************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
16+
#include "qgsrasterchangecoords.h"
17+
18+
#include<qgspoint.h>
19+
#include <gdal.h>
20+
21+
#if defined(GDAL_VERSION_NUM) && GDAL_VERSION_NUM >= 1800
22+
#define TO8F(x) (x).toUtf8().constData()
23+
#else
24+
#define TO8F(x) QFile::encodeName( x ).constData()
25+
#endif
26+
27+
QgsRasterChangeCoords::QgsRasterChangeCoords()
28+
{
29+
mHasCrs = false;
30+
}
31+
32+
void QgsRasterChangeCoords::setRaster( const QString &fileRaster )
33+
{
34+
GDALAllRegister();
35+
GDALDatasetH hDS = GDALOpen( TO8F( fileRaster ), GA_ReadOnly );
36+
double adfGeoTransform[6];
37+
if( GDALGetProjectionRef( hDS ) != NULL && GDALGetGeoTransform(hDS, adfGeoTransform) == CE_None)
38+
//if ( false )
39+
{
40+
mHasCrs = true;
41+
mUL_X = adfGeoTransform[0];
42+
mUL_Y = adfGeoTransform[3];
43+
mResX = adfGeoTransform[1];
44+
mResY = adfGeoTransform[5];
45+
}
46+
else
47+
{
48+
mHasCrs = false;
49+
}
50+
GDALClose( hDS );
51+
}
52+
53+
std::vector<QgsPoint> QgsRasterChangeCoords::getPixelCoords(const std::vector<QgsPoint> &mapCoords)
54+
{
55+
const int size = mapCoords.size();
56+
std::vector<QgsPoint> pixelCoords( size );
57+
for ( int i = 0; i < size; i++ )
58+
{
59+
pixelCoords[i] = toColumnLine( mapCoords.at( i ) );
60+
}
61+
return pixelCoords;
62+
}
63+
64+
QgsRectangle QgsRasterChangeCoords::getBoundingBox(const QgsRectangle &rect, bool toPixel)
65+
{
66+
QgsRectangle rectReturn;
67+
QgsPoint p1( rect.xMinimum(), rect.yMinimum() );
68+
QgsPoint p2( rect.xMaximum(), rect.yMaximum() );
69+
QgsPoint ( QgsRasterChangeCoords::* func )( const QgsPoint & );
70+
71+
func = toPixel ? &QgsRasterChangeCoords::toColumnLine : &QgsRasterChangeCoords::toXY;
72+
rectReturn.set( ( this->*func ) (p1), ( this->*func )(p2) );
73+
74+
return rectReturn;
75+
}
76+
77+
QgsPoint QgsRasterChangeCoords::toColumnLine(const QgsPoint &pntMap)
78+
{
79+
double col = ( pntMap.x() - mUL_X ) / mResX;
80+
double line = ( mUL_Y - pntMap.y() ) / mResY;
81+
return QgsPoint(col, line);
82+
}
83+
84+
QgsPoint QgsRasterChangeCoords::toXY(const QgsPoint &pntPixel)
85+
{
86+
double x = mUL_X + ( pntPixel.x() * mResX );
87+
double y = mUL_Y + ( pntPixel.y() * -mResY );
88+
return QgsPoint(x, y);
89+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/***************************************************************************
2+
qgsrasterchangecoords.h
3+
--------------------------------------
4+
Date : 25-June-2011
5+
Copyright : (C) 2011 by Luiz Motta
6+
Email : motta.luiz at gmail.com
7+
***************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
16+
#ifndef QGSRASTERCHANGECOORDS_H
17+
#define QGSRASTERCHANGECOORDS_H
18+
19+
#include <vector>
20+
21+
#include "qgspoint.h"
22+
#include "qgsrectangle.h"
23+
24+
class QgsRasterChangeCoords
25+
{
26+
public:
27+
QgsRasterChangeCoords( );
28+
void setRaster( const QString &fileRaster );
29+
bool hasCrs() const { return mHasCrs; }
30+
std::vector<QgsPoint> getPixelCoords(const std::vector<QgsPoint> &mapCoords);
31+
QgsRectangle getBoundingBox(const QgsRectangle &rect, bool toPixel);
32+
QgsPoint toColumnLine(const QgsPoint &pntMap);
33+
QgsPoint toXY(const QgsPoint &pntPixel);
34+
35+
private:
36+
bool mHasCrs;
37+
double mUL_X;
38+
double mUL_Y;
39+
double mResX;
40+
double mResY;
41+
};
42+
43+
#endif // QGSRASTERCHANGECOORDS_H

0 commit comments

Comments
 (0)
Please sign in to comment.