GRASS GIS 8 Programmer's Manual  8.2.2dev(2023)-3d2c704037
points.c
Go to the documentation of this file.
1 #include <grass/imagery.h>
2 #include <grass/glocale.h>
3 
4 #define POINT_FILE "POINTS"
5 
6 static int I_read_control_points(FILE * fd, struct Control_Points *cp)
7 {
8  char buf[100];
9  double e1, e2, n1, n2;
10  int status;
11 
12  cp->count = 0;
13 
14  /* read the control point lines. format is:
15  image_east image_north target_east target_north status
16  */
17  cp->e1 = NULL;
18  cp->e2 = NULL;
19  cp->n1 = NULL;
20  cp->n2 = NULL;
21  cp->status = NULL;
22 
23  while (G_getl2(buf, sizeof buf, fd)) {
24  G_strip(buf);
25  if (*buf == '#' || *buf == 0)
26  continue;
27  if (sscanf(buf, "%lf%lf%lf%lf%d", &e1, &n1, &e2, &n2, &status) == 5)
28  I_new_control_point(cp, e1, n1, e2, n2, status);
29  else
30  return -4;
31  }
32 
33  return 1;
34 }
35 
36 
37 /*!
38  * \brief add new control point
39  *
40  * Once the
41  * control points have been read into the <b>cp</b> structure, this routine
42  * adds new points to it. The new control point is given by <b>e1</b> (column)
43  * and <b>n1</b> (row) on the image, and the <b>e2</b> (east) and <b>n2</b>
44  * (north) for the target database. The value of <b>status</b> should be 1 if
45  * the point is a valid point; 0 otherwise.\remarks{Use of this routine implies
46  * that the point is probably good, so <b>status</b> should be set to 1.}
47  *
48  * \param cp
49  * \param e1
50  * \param n1
51  * \param e2
52  * \param n2
53  * \param status
54  * \return int
55  */
56 
58  double e1, double n1, double e2, double n2,
59  int status)
60 {
61  int i;
62  unsigned int size;
63 
64  if (status < 0)
65  return 1;
66  i = (cp->count)++;
67  size = cp->count * sizeof(double);
68  cp->e1 = (double *)G_realloc(cp->e1, size);
69  cp->e2 = (double *)G_realloc(cp->e2, size);
70  cp->n1 = (double *)G_realloc(cp->n1, size);
71  cp->n2 = (double *)G_realloc(cp->n2, size);
72  size = cp->count * sizeof(int);
73  cp->status = (int *)G_realloc(cp->status, size);
74 
75  cp->e1[i] = e1;
76  cp->e2[i] = e2;
77  cp->n1[i] = n1;
78  cp->n2[i] = n2;
79  cp->status[i] = status;
80 
81  return 0;
82 }
83 
84 static int I_write_control_points(FILE * fd, const struct Control_Points *cp)
85 {
86  int i;
87 
88  fprintf(fd, "# %7s %15s %15s %15s %9s status\n", "", "image", "",
89  "target", "");
90  fprintf(fd, "# %15s %15s %15s %15s (1=ok)\n", "east", "north", "east",
91  "north");
92  fprintf(fd, "#\n");
93  for (i = 0; i < cp->count; i++)
94  if (cp->status[i] >= 0)
95  fprintf(fd, " %15f %15f %15f %15f %4d\n",
96  cp->e1[i], cp->n1[i], cp->e2[i], cp->n2[i],
97  cp->status[i]);
98 
99  return 0;
100 }
101 
102 
103 /*!
104  * \brief read group control points
105  *
106  * Reads the control points from the POINTS file
107  * for the <b>group</b> into the <b>cp</b> structure. Returns 1 if
108  * successful; 0 otherwise (and prints a diagnostic error).
109  * <b>Note.</b> An error message is printed if the POINTS file is invalid, or
110  * does not exist.
111  *
112  * \param group
113  * \param cp
114  * \return int
115  */
116 
117 int I_get_control_points(const char *group, struct Control_Points *cp)
118 {
119  FILE *fd;
120  int stat;
121 
122  fd = I_fopen_group_file_old(group, POINT_FILE);
123  if (fd == NULL) {
124  G_warning(_("Unable to open control point file for group [%s in %s]"),
125  group, G_mapset());
126  return 0;
127  }
128 
129  stat = I_read_control_points(fd, cp);
130  fclose(fd);
131  if (stat < 0) {
132  G_warning(_("Bad format in control point file for group [%s in %s]"),
133  group, G_mapset());
134  return 0;
135  }
136  return 1;
137 }
138 
139 
140 /*!
141  * \brief write group control points
142  *
143  * Writes the control points from the
144  * <b>cp</b> structure to the POINTS file for the specified group.
145  * <b>Note.</b> Points in <b>cp</b> with a negative <i>status</i> are not
146  * written to the POINTS file.
147  *
148  * \param group
149  * \param cp
150  * \return int
151  */
152 
153 int I_put_control_points(const char *group, const struct Control_Points *cp)
154 {
155  FILE *fd;
156 
157  fd = I_fopen_group_file_new(group, POINT_FILE);
158  if (fd == NULL) {
159  G_warning(_("Unable to create control point file for group [%s in %s]"),
160  group, G_mapset());
161  return 0;
162  }
163 
164  I_write_control_points(fd, cp);
165  fclose(fd);
166  return 1;
167 }
int G_getl2(char *, int, FILE *)
Gets a line of text from a file of any pedigree.
Definition: getl.c:64
#define POINT_FILE
Definition: points.c:4
int I_new_control_point(struct Control_Points *cp, double e1, double n1, double e2, double n2, int status)
add new control point
Definition: points.c:57
FILE * I_fopen_group_file_old(const char *, const char *)
Open group file for reading.
Definition: fopen.c:105
void G_strip(char *)
Removes all leading and trailing white space from string.
Definition: strings.c:300
#define NULL
Definition: ccmath.h:32
double * n2
Definition: imagery.h:47
int I_put_control_points(const char *group, const struct Control_Points *cp)
write group control points
Definition: points.c:153
double * n1
Definition: imagery.h:44
int I_get_control_points(const char *group, struct Control_Points *cp)
read group control points
Definition: points.c:117
int * status
Definition: imagery.h:49
double * e2
Definition: imagery.h:46
FILE * I_fopen_group_file_new(const char *, const char *)
Definition: fopen.c:70
const char * G_mapset(void)
Get current mapset name.
Definition: gis/mapset.c:33
void G_warning(const char *,...) __attribute__((format(printf
#define G_realloc(p, n)
Definition: defs/gis.h:114
#define _(str)
Definition: glocale.h:10
double * e1
Definition: imagery.h:43