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 "qgsogrutils.h"
20 
21 #include <gdal.h>
22 
23 #include <QFile>
24 
setRaster(const QString & fileRaster)25 void QgsRasterChangeCoords::setRaster( const QString &fileRaster )
26 {
27   GDALAllRegister();
28   gdal::dataset_unique_ptr hDS( GDALOpen( fileRaster.toUtf8().constData(), GA_ReadOnly ) );
29   double adfGeoTransform[6];
30   if ( GDALGetProjectionRef( hDS.get() ) && GDALGetGeoTransform( hDS.get(), adfGeoTransform ) == CE_None )
31     //if ( false )
32   {
33     mHasCrs = true;
34     mUL_X = adfGeoTransform[0];
35     mUL_Y = adfGeoTransform[3];
36     mResX = adfGeoTransform[1];
37     mResY = adfGeoTransform[5];
38   }
39   else
40   {
41     mHasCrs = false;
42   }
43 }
44 
getPixelCoords(const QVector<QgsPointXY> & mapCoords)45 QVector<QgsPointXY> QgsRasterChangeCoords::getPixelCoords( const QVector<QgsPointXY> &mapCoords )
46 {
47   const int size = mapCoords.size();
48   QVector<QgsPointXY> pixelCoords( size );
49   for ( int i = 0; i < size; i++ )
50   {
51     pixelCoords[i] = toColumnLine( mapCoords.at( i ) );
52   }
53   return pixelCoords;
54 }
55 
getBoundingBox(const QgsRectangle & rect,bool toPixel)56 QgsRectangle QgsRasterChangeCoords::getBoundingBox( const QgsRectangle &rect, bool toPixel )
57 {
58   QgsRectangle rectReturn;
59   QgsPointXY p1( rect.xMinimum(), rect.yMinimum() );
60   QgsPointXY p2( rect.xMaximum(), rect.yMaximum() );
61   QgsPointXY( QgsRasterChangeCoords::* func )( const QgsPointXY & );
62 
63   func = toPixel ? &QgsRasterChangeCoords::toColumnLine : &QgsRasterChangeCoords::toXY;
64   rectReturn.set( ( this->*func )( p1 ), ( this->*func )( p2 ) );
65 
66   return rectReturn;
67 }
68 
toColumnLine(const QgsPointXY & pntMap)69 QgsPointXY QgsRasterChangeCoords::toColumnLine( const QgsPointXY &pntMap )
70 {
71   double col = ( pntMap.x() - mUL_X ) / mResX;
72   double line = ( mUL_Y - pntMap.y() ) / mResY;
73   return QgsPointXY( col, line );
74 }
75 
toXY(const QgsPointXY & pntPixel)76 QgsPointXY QgsRasterChangeCoords::toXY( const QgsPointXY &pntPixel )
77 {
78   double x = mUL_X + ( pntPixel.x() *  mResX );
79   double y = mUL_Y + ( pntPixel.y() * -mResY );
80   return QgsPointXY( x, y );
81 }
82