openModeller  Version 1.5.0
MapFormat.cpp
Go to the documentation of this file.
1 
34 #include <openmodeller/Log.hh>
35 
36 using std::string;
37 
38 #define DEFAULT_FORMAT ByteHFA
39 
41  format( DEFAULT_FORMAT ),
42  xcel( 0.0 ),
43  xcelIsSet( false ),
44  ycel( 0.0 ),
45  ycelIsSet( false ),
46  xmin( 0 ),
47  xminIsSet( false ),
48  ymin( 0 ),
49  yminIsSet( false ),
50  xmax( 0 ),
51  xmaxIsSet( false ),
52  ymax( 0 ),
53  ymaxIsSet( false ),
54  noval( 0 ),
55  novalIsSet( false ),
56  proj( ),
57  projIsSet( false )
58 {}
59 
60 MapFormat::MapFormat( Coord xcel, Coord ycel, Coord xmin, Coord ymin,
61  Coord xmax, Coord ymax, Scalar noval, char const * proj ) :
62  format( DEFAULT_FORMAT ),
63  xcel( xcel ),
64  xcelIsSet( true ),
65  ycel( ycel ),
66  ycelIsSet( true ),
67  xmin( xmin ),
68  xminIsSet( true ),
69  ymin( ymin ),
70  yminIsSet( true ),
71  xmax( xmax ),
72  xmaxIsSet( true ),
73  ymax( ymax ),
74  ymaxIsSet( true ),
75  noval( noval ),
76  novalIsSet( true ),
77  proj( proj ),
78  projIsSet( true )
79 {}
80 
81 MapFormat::MapFormat( char const *filenameWithFormat ) :
82  format( DEFAULT_FORMAT )
83 {
84  // TODO - determine the format from the raster file....
85  Raster* r = RasterFactory::instance().create( filenameWithFormat );
86 
87  Header h = r->header();
88 
89  setXCel( h.xcel );
90  setYCel( h.ycel );
91  setXMin( h.xmin );
92  setYMin( h.ymin );
93  setXMax( h.xmax );
94  setYMax( h.ymax );
95  setNoDataValue( h.noval );
96  setProjection( h.proj );
97 
98  delete r;
99 }
100 
101 void MapFormat::copyDefaults( const Map& map ) {
102 
103  Header h = map.getHeader();
104 
105  if ( ! xcelIsSet ) {
106 
107  Log::instance()->debug( "Copying cell width = %d\n", h.xcel );
108  setXCel( h.xcel );
109  }
110 
111  if ( ! ycelIsSet ) {
112 
113  Log::instance()->debug( "Copying cell height = %d\n", h.ycel );
114  setYCel( h.ycel );
115  }
116 
117  if ( ! xminIsSet ) {
118 
119  Log::instance()->debug( "Copying xmin = %f\n", h.xmin );
120  setXMin( h.xmin );
121  }
122 
123  if ( ! yminIsSet ) {
124 
125  Log::instance()->debug( "Copying ymin = %f\n", h.ymin );
126  setYMin( h.ymin );
127  }
128 
129  if ( ! xmaxIsSet ) {
130 
131  Log::instance()->debug( "Copying xmax = %f\n", h.xmax );
132  setXMax( h.xmax );
133  }
134 
135  if ( ! ymaxIsSet ) {
136 
137  Log::instance()->debug( "Copying ymax = %f\n", h.ymax );
138  setYMax( h.ymax );
139  }
140 
141  if ( ! projIsSet ) {
142 
143  Log::instance()->debug( "Copying projection\n" );
144  setProjection( h.proj );
145  }
146 }
147 
149 {}
150 
151 void MapFormat::setFormat( int f ) {
152 
153  format = f;
154 
155  if ( format < 0 || format > FloatingASC ) {
156 
158  }
159 }
160 
161 void MapFormat::setFormat( std::string format ) {
162 
163  if ( format == "GreyTiff" ) {
164 
165  setFormat( GreyTiff );
166  }
167  else if ( format == "GreyTiff100" ) {
168 
170  }
171  else if ( format == "FloatingTiff" ) {
172 
174  }
175  else if ( format == "GreyBMP" ) {
176 
177  setFormat( GreyBMP );
178  }
179  else if ( format == "FloatingHFA" ) {
180 
182  }
183  else if ( format == "ByteHFA" ) {
184 
185  setFormat( ByteHFA );
186  }
187  else if ( format == "ByteASC" ) {
188 
189  setFormat( ByteASC );
190  }
191  else if ( format == "FloatingASC" ) {
192 
194  }
195  else {
196 
197  std::string msg = "Unknown map format: ";
198  msg.append( format );
199  msg.append( "\n" );
200 
201  Log::instance()->warn( msg.c_str() );
202  }
203 }
204 
206  xcel = v;
207  xcelIsSet = true;
208 }
209 
211  ycel = v;
212  ycelIsSet = true;
213 }
214 
216  xmin = v;
217  xminIsSet = true;
218 }
219 
221  ymin = v;
222  yminIsSet = true;
223 }
224 
226  xmax = v;
227  xmaxIsSet = true;
228 }
229 
231  ymax = v;
232  ymaxIsSet = true;
233 }
234 
236  noval = v;
237  novalIsSet = true;
238 }
239 
240 void MapFormat::setProjection( const string& v ) {
241  proj = v;
242  projIsSet = true;
243 }
244 
245 int MapFormat::getWidth() const {
246 
247  Coord xmin = getXMin();
248  Coord xmax = getXMax();
249  Coord xcel = getXCel();
250 
251  int width = static_cast<int>( (xmax-xmin) / xcel +0.5);
252 
253  return width;
254 }
255 
256 int MapFormat::getHeight() const {
257 
258  int height = static_cast<int>( (ymax-ymin) / ycel +0.5);
259 
260  return height;
261 }
262 
264 
265  if ( ! xcelIsSet ) {
266 
267  std::string msg = "Cell width not set.\n";
268 
269  Log::instance()->error( msg.c_str() );
270 
271  throw OmException( msg );
272  }
273 
274  return xcel;
275 }
276 
278 
279  if ( ! ycelIsSet ) {
280 
281  std::string msg = "Cell height not set.\n";
282 
283  Log::instance()->error( msg.c_str() );
284 
285  throw OmException( msg );
286  }
287 
288  return ycel;
289 }
290 
292 
293  if ( ! xminIsSet ) {
294 
295  std::string msg = "XMin not set.\n";
296 
297  Log::instance()->error( msg.c_str() );
298 
299  throw OmException( msg );
300  }
301 
302  return xmin;
303 }
304 
306 
307  if ( !yminIsSet ) {
308 
309  std::string msg = "Ymin not set.\n";
310 
311  Log::instance()->error( msg.c_str() );
312 
313  throw OmException( msg );
314  }
315 
316  return ymin;
317 }
318 
320 
321  if ( ! xmaxIsSet ) {
322 
323  std::string msg = "Xmax not set.\n";
324 
325  Log::instance()->error( msg.c_str() );
326 
327  throw OmException( msg );
328  }
329 
330  return xmax;
331 }
332 
334 
335  if ( ! ymaxIsSet ) {
336 
337  std::string msg = "Ymax not set.\n";
338 
339  Log::instance()->error( msg.c_str() );
340 
341  throw OmException( msg );
342  }
343 
344  return ymax;
345 }
346 
348 
349  if ( ! novalIsSet ) {
350 
351  std::string msg = "NoDataValue not set.\n";
352 
353  Log::instance()->error( msg.c_str() );
354 
355  throw OmException( msg );
356  }
357 
358  return noval;
359 }
360 
361 string MapFormat::getProjection() const {
362 
363  if ( ! projIsSet ) {
364 
365  std::string msg = "Projection not set.\n";
366 
367  Log::instance()->error( msg.c_str() );
368 
369  throw OmException( msg );
370  }
371 
372  return proj;
373 }
374 
Coord ycel
Definition: MapFormat.hh:115
Coord xcel
Definition: Header.hh:78
void setNoDataValue(Scalar noval)
Definition: MapFormat.cpp:235
bool novalIsSet
Definition: MapFormat.hh:131
void warn(const char *format,...)
'Warn' level.
Definition: Log.cpp:273
Coord getYMin() const
Definition: MapFormat.cpp:305
std::string getProjection() const
Definition: MapFormat.cpp:361
bool ymaxIsSet
Definition: MapFormat.hh:128
Coord ycel
Definition: Header.hh:79
double Scalar
Type of map values.
Definition: om_defs.hh:39
bool xcelIsSet
Definition: MapFormat.hh:113
Coord xmin
Definition: Header.hh:74
Coord xmax
Definition: Header.hh:76
Scalar getNoDataValue() const
Definition: MapFormat.cpp:347
static Log * instance()
Returns the instance pointer, creating the object on the first call.
Definition: Log.cpp:45
#define DEFAULT_FORMAT
Definition: MapFormat.cpp:38
Coord xcel
Definition: MapFormat.hh:112
A common interface to rasters.
Definition: Raster.hh:44
Coord ymax
Definition: MapFormat.hh:127
bool ycelIsSet
Definition: MapFormat.hh:116
Coord getXMax() const
Definition: MapFormat.cpp:319
Scalar noval
Definition: Header.hh:82
Definition: Header.hh:45
void setYMin(Coord ymin)
Definition: MapFormat.cpp:220
void error(const char *format,...)
'Error' level.
Definition: Log.cpp:290
std::string proj
Definition: Header.hh:104
Coord ymin
Definition: Header.hh:75
Coord getYMax() const
Definition: MapFormat.cpp:333
Coord xmax
Definition: MapFormat.hh:124
Coord xmin
Definition: MapFormat.hh:118
Coord ymax
Definition: Header.hh:77
const Header & getHeader() const
Definition: Map.hh:68
Coord getXMin() const
Definition: MapFormat.cpp:291
Coord getYCel() const
Definition: MapFormat.cpp:277
Definition: Map.hh:49
int getWidth() const
Definition: MapFormat.cpp:245
bool xminIsSet
Definition: MapFormat.hh:119
int getHeight() const
Definition: MapFormat.cpp:256
int format
Definition: MapFormat.hh:110
static RasterFactory & instance()
bool yminIsSet
Definition: MapFormat.hh:122
void setXMax(Coord xmax)
Definition: MapFormat.cpp:225
Coord getXCel() const
Definition: MapFormat.cpp:263
std::string proj
Definition: MapFormat.hh:137
void setYCel(Coord ycel)
Definition: MapFormat.cpp:210
void setFormat(int format)
Definition: MapFormat.cpp:151
Raster * create(const string &source, int categ=0)
void copyDefaults(const Map &map)
Definition: MapFormat.cpp:101
void setXCel(Coord xcel)
Definition: MapFormat.cpp:205
double Coord
Type of map coordinates.
Definition: om_defs.hh:38
Header & header()
Definition: Raster.hh:75
void setProjection(const std::string &proj)
Definition: MapFormat.cpp:240
Scalar noval
Definition: MapFormat.hh:130
bool projIsSet
Definition: MapFormat.hh:138
void setXMin(Coord xmin)
Definition: MapFormat.cpp:215
void setYMax(Coord ymax)
Definition: MapFormat.cpp:230
void debug(const char *format,...)
'Debug' level.
Definition: Log.cpp:237
Coord ymin
Definition: MapFormat.hh:121
bool xmaxIsSet
Definition: MapFormat.hh:125