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