| 1 |
#include <unistd.h> |
| 2 |
#include <grass/imagery.h> |
| 3 |
#include "orthophoto.h" |
| 4 |
#include <grass/gis.h> |
| 5 |
|
| 6 |
#define POINT_FILE "CONTROL_POINTS" |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
int I_read_con_points(FILE *fd, struct Ortho_Control_Points *cp) |
| 11 |
{ |
| 12 |
char buf[300]; |
| 13 |
double e1, e2, n1, n2, z1, z2; |
| 14 |
int status; |
| 15 |
|
| 16 |
cp->count = 0; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
cp->e1 = NULL; |
| 23 |
cp->e2 = NULL; |
| 24 |
cp->n1 = NULL; |
| 25 |
cp->n2 = NULL; |
| 26 |
cp->z1 = NULL; |
| 27 |
cp->z2 = NULL; |
| 28 |
|
| 29 |
cp->status = NULL; |
| 30 |
|
| 31 |
while (G_getl(buf, sizeof(buf), fd)) { |
| 32 |
G_strip(buf); |
| 33 |
if (*buf == '#' || *buf == 0) |
| 34 |
continue; |
| 35 |
if (sscanf(buf, "%lf%lf%lf%lf%lf%lf%d", &e1, &n1, &z1, &e2, &n2, &z2, |
| 36 |
&status) == 7) |
| 37 |
I_new_con_point(cp, e1, n1, z1, e2, n2, z2, status); |
| 38 |
else |
| 39 |
return -4; |
| 40 |
} |
| 41 |
|
| 42 |
return 1; |
| 43 |
} |
| 44 |
|
| 45 |
int I_new_con_point(struct Ortho_Control_Points *cp, double e1, double n1, |
| 46 |
double z1, double e2, double n2, double z2, int status) |
| 47 |
{ |
| 48 |
int i; |
| 49 |
size_t size; |
| 50 |
|
| 51 |
if (status < 0) |
| 52 |
return 1; |
| 53 |
i = (cp->count)++; |
| 54 |
size = cp->count * sizeof(double); |
| 55 |
cp->e1 = (double *)G_realloc(cp->e1, size); |
| 56 |
cp->e2 = (double *)G_realloc(cp->e2, size); |
| 57 |
cp->n1 = (double *)G_realloc(cp->n1, size); |
| 58 |
cp->n2 = (double *)G_realloc(cp->n2, size); |
| 59 |
cp->z1 = (double *)G_realloc(cp->z1, size); |
| 60 |
cp->z2 = (double *)G_realloc(cp->z2, size); |
| 61 |
|
| 62 |
size = cp->count * sizeof(int); |
| 63 |
cp->status = (int *)G_realloc(cp->status, size); |
| 64 |
|
| 65 |
cp->e1[i] = e1; |
| 66 |
cp->e2[i] = e2; |
| 67 |
cp->n1[i] = n1; |
| 68 |
cp->n2[i] = n2; |
| 69 |
cp->z1[i] = z1; |
| 70 |
cp->z2[i] = z2; |
| 71 |
|
| 72 |
cp->status[i] = status; |
| 73 |
|
| 74 |
return 0; |
| 75 |
} |
| 76 |
|
| 77 |
int I_write_con_points(FILE *fd, struct Ortho_Control_Points *cp) |
| 78 |
{ |
| 79 |
int i; |
| 80 |
|
| 81 |
fprintf(fd, "# %7s %15s %30s %15s %9s status\n", "", "photo", "", "control", |
| 82 |
""); |
| 83 |
fprintf(fd, "# %15s %15s %15s %15s %15s %15s (1=ok)\n", "x", "y", "-cfl", |
| 84 |
"east", "north", "elev."); |
| 85 |
fprintf(fd, "#\n"); |
| 86 |
for (i = 0; i < cp->count; i++) |
| 87 |
if (cp->status[i] >= 0) |
| 88 |
fprintf(fd, " %15f %15f %15f %15f %15f %15f %4d\n", cp->e1[i], |
| 89 |
cp->n1[i], cp->z1[i], cp->e2[i], cp->n2[i], cp->z2[i], |
| 90 |
cp->status[i]); |
| 91 |
|
| 92 |
return 0; |
| 93 |
} |
| 94 |
|
| 95 |
int I_get_con_points(char *group, struct Ortho_Control_Points *cp) |
| 96 |
{ |
| 97 |
FILE *fd; |
| 98 |
char msg[100]; |
| 99 |
int stat; |
| 100 |
|
| 101 |
fd = I_fopen_group_file_old(group, POINT_FILE); |
| 102 |
if (fd == NULL) { |
| 103 |
snprintf(msg, sizeof(msg), |
| 104 |
"unable to open control point (Z) file for group [%s in %s]", |
| 105 |
group, G_mapset()); |
| 106 |
G_warning("%s", msg); |
| 107 |
G_sleep(4); |
| 108 |
return 0; |
| 109 |
} |
| 110 |
|
| 111 |
stat = I_read_con_points(fd, cp); |
| 112 |
fclose(fd); |
| 113 |
if (stat < 0) { |
| 114 |
snprintf(msg, sizeof(msg), |
| 115 |
"bad format in control point file for group [%s in %s]", group, |
| 116 |
G_mapset()); |
| 117 |
G_warning("%s", msg); |
| 118 |
G_sleep(4); |
| 119 |
return 0; |
| 120 |
} |
| 121 |
return 1; |
| 122 |
} |
| 123 |
|
| 124 |
int I_put_con_points(char *group, struct Ortho_Control_Points *cp) |
| 125 |
{ |
| 126 |
FILE *fd; |
| 127 |
char msg[100]; |
| 128 |
|
| 129 |
fd = I_fopen_group_file_new(group, POINT_FILE); |
| 130 |
if (fd == NULL) { |
| 131 |
snprintf(msg, sizeof(msg), |
| 132 |
"unable to create control point file for group [%s in %s]", |
| 133 |
group, G_mapset()); |
| 134 |
G_warning("%s", msg); |
| 135 |
G_sleep(4); |
| 136 |
return 0; |
| 137 |
} |
| 138 |
|
| 139 |
I_write_con_points(fd, cp); |
| 140 |
fclose(fd); |
| 141 |
return 1; |
| 142 |
} |
| 143 |
|
| 144 |
int I_convert_con_points(char *group, struct Ortho_Control_Points *con_cp, |
| 145 |
struct Ortho_Control_Points *photo_cp, double E12[3], |
| 146 |
double N12[3]) |
| 147 |
{ |
| 148 |
FILE *fd; |
| 149 |
char msg[100]; |
| 150 |
int i, stat, status; |
| 151 |
double e1, e2, n1, n2, z1, z2, e0, n0; |
| 152 |
|
| 153 |
fd = I_fopen_group_file_old(group, POINT_FILE); |
| 154 |
if (fd == NULL) { |
| 155 |
snprintf(msg, sizeof(msg), |
| 156 |
"unable to open control point (Z) file for group [%s in %s]", |
| 157 |
group, G_mapset()); |
| 158 |
G_warning("%s", msg); |
| 159 |
G_sleep(4); |
| 160 |
return 0; |
| 161 |
} |
| 162 |
|
| 163 |
stat = I_read_con_points(fd, con_cp); |
| 164 |
fclose(fd); |
| 165 |
if (stat < 0) { |
| 166 |
snprintf(msg, sizeof(msg), |
| 167 |
"bad format in control point file for group [%s in %s]", group, |
| 168 |
G_mapset()); |
| 169 |
G_warning("%s", msg); |
| 170 |
G_sleep(4); |
| 171 |
return 0; |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
photo_cp->count = 0; |
| 176 |
for (i = 0; i < con_cp->count; i++) { |
| 177 |
status = con_cp->status[i]; |
| 178 |
e1 = con_cp->e1[i]; |
| 179 |
n1 = con_cp->n1[i]; |
| 180 |
z1 = con_cp->z1[i]; |
| 181 |
e2 = con_cp->e2[i]; |
| 182 |
n2 = con_cp->n2[i]; |
| 183 |
z2 = con_cp->z2[i]; |
| 184 |
|
| 185 |
I_georef(e1, n1, &e0, &n0, E12, N12, 1); |
| 186 |
|
| 187 |
I_new_con_point(photo_cp, e0, n0, z1, e2, n2, z2, status); |
| 188 |
} |
| 189 |
|
| 190 |
return 1; |
| 191 |
} |