GRASS Programmer's Manual
6.4.2(2012)
|
00001 #include <grass/imagery.h> 00002 #include <grass/glocale.h> 00003 00004 #define POINT_FILE "POINTS" 00005 00006 static int I_read_control_points(FILE * fd, struct Control_Points *cp) 00007 { 00008 char buf[100]; 00009 double e1, e2, n1, n2; 00010 int status; 00011 00012 cp->count = 0; 00013 00014 /* read the control point lines. format is: 00015 image_east image_north target_east target_north status 00016 */ 00017 cp->e1 = NULL; 00018 cp->e2 = NULL; 00019 cp->n1 = NULL; 00020 cp->n2 = NULL; 00021 cp->status = NULL; 00022 00023 while (G_getl2(buf, sizeof buf, fd)) { 00024 G_strip(buf); 00025 if (*buf == '#' || *buf == 0) 00026 continue; 00027 if (sscanf(buf, "%lf%lf%lf%lf%d", &e1, &n1, &e2, &n2, &status) == 5) 00028 I_new_control_point(cp, e1, n1, e2, n2, status); 00029 else 00030 return -4; 00031 } 00032 00033 return 1; 00034 } 00035 00036 00057 int I_new_control_point(struct Control_Points *cp, 00058 double e1, double n1, double e2, double n2, 00059 int status) 00060 { 00061 int i; 00062 unsigned int size; 00063 00064 if (status < 0) 00065 return 1; 00066 i = (cp->count)++; 00067 size = cp->count * sizeof(double); 00068 cp->e1 = (double *)G_realloc(cp->e1, size); 00069 cp->e2 = (double *)G_realloc(cp->e2, size); 00070 cp->n1 = (double *)G_realloc(cp->n1, size); 00071 cp->n2 = (double *)G_realloc(cp->n2, size); 00072 size = cp->count * sizeof(int); 00073 cp->status = (int *)G_realloc(cp->status, size); 00074 00075 cp->e1[i] = e1; 00076 cp->e2[i] = e2; 00077 cp->n1[i] = n1; 00078 cp->n2[i] = n2; 00079 cp->status[i] = status; 00080 00081 return 0; 00082 } 00083 00084 static int I_write_control_points(FILE * fd, const struct Control_Points *cp) 00085 { 00086 int i; 00087 00088 fprintf(fd, "# %7s %15s %15s %15s %9s status\n", "", "image", "", 00089 "target", ""); 00090 fprintf(fd, "# %15s %15s %15s %15s (1=ok)\n", "east", "north", "east", 00091 "north"); 00092 fprintf(fd, "#\n"); 00093 for (i = 0; i < cp->count; i++) 00094 if (cp->status[i] >= 0) 00095 fprintf(fd, " %15f %15f %15f %15f %4d\n", 00096 cp->e1[i], cp->n1[i], cp->e2[i], cp->n2[i], 00097 cp->status[i]); 00098 00099 return 0; 00100 } 00101 00102 00117 int I_get_control_points(const char *group, struct Control_Points *cp) 00118 { 00119 FILE *fd; 00120 int stat; 00121 00122 fd = I_fopen_group_file_old(group, POINT_FILE); 00123 if (fd == NULL) { 00124 G_warning(_("Unable to open control point file for group [%s in %s]"), 00125 group, G_mapset()); 00126 return 0; 00127 } 00128 00129 stat = I_read_control_points(fd, cp); 00130 fclose(fd); 00131 if (stat < 0) { 00132 G_warning(_("Bad format in control point file for group [%s in %s]"), 00133 group, G_mapset()); 00134 return 0; 00135 } 00136 return 1; 00137 } 00138 00139 00153 int I_put_control_points(const char *group, const struct Control_Points *cp) 00154 { 00155 FILE *fd; 00156 00157 fd = I_fopen_group_file_new(group, POINT_FILE); 00158 if (fd == NULL) { 00159 G_warning(_("Unable to create control point file for group [%s in %s]"), 00160 group, G_mapset()); 00161 return 0; 00162 } 00163 00164 I_write_control_points(fd, cp); 00165 fclose(fd); 00166 return 1; 00167 }