openModeller  Version 1.5.0
Map.cpp
Go to the documentation of this file.
1 
31 
34 #include <openmodeller/Log.hh>
35 
36 #include <math.h>
37 
38 /****************************************************************/
39 /****************************** Map *****************************/
40 
41 /******************/
42 /*** construtor ***/
43 Map::Map( Raster *rst )
44 {
45  _rst = rst;
47 }
48 
49 
50 /*****************/
51 /*** destrutor ***/
52 
54 {
55  delete _gt;
56  delete _rst;
57 }
58 
59 
60 /******************/
61 /*** getExtent ***/
62 int
63 Map::getExtent( Coord *xmin, Coord *ymin, Coord *xmax, Coord *ymax) const
64 {
65  int result = 0;
66 
67  if (_rst->hasCustomGeotransform()) {
68 
69  result = _rst->getExtentInStandardCs(xmin, ymin, xmax, ymax);
70  }
71  else {
72 
73  *xmin = _rst->xMin();
74  *ymin = _rst->yMin();
75  *xmax = _rst->xMax();
76  *ymax = _rst->yMax();
77 
78  //Log::instance()->debug( "Raster boundaries before geotransform: xmin=%f, xmax=%f, ymin=%f, ymax=%f\n", *xmin, *xmax, *ymin, *ymax );
79 
80  result = _gt->transfOut( xmin, ymin ) && _gt->transfOut( xmax, ymax );
81 
82  //Log::instance()->debug( "Raster boundaries after geotransform: xmin=%f, xmax=%f, ymin=%f, ymax=%f\n", *xmin, *xmax, *ymin, *ymax );
83  }
84 
85  if (*xmin > *xmax) {
86 
87  *xmin = -180;
88  *xmax = 180;
89  }
90 
91  if (*ymin > *ymax) {
92 
93  *ymin = -90;
94  *ymax = 90;
95  }
96 
97  return result;
98 }
99 
100 
101 /***********/
102 /*** get ***/
103 int
104 Map::get( Coord x, Coord y, Scalar *val ) const
105 {
106  return _gt->transfIn( &x, &y ) ? _rst->get( x, y, val ) : 0;
107 }
108 
109 /***********/
110 /*** put ***/
111 int
113 {
114  return _gt->transfIn( &x, &y ) ? _rst->put( x, y, val ) : 0;
115 }
116 
117 /***********/
118 /*** put ***/
119 int
121 {
122  return _gt->transfIn(&x,&y) ? _rst->put( x,y ) : 0;
123 }
124 
125 /**********************/
126 /*** get row column ***/
127 int
128 Map::getRowColumn( Coord x, Coord y, int *row, int *col )
129 {
130  // Transform the given coordinates into the raster coordinate system & projection
131  int result = _gt->transfIn( &x, &y );
132 
133  Coord xmin = _rst->xMin();
134  Coord ymin = _rst->yMin();
135  Coord xmax = _rst->xMax();
136  Coord ymax = _rst->yMax();
137 
138  int xdim = _rst->dimX();
139  int ydim = _rst->dimY();
140 
141  double xres = (xmax - xmin) / xdim;
142  double yres = (ymax - ymin) / ydim;
143 
144  // Offset, not the cell index -> floor
145  *col = (int) floor ( (x - xmin) / xres );
146  *row = (int) floor ( (ymax - y) / yres );
147 
148  return result;
149 }
150 
151 /**************/
152 /*** finish ***/
153 void
155 {
156  _rst->finish();
157 }
158 
159 /*********************/
160 /*** delete Raster ***/
161 int
163 {
164  if ( _rst ) {
165 
166  int retVal = _rst->deleteRaster();
167 
168  _rst = 0;
169 
170  return retVal;
171  }
172 
173  return 1;
174 }
virtual bool hasCustomGeotransform()
Definition: Raster.hh:165
virtual void finish()
Definition: Raster.hh:154
Map(Raster *rst)
Definition: Map.cpp:43
Coord yMax() const
Definition: Raster.hh:90
int get(Coord x, Coord y, Scalar *val) const
Definition: Map.cpp:104
double Scalar
Type of map values.
Definition: om_defs.hh:39
void finish()
Definition: Map.cpp:154
int transfIn(double *x, double *y) const
virtual int put(Coord px, Coord py, Scalar val)=0
A common interface to rasters.
Definition: Raster.hh:44
~Map()
Definition: Map.cpp:53
GeoTransform * _gt
Definition: Map.hh:149
std::string proj
Definition: Header.hh:104
int put(Coord x, Coord y, Scalar val)
Definition: Map.cpp:112
int transfOut(double *x, double *y) const
Coord yMin() const
Definition: Raster.hh:84
static char const * getDefaultCS()
Coord xMax() const
Definition: Raster.hh:87
int getExtent(Coord *xmin, Coord *ymin, Coord *xmax, Coord *ymax) const
Definition: Map.cpp:63
int dimX() const
Definition: Raster.hh:93
Raster * _rst
Definition: Map.hh:148
int deleteRaster()
Definition: Map.cpp:162
virtual int get(Coord px, Coord py, Scalar *val)=0
double Coord
Type of map coordinates.
Definition: om_defs.hh:38
Header & header()
Definition: Raster.hh:75
virtual int deleteRaster()=0
int dimY() const
Definition: Raster.hh:96
virtual int getExtentInStandardCs(Coord *xmin, Coord *ymin, Coord *xmax, Coord *ymax)
Definition: Raster.hh:180
Coord xMin() const
Definition: Raster.hh:81
int getRowColumn(Coord x, Coord y, int *row, int *col)
Definition: Map.cpp:128