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