Debian Codegraph @

imagery/i.ortho.photo/i.ortho.rectify/rectify.c

raw symbols SCIP
1 /* rectification code */
2
3 /* 1/2002: updated to GRASS 5 write routines and
4 CELL/FP elevation - Markus Neteler
5 */
6
7 #include <stdlib.h>
8 #include <string.h>
9 #include <unistd.h>
10 #include "global.h"
11
12 int rectify(struct Ortho_Image_Group *group, char *name, char *mapset,
13 struct cache *ebuffer, double aver_z, char *result,
14 char *interp_method)
15 {
16 struct Cell_head cellhd;
17 int ncols, nrows;
18 int row, col;
19 double row_idx, col_idx;
20 int infd, outfd;
21 RASTER_MAP_TYPE map_type;
22 int cell_size;
23 void *trast, *tptr;
24 double n1, e1, z1;
25 double nx, ex, nx1, ex1, zx1;
26 struct cache *ibuffer;
27
28 select_current_env();
29 Rast_get_cellhd(name, mapset, &cellhd);
30
31 /* open the file to be rectified
32 * set window to cellhd first to be able to read file exactly
33 */
34 Rast_set_input_window(&cellhd);
35 infd = Rast_open_old(name, mapset);
36 map_type = Rast_get_map_type(infd);
37 cell_size = Rast_cell_size(map_type);
38
39 ibuffer = readcell(infd, seg_mb_img, 0);
40
41 Rast_close(infd); /* (pmx) 17 april 2000 */
42
43 G_message(_("Rectify <%s@%s> (project <%s>)"), name, mapset, G_location());
44 select_target_env();
45 G_set_window(&target_window);
46 G_message(_("into <%s@%s> (project <%s>) ..."), result, G_mapset(),
47 G_location());
48
49 nrows = target_window.rows;
50 ncols = target_window.cols;
51
52 if (strcmp(interp_method, "nearest") != 0) {
53 map_type = DCELL_TYPE;
54 cell_size = Rast_cell_size(map_type);
55 }
56
57 /* open the result file into target window
58 * this open must be first since we change the window later
59 * raster maps open for writing are not affected by window changes
60 * but those open for reading are
61 */
62
63 outfd = Rast_open_new(result, map_type);
64 trast = Rast_allocate_output_buf(map_type);
65
66 for (row = 0; row < nrows; row++) {
67 n1 = target_window.north - (row + 0.5) * target_window.ns_res;
68
69 G_percent(row, nrows, 2);
70
71 Rast_set_null_value(trast, ncols, map_type);
72 tptr = trast;
73 for (col = 0; col < ncols; col++) {
74 DCELL *zp = CPTR(ebuffer, row, col);
75
76 e1 = target_window.west + (col + 0.5) * target_window.ew_res;
77
78 /* if target cell has no elevation, set to aver_z */
79 if (Rast_is_d_null_value(zp)) {
80 G_warning(_("No elevation available at row = %d, col = %d"),
81 row, col);
82 z1 = aver_z;
83 }
84 else
85 z1 = *zp;
86
87 /* target coordinates e1, n1 to photo coordinates ex1, nx1 */
88 I_ortho_ref(e1, n1, z1, &ex1, &nx1, &zx1, &group->camera_ref,
89 group->XC, group->YC, group->ZC, group->M);
90
91 G_debug(5, "\t\tAfter ortho ref (photo cords): ex = %f \t nx = %f",
92 ex1, nx1);
93
94 /* photo coordinates ex1, nx1 to image coordinates ex, nx */
95 I_georef(ex1, nx1, &ex, &nx, group->E21, group->N21, 1);
96
97 G_debug(5, "\t\tAfter geo ref: ex = %f \t nx = %f", ex, nx);
98
99 /* convert to row/column indices of source raster */
100 row_idx = (cellhd.north - nx) / cellhd.ns_res;
101 col_idx = (ex - cellhd.west) / cellhd.ew_res;
102
103 /* resample data point */
104 interpolate(ibuffer, tptr, map_type, &row_idx, &col_idx, &cellhd);
105
106 tptr = G_incr_void_ptr(tptr, cell_size);
107 }
108 Rast_put_row(outfd, trast, map_type);
109 }
110 G_percent(1, 1, 1);
111
112 Rast_close(outfd); /* (pmx) 17 april 2000 */
113 G_free(trast);
114
115 close(ibuffer->fd);
116 release_cache(ibuffer);
117
118 Rast_get_cellhd(result, G_mapset(), &cellhd);
119
120 if (cellhd.proj == 0) { /* x,y imagery */
121 cellhd.proj = target_window.proj;
122 cellhd.zone = target_window.zone;
123 }
124
125 if (target_window.proj != cellhd.proj) {
126 cellhd.proj = target_window.proj;
127 G_warning(
128 _("Raster map <%s@%s>: projection don't match current settings"),
129 name, mapset);
130 }
131
132 if (target_window.zone != cellhd.zone) {
133 cellhd.zone = target_window.zone;
134 G_warning(_("Raster map <%s@%s>: zone don't match current settings"),
135 name, mapset);
136 }
137
138 select_current_env();
139
140 return 1;
141 }