openModeller  Version 1.4.0
Map.cpp
Go to the documentation of this file.
00001 
00030 #include <openmodeller/env_io/Map.hh>
00031 
00032 #include <openmodeller/env_io/GeoTransform.hh>
00033 #include <openmodeller/env_io/Raster.hh>
00034 #include <openmodeller/Log.hh>
00035 
00036 #include <math.h>
00037 
00038 /****************************************************************/
00039 /****************************** Map *****************************/
00040 
00041 /******************/
00042 /*** construtor ***/
00043 Map::Map( Raster *rst ) 
00044 {
00045   _rst = rst;
00046   _gt  = new GeoTransform( rst->header().proj, GeoTransform::getDefaultCS() );
00047 }
00048 
00049 
00050 /*****************/
00051 /*** destrutor ***/
00052 
00053 Map::~Map()
00054 {
00055   delete _gt;
00056   delete _rst;
00057 }
00058 
00059 
00060 /******************/
00061 /*** getExtent  ***/
00062 int
00063 Map::getExtent( Coord *xmin, Coord *ymin, Coord *xmax, Coord *ymax) const
00064 {
00065   int result = 0;
00066 
00067   if (_rst->hasCustomGeotransform()) {
00068 
00069     result = _rst->getExtentInStandardCs(xmin, ymin, xmax, ymax);
00070   }
00071   else {
00072 
00073     *xmin = _rst->xMin();
00074     *ymin = _rst->yMin();
00075     *xmax = _rst->xMax();
00076     *ymax = _rst->yMax();
00077 
00078     //Log::instance()->debug( "Raster boundaries before geotransform: xmin=%f, xmax=%f, ymin=%f, ymax=%f\n", *xmin, *xmax, *ymin, *ymax );
00079 
00080     result = _gt->transfOut( xmin, ymin ) && _gt->transfOut( xmax, ymax );
00081 
00082     //Log::instance()->debug( "Raster boundaries after geotransform: xmin=%f, xmax=%f, ymin=%f, ymax=%f\n", *xmin, *xmax, *ymin, *ymax );
00083   }
00084 
00085   if (*xmin > *xmax) {
00086 
00087     *xmin = -180;
00088     *xmax = 180;
00089   }
00090 
00091   if (*ymin > *ymax) {
00092 
00093     *ymin = -90;
00094     *ymax = 90;
00095   }
00096 
00097   return result;
00098 }
00099 
00100 
00101 /***********/
00102 /*** get ***/
00103 int
00104 Map::get( Coord x, Coord y, Scalar *val ) const
00105 {
00106   return _gt->transfIn( &x, &y ) ? _rst->get( x, y, val ) : 0;
00107 }
00108 
00109 /***********/
00110 /*** put ***/
00111 int
00112 Map::put( Coord x, Coord y, Scalar val )
00113 {
00114   return _gt->transfIn( &x, &y ) ? _rst->put( x, y, val ) : 0;
00115 }
00116 
00117 /***********/
00118 /*** put ***/
00119 int
00120 Map::put( Coord x, Coord y )
00121 {
00122   return _gt->transfIn(&x,&y) ? _rst->put( x,y ) : 0;
00123 }
00124 
00125 /**********************/
00126 /*** get row column ***/
00127 int
00128 Map::getRowColumn( Coord x, Coord y, int *row, int *col )
00129 {
00130   // Transform the given coordinates into the raster coordinate system & projection
00131   int result = _gt->transfIn( &x, &y );
00132 
00133   Coord xmin = _rst->xMin();
00134   Coord ymin = _rst->yMin();
00135   Coord xmax = _rst->xMax();
00136   Coord ymax = _rst->yMax();
00137 
00138   int xdim = _rst->dimX();
00139   int ydim = _rst->dimY();
00140 
00141   double xres = (xmax - xmin) / xdim;
00142   double yres = (ymax - ymin) / ydim;
00143 
00144   // Offset, not the cell index -> floor
00145   *col = (int) floor ( (x - xmin) / xres );
00146   *row = (int) floor ( (ymax - y) / yres );
00147 
00148   return result;
00149 }
00150 
00151 /**************/
00152 /*** finish ***/
00153 void 
00154 Map::finish()
00155 {
00156   _rst->finish();
00157 }
00158 
00159 /*********************/
00160 /*** delete Raster ***/
00161 int 
00162 Map::deleteRaster()
00163 {
00164   if ( _rst ) {
00165 
00166     int retVal = _rst->deleteRaster();
00167 
00168     _rst = 0;
00169 
00170     return retVal;
00171   }
00172 
00173   return 1;
00174 }