OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimTiffWorld.cpp
Go to the documentation of this file.
1 //*******************************************************************
2 //
3 // License: LGPL
4 //
5 // See LICENSE.txt file in the top level directory for more details.
6 //
7 // Author: Ken Melero
8 //
9 // Description: Container class for a tiff world file data.
10 //
11 //********************************************************************
12 // $Id: ossimTiffWorld.cpp 19682 2011-05-31 14:21:20Z dburken $
13 
17 #include <ossim/base/ossimString.h>
20 #include <ossim/base/ossimNotify.h>
21 #include <cmath>
22 #include <fstream>
23 #include <iomanip>
24 #include <iostream>
25 using namespace std;
26 
27 //**************************************************************************
28 // ossimTiffWorld::ossimTiffWorld()
29 //***************************************************************************
31  :
32  theXform1(1.0),
33  theXform2(0.0),
34  theXform3(0.0),
35  theXform4(-1.0),
36  theTranslation(0.0,0.0),
37  thePixelType(OSSIM_PIXEL_IS_AREA),
38  theUnit(OSSIM_METERS),
39  theComputedScale(0.0, 0.0),
40  theComputedRotation(0.0)
41 {}
42 
43 //**************************************************************************
44 // ossimTiffWorld::ossimTiffWorld(const char* file, PixelType ptype)
45 //***************************************************************************
47  ossimPixelType ptype,
48  ossimUnitType scaleUnits)
49  :
50  theXform1(1.0),
51  theXform2(0.0),
52  theXform3(0.0),
53  theXform4(-1.0),
54  theTranslation(0.0,0.0),
55  thePixelType(ptype),
56  theUnit(scaleUnits),
57  theComputedScale(0.0, 0.0),
58  theComputedRotation(0.0)
59 {
60  open(ossimFilename(file), ptype, scaleUnits);
61 }
62 
64 {
65  bool result = false;
66 
67  ifstream is;
68  is.open(file.c_str());
69 
70  if( !is.is_open() )
71  {
72  // ESH 07/2008, Trac #234: OSSIM is case sensitive
73  // when using worldfile templates during ingest
74  // -- If first you don't succeed with the user-specified
75  // filename, try again with the results of a case insensitive search.
76  ossimFilename fullName(file);
77  ossimDirectory directory(fullName.path());
78  ossimFilename filename(fullName.file());
79 
80  std::vector<ossimFilename> result;
81  bool bSuccess = directory.findCaseInsensitiveEquivalents(
82  filename, result );
83  if ( bSuccess == true )
84  {
85  int numResults = (int)result.size();
86  int i;
87  for ( i=0; i<numResults && !is.is_open(); ++i )
88  {
89  is.open( result[i].c_str() );
90  }
91  }
92  }
93 
94  if ( is.is_open() )
95  {
96  double x,y;
97  is >> theXform1 >> theXform2 >> theXform3 >> theXform4 >> x >> y;
99 
100  // Compute the affine parameters from the transform:
102 
103 /*
104  * Commented out warning.
105  * Used all the time for tie and scale, NOT for affine. We could wrap around trace
106  * if we added trace to class. (drb - 20110115)
107  */
108 #if 0
109  double angle2 = atan2(theXform4,theXform3);
110  if (fabs(theComputedRotation - angle2) > 0.00001)
111  {
113  << "ossimTiffWorld -- Non-affine transform encountered."
114  << " Use of an affine transform to represent this world file geometry will result in errors."
115  << endl;
116  }
117 #endif
118 
119  double cos_rot = cos(theComputedRotation);
120  if (cos_rot != 0.0)
121  {
122  theComputedScale.x = theXform1/cos_rot;
123  theComputedScale.y = theXform4/cos_rot;
124  }
125  else
126  {
129  }
130  thePixelType = ptype;
131  theUnit = unit;
132  is.close();
133  result = true;
134  }
135  return result;
136 }
137 
139 {
140 }
141 
143  ossimDpt& transformedPoint)
144 {
145  transformedPoint.x = ip.x*theXform1 + ip.y*theXform2 + theTranslation.x;
146  transformedPoint.y = ip.x*theXform3 + ip.y*theXform4 + theTranslation.y;
147 }
148 
149 bool ossimTiffWorld::saveToOssimGeom(ossimKeywordlist& kwl, const char* prefix)const
150 {
151  ossimDpt scale(fabs(theXform1), fabs(theXform4));
153 
154  if ( (theUnit == OSSIM_FEET) || (theUnit == OSSIM_US_SURVEY_FEET) )
155  {
156  // Convert to meters.
157  scale.x = ossimUnitConversionTool(scale.x, theUnit).getMeters();
158  scale.y = ossimUnitConversionTool(scale.y, theUnit).getMeters();
161  }
162 
163  // Shift the tie point to be relative to the center of the pixel.
165  {
166  tie.x += (scale.x/2.0);
167  tie.y -= (scale.y/2.0);
168 
169  // Adjust the keyword list to reflect pixel is point.
170  kwl.add(prefix,
172  "pixel_is_point",
173  true);
174  }
175 
176  // Units in feet converted to meters up above.
177  ossimString units = "meters";
178  if(theUnit == OSSIM_DEGREES)
179  {
180  units = "degrees";
181  }
182 
183  kwl.add(prefix,
185  tie.toString().c_str(),
186  true);
187 
188  kwl.add(prefix,
190  units.c_str(),
191  true);
192 
193  kwl.add(prefix,
195  scale.toString().c_str(),
196  true);
197 
198  kwl.add(prefix,
200  units.c_str(),
201  true);
202 
203  return true;
204 }
205 
206 bool ossimTiffWorld::loadFromOssimGeom(const ossimKeywordlist& kwl, const char* prefix)
207 {
208  theXform2 = 0.0;
209  theXform3 = 0.0;
210 
211  const char* lookup;
212 
213  // Get the scale...
214  lookup = kwl.find(prefix, ossimKeywordNames::PIXEL_SCALE_XY_KW);
215  if (lookup)
216  {
217  ossimDpt scale;
218  scale.toPoint(std::string(lookup));
219  theXform1 = scale.x;
220  theXform4 = -(scale.y);
221  }
222  else // BACKWARDS COMPATIBILITY LOOKUPS...
223  {
226  theXform1 = xscale.toDouble();
227  theXform4 = -(yscale.toDouble());
228  }
229 
230  // Get the tie...
231  lookup = kwl.find(prefix, ossimKeywordNames::TIE_POINT_XY_KW);
232  if (lookup)
233  {
234  ossimDpt tie;
235  tie.toPoint(std::string(lookup));
236  theTranslation.x = tie.x;
237  theTranslation.y = tie.y;
238  }
239  else // BACKWARDS COMPATIBILITY LOOKUPS...
240  {
241  ossimString easting =
243  ossimString northing =
245  theTranslation.x = easting.toDouble();
246  theTranslation.y = northing.toDouble();
247  }
248 
249  return true;
250 }
251 
253 {
254  out << setiosflags(ios::fixed) << setprecision(15)
255  << theXform1 << "\n"
256  << theXform2 << "\n"
257  << theXform3 << "\n"
258  << theXform4 << "\n"
259  << theTranslation
260  << endl;
261  return out;
262 }
263 
265 {
266  return obj.print(out);
267 }
268 
ossim_uint32 x
bool open(const ossimFilename &file, ossimPixelType ptype, ossimUnitType unit)
ossimUnitType
Represents serializable keyword/value map.
ossimDpt theComputedScale
ossim_uint32 y
std::basic_ifstream< char > ifstream
Class for char input file streams.
Definition: ossimIosFwd.h:44
const char * find(const char *key) const
bool saveToOssimGeom(ossimKeywordlist &kwl, const char *prefix=NULL) const
double theComputedRotation
Radians.
double y
Definition: ossimDpt.h:165
static const char * METERS_PER_PIXEL_Y_KW
static const char * PIXEL_SCALE_XY_KW
static const char * TIE_POINT_NORTHING_KW
void add(const char *prefix, const ossimKeywordlist &kwl, bool overwrite=true)
static const char * TIE_POINT_XY_KW
static const char * TIE_POINT_EASTING_KW
std::string::size_type size() const
Definition: ossimString.h:405
void toPoint(const std::string &s)
Initializes this point from string.
Definition: ossimDpt.cpp:192
bool loadFromOssimGeom(const ossimKeywordlist &kwl, const char *prefix=NULL)
double toDouble() const
std::ostream & operator<<(std::ostream &out, const ossimTiffWorld &obj)
ossimPixelType thePixelType
ossimDpt theTranslation
ossimPixelType
ossimString toString(ossim_uint32 precision=15) const
Definition: ossimDpt.cpp:160
void forward(const ossimDpt &imagePoint, ossimDpt &transformedPoint)
static const char * PIXEL_TYPE_KW
double x
Definition: ossimDpt.h:164
const char * c_str() const
Returns a pointer to a null-terminated array of characters representing the string&#39;s contents...
Definition: ossimString.h:396
ossimUnitType theUnit
ossimFilename file() const
ossimFilename path() const
std::ostream & print(std::ostream &out) const
static const char * METERS_PER_PIXEL_X_KW
OSSIMDLLEXPORT std::ostream & ossimNotify(ossimNotifyLevel level=ossimNotifyLevel_WARN)
std::basic_ostream< char > ostream
Base class for char output streams.
Definition: ossimIosFwd.h:23
static const char * PIXEL_SCALE_UNITS_KW
static const char * TIE_POINT_UNITS_KW