| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#include <stdlib.h> |
| 22 |
#include <string.h> |
| 23 |
#include "global.h" |
| 24 |
|
| 25 |
int seg_mb_img, seg_mb_elev; |
| 26 |
|
| 27 |
char *elev_name; |
| 28 |
char *elev_mapset; |
| 29 |
|
| 30 |
func interpolate; |
| 31 |
|
| 32 |
struct Cell_head target_window; |
| 33 |
|
| 34 |
void err_exit(struct Ref *, char *, char *); |
| 35 |
|
| 36 |
|
| 37 |
struct menu menu[] = { |
| 38 |
{p_nearest, "nearest", "nearest neighbor"}, |
| 39 |
{p_bilinear, "linear", "linear interpolation"}, |
| 40 |
{p_cubic, "cubic", "cubic convolution"}, |
| 41 |
{p_lanczos, "lanczos", "lanczos filter"}, |
| 42 |
{p_bilinear_f, "linear_f", "linear interpolation with fallback"}, |
| 43 |
{p_cubic_f, "cubic_f", "cubic convolution with fallback"}, |
| 44 |
{p_lanczos_f, "lanczos_f", "lanczos filter with fallback"}, |
| 45 |
{NULL, NULL, NULL}}; |
| 46 |
|
| 47 |
static char *make_ipol_list(void); |
| 48 |
|
| 49 |
int main(int argc, char *argv[]) |
| 50 |
{ |
| 51 |
char extension[INAME_LEN]; |
| 52 |
char *ipolname; |
| 53 |
int method; |
| 54 |
char *seg_mb; |
| 55 |
int i, m, k = 0; |
| 56 |
int got_file = 0, target_overwrite = 0; |
| 57 |
char *overstr; |
| 58 |
|
| 59 |
struct Ortho_Image_Group group; |
| 60 |
int *ref_list; |
| 61 |
int n; |
| 62 |
char *camera; |
| 63 |
char tl[100]; |
| 64 |
char math_exp[100]; |
| 65 |
char units[100]; |
| 66 |
char nd[100]; |
| 67 |
|
| 68 |
struct Cell_head cellhd, elevhd; |
| 69 |
|
| 70 |
struct Option *grp, |
| 71 |
*ifile, |
| 72 |
*ext, |
| 73 |
*tres, |
| 74 |
*mem, |
| 75 |
*angle, |
| 76 |
*interpol; |
| 77 |
|
| 78 |
|
| 79 |
struct Flag *c, *a, *pan_flag; |
| 80 |
struct GModule *module; |
| 81 |
|
| 82 |
G_gisinit(argv[0]); |
| 83 |
|
| 84 |
module = G_define_module(); |
| 85 |
G_add_keyword(_("imagery")); |
| 86 |
G_add_keyword(_("orthorectify")); |
| 87 |
module->description = _("Orthorectifies an image by using the image to " |
| 88 |
"photo coordinate transformation matrix."); |
| 89 |
|
| 90 |
grp = G_define_standard_option(G_OPT_I_GROUP); |
| 91 |
|
| 92 |
ifile = G_define_standard_option(G_OPT_R_INPUTS); |
| 93 |
ifile->required = NO; |
| 94 |
|
| 95 |
ext = G_define_option(); |
| 96 |
ext->key = "extension"; |
| 97 |
ext->type = TYPE_STRING; |
| 98 |
ext->required = YES; |
| 99 |
ext->multiple = NO; |
| 100 |
ext->description = _("Output raster map(s) suffix"); |
| 101 |
|
| 102 |
tres = G_define_option(); |
| 103 |
tres->key = "resolution"; |
| 104 |
tres->type = TYPE_DOUBLE; |
| 105 |
tres->required = NO; |
| 106 |
tres->description = _("Target resolution (ignored if -c flag used)"); |
| 107 |
|
| 108 |
mem = G_define_standard_option(G_OPT_MEMORYMB); |
| 109 |
|
| 110 |
ipolname = make_ipol_list(); |
| 111 |
|
| 112 |
interpol = G_define_option(); |
| 113 |
interpol->key = "method"; |
| 114 |
interpol->type = TYPE_STRING; |
| 115 |
interpol->required = NO; |
| 116 |
interpol->answer = "nearest"; |
| 117 |
interpol->options = ipolname; |
| 118 |
interpol->description = _("Interpolation method to use"); |
| 119 |
|
| 120 |
angle = G_define_standard_option(G_OPT_R_OUTPUT); |
| 121 |
angle->key = "angle"; |
| 122 |
angle->required = NO; |
| 123 |
angle->description = |
| 124 |
_("Raster map with camera angle relative to ground surface"); |
| 125 |
|
| 126 |
c = G_define_flag(); |
| 127 |
c->key = 'c'; |
| 128 |
c->description = |
| 129 |
_("Use current region settings in target project (location) " |
| 130 |
"(def.=calculate smallest area)"); |
| 131 |
|
| 132 |
a = G_define_flag(); |
| 133 |
a->key = 'a'; |
| 134 |
a->description = _("Rectify all raster maps in group"); |
| 135 |
|
| 136 |
pan_flag = G_define_flag(); |
| 137 |
pan_flag->key = 'p'; |
| 138 |
pan_flag->description = _("Enable panorama camera correction"); |
| 139 |
|
| 140 |
if (G_parser(argc, argv)) |
| 141 |
exit(EXIT_FAILURE); |
| 142 |
|
| 143 |
|
| 144 |
for (method = 0; (ipolname = menu[method].name); method++) |
| 145 |
if (strcmp(ipolname, interpol->answer) == 0) |
| 146 |
break; |
| 147 |
|
| 148 |
if (!ipolname) |
| 149 |
G_fatal_error(_("<%s=%s> unknown %s"), interpol->key, interpol->answer, |
| 150 |
interpol->key); |
| 151 |
interpolate = menu[method].method; |
| 152 |
|
| 153 |
G_strip(grp->answer); |
| 154 |
if (G_strlcpy(group.name, grp->answer, sizeof(group.name)) >= |
| 155 |
sizeof(group.name)) |
| 156 |
G_fatal_error(_("Group name <%s> is too long"), grp->answer); |
| 157 |
if (G_strlcpy(extension, ext->answer, sizeof(extension)) >= |
| 158 |
sizeof(extension)) |
| 159 |
G_fatal_error(_("Extension <%s> is too long"), ext->answer); |
| 160 |
|
| 161 |
seg_mb = NULL; |
| 162 |
if (mem->answer) { |
| 163 |
if (atoi(mem->answer) > 0) |
| 164 |
seg_mb = mem->answer; |
| 165 |
} |
| 166 |
|
| 167 |
if (!ifile->answers) |
| 168 |
a->answer = 1; |
| 169 |
|
| 170 |
|
| 171 |
if (!a->answer) { |
| 172 |
for (k = 0; ifile->answers[k]; k++) |
| 173 |
; |
| 174 |
} |
| 175 |
|
| 176 |
camera = (char *)G_malloc(GNAME_MAX * sizeof(char)); |
| 177 |
elev_name = (char *)G_malloc(GNAME_MAX * sizeof(char)); |
| 178 |
elev_mapset = (char *)G_malloc(GMAPSET_MAX * sizeof(char)); |
| 179 |
|
| 180 |
|
| 181 |
if (!I_find_group(group.name)) { |
| 182 |
G_fatal_error(_("Group <%s> not found"), group.name); |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
if (!I_get_group_ref(group.name, &group.group_ref)) { |
| 187 |
G_warning(_("Project (location): %s"), G_location()); |
| 188 |
G_warning(_("Mapset: %s"), G_mapset()); |
| 189 |
G_fatal_error(_("Could not read REF file for group <%s>"), group.name); |
| 190 |
} |
| 191 |
|
| 192 |
if (group.group_ref.nfiles <= 0) { |
| 193 |
G_important_message( |
| 194 |
_("Group <%s> contains no raster maps; run i.group"), grp->answer); |
| 195 |
exit(EXIT_SUCCESS); |
| 196 |
} |
| 197 |
|
| 198 |
ref_list = (int *)G_malloc(group.group_ref.nfiles * sizeof(int)); |
| 199 |
|
| 200 |
if (a->answer) { |
| 201 |
for (n = 0; n < group.group_ref.nfiles; n++) { |
| 202 |
ref_list[n] = 1; |
| 203 |
} |
| 204 |
} |
| 205 |
else { |
| 206 |
char xname[GNAME_MAX], xmapset[GMAPSET_MAX], *name, *mapset; |
| 207 |
|
| 208 |
for (n = 0; n < group.group_ref.nfiles; n++) |
| 209 |
ref_list[n] = 0; |
| 210 |
|
| 211 |
for (m = 0; m < k; m++) { |
| 212 |
got_file = 0; |
| 213 |
if (G_name_is_fully_qualified(ifile->answers[m], xname, xmapset)) { |
| 214 |
name = xname; |
| 215 |
mapset = xmapset; |
| 216 |
} |
| 217 |
else { |
| 218 |
name = ifile->answers[m]; |
| 219 |
mapset = NULL; |
| 220 |
} |
| 221 |
|
| 222 |
got_file = 0; |
| 223 |
for (n = 0; n < group.group_ref.nfiles; n++) { |
| 224 |
if (mapset) { |
| 225 |
if (strcmp(name, group.group_ref.file[n].name) == 0 && |
| 226 |
strcmp(mapset, group.group_ref.file[n].mapset) == 0) { |
| 227 |
got_file = 1; |
| 228 |
ref_list[n] = 1; |
| 229 |
break; |
| 230 |
} |
| 231 |
} |
| 232 |
else { |
| 233 |
if (strcmp(name, group.group_ref.file[n].name) == 0) { |
| 234 |
got_file = 1; |
| 235 |
ref_list[n] = 1; |
| 236 |
break; |
| 237 |
} |
| 238 |
} |
| 239 |
} |
| 240 |
if (got_file == 0) |
| 241 |
err_exit(&group.group_ref, ifile->answers[m], group.name); |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
if (!I_get_group_camera(group.name, camera)) |
| 247 |
G_fatal_error(_("No camera reference file selected for group <%s>"), |
| 248 |
group.name); |
| 249 |
|
| 250 |
if (!I_get_cam_info(camera, &group.camera_ref)) |
| 251 |
G_fatal_error(_("Bad format in camera file for group <%s>"), |
| 252 |
group.name); |
| 253 |
|
| 254 |
|
| 255 |
if (I_find_initial(group.name)) { |
| 256 |
if (!I_get_init_info(group.name, &group.camera_exp)) |
| 257 |
G_warning( |
| 258 |
_("Bad format in initial exposure station file for group <%s>"), |
| 259 |
group.name); |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
if (pan_flag->answer) |
| 264 |
I_ortho_panorama(); |
| 265 |
|
| 266 |
|
| 267 |
get_target(group.name); |
| 268 |
|
| 269 |
|
| 270 |
get_ref_points(&group); |
| 271 |
|
| 272 |
|
| 273 |
get_conz_points(&group); |
| 274 |
|
| 275 |
|
| 276 |
if ((overstr = getenv("GRASS_OVERWRITE"))) |
| 277 |
target_overwrite = atoi(overstr); |
| 278 |
|
| 279 |
if (!target_overwrite) { |
| 280 |
|
| 281 |
char result[GNAME_MAX]; |
| 282 |
|
| 283 |
select_target_env(); |
| 284 |
for (i = 0; i < group.group_ref.nfiles; i++) { |
| 285 |
if (!ref_list[i]) |
| 286 |
continue; |
| 287 |
|
| 288 |
if (G_strlcpy(result, group.group_ref.file[i].name, |
| 289 |
sizeof(result)) >= sizeof(result)) |
| 290 |
G_fatal_error(_("Map name <%s> is too long"), |
| 291 |
group.group_ref.file[i].name); |
| 292 |
strcat(result, extension); |
| 293 |
|
| 294 |
if (G_legal_filename(result) < 0) |
| 295 |
G_fatal_error(_("Extension <%s> is illegal"), extension); |
| 296 |
|
| 297 |
if (G_find_raster2(result, G_mapset())) { |
| 298 |
G_warning(_("The following raster map already exists in")); |
| 299 |
G_warning(_("target project (location) %s, mapset %s:"), |
| 300 |
G_location(), G_mapset()); |
| 301 |
G_warning("<%s>", result); |
| 302 |
G_fatal_error(_("Orthorectification cancelled.")); |
| 303 |
} |
| 304 |
} |
| 305 |
if (angle->answer) { |
| 306 |
if (G_find_raster2(angle->answer, G_mapset())) { |
| 307 |
G_warning(_("The following raster map already exists in")); |
| 308 |
G_warning(_("target project (location) %s, mapset %s:"), |
| 309 |
G_location(), G_mapset()); |
| 310 |
G_warning("<%s>", angle->answer); |
| 311 |
G_fatal_error(_("Orthorectification cancelled.")); |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
select_current_env(); |
| 316 |
} |
| 317 |
else |
| 318 |
G_debug(1, "Overwriting OK"); |
| 319 |
|
| 320 |
|
| 321 |
if (!c->answer) { |
| 322 |
double res = -1; |
| 323 |
|
| 324 |
if (tres->answer) { |
| 325 |
if (!((res = atof(tres->answer)) > 0)) |
| 326 |
G_warning(_("Target resolution must be > 0, ignored")); |
| 327 |
} |
| 328 |
|
| 329 |
get_ref_window(&group.group_ref, ref_list, &cellhd); |
| 330 |
georef_window(&group, &cellhd, &target_window, res); |
| 331 |
} |
| 332 |
|
| 333 |
G_verbose_message(_("Using region: N=%f S=%f, E=%f W=%f"), |
| 334 |
target_window.north, target_window.south, |
| 335 |
target_window.east, target_window.west); |
| 336 |
|
| 337 |
G_debug(1, "Looking for elevation file in group: <%s>", group.name); |
| 338 |
|
| 339 |
|
| 340 |
if (!I_get_group_elev(group.name, elev_name, elev_mapset, tl, math_exp, |
| 341 |
units, nd)) |
| 342 |
G_fatal_error(_("No target elevation model selected for group <%s>"), |
| 343 |
group.name); |
| 344 |
|
| 345 |
G_debug(1, "Block elevation: <%s> in <%s>", elev_name, elev_mapset); |
| 346 |
|
| 347 |
|
| 348 |
select_target_env(); |
| 349 |
Rast_get_cellhd(elev_name, elev_mapset, &elevhd); |
| 350 |
select_current_env(); |
| 351 |
|
| 352 |
|
| 353 |
seg_mb_img = seg_mb_elev = -1; |
| 354 |
if (seg_mb) { |
| 355 |
int max_rows, max_cols; |
| 356 |
int nx, ny; |
| 357 |
double max_mb_img, max_mb_elev; |
| 358 |
int seg_mb_total; |
| 359 |
|
| 360 |
max_rows = max_cols = 0; |
| 361 |
for (i = 0; i < group.group_ref.nfiles; i++) { |
| 362 |
if (ref_list[i]) { |
| 363 |
Rast_get_cellhd(group.group_ref.file[i].name, |
| 364 |
group.group_ref.file[i].mapset, &cellhd); |
| 365 |
if (max_rows < cellhd.rows) |
| 366 |
max_rows = cellhd.rows; |
| 367 |
if (max_cols < cellhd.cols) |
| 368 |
max_cols = cellhd.cols; |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
ny = (max_rows + BDIM - 1) / BDIM; |
| 373 |
nx = (max_cols + BDIM - 1) / BDIM; |
| 374 |
|
| 375 |
max_mb_img = ((double)nx * ny * sizeof(block)) / (1 << 20); |
| 376 |
|
| 377 |
ny = (target_window.rows + BDIM - 1) / BDIM; |
| 378 |
nx = (target_window.cols + BDIM - 1) / BDIM; |
| 379 |
|
| 380 |
max_mb_elev = ((double)nx * ny * sizeof(block)) / (1 << 20); |
| 381 |
|
| 382 |
if ((seg_mb_total = atoi(seg_mb)) > 0) { |
| 383 |
seg_mb_elev = |
| 384 |
max_mb_elev * (seg_mb_total / (max_mb_img + max_mb_elev)) + 0.5; |
| 385 |
seg_mb_img = |
| 386 |
max_mb_img * (seg_mb_total / (max_mb_img + max_mb_elev)) + 0.5; |
| 387 |
} |
| 388 |
} |
| 389 |
|
| 390 |
|
| 391 |
exec_rectify(&group, ref_list, extension, interpol->answer, angle->answer); |
| 392 |
|
| 393 |
G_done_msg(" "); |
| 394 |
|
| 395 |
exit(EXIT_SUCCESS); |
| 396 |
} |
| 397 |
|
| 398 |
void err_exit(struct Ref *ref, char *file, char *grp) |
| 399 |
{ |
| 400 |
int n; |
| 401 |
|
| 402 |
G_warning(_("Input raster map <%s> does not exist in group <%s>."), file, |
| 403 |
grp); |
| 404 |
G_message(_("Try:")); |
| 405 |
|
| 406 |
for (n = 0; n < ref->nfiles; n++) |
| 407 |
G_message("%s@%s", ref->file[n].name, ref->file[n].mapset); |
| 408 |
|
| 409 |
G_fatal_error(_("Exit!")); |
| 410 |
} |
| 411 |
|
| 412 |
static char *make_ipol_list(void) |
| 413 |
{ |
| 414 |
int size = 0; |
| 415 |
int i; |
| 416 |
char *buf; |
| 417 |
|
| 418 |
for (i = 0; menu[i].name; i++) |
| 419 |
size += strlen(menu[i].name) + 1; |
| 420 |
|
| 421 |
buf = G_malloc(size); |
| 422 |
*buf = '\0'; |
| 423 |
|
| 424 |
for (i = 0; menu[i].name; i++) { |
| 425 |
if (i) |
| 426 |
strcat(buf, ","); |
| 427 |
strcat(buf, menu[i].name); |
| 428 |
} |
| 429 |
|
| 430 |
return buf; |
| 431 |
} |