openModeller  Version 1.4.0
MapFormat.cpp
Go to the documentation of this file.
00001 
00030 #include <openmodeller/MapFormat.hh>
00031 #include <openmodeller/env_io/Map.hh>
00032 #include <openmodeller/env_io/RasterFactory.hh>
00033 #include <openmodeller/Exceptions.hh>
00034 #include <openmodeller/Log.hh>
00035 
00036 using std::string;
00037 
00038 #define DEFAULT_FORMAT ByteHFA
00039 
00040 MapFormat::MapFormat( ) :
00041   format( DEFAULT_FORMAT ),
00042   xcel( 0.0 ),
00043   xcelIsSet( false ),
00044   ycel( 0.0 ),
00045   ycelIsSet( false ),
00046   xmin( 0 ),
00047   xminIsSet( false ),
00048   ymin( 0 ),
00049   yminIsSet( false ),
00050   xmax( 0 ),
00051   xmaxIsSet( false ),
00052   ymax( 0 ),
00053   ymaxIsSet( false ),
00054   noval( 0 ),
00055   novalIsSet( false ),
00056   proj( ),
00057   projIsSet( false )
00058 {}
00059 
00060 MapFormat::MapFormat( Coord xcel, Coord ycel, Coord xmin, Coord ymin,
00061           Coord xmax, Coord ymax, Scalar noval, char const * proj ) :
00062   format( DEFAULT_FORMAT ),
00063   xcel( xcel ),
00064   xcelIsSet( true ),
00065   ycel( ycel ),
00066   ycelIsSet( true ),
00067   xmin( xmin ),
00068   xminIsSet( true ),
00069   ymin( ymin ),
00070   yminIsSet( true ),
00071   xmax( xmax ),
00072   xmaxIsSet( true ),
00073   ymax( ymax ),
00074   ymaxIsSet( true ),
00075   noval( noval ),
00076   novalIsSet( true ),
00077   proj( proj ),
00078   projIsSet( true )
00079 {}
00080 
00081 MapFormat::MapFormat( char const *filenameWithFormat ) :
00082   format( DEFAULT_FORMAT )
00083 {
00084   // TODO - determine the format from the raster file....
00085   Raster* r = RasterFactory::instance().create( filenameWithFormat );
00086 
00087   Header h = r->header();
00088 
00089   setXCel( h.xcel );
00090   setYCel( h.ycel );
00091   setXMin( h.xmin );
00092   setYMin( h.ymin );
00093   setXMax( h.xmax );
00094   setYMax( h.ymax );
00095   setNoDataValue( h.noval );
00096   setProjection( h.proj );
00097 
00098   delete r;
00099 }
00100 
00101 void MapFormat::copyDefaults( const Map& map ) {
00102 
00103   Header h = map.getHeader();
00104 
00105   if ( ! xcelIsSet ) {
00106 
00107     Log::instance()->debug( "Copying cell width = %d\n", h.xcel );
00108     setXCel( h.xcel );
00109   }
00110 
00111   if ( ! ycelIsSet ) {
00112 
00113     Log::instance()->debug( "Copying cell height = %d\n", h.ycel );
00114     setYCel( h.ycel );
00115   }
00116 
00117   if ( ! xminIsSet ) {
00118 
00119     Log::instance()->debug( "Copying xmin = %f\n", h.xmin );
00120     setXMin( h.xmin );
00121   }
00122 
00123   if ( ! yminIsSet ) {
00124 
00125     Log::instance()->debug( "Copying ymin = %f\n", h.ymin );
00126     setYMin( h.ymin );
00127   }
00128 
00129   if ( ! xmaxIsSet ) {
00130 
00131     Log::instance()->debug( "Copying xmax = %f\n", h.xmax );
00132     setXMax( h.xmax );
00133   }
00134 
00135   if ( ! ymaxIsSet ) {
00136 
00137     Log::instance()->debug( "Copying ymax = %f\n", h.ymax );
00138     setYMax( h.ymax );
00139   }
00140 
00141   if ( ! projIsSet ) {
00142 
00143     Log::instance()->debug( "Copying projection\n" );
00144     setProjection( h.proj );
00145   }
00146 }
00147 
00148 MapFormat::~MapFormat()
00149 {}
00150 
00151 void MapFormat::setFormat( int f ) {
00152 
00153   format = f;
00154 
00155   if ( format < 0 || format > FloatingASC ) {
00156 
00157     format = DEFAULT_FORMAT;
00158   }
00159 }
00160 
00161 void MapFormat::setFormat( std::string format ) {
00162 
00163   if ( format == "GreyTiff" ) {
00164 
00165     setFormat( GreyTiff );
00166   }
00167   else if ( format == "GreyTiff100" ) {
00168 
00169     setFormat( GreyTiff100 );
00170   }
00171   else if ( format == "FloatingTiff" ) {
00172 
00173     setFormat( FloatingTiff );
00174   }
00175   else if ( format == "GreyBMP" ) {
00176 
00177     setFormat( GreyBMP );
00178   }
00179   else if ( format == "FloatingHFA" ) {
00180 
00181     setFormat( FloatingHFA );
00182   }
00183   else if ( format == "ByteHFA" ) {
00184 
00185     setFormat( ByteHFA );
00186   }
00187   else if ( format == "ByteASC" ) {
00188 
00189     setFormat( ByteASC );
00190   }
00191   else if ( format == "FloatingASC" ) {
00192 
00193     setFormat( FloatingASC );
00194   }
00195   else {
00196 
00197     std::string msg = "Unknown map format: ";
00198     msg.append( format );
00199     msg.append( "\n" );
00200 
00201     Log::instance()->warn( msg.c_str() );
00202   }
00203 }
00204 
00205 void MapFormat::setXCel( Coord v ) {
00206   xcel = v;
00207   xcelIsSet = true;
00208 }
00209 
00210 void MapFormat::setYCel( Coord v ) {
00211   ycel = v;
00212   ycelIsSet = true;
00213 }
00214 
00215 void MapFormat::setXMin( Coord v ) {
00216   xmin = v;
00217   xminIsSet = true;
00218 }
00219 
00220 void MapFormat::setYMin( Coord v ) {
00221   ymin = v;
00222   yminIsSet = true;
00223 }
00224 
00225 void MapFormat::setXMax( Coord v ) {
00226   xmax = v;
00227   xmaxIsSet = true;
00228 }
00229 
00230 void MapFormat::setYMax( Coord v ) {
00231   ymax = v;
00232   ymaxIsSet = true;
00233 }
00234 
00235 void MapFormat::setNoDataValue( Scalar v ) {
00236   noval = v;
00237   novalIsSet = true;
00238 }
00239 
00240 void MapFormat::setProjection( const string& v ) {
00241   proj = v;
00242   projIsSet = true;
00243 }
00244 
00245 int MapFormat::getWidth() const {
00246 
00247   Coord xmin = getXMin();
00248   Coord xmax = getXMax();
00249   Coord xcel = getXCel();
00250 
00251   int width = static_cast<int>(  (xmax-xmin) / xcel +0.5);
00252 
00253   return width;
00254 }
00255 
00256 int MapFormat::getHeight() const {
00257 
00258   int height = static_cast<int>(  (ymax-ymin) / ycel +0.5);
00259 
00260   return height;
00261 }
00262 
00263 Coord MapFormat::getXCel() const {
00264 
00265   if ( ! xcelIsSet ) {
00266 
00267     std::string msg = "Cell width not set.\n";
00268 
00269     Log::instance()->error( msg.c_str() );
00270 
00271     throw OmException( msg );
00272   }
00273 
00274   return xcel;
00275 }
00276 
00277 Coord MapFormat::getYCel() const {
00278 
00279   if ( ! ycelIsSet ) {
00280 
00281     std::string msg = "Cell height not set.\n";
00282 
00283     Log::instance()->error( msg.c_str() );
00284 
00285     throw OmException( msg );
00286   }
00287 
00288   return ycel;
00289 }
00290 
00291 Coord MapFormat::getXMin() const {
00292 
00293   if ( ! xminIsSet ) {
00294 
00295     std::string msg = "XMin not set.\n";
00296 
00297     Log::instance()->error( msg.c_str() );
00298 
00299     throw OmException( msg );
00300   }
00301 
00302   return xmin;
00303 }
00304 
00305 Coord MapFormat::getYMin() const {
00306 
00307   if ( !yminIsSet ) {
00308 
00309     std::string msg = "Ymin not set.\n";
00310 
00311     Log::instance()->error( msg.c_str() );
00312 
00313     throw OmException( msg );
00314   }
00315 
00316   return ymin;
00317 }
00318 
00319 Coord MapFormat::getXMax() const {
00320 
00321   if ( ! xmaxIsSet ) {
00322 
00323     std::string msg = "Xmax not set.\n";
00324 
00325     Log::instance()->error( msg.c_str() );
00326 
00327     throw OmException( msg );
00328   }
00329 
00330   return xmax;
00331 }
00332 
00333 Coord MapFormat::getYMax() const {
00334 
00335   if ( ! ymaxIsSet ) {
00336 
00337     std::string msg = "Ymax not set.\n";
00338 
00339     Log::instance()->error( msg.c_str() );
00340 
00341     throw OmException( msg );
00342   }
00343 
00344   return ymax;
00345 }
00346 
00347 Scalar MapFormat::getNoDataValue() const {
00348 
00349   if ( ! novalIsSet ) {
00350 
00351     std::string msg = "NoDataValue not set.\n";
00352 
00353     Log::instance()->error( msg.c_str() );
00354 
00355     throw OmException( msg );
00356   }
00357 
00358   return noval;
00359 }
00360 
00361 string MapFormat::getProjection() const {
00362 
00363   if ( ! projIsSet ) {
00364 
00365     std::string msg = "Projection not set.\n";
00366 
00367     Log::instance()->error( msg.c_str() );
00368 
00369     throw OmException( msg );
00370   }
00371 
00372   return proj;
00373 }
00374