GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
point2d.c
Go to the documentation of this file.
1 
2 /*-
3  *
4  * Original program and various modifications:
5  * Lubos Mitas
6  *
7  * GRASS4.1 version of the program and GRASS4.2 modifications:
8  * H. Mitasova
9  * I. Kosinovsky, D. Gerdes
10  * D. McCauley
11  *
12  * Copyright 1993, 1995:
13  * L. Mitas ,
14  * H. Mitasova ,
15  * I. Kosinovsky, ,
16  * D.Gerdes
17  * D. McCauley
18  *
19  * modified by McCauley in August 1995
20  * modified by Mitasova in August 1995, Nov. 1996
21  *
22  */
23 
24 
25 #include <stdio.h>
26 #include <math.h>
27 #include <unistd.h>
28 #include <grass/gis.h>
29 #include <grass/site.h>
30 #include <grass/Vect.h>
31 #include <grass/dbmi.h>
32 
33 #define POINT2D_C
34 #include <grass/interpf.h>
35 
36 /* needed for AIX */
37 #ifdef hz
38 #undef hz
39 #endif
40 
41 int IL_check_at_points_2d(struct interp_params *params, struct quaddata *data, /* current region */
42  double *b, /* solution of linear equations */
43  double *ertot, /* total error */
44  double zmin, /* min z-value */
45  double dnorm, struct triple skip_point)
46 
47 /*
48  * Checks if interpolating function interp() evaluates correct z-values at
49  * given points. If smoothing is used calculate the maximum error caused
50  * by smoothing.
51  */
52 {
53  int n_points = data->n_points; /* number of points */
54  struct triple *points = data->points; /* points for interpolation */
55  double east = data->xmax;
56  double west = data->x_orig;
57  double north = data->ymax;
58  double south = data->y_orig;
59  double rfsta2, errmax, h, xx, yy, r2, hz, zz, err, xmm, ymm, r;
60  double skip_err;
61  int n1, mm, m, cat;
62  double fstar2;
63  int inside;
64 
65  /* Site *site; */
66  char buf[1024];
67 
68 
69  /* if ((site = G_site_new_struct (-1, 2, 0, 1)) == NULL)
70  G_fatal_error ("Memory error for site struct"); */
71 
72  fstar2 = params->fi * params->fi / 4.;
73  errmax = .0;
74  n1 = n_points + 1;
75  for (mm = 1; mm <= n_points; mm++) {
76  h = b[0];
77  for (m = 1; m <= n_points; m++) {
78  xx = points[mm - 1].x - points[m - 1].x;
79  yy = points[mm - 1].y - points[m - 1].y;
80  r2 = yy * yy + xx * xx;
81  if (r2 != 0.) {
82  rfsta2 = fstar2 * r2;
83  r = r2;
84  h = h + b[m] * params->interp(r, params->fi);
85  }
86  }
87  /* modified by helena january 1997 - normalization of z was
88  removed from segm2d.c and interp2d.c
89  hz = (h * dnorm) + zmin;
90  zz = (points[mm - 1].z * dnorm) + zmin;
91  */
92  hz = h + zmin;
93  zz = points[mm - 1].z + zmin;
94  err = hz - zz;
95  xmm = points[mm - 1].x * dnorm + params->x_orig + west;
96  ymm = points[mm - 1].y * dnorm + params->y_orig + south;
97  if ((xmm >= west + params->x_orig) && (xmm <= east + params->x_orig)
98  && (ymm >= south + params->y_orig) &&
99  (ymm <= north + params->y_orig))
100  inside = 1;
101  else
102  inside = 0;
103 
104  if (params->fddevi != NULL) {
105 
106  if (inside) { /* if the point is inside the region */
109 
110  Vect_append_point(Pnts, xmm, ymm, zz);
111  cat = count;
112  Vect_cat_set(Cats2, 1, cat);
113  Vect_write_line(&Map2, GV_POINT, Pnts, Cats2);
114 
116  sprintf(buf, "insert into %s values ( %d ", ff->table, cat);
117  db_append_string(&sql2, buf);
118 
119  sprintf(buf, ", %f", err);
120  db_append_string(&sql2, buf);
121  db_append_string(&sql2, ")");
122  G_debug(3, "IL_check_at_points_2d: %s", db_get_string(&sql2));
123 
124  if (db_execute_immediate(driver2, &sql2) != DB_OK) {
127  G_fatal_error("Cannot insert new row: %s",
128  db_get_string(&sql2));
129  }
130  count++;
131 
132  }
133  }
134  (*ertot) += err * err;
135  }
136 
137  /* cv stuff */
138  if (params->cv) {
139  h = b[0];
140  for (m = 1; m <= n_points - 1; m++) {
141  xx = points[m - 1].x - skip_point.x;
142  yy = points[m - 1].y - skip_point.y;
143  r2 = yy * yy + xx * xx;
144  if (r2 != 0.) {
145  rfsta2 = fstar2 * r2;
146  r = r2;
147  h = h + b[m] * params->interp(r, params->fi);
148  }
149  }
150  hz = h + zmin;
151  zz = skip_point.z + zmin;
152  skip_err = hz - zz;
153  xmm = skip_point.x * dnorm + params->x_orig + west;
154  ymm = skip_point.y * dnorm + params->y_orig + south;
155 
156  if ((xmm >= west + params->x_orig) && (xmm <= east + params->x_orig)
157  && (ymm >= south + params->y_orig) &&
158  (ymm <= north + params->y_orig))
159  inside = 1;
160  else
161  inside = 0;
162 
163  if (inside) { /* if the point is inside the region */
166 
167  Vect_append_point(Pnts, xmm, ymm, zz);
168  cat = count;
169  Vect_cat_set(Cats2, 1, cat);
170  Vect_write_line(&Map2, GV_POINT, Pnts, Cats2);
171 
173  sprintf(buf, "insert into %s values ( %d ", ff->table, cat);
174  db_append_string(&sql2, buf);
175 
176  sprintf(buf, ", %f", skip_err);
177  db_append_string(&sql2, buf);
178  db_append_string(&sql2, ")");
179  G_debug(3, "IL_check_at_points_2d: %s", db_get_string(&sql2));
180 
181  if (db_execute_immediate(driver2, &sql2) != DB_OK) {
184  G_fatal_error("Cannot insert new row: %s",
185  db_get_string(&sql2));
186  }
187  count++;
188  }
189  } /* cv */
190 
191 
192  return 1;
193 }
void db_zero_string(dbString *x)
Definition: string.c:68
sprintf(buf2,"%s", G3D_CATS_ELEMENT)
float b
Definition: named_colr.c:8
double y_orig
Definition: dataquad.h:33
dbString sql2
int db_shutdown_driver(dbDriver *driver)
Closedown the driver, and free the driver structure.
Definition: shutdown.c:36
int db_close_database(dbDriver *driver)
Close database connection.
Definition: c_closedb.c:26
float r
Definition: named_colr.c:8
double z
Definition: dataquad.h:26
int count
double y_orig
Definition: interpf.h:55
const char * err
Definition: g3dcolor.c:50
int Vect_reset_line(struct line_pnts *Points)
Reset line.
Definition: line.c:148
int Vect_reset_cats(struct line_cats *Cats)
Reset category structure to make sure cats structure is clean to be re-used.
double(* interp)()
Definition: interpf.h:68
double x_orig
Definition: dataquad.h:32
int IL_check_at_points_2d(struct interp_params *, struct quaddata *, double *, double *, double, double, struct triple)
Definition: point2d.c:41
int db_execute_immediate(dbDriver *driver, dbString *SQLstatement)
Execute SQL statements.
Definition: c_execute.c:27
int Vect_append_point(struct line_pnts *Points, double x, double y, double z)
Appends one point to the end of a line.
Definition: line.c:168
struct triple * points
Definition: dataquad.h:39
int db_append_string(dbString *x, const char *s)
Definition: string.c:193
int Vect_cat_set(struct line_cats *Cats, int field, int cat)
Add new field/cat to category structure if doesn&#39;t exist yet.
FILE * fddevi
Definition: interpf.h:62
dbDriver * driver2
tuple data
struct field_info * ff
double x
Definition: dataquad.h:24
double fi
Definition: interpf.h:49
double x_orig
Definition: interpf.h:55
double ymax
Definition: dataquad.h:35
char buf[GNAME_MAX+sizeof(G3D_DIRECTORY)+2]
Definition: g3drange.c:62
return NULL
Definition: dbfopen.c:1394
char * db_get_string(dbString *x)
Definition: string.c:131
int G_debug(int level, const char *msg,...)
Print debugging message.
Definition: gis/debug.c:51
long Vect_write_line(struct Map_info *Map, int type, struct line_pnts *points, struct line_cats *cats)
Writes new feature to the end of file (table)
CELL cat
Definition: g3dcats.c:90
double xmax
Definition: dataquad.h:34
int G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
struct line_pnts * Pnts
double y
Definition: dataquad.h:25
struct Map_info Map2
tuple h
panel.defaultSize = wx.CheckBox(panel, id = wx.ID_ANY, label = _(&quot;Use default size&quot;)) panel...
struct line_cats * Cats2
int n_points
Definition: dataquad.h:38