Quantum GIS API Documentation
1.8
|
00001 /*************************************************************************** 00002 qgsmaptopixel.cpp - description 00003 ------------------- 00004 begin : Sat Jun 22 2002 00005 copyright : (C) 2002 by Gary E.Sherman 00006 email : sherman at mrcc.com 00007 ***************************************************************************/ 00008 00009 /*************************************************************************** 00010 * * 00011 * This program is free software; you can redistribute it and/or modify * 00012 * it under the terms of the GNU General Public License as published by * 00013 * the Free Software Foundation; either version 2 of the License, or * 00014 * (at your option) any later version. * 00015 * * 00016 ***************************************************************************/ 00017 #include "qgsmaptopixel.h" 00018 #include <QPoint> 00019 #include <QTextStream> 00020 #include "qgslogger.h" 00021 00022 QgsMapToPixel::QgsMapToPixel( double mapUnitsPerPixel, 00023 double ymax, 00024 double ymin, 00025 double xmin ) 00026 : mMapUnitsPerPixel( mapUnitsPerPixel ), 00027 yMax( ymax ), 00028 yMin( ymin ), 00029 xMin( xmin ), 00030 xMax( 0 ) // XXX wasn't originally specified? Why? 00031 { 00032 } 00033 00034 QgsMapToPixel::~QgsMapToPixel() 00035 { 00036 } 00037 00038 QgsPoint QgsMapToPixel::toMapPoint( double x, double y ) const 00039 { 00040 double mx = x * mMapUnitsPerPixel + xMin; 00041 double my = -1 * (( y - yMax ) * mMapUnitsPerPixel - yMin ); 00042 return QgsPoint( mx, my ); 00043 } 00044 00045 QgsPoint QgsMapToPixel::toMapCoordinates( QPoint p ) const 00046 { 00047 QgsPoint mapPt = toMapPoint( p.x(), p.y() ); 00048 return QgsPoint( mapPt ); 00049 } 00050 00051 QgsPoint QgsMapToPixel::toMapCoordinates( int x, int y ) const 00052 { 00053 return toMapPoint( x, y ); 00054 } 00055 00056 QgsPoint QgsMapToPixel::toMapCoordinatesF( double x, double y ) const 00057 { 00058 return toMapPoint( x, y ); 00059 } 00060 00061 void QgsMapToPixel::setMapUnitsPerPixel( double mapUnitsPerPixel ) 00062 { 00063 mMapUnitsPerPixel = mapUnitsPerPixel; 00064 } 00065 00066 double QgsMapToPixel::mapUnitsPerPixel() const 00067 { 00068 return mMapUnitsPerPixel; 00069 } 00070 00071 void QgsMapToPixel::setYMaximum( double ymax ) 00072 { 00073 yMax = ymax; 00074 } 00075 00076 void QgsMapToPixel::setYMinimum( double ymin ) 00077 { 00078 yMin = ymin; 00079 } 00080 00081 void QgsMapToPixel::setXMinimum( double xmin ) 00082 { 00083 xMin = xmin; 00084 } 00085 00086 void QgsMapToPixel::setParameters( double mapUnitsPerPixel, double xmin, double ymin, double ymax ) 00087 { 00088 mMapUnitsPerPixel = mapUnitsPerPixel; 00089 xMin = xmin; 00090 yMin = ymin; 00091 yMax = ymax; 00092 00093 } 00094 00095 QString QgsMapToPixel::showParameters() 00096 { 00097 QString rep; 00098 QTextStream( &rep ) << "Map units/pixel: " << mMapUnitsPerPixel 00099 << " X minimum: " << xMin << " Y minimum: " << yMin << " Y maximum: " << yMax; 00100 return rep; 00101 00102 } 00103 00104 00105 QgsPoint QgsMapToPixel::transform( double x, double y ) const 00106 { 00107 transformInPlace( x, y ); 00108 return QgsPoint( x, y ); 00109 } 00110 00111 QgsPoint QgsMapToPixel::transform( const QgsPoint& p ) const 00112 { 00113 double dx = p.x(); 00114 double dy = p.y(); 00115 transformInPlace( dx, dy ); 00116 00117 // QgsDebugMsg(QString("Point to pixel...X : %1-->%2, Y: %3 -->%4").arg(p.x()).arg(dx).arg(p.y()).arg(dy)); 00118 return QgsPoint( dx, dy ); 00119 } 00120 00121 void QgsMapToPixel::transform( QgsPoint* p ) const 00122 { 00123 double x = p->x(); 00124 double y = p->y(); 00125 transformInPlace( x, y ); 00126 00127 #ifdef QGISDEBUG 00128 // QgsDebugMsg(QString("Point to pixel...X : %1-->%2, Y: %3 -->%4").arg(p->x()).arg(x).arg(p->y()).arg(y)); 00129 #endif 00130 p->set( x, y ); 00131 } 00132 00133 void QgsMapToPixel::transformInPlace( double& x, double& y ) const 00134 { 00135 x = ( x - xMin ) / mMapUnitsPerPixel; 00136 y = yMax - ( y - yMin ) / mMapUnitsPerPixel; 00137 } 00138 00139 void QgsMapToPixel::transformInPlace( std::vector<double>& x, 00140 std::vector<double>& y ) const 00141 { 00142 assert( x.size() == y.size() ); 00143 for ( unsigned int i = 0; i < x.size(); ++i ) 00144 transformInPlace( x[i], y[i] ); 00145 } 00146 00147 #ifdef ANDROID 00148 void QgsMapToPixel::transformInPlace( float& x, float& y ) const 00149 { 00150 x = ( x - xMin ) / mMapUnitsPerPixel; 00151 y = yMax - ( y - yMin ) / mMapUnitsPerPixel; 00152 } 00153 00154 void QgsMapToPixel::transformInPlace( std::vector<float>& x, 00155 std::vector<float>& y ) const 00156 { 00157 assert( x.size() == y.size() ); 00158 for ( unsigned int i = 0; i < x.size(); ++i ) 00159 transformInPlace( x[i], y[i] ); 00160 } 00161 #endif