| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#include "orthophoto.h" |
| 6 |
#include <grass/imagery.h> |
| 7 |
|
| 8 |
#define REF_POINT_FILE "REF_POINTS" |
| 9 |
|
| 10 |
int I_read_ref_points(FILE *fd, struct Ortho_Photo_Points *cp) |
| 11 |
{ |
| 12 |
char buf[100]; |
| 13 |
double e1, e2, n1, n2; |
| 14 |
int status; |
| 15 |
|
| 16 |
cp->count = 0; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
cp->e1 = NULL; |
| 22 |
cp->e2 = NULL; |
| 23 |
cp->n1 = NULL; |
| 24 |
cp->n2 = NULL; |
| 25 |
cp->status = NULL; |
| 26 |
|
| 27 |
|
| 28 |
while (G_getl(buf, sizeof(buf), fd)) { |
| 29 |
G_strip(buf); |
| 30 |
if (*buf == '#' || *buf == 0) |
| 31 |
continue; |
| 32 |
if (sscanf(buf, "%lf%lf%lf%lf%d", &e1, &n1, &e2, &n2, &status) == 5) |
| 33 |
I_new_ref_point(cp, e1, n1, e2, n2, status); |
| 34 |
else |
| 35 |
return -4; |
| 36 |
} |
| 37 |
|
| 38 |
return 1; |
| 39 |
} |
| 40 |
|
| 41 |
int I_new_ref_point(struct Ortho_Photo_Points *cp, double e1, double n1, |
| 42 |
double e2, double n2, int status) |
| 43 |
{ |
| 44 |
int i; |
| 45 |
size_t size; |
| 46 |
|
| 47 |
|
| 48 |
if (status < 0) |
| 49 |
return 0; |
| 50 |
i = (cp->count)++; |
| 51 |
size = cp->count * sizeof(double); |
| 52 |
cp->e1 = (double *)G_realloc(cp->e1, size); |
| 53 |
cp->e2 = (double *)G_realloc(cp->e2, size); |
| 54 |
cp->n1 = (double *)G_realloc(cp->n1, size); |
| 55 |
cp->n2 = (double *)G_realloc(cp->n2, size); |
| 56 |
size = cp->count * sizeof(int); |
| 57 |
cp->status = (int *)G_realloc(cp->status, size); |
| 58 |
|
| 59 |
cp->e1[i] = e1; |
| 60 |
cp->e2[i] = e2; |
| 61 |
cp->n1[i] = n1; |
| 62 |
cp->n2[i] = n2; |
| 63 |
cp->status[i] = status; |
| 64 |
|
| 65 |
return 0; |
| 66 |
} |
| 67 |
|
| 68 |
int I_write_ref_points(FILE *fd, struct Ortho_Photo_Points *cp) |
| 69 |
{ |
| 70 |
int i; |
| 71 |
|
| 72 |
fprintf(fd, "# %7s %15s %15s %15s %9s status\n", "", "image", "", "photo", |
| 73 |
""); |
| 74 |
fprintf(fd, "# %15s %15s %15s %15s (1=ok)\n", "east", "north", "x", "y"); |
| 75 |
fprintf(fd, "#\n"); |
| 76 |
for (i = 0; i < cp->count; i++) |
| 77 |
if (cp->status[i] >= 0) |
| 78 |
fprintf(fd, " %15f %15f %15f %15f %d\n", cp->e1[i], cp->n1[i], |
| 79 |
cp->e2[i], cp->n2[i], cp->status[i]); |
| 80 |
|
| 81 |
return 0; |
| 82 |
} |
| 83 |
|
| 84 |
int I_get_ref_points(char *groupname, struct Ortho_Photo_Points *cp) |
| 85 |
{ |
| 86 |
FILE *fd; |
| 87 |
char msg[100]; |
| 88 |
int stat; |
| 89 |
|
| 90 |
|
| 91 |
fd = I_fopen_group_file_old(groupname, REF_POINT_FILE); |
| 92 |
if (fd == NULL) { |
| 93 |
snprintf(msg, sizeof(msg), |
| 94 |
"unable to open reference point file for group [%s in %s]", |
| 95 |
groupname, G_mapset()); |
| 96 |
G_warning("%s", msg); |
| 97 |
return 0; |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
stat = I_read_ref_points(fd, cp); |
| 102 |
fclose(fd); |
| 103 |
if (stat < 0) { |
| 104 |
snprintf(msg, sizeof(msg), |
| 105 |
"bad format in reference point file for group [%s in %s]", |
| 106 |
groupname, G_mapset()); |
| 107 |
G_warning("%s", msg); |
| 108 |
return 0; |
| 109 |
} |
| 110 |
return 1; |
| 111 |
} |
| 112 |
|
| 113 |
int I_put_ref_points(char *groupname, struct Ortho_Photo_Points *cp) |
| 114 |
{ |
| 115 |
FILE *fd; |
| 116 |
char msg[100]; |
| 117 |
|
| 118 |
fd = I_fopen_group_file_new(groupname, REF_POINT_FILE); |
| 119 |
if (fd == NULL) { |
| 120 |
snprintf(msg, sizeof(msg), |
| 121 |
"unable to create reference point file for group [%s in %s]", |
| 122 |
groupname, G_mapset()); |
| 123 |
G_warning("%s", msg); |
| 124 |
return 0; |
| 125 |
} |
| 126 |
|
| 127 |
I_write_ref_points(fd, cp); |
| 128 |
fclose(fd); |
| 129 |
return 1; |
| 130 |
} |