GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-b4e4cb0fe9
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 
56 int I_new_control_point(struct Control_Points *cp, double e1, double n1,
57  double e2, double n2, int status)
58 {
59  int i;
60  unsigned int size;
61 
62  if (status < 0)
63  return 1;
64  i = (cp->count)++;
65  size = cp->count * sizeof(double);
66  cp->e1 = (double *)G_realloc(cp->e1, size);
67  cp->e2 = (double *)G_realloc(cp->e2, size);
68  cp->n1 = (double *)G_realloc(cp->n1, size);
69  cp->n2 = (double *)G_realloc(cp->n2, size);
70  size = cp->count * sizeof(int);
71  cp->status = (int *)G_realloc(cp->status, size);
72 
73  cp->e1[i] = e1;
74  cp->e2[i] = e2;
75  cp->n1[i] = n1;
76  cp->n2[i] = n2;
77  cp->status[i] = status;
78 
79  return 0;
80 }
81 
82 static int I_write_control_points(FILE *fd, const struct Control_Points *cp)
83 {
84  int i;
85 
86  fprintf(fd, "# %7s %15s %15s %15s %9s status\n", "", "image", "", "target",
87  "");
88  fprintf(fd, "# %15s %15s %15s %15s (1=ok)\n", "east", "north", "east",
89  "north");
90  fprintf(fd, "#\n");
91  for (i = 0; i < cp->count; i++)
92  if (cp->status[i] >= 0)
93  fprintf(fd, " %15f %15f %15f %15f %4d\n", cp->e1[i], cp->n1[i],
94  cp->e2[i], cp->n2[i], cp->status[i]);
95 
96  return 0;
97 }
98 
99 /*!
100  * \brief read group control points
101  *
102  * Reads the control points from the POINTS file
103  * for the <b>group</b> into the <b>cp</b> structure. Returns 1 if
104  * successful; 0 otherwise (and prints a diagnostic error).
105  * <b>Note.</b> An error message is printed if the POINTS file is invalid, or
106  * does not exist.
107  *
108  * \param group
109  * \param cp
110  * \return int
111  */
112 
113 int I_get_control_points(const char *group, struct Control_Points *cp)
114 {
115  FILE *fd;
116  int stat;
117 
118  fd = I_fopen_group_file_old(group, POINT_FILE);
119  if (fd == NULL) {
120  G_warning(_("Unable to open control point file for group [%s in %s]"),
121  group, G_mapset());
122  return 0;
123  }
124 
125  stat = I_read_control_points(fd, cp);
126  fclose(fd);
127  if (stat < 0) {
128  G_warning(_("Bad format in control point file for group [%s in %s]"),
129  group, G_mapset());
130  return 0;
131  }
132  return 1;
133 }
134 
135 /*!
136  * \brief write group control points
137  *
138  * Writes the control points from the
139  * <b>cp</b> structure to the POINTS file for the specified group.
140  * <b>Note.</b> Points in <b>cp</b> with a negative <i>status</i> are not
141  * written to the POINTS file.
142  *
143  * \param group
144  * \param cp
145  * \return int
146  */
147 
148 int I_put_control_points(const char *group, const struct Control_Points *cp)
149 {
150  FILE *fd;
151 
152  fd = I_fopen_group_file_new(group, POINT_FILE);
153  if (fd == NULL) {
154  G_warning(_("Unable to create control point file for group [%s in %s]"),
155  group, G_mapset());
156  return 0;
157  }
158 
159  I_write_control_points(fd, cp);
160  fclose(fd);
161  return 1;
162 }
#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:113
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:56
int I_put_control_points(const char *group, const struct Control_Points *cp)
write group control points
Definition: points.c:148
#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