|
| 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 | +} |
0 commit comments