| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
#include <stdlib.h> |
| 24 |
#include <string.h> |
| 25 |
|
| 26 |
#include <grass/gis.h> |
| 27 |
#include <grass/glocale.h> |
| 28 |
#include "orthophoto.h" |
| 29 |
|
| 30 |
int main(int argc, char *argv[]) |
| 31 |
{ |
| 32 |
struct GModule *module; |
| 33 |
struct Option *group_opt, |
| 34 |
*camera_opt, |
| 35 |
*cname_opt, |
| 36 |
*cid_opt, |
| 37 |
*cfl_opt, |
| 38 |
*pp_opt, |
| 39 |
*fid_opt; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
const char *location; |
| 45 |
const char *mapset; |
| 46 |
char *group; |
| 47 |
char *camera, *cam_name, *cam_id; |
| 48 |
double ppx, ppy, cfl; |
| 49 |
struct Ortho_Camera_File_Ref cam_info; |
| 50 |
int put_cam_info = 0; |
| 51 |
|
| 52 |
G_gisinit(argv[0]); |
| 53 |
|
| 54 |
module = G_define_module(); |
| 55 |
G_add_keyword(_("imagery")); |
| 56 |
G_add_keyword(_("orthorectify")); |
| 57 |
module->description = |
| 58 |
_("Select and modify the imagery group camera reference file."); |
| 59 |
|
| 60 |
group_opt = G_define_standard_option(G_OPT_I_GROUP); |
| 61 |
group_opt->required = NO; |
| 62 |
group_opt->description = _("Name of imagery group for ortho-rectification"); |
| 63 |
|
| 64 |
camera_opt = G_define_standard_option(G_OPT_F_INPUT); |
| 65 |
camera_opt->key = "camera"; |
| 66 |
camera_opt->required = YES; |
| 67 |
camera_opt->gisprompt = "old,camera,file"; |
| 68 |
camera_opt->label = _("Name of camera reference file"); |
| 69 |
|
| 70 |
cname_opt = G_define_option(); |
| 71 |
cname_opt->type = TYPE_STRING; |
| 72 |
cname_opt->key = "name"; |
| 73 |
cname_opt->label = _("Camera name"); |
| 74 |
|
| 75 |
cid_opt = G_define_option(); |
| 76 |
cid_opt->type = TYPE_STRING; |
| 77 |
cid_opt->key = "id"; |
| 78 |
cid_opt->label = _("Camera id"); |
| 79 |
|
| 80 |
cfl_opt = G_define_option(); |
| 81 |
cfl_opt->type = TYPE_DOUBLE; |
| 82 |
cfl_opt->key = "clf"; |
| 83 |
cfl_opt->label = _("Calibrated focal length"); |
| 84 |
|
| 85 |
pp_opt = G_define_standard_option(G_OPT_M_COORDS); |
| 86 |
pp_opt->key = "pp"; |
| 87 |
pp_opt->label = _("Principal point coordinates"); |
| 88 |
|
| 89 |
fid_opt = G_define_standard_option(G_OPT_M_COORDS); |
| 90 |
fid_opt->key = "fid"; |
| 91 |
fid_opt->multiple = YES; |
| 92 |
fid_opt->label = _("Fiducial coordinates"); |
| 93 |
|
| 94 |
if (G_parser(argc, argv)) |
| 95 |
exit(EXIT_FAILURE); |
| 96 |
|
| 97 |
location = G_location(); |
| 98 |
mapset = G_mapset(); |
| 99 |
|
| 100 |
group = group_opt->answer; |
| 101 |
camera = camera_opt->answer; |
| 102 |
cam_name = cname_opt->answer; |
| 103 |
cam_id = cid_opt->answer; |
| 104 |
|
| 105 |
if (G_legal_filename(camera) < 0) |
| 106 |
G_fatal_error(_("<%s> is an illegal file name"), camera); |
| 107 |
|
| 108 |
if (cam_name || cam_id || cfl_opt->answer || pp_opt->answers || |
| 109 |
fid_opt->answers) |
| 110 |
put_cam_info = 1; |
| 111 |
|
| 112 |
ppx = ppy = .0; |
| 113 |
if (pp_opt->answers) { |
| 114 |
sscanf(pp_opt->answers[0], "%lf", &ppx); |
| 115 |
sscanf(pp_opt->answers[1], "%lf", &ppy); |
| 116 |
} |
| 117 |
|
| 118 |
if (G_find_file2("camera", camera, G_mapset())) { |
| 119 |
|
| 120 |
if (!I_get_cam_info(camera, &cam_info)) |
| 121 |
G_fatal_error(_("Can not read camera file '%s'"), camera); |
| 122 |
|
| 123 |
if (cam_name && strcmp(cam_name, cam_info.cam_name)) { |
| 124 |
G_message(_("Replacing camera name '%s' with '%s'"), |
| 125 |
cam_info.cam_name, cam_name); |
| 126 |
strcpy(cam_info.cam_name, cam_name); |
| 127 |
} |
| 128 |
|
| 129 |
if (cam_id && strcmp(cam_id, cam_info.cam_id)) { |
| 130 |
G_message(_("Replacing camera cam_id '%s' with '%s'"), |
| 131 |
cam_info.cam_id, cam_id); |
| 132 |
strcpy(cam_info.cam_id, cam_id); |
| 133 |
} |
| 134 |
|
| 135 |
if (cfl_opt->answer) { |
| 136 |
cfl = atof(cfl_opt->answer); |
| 137 |
if (cfl != cam_info.CFL) { |
| 138 |
G_message(_("Replacing calibrated focal length '%g' with '%g'"), |
| 139 |
cam_info.CFL, cfl); |
| 140 |
cam_info.CFL = cfl; |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
if (pp_opt->answers) { |
| 145 |
G_message(_("Replacing coordinates of principal point '%.17g, " |
| 146 |
"%.17g' with '%.17g, %.17g'"), |
| 147 |
cam_info.Xp, cam_info.Yp, ppx, ppy); |
| 148 |
cam_info.Xp = ppx; |
| 149 |
cam_info.Yp = ppy; |
| 150 |
} |
| 151 |
} |
| 152 |
else { |
| 153 |
|
| 154 |
|
| 155 |
if (!cam_name) |
| 156 |
G_fatal_error( |
| 157 |
_("Please provide a camera name for a new camera definition")); |
| 158 |
strcpy(cam_info.cam_name, cam_name); |
| 159 |
|
| 160 |
if (!cam_id) |
| 161 |
G_fatal_error( |
| 162 |
_("Please provide a camera ID for a new camera definition")); |
| 163 |
strcpy(cam_info.cam_id, cam_id); |
| 164 |
|
| 165 |
if (!cfl_opt->answer) |
| 166 |
G_fatal_error(_("Please provide calibrated focal length for a new " |
| 167 |
"camera definition")); |
| 168 |
cam_info.CFL = atof(cfl_opt->answer); |
| 169 |
|
| 170 |
if (!pp_opt->answers) |
| 171 |
G_message(_( |
| 172 |
"Using default coordinates 0.0, 0.0 for the principal point")); |
| 173 |
cam_info.Xp = ppx; |
| 174 |
cam_info.Yp = ppy; |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
cam_info.num_fid = 0; |
| 179 |
if (fid_opt->answers) { |
| 180 |
int i, fid_no; |
| 181 |
double Xf, Yf; |
| 182 |
|
| 183 |
for (i = 0, fid_no = 0; fid_opt->answers[i] != NULL; i += 2, fid_no++) { |
| 184 |
|
| 185 |
if (fid_no > 19) { |
| 186 |
G_warning(_("Too many fiducials!")); |
| 187 |
break; |
| 188 |
} |
| 189 |
sscanf(fid_opt->answers[i], "%lf", &Xf); |
| 190 |
cam_info.fiducials[fid_no].Xf = Xf; |
| 191 |
sscanf(fid_opt->answers[i + 1], "%lf", &Yf); |
| 192 |
cam_info.fiducials[fid_no].Yf = Yf; |
| 193 |
|
| 194 |
snprintf(cam_info.fiducials[fid_no].fid_id, |
| 195 |
sizeof(cam_info.fiducials[fid_no].fid_id), "%d", fid_no); |
| 196 |
} |
| 197 |
cam_info.num_fid = fid_no; |
| 198 |
} |
| 199 |
|
| 200 |
if (put_cam_info) { |
| 201 |
|
| 202 |
I_put_cam_info(camera, &cam_info); |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
if (group) { |
| 207 |
|
| 208 |
if (!I_find_group(group)) |
| 209 |
G_fatal_error(_("No group '%s' in current mapset"), group); |
| 210 |
|
| 211 |
I_put_group_camera(group, camera); |
| 212 |
|
| 213 |
G_message(_("Group [%s] in project (location) [%s] mapset [%s] now " |
| 214 |
"uses camera " |
| 215 |
"file [%s]"), |
| 216 |
group, location, mapset, camera); |
| 217 |
} |
| 218 |
|
| 219 |
exit(EXIT_SUCCESS); |
| 220 |
} |