vpgl_geo_camera.h
Go to the documentation of this file.
1 #ifndef vpgl_geo_camera_h_
2 #define vpgl_geo_camera_h_
3 //:
4 // \file
5 // \brief A geotiff image deduced camera class.
6 // \author Gamze Tunali
7 // \date October 24, 2008
8 // \author Gamze Tunali
9 //
10 // Geotiff images contain information about the corresponding world coordinate
11 // values of the pixel positions. This class is a wrapper of that information
12 // to project and backproject the 3D points in local coordinates to pixel positions
13 
14 
15 #include <iosfwd>
16 #include <vector>
17 #ifdef _MSC_VER
18 # include <vcl_msvc_warnings.h>
19 #endif
20 
21 #include <vpgl/vpgl_lvcs_sptr.h>
22 #include <vpgl/vpgl_lvcs.h>
23 #include <vnl/vnl_matrix.h>
24 
25 #include <vpgl/vpgl_camera.h>
26 
27 #include <vil/vil_image_resource_sptr.h>
28 
29 class vpgl_geo_camera : public vpgl_camera<double>
30 {
31  public:
32  //: creates identity matrix and all zero tiepoints
34 
35  //: if scale tag is false be sure that trans_matrix[0][0] and trans_matrix[1][1] is 1.0 otherwise set it to true
38  : trans_matrix_(trans_matrix), is_utm(false), scale_tag_(false) {if(lvcs) this->set_lvcs(lvcs); }
39 
40  // copy constructor
41  vpgl_geo_camera(vpgl_geo_camera const& rhs);
42 
44 
45  //: uses lvcs to convert local x-y to global longitude and latitude
46  static bool init_geo_camera(vil_image_resource_sptr const& geotiff_img,
47  const vpgl_lvcs_sptr& lvcs,
48  vpgl_geo_camera*& camera);
49 
50  //: Assumes geographic coordinates are global
51  static bool init_geo_camera(vil_image_resource_sptr const geotiff_img,
52  vpgl_geo_camera*& camera){
53  vpgl_lvcs_sptr lvcs = nullptr;
54  return init_geo_camera(geotiff_img, lvcs, camera);
55  }
56 
57  //: warning, use this camera cautiously, the output of img_to_global method needs to be adjusted sign wise
58  // for 'S' use -lat and for 'W' -lon
59  // TODO: generalize geo_camera so that img_to_global method makes this adjustment internally if camera is created using this method
60  static bool init_geo_camera(const std::string& img_name, unsigned ni, unsigned nj, const vpgl_lvcs_sptr& lvcs, vpgl_geo_camera*& camera);
61 
62  // loads a geo_camera from the file and uses global WGS84 coordinates, so no need to convert negative values to positives in the global_to_img method as in the previous method
63  static bool init_geo_camera_from_filename(const std::string& img_name, unsigned ni, unsigned nj, const vpgl_lvcs_sptr& lvcs, vpgl_geo_camera*& camera);
64 
65  //: init using a tfw file, reads the transformation matrix from the tfw
66  static bool init_geo_camera(const std::string& tfw_name, const vpgl_lvcs_sptr& lvcs, int utm_zone, unsigned northing, vpgl_geo_camera*& camera);
67 
68  //: init without lvcs. Assumes geographic coordinates are global
69  static bool init_geo_camera(std::string tfw_name, int utm_zone, unsigned northing, vpgl_geo_camera*& camera){
70  vpgl_lvcs_sptr lvcs = nullptr;
71  return init_geo_camera(tfw_name, lvcs, utm_zone, northing, camera);
72  }
73  ~vpgl_geo_camera() override = default;
74 
75  std::string type_name() const override { return "vpgl_geo_camera"; }
76 
77  //northing=0 means North, 1 is south
78  void set_utm(int utm_zone, unsigned northing) { is_utm=true, utm_zone_=utm_zone; northing_=northing; }
79 
81 
82  void set_scale_format(bool scale_tag) { scale_tag_=scale_tag; }
83 
84  vpgl_lvcs_sptr const lvcs() {return lvcs_;}
85 
86  //: Implementing the generic camera interface of vpgl_camera.
87  // x,y,z are in local coordinates, u represents image column, v image row
88  void project(const double x, const double y, const double z, double& u, double& v) const override;
89 
90  //: backprojects an image point into local coordinates (based on lvcs_)
91  void backproject(const double u, const double v, double& x, double& y, double& z);
92 
93  // adds translation to the trans matrix
94  void translate(double tx, double ty, double z);
95 
96  //: the lidar pixel size in meters assumes square pixels
97  double pixel_spacing() { if (scale_tag_) return trans_matrix_[0][0];
98  else return 1.0; }
99 
100  bool operator ==(vpgl_geo_camera const& rhs) const;
101 
102  static bool comp_trans_matrix(double sx1, double sy1, double sz1,
103  std::vector<std::vector<double> > tiepoints,
105  bool scale_tag = false);
106 
107  //: Return a platform independent string identifying the class
108  virtual std::string is_a() const { return std::string("vpgl_geo_camera"); }
109 
110  //: Return true if the argument matches the string identifying the class or any parent class
111  virtual bool is_class(std::string const& cls) const
112  { return cls==is_a() || cls==std::string("vpgl_geo_camera"); }
113 
114  //: save the camera as tfw
115  void save_as_tfw(std::string const& tfw_filename);
116 
117  //: Write camera to stream
118  friend std::ostream& operator<<(std::ostream& s, vpgl_geo_camera const& p);
119 
120  //: Read camera from stream
121  friend std::istream& operator>>(std::istream& s, vpgl_geo_camera& p);
122 
123  //: returns the corresponding geographical coordinates for a given pixel position (i,j)
124  // the output global coord is wgs84
125  void img_to_global(const double i, const double j,
126  double& lon, double& lat) const;
127 
128  //: returns the corresponding pixel position for given geographical coordinates
129  // the input global coord is wgs84
130  void global_to_img(const double lon, const double lat, const double elev,
131  double& u, double& v) const;
132 
133  //: returns the corresponding geographical coordinates for a given pixel position (i,j)
134  // the output global coord is UTM: x east, y north
135  void img_to_global_utm(const double i, const double j,
136  double& x, double& y) const;
137 
138  //: returns the corresponding pixel position for given geographical coordinates
139  // the input global coord is UTM: x east, for y north
140  void global_utm_to_img(const double x, const double y, int zone, double elev,
141  double& u, double& v) const;
142 
143  //: returns the corresponding utm location for the given local position
144  void local_to_utm(const double x, const double y, const double z, double& e, double& n, int& utm_zone);
145 
146  int utm_zone() { return utm_zone_; }
147 
148  bool img_four_corners_in_utm(const unsigned ni, const unsigned nj, double elev, double& e1, double& n1, double& e2, double& n2);
149 
150  //: returns the corresponding geographical coordinate (lon, lat, elev) for a given pixel position (i,j,k)
151  // Note: not yet implemented -- PVr, 16 aug 2012
152  void img_to_wgs(unsigned i, unsigned j, unsigned k, double& lon, double& lat, double& elev);
153 
155 
156 #if 0
157  //: returns the corresponding pixel position (i,j) for a given geographical coordinate (lon, lat)
158  void wgs_to_img(double lon, double lat,
159  unsigned& i, unsigned& j);
160 #endif // 0
161 
162  //: Binary save self to stream.
163  void b_write(vsl_b_ostream &os) const;
164 
165  //: Binary load self from stream.
166  void b_read(vsl_b_istream &is);
167 
168  //: Return IO version number;
169  short version() const { return 1; }
170 
171  private:
172 
174  //: lvcs of world parameters
176  bool is_utm;
178  int northing_; //0 North, 1 South
180 };
181 
182 #endif // vpgl_geo_camera_h_
static bool init_geo_camera_from_filename(const std::string &img_name, unsigned ni, unsigned nj, const vpgl_lvcs_sptr &lvcs, vpgl_geo_camera *&camera)
void img_to_global_utm(const double i, const double j, double &x, double &y) const
returns the corresponding geographical coordinates for a given pixel position (i,j).
double pixel_spacing()
the lidar pixel size in meters assumes square pixels.
A geographic coordinate system.
void set_utm(int utm_zone, unsigned northing)
friend std::ostream & operator<<(std::ostream &s, vpgl_geo_camera const &p)
Write camera to stream.
void set_lvcs(vpgl_lvcs_sptr lvcs)
short version() const
Return IO version number;.
vnl_matrix< double > trans_matrix_
void global_utm_to_img(const double x, const double y, int zone, double elev, double &u, double &v) const
returns the corresponding pixel position for given geographical coordinates.
vnl_matrix< double > trans_matrix()
void img_to_global(const double i, const double j, double &lon, double &lat) const
returns the corresponding geographical coordinates for a given pixel position (i,j).
void save_as_tfw(std::string const &tfw_filename)
save the camera as tfw.
void backproject(const double u, const double v, double &x, double &y, double &z)
backprojects an image point into local coordinates (based on lvcs_).
void b_write(vsl_b_ostream &os) const
Binary save self to stream.
void global_to_img(const double lon, const double lat, const double elev, double &u, double &v) const
returns the corresponding pixel position for given geographical coordinates.
virtual std::string is_a() const
Return a platform independent string identifying the class.
friend std::istream & operator>>(std::istream &s, vpgl_geo_camera &p)
Read camera from stream.
vpgl_lvcs_sptr const lvcs()
void set_scale_format(bool scale_tag)
void translate(double tx, double ty, double z)
void local_to_utm(const double x, const double y, const double z, double &e, double &n, int &utm_zone)
returns the corresponding utm location for the given local position.
A general camera class.
static bool init_geo_camera(vil_image_resource_sptr const &geotiff_img, const vpgl_lvcs_sptr &lvcs, vpgl_geo_camera *&camera)
uses lvcs to convert local x-y to global longitude and latitude.
std::string type_name() const override
class identity functions for casting.
bool img_four_corners_in_utm(const unsigned ni, const unsigned nj, double elev, double &e1, double &n1, double &e2, double &n2)
static bool init_geo_camera(vil_image_resource_sptr const geotiff_img, vpgl_geo_camera *&camera)
Assumes geographic coordinates are global.
~vpgl_geo_camera() override=default
vpgl_geo_camera()
creates identity matrix and all zero tiepoints.
static bool comp_trans_matrix(double sx1, double sy1, double sz1, std::vector< std::vector< double > > tiepoints, vnl_matrix< double > &trans_matrix, bool scale_tag=false)
static bool init_geo_camera(std::string tfw_name, int utm_zone, unsigned northing, vpgl_geo_camera *&camera)
init without lvcs. Assumes geographic coordinates are global.
vpgl_geo_camera(vnl_matrix< double > trans_matrix, vpgl_lvcs_sptr lvcs)
if scale tag is false be sure that trans_matrix[0][0] and trans_matrix[1][1] is 1....
vpgl_lvcs_sptr lvcs_
lvcs of world parameters.
void project(const double x, const double y, const double z, double &u, double &v) const override
Implementing the generic camera interface of vpgl_camera.
void b_read(vsl_b_istream &is)
Binary load self from stream.
bool operator==(vpgl_geo_camera const &rhs) const
void img_to_wgs(unsigned i, unsigned j, unsigned k, double &lon, double &lat, double &elev)
returns the corresponding geographical coordinate (lon, lat, elev) for a given pixel position (i,...
virtual bool is_class(std::string const &cls) const
Return true if the argument matches the string identifying the class or any parent class.