|
QGIS API Documentation
master-28efcda
|
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 00019 #include <QPoint> 00020 #include <QTextStream> 00021 #include <QVector> 00022 00023 #include "qgslogger.h" 00024 00025 QgsMapToPixel::QgsMapToPixel( double mapUnitsPerPixel, 00026 double ymax, 00027 double ymin, 00028 double xmin ) 00029 : mMapUnitsPerPixel( mapUnitsPerPixel ) 00030 , yMax( ymax ) 00031 , yMin( ymin ) 00032 , xMin( xmin ) 00033 , xMax( 0 ) // XXX wasn't originally specified? Why? 00034 { 00035 } 00036 00037 QgsMapToPixel::~QgsMapToPixel() 00038 { 00039 } 00040 00041 QgsPoint QgsMapToPixel::toMapPoint( double x, double y ) const 00042 { 00043 double mx = x * mMapUnitsPerPixel + xMin; 00044 double my = -1 * (( y - yMax ) * mMapUnitsPerPixel - yMin ); 00045 return QgsPoint( mx, my ); 00046 } 00047 00048 QgsPoint QgsMapToPixel::toMapCoordinates( QPoint p ) const 00049 { 00050 QgsPoint mapPt = toMapPoint( p.x(), p.y() ); 00051 return QgsPoint( mapPt ); 00052 } 00053 00054 QgsPoint QgsMapToPixel::toMapCoordinates( int x, int y ) const 00055 { 00056 return toMapPoint( x, y ); 00057 } 00058 00059 QgsPoint QgsMapToPixel::toMapCoordinatesF( double x, double y ) const 00060 { 00061 return toMapPoint( x, y ); 00062 } 00063 00064 void QgsMapToPixel::setMapUnitsPerPixel( double mapUnitsPerPixel ) 00065 { 00066 mMapUnitsPerPixel = mapUnitsPerPixel; 00067 } 00068 00069 double QgsMapToPixel::mapUnitsPerPixel() const 00070 { 00071 return mMapUnitsPerPixel; 00072 } 00073 00074 void QgsMapToPixel::setYMaximum( double ymax ) 00075 { 00076 yMax = ymax; 00077 } 00078 00079 void QgsMapToPixel::setYMinimum( double ymin ) 00080 { 00081 yMin = ymin; 00082 } 00083 00084 void QgsMapToPixel::setXMinimum( double xmin ) 00085 { 00086 xMin = xmin; 00087 } 00088 00089 void QgsMapToPixel::setParameters( double mapUnitsPerPixel, double xmin, double ymin, double ymax ) 00090 { 00091 mMapUnitsPerPixel = mapUnitsPerPixel; 00092 xMin = xmin; 00093 yMin = ymin; 00094 yMax = ymax; 00095 00096 } 00097 00098 QString QgsMapToPixel::showParameters() 00099 { 00100 QString rep; 00101 QTextStream( &rep ) << "Map units/pixel: " << mMapUnitsPerPixel 00102 << " X minimum: " << xMin << " Y minimum: " << yMin << " Y maximum: " << yMax; 00103 return rep; 00104 00105 } 00106 00107 00108 QgsPoint QgsMapToPixel::transform( double x, double y ) const 00109 { 00110 transformInPlace( x, y ); 00111 return QgsPoint( x, y ); 00112 } 00113 00114 QgsPoint QgsMapToPixel::transform( const QgsPoint& p ) const 00115 { 00116 double dx = p.x(); 00117 double dy = p.y(); 00118 transformInPlace( dx, dy ); 00119 00120 // QgsDebugMsg(QString("Point to pixel...X : %1-->%2, Y: %3 -->%4").arg(p.x()).arg(dx).arg(p.y()).arg(dy)); 00121 return QgsPoint( dx, dy ); 00122 } 00123 00124 void QgsMapToPixel::transform( QgsPoint* p ) const 00125 { 00126 double x = p->x(); 00127 double y = p->y(); 00128 transformInPlace( x, y ); 00129 00130 #ifdef QGISDEBUG 00131 // QgsDebugMsg(QString("Point to pixel...X : %1-->%2, Y: %3 -->%4").arg(p->x()).arg(x).arg(p->y()).arg(y)); 00132 #endif 00133 p->set( x, y ); 00134 } 00135 00136 void QgsMapToPixel::transformInPlace( double& x, double& y ) const 00137 { 00138 x = ( x - xMin ) / mMapUnitsPerPixel; 00139 y = yMax - ( y - yMin ) / mMapUnitsPerPixel; 00140 } 00141 00142 void QgsMapToPixel::transformInPlace( QVector<double>& x, 00143 QVector<double>& y ) const 00144 { 00145 assert( x.size() == y.size() ); 00146 for ( int i = 0; i < x.size(); ++i ) 00147 transformInPlace( x[i], y[i] ); 00148 } 00149 00150 #ifdef ANDROID 00151 void QgsMapToPixel::transformInPlace( float& x, float& y ) const 00152 { 00153 x = ( x - xMin ) / mMapUnitsPerPixel; 00154 y = yMax - ( y - yMin ) / mMapUnitsPerPixel; 00155 } 00156 00157 void QgsMapToPixel::transformInPlace( QVector<float>& x, 00158 QVector<float>& y ) const 00159 { 00160 assert( x.size() == y.size() ); 00161 for ( unsigned int i = 0; i < x.size(); ++i ) 00162 transformInPlace( x[i], y[i] ); 00163 } 00164 #endif