8 # include <vcl_msvc_warnings.h> 17 #include <vul/vul_file.h> 19 #include <vil/file_formats/vil_geotiff_header.h> 20 #include <vil/file_formats/vil_tiff.h> 21 #include <vil/file_formats/vil_nitf2_image.h> 34 trans_matrix_(rhs.trans_matrix_),
37 utm_zone_(rhs.utm_zone_),
38 scale_tag_(rhs.scale_tag_)
46 auto* geotiff_tiff = dynamic_cast<vil_tiff_image*> (geotiff_img.ptr());
48 std::cerr <<
"vpgl_geo_camera::init_geo_camera : Error casting vil_image_resource to a tiff image.\n";
51 if (!geotiff_tiff->get_geotiff_header()) {
52 std::cerr <<
"no geotiff header!\n";
57 if (!geotiff_tiff->is_GEOTIFF()) {
58 std::cerr <<
"vpgl_geo_camera::init_geo_camera -- The image should be a GEOTIFF!\n";
62 vil_geotiff_header* gtif = geotiff_tiff->get_geotiff_header();
64 vil_geotiff_header::GTIF_HEMISPH h;
66 std::vector<std::vector<double> > tiepoints;
67 gtif->gtif_tiepoints(tiepoints);
72 double* trans_matrix_values;
75 if (gtif->gtif_trans_matrix(trans_matrix_values)) {
76 std::cout <<
"Transfer matrix is given, using that...." << std::endl;
78 std::cout <<
"Warning LIDAR sample spacing different than 1 meter will not be handled correctly!\n";
80 else if (gtif->gtif_pixelscale(sx1, sy1, sz1)) {
86 std::cout <<
"vpgl_geo_camera::init_geo_camera comp_trans_matrix -- Transform matrix cannot be formed..\n";
95 if (gtif->GCS_WGS84_MET_DEG())
99 if (gtif->PCS_WGS84_UTM_zone(
utm_zone, h) || gtif->PCS_NAD83_UTM_zone(
utm_zone, h))
105 std::cout <<
"vpgl_geo_camera::init_geo_camera()-- if UTM only PCS_WGS84_UTM and PCS_NAD83_UTM, if geographic (GCS_WGS_84) only linear units in meters, angular units in degrees are supported, please define otherwise!" << std::endl;
114 std::string name = vul_file::strip_directory(img_name);
115 name = name.substr(name.find_first_of(
'_')+1, name.size());
117 std::string n_coords = name.substr(0, name.find_first_of(
'_'));
118 std::string n_scale = name.substr(name.find_first_of(
'_')+1, name.find_last_of(
'_')-name.find_first_of(
'_')-1);
119 std::cout <<
"will determine transformation matrix from the file name: " << name << std::endl;
122 std::string hemisphere, direction;
123 float lon, lat, scale_lat, scale_lon;
124 std::size_t n = n_coords.find(
'N');
125 if (n < n_coords.size())
129 n = n_coords.find(
'E');
130 if (n < n_coords.size())
135 std::string n_str = n_coords.substr(n_coords.find_first_of(hemisphere)+1, n_coords.find_first_of(direction)-n_coords.find_first_of(hemisphere)-1);
136 std::stringstream str(n_str); str >> lat;
138 n_str = n_coords.substr(n_coords.find_first_of(direction)+1, n_coords.size());
139 std::stringstream str2(n_str); str2 >> lon;
141 n_str = n_scale.substr(n_scale.find_first_of(
'S')+1, n_scale.find_first_of(
'x')-n_scale.find_first_of(
'S')-1);
142 std::stringstream str3(n_str); str3 >> scale_lat;
144 n_str = n_scale.substr(n_scale.find_first_of(
'x')+1, n_scale.size());
145 std::stringstream str4(n_str); str4 >> scale_lon;
147 std::cout <<
" hemisphere: " << hemisphere <<
" direction: " << direction
148 <<
"\n lat: " << lat <<
" lon: " << lon
149 <<
"\n scale_lat: " << scale_lat <<
" scale_lon: " << scale_lon << std::endl;
152 if (hemisphere ==
"N")
153 std::cout <<
" upper left corner in the image is: " << hemisphere << lat+scale_lat << direction << lon << std::endl;
155 std::cout <<
" upper left corner in the image is: " << hemisphere << lat-scale_lat << direction << lon << std::endl;
156 if (direction ==
"W")
157 std::cout <<
" lower right corner in the image is: " << hemisphere << lat << direction << lon-scale_lon << std::endl;
159 std::cout <<
" lower right corner in the image is: " << hemisphere << lat << direction << lon+scale_lon << std::endl;
162 if (direction ==
"E") {
170 if (hemisphere ==
"N") {
183 std::string n = name.substr(name.find_first_of(
'N')+1, name.find_first_of(
'W'));
184 float lon, lat, scale;
185 std::stringstream str(n); str >> lat;
186 n = name.substr(name.find_first_of(
'W')+1, name.find_first_of(
'_'));
187 std::stringstream str2(n); str2 >> lon;
188 n = name.substr(name.find_first_of(
'x')+1, name.find_last_of(
'.'));
189 std::stringstream str3(n); str3 >> scale;
190 std::cout <<
" lat: " << lat <<
" lon: " << lon <<
" WARNING: using same scale for both ni and nj: scale:" << scale << std::endl;
193 std::cout <<
"upper left corner in the image is: " << lat+scale <<
" N " << lon <<
" W\n" 194 <<
"lower right corner in the image is: " << lat <<
" N " << lon-scale <<
" W" << std::endl;
208 std::string name = vul_file::strip_directory(img_name);
209 name = name.substr(name.find_first_of(
'_')+1, name.size());
211 std::string n_coords = name.substr(0, name.find_first_of(
'_'));
212 std::string n_scale = name.substr(name.find_first_of(
'_')+1, name.find_last_of(
'_')-name.find_first_of(
'_')-1);
213 std::cout <<
"will determine transformation matrix from the file name: " << name << std::endl;
216 std::string hemisphere, direction;
217 float lon, lat, scale;
218 std::size_t n = n_coords.find(
'N');
219 if (n < n_coords.size())
223 n = n_coords.find(
'E');
224 if (n < n_coords.size())
229 std::string n_str = n_coords.substr(n_coords.find_first_of(hemisphere)+1, n_coords.find_first_of(direction)-n_coords.find_first_of(hemisphere)-1);
230 std::stringstream str(n_str); str >> lat;
232 n_str = n_coords.substr(n_coords.find_first_of(direction)+1, n_coords.size());
233 std::stringstream str2(n_str); str2 >> lon;
235 n_str = n_scale.substr(n_scale.find_first_of(
'S')+1, n_scale.find_first_of(
'x')-n_scale.find_first_of(
'S')-1);
236 std::stringstream str3(n_str); str3 >> scale;
238 std::cout <<
" hemisphere: " << hemisphere <<
" direction: " << direction
239 <<
"\n lat: " << lat <<
" lon: " << lon
240 <<
"\n WARNING: using same scale for both ni and nj: " << scale << std::endl;
243 if (hemisphere ==
"S")
245 if (direction ==
"W")
249 std::cout <<
" upper left corner in the image is: " << hemisphere << lat+scale << direction << lon << std::endl;
250 std::cout <<
" lower right corner in the image is: " << hemisphere << lat << direction << lon+scale << std::endl;
268 std::ifstream ifs(tfw_name.c_str());
270 if (!ifs.is_open()) {
271 std::cerr <<
"in vpgl_geo_camera::init_geo_camera() -- cannot open: " << tfw_name <<
'\n';
295 double& u,
double& v)
const 323 double& x,
double& y,
double& z)
338 double lat, lon, elev;
368 std::cout <<
"Warning! Translation offset will only be computed correctly for lidar pixel spacing = 1 meter\n";
377 double& lon,
double& lat)
const 396 lon =
v[0]; lat =
v[1];
403 double& u,
double& v)
const 406 double x1=lon, y1=lat, z1=gz;
429 res = trans_matrix_inv*vec;
473 utm.
transform(zone, x, y, elev, lat, lon, z);
490 res = trans_matrix_inv*vec;
509 std::ofstream ofs(tfw_filename.c_str());
523 std::cerr <<
"In vpgl_geo_camera::img_four_corners_in_utm() -- UTM hasn't been set!\n";
549 s <<
"geocam is using UTM with zone: " << p.
utm_zone_ <<
'\n';
568 std::vector<std::vector<double> > tiepoints,
574 assert (tiepoints.size() > 0);
575 assert (tiepoints[0].size() == 6);
576 double I = tiepoints[0][0];
577 double J = tiepoints[0][1];
578 double K = tiepoints[0][2];
579 double X = tiepoints[0][3];
580 double Y = tiepoints[0][4];
581 double Z = tiepoints[0][5];
594 double sx = 1.0, sy = 1.0, sz = 1.0;
596 sx = sx1; sy = sy1; sz = sz1;
598 double Tx = X - I*sx;
599 double Ty = Y + J*sy;
600 double Tz = Z - K*sz;
618 assert(!
"Not yet implemented");
648 unsigned nrows, ncols;
652 for (
unsigned i = 0; i < nrows; i++)
653 for (
unsigned j = 0; j < ncols; j++)
665 std::cerr <<
"I/O ERROR: vpgl_geo_camera::b_read(vsl_b_istream&)\n" 666 <<
" Unknown version number "<< ver <<
'\n';
667 is.
is().clear(std::ios::badbit);
unsigned int cols() const
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).
A geographic coordinate system.
vnl_matrix & copy_in(double const *)
vnl_matrix & fill_diagonal(double const &)
void set_utm(int utm_zone, unsigned northing)
std::istream & operator>>(std::istream &is, vpgl_local_rational_camera< T > &p)
Read from stream.
void vsl_b_write(vsl_b_ostream &os, vpgl_affine_camera< T > const &camera)
Binary save camera to stream.
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).
std::istream & is() const
void vsl_b_read(vsl_b_istream &is, vpgl_affine_camera< T > &camera)
Binary load camera from stream.
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.
A rip-off of the IUE utm_geodedic and geodetic_utm transform classes which allows the GeoPt to suppor...
vnl_matrix_fixed< T, 1, 1 > vnl_inverse(vnl_matrix_fixed< T, 1, 1 > const &m)
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.
vpgl_lvcs_sptr const lvcs()
A geotiff image deduced camera class.
void set_scale_format(bool scale_tag)
vnl_matrix & fill(double const &)
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.
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::ostream & operator<<(std::ostream &s, const vpgl_local_rational_camera< T > &p)
Write to stream.
bool img_four_corners_in_utm(const unsigned ni, const unsigned nj, double elev, double &e1, double &n1, double &e2, double &n2)
unsigned int rows() const
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)
bool set_size(unsigned r, unsigned c)
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.
void transform(int utm_zone, double x, double y, double z, double &lat, double &lon, double &elev, bool south_flag=false, double utm_central_meridian=0)
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,...