raster.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. #include <grass/config.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <grass/lidar.h>
  6. /*------------------------------------------------------------------------------------------------*/
  7. void
  8. P_Sparse_Points(struct Map_info *Out, struct Cell_head *Elaboration,
  9. struct bound_box General, struct bound_box Overlap, double **obs,
  10. double *param, int *line_num, double pe, double pn,
  11. double overlap, int nsplx, int nsply, int num_points,
  12. int bilin, struct line_cats *categories, dbDriver * driver,
  13. double mean, char *tab_name)
  14. {
  15. int i;
  16. char buf[1024];
  17. dbString sql;
  18. double interpolation, csi, eta, weight;
  19. struct line_pnts *point;
  20. point = Vect_new_line_struct();
  21. db_begin_transaction(driver);
  22. for (i = 0; i < num_points; i++) {
  23. if (Vect_point_in_box(obs[i][0], obs[i][1], mean, &General)) { /*Here mean is just for asking if obs point is in box */
  24. if (bilin)
  25. interpolation =
  26. dataInterpolateBilin(obs[i][0], obs[i][1], pe, pn, nsplx,
  27. nsply, Elaboration->west,
  28. Elaboration->south, param);
  29. else
  30. interpolation =
  31. dataInterpolateBicubic(obs[i][0], obs[i][1], pe, pn,
  32. nsplx, nsply, Elaboration->west,
  33. Elaboration->south, param);
  34. interpolation += mean;
  35. Vect_copy_xyz_to_pnts(point, &obs[i][0], &obs[i][1],
  36. &interpolation, 1);
  37. if (Vect_point_in_box(obs[i][0], obs[i][1], interpolation, &Overlap)) { /*(5) */
  38. Vect_write_line(Out, GV_POINT, point, categories);
  39. }
  40. else {
  41. db_init_string(&sql);
  42. sprintf(buf, "INSERT INTO %s (ID, X, Y, Interp)", tab_name);
  43. db_append_string(&sql, buf);
  44. sprintf(buf, " VALUES (");
  45. db_append_string(&sql, buf);
  46. sprintf(buf, "%d, %f, %f, ", line_num[i], obs[i][0],
  47. obs[i][1]);
  48. db_append_string(&sql, buf);
  49. if ((*point->x > Overlap.E) && (*point->x < General.E)) {
  50. if ((*point->y > Overlap.N) && (*point->y < General.N)) { /*(3) */
  51. csi = (General.E - *point->x) / overlap;
  52. eta = (General.N - *point->y) / overlap;
  53. weight = csi * eta;
  54. *point->z = weight * interpolation;
  55. sprintf(buf, "%lf", *point->z);
  56. db_append_string(&sql, buf);
  57. sprintf(buf, ")");
  58. db_append_string(&sql, buf);
  59. if (db_execute_immediate(driver, &sql) != DB_OK)
  60. G_fatal_error(_("Unable to access table <%s>"),
  61. tab_name);
  62. }
  63. else if ((*point->y < Overlap.S) && (*point->y > General.S)) { /*(1) */
  64. csi = (General.E - *point->x) / overlap;
  65. eta = (*point->y - General.S) / overlap;
  66. weight = csi * eta;
  67. *point->z = weight * interpolation;
  68. sprintf(buf, "%lf", *point->z);
  69. db_append_string(&sql, buf);
  70. sprintf(buf, ")");
  71. db_append_string(&sql, buf);
  72. if (db_execute_immediate(driver, &sql) != DB_OK)
  73. G_fatal_error(_("Unable to access table <%s>"),
  74. tab_name);
  75. }
  76. else if ((*point->y <= Overlap.N) && (*point->y >= Overlap.S)) { /*(1) */
  77. weight = (General.E - *point->x) / overlap;
  78. *point->z = weight * interpolation;
  79. sprintf(buf, "%lf", *point->z);
  80. db_append_string(&sql, buf);
  81. sprintf(buf, ")");
  82. db_append_string(&sql, buf);
  83. if (db_execute_immediate(driver, &sql) != DB_OK)
  84. G_fatal_error(_("Unable to access table <%s>"),
  85. tab_name);
  86. }
  87. }
  88. else if ((*point->x < Overlap.W) && (*point->x > General.W)) {
  89. if ((*point->y > Overlap.N) && (*point->y < General.N)) { /*(4) */
  90. csi = (*point->x - General.W) / overlap;
  91. eta = (General.N - *point->y) / overlap;
  92. weight = eta * csi;
  93. *point->z = weight * interpolation;
  94. sprintf(buf, "%lf", *point->z);
  95. db_append_string(&sql, buf);
  96. sprintf(buf, ")");
  97. db_append_string(&sql, buf);
  98. if (db_execute_immediate(driver, &sql) != DB_OK)
  99. G_fatal_error(_("Unable to access table <%s>"),
  100. tab_name);
  101. }
  102. else if ((*point->y < Overlap.S) && (*point->y > General.S)) { /*(2) */
  103. csi = (*point->x - General.W) / overlap;
  104. eta = (*point->y - General.S) / overlap;
  105. weight = csi * eta;
  106. *point->z = weight * interpolation;
  107. sprintf(buf, "%lf", *point->z);
  108. db_append_string(&sql, buf);
  109. sprintf(buf, ")");
  110. db_append_string(&sql, buf);
  111. if (db_execute_immediate(driver, &sql) != DB_OK)
  112. G_fatal_error(_("Unable to access table <%s>"),
  113. tab_name);
  114. }
  115. else if ((*point->y >= Overlap.S) && (*point->y <= Overlap.N)) { /*(2) */
  116. weight = (*point->x - General.W) / overlap;
  117. *point->z = weight * interpolation;
  118. sprintf(buf, "%lf", *point->z);
  119. db_append_string(&sql, buf);
  120. sprintf(buf, ")");
  121. db_append_string(&sql, buf);
  122. if (db_execute_immediate(driver, &sql) != DB_OK)
  123. G_fatal_error(_("Unable to access table <%s>"),
  124. tab_name);
  125. }
  126. }
  127. else if ((*point->x >= Overlap.W) && (*point->x <= Overlap.E)){
  128. if ((*point->y > Overlap.N) && (*point->y < General.N)) { /*(3) */
  129. weight = (General.N - *point->y) / overlap;
  130. *point->z = weight * interpolation;
  131. sprintf(buf, "%lf", *point->z);
  132. db_append_string(&sql, buf);
  133. sprintf(buf, ")");
  134. db_append_string(&sql, buf);
  135. if (db_execute_immediate(driver, &sql) != DB_OK)
  136. G_fatal_error(_("Unable to access table <%s>"),
  137. tab_name);
  138. }
  139. else if ((*point->y < Overlap.S) && (*point->y > General.S)) { /*(1) */
  140. weight = (*point->y - General.S) / overlap;
  141. *point->z = (1 - weight) * interpolation;
  142. sprintf(buf, "%lf", *point->z);
  143. db_append_string(&sql, buf);
  144. sprintf(buf, ")");
  145. db_append_string(&sql, buf);
  146. if (db_execute_immediate(driver, &sql) != DB_OK)
  147. G_fatal_error(_("Unable to access table <%s>"),
  148. tab_name);
  149. }
  150. }
  151. }
  152. } /*IF*/
  153. } /*FOR*/
  154. db_commit_transaction(driver);
  155. return;
  156. }
  157. /*------------------------------------------------------------------------------------------------*/
  158. int P_Regular_Points(struct Cell_head *Elaboration, struct Cell_head *Original,
  159. struct bound_box General, struct bound_box Overlap,
  160. SEGMENT *out_seg, double *param,
  161. double passoN, double passoE, double overlap,
  162. double mean, int nsplx, int nsply,
  163. int nrows, int ncols, int bilin)
  164. {
  165. int col, row, startcol, endcol, startrow, endrow;
  166. double X, Y, interpolation, weight, csi, eta, dval;
  167. /* G_get_window(&Original); */
  168. if (Original->north > General.N)
  169. startrow = (Original->north - General.N) / Original->ns_res - 1;
  170. else
  171. startrow = 0;
  172. if (Original->north > General.S) {
  173. endrow = (Original->north - General.S) / Original->ns_res + 1;
  174. if (endrow > nrows)
  175. endrow = nrows;
  176. }
  177. else
  178. endrow = nrows;
  179. if (General.W > Original->west)
  180. startcol = (General.W - Original->west) / Original->ew_res - 1;
  181. else
  182. startcol = 0;
  183. if (General.E > Original->west) {
  184. endcol = (General.E - Original->west) / Original->ew_res + 1;
  185. if (endcol > ncols)
  186. endcol = ncols;
  187. }
  188. else
  189. endcol = ncols;
  190. for (row = startrow; row < endrow; row++) {
  191. for (col = startcol; col < endcol; col++) {
  192. X = Rast_col_to_easting((double)(col) + 0.5, Original);
  193. Y = Rast_row_to_northing((double)(row) + 0.5, Original);
  194. if (Vect_point_in_box(X, Y, mean, &General)) { /* Here, mean is just for asking if obs point is in box */
  195. if (bilin)
  196. interpolation =
  197. dataInterpolateBilin(X, Y, passoE, passoN, nsplx,
  198. nsply, Elaboration->west,
  199. Elaboration->south, param);
  200. else
  201. interpolation =
  202. dataInterpolateBicubic(X, Y, passoE, passoN, nsplx,
  203. nsply, Elaboration->west,
  204. Elaboration->south, param);
  205. interpolation += mean;
  206. if (Vect_point_in_box(X, Y, interpolation, &Overlap)) { /* (5) */
  207. dval = interpolation;
  208. }
  209. else {
  210. Segment_get(out_seg, &dval, row, col);
  211. if ((X > Overlap.E) && (X < General.E)) {
  212. if ((Y > Overlap.N) && (Y < General.N)) { /* (3) */
  213. csi = (General.E - X) / overlap;
  214. eta = (General.N - Y) / overlap;
  215. weight = csi * eta;
  216. interpolation *= weight;
  217. dval += interpolation;
  218. }
  219. else if ((Y < Overlap.S) && (Y > General.S)) { /* (1) */
  220. csi = (General.E - X) / overlap;
  221. eta = (Y - General.S) / overlap;
  222. weight = csi * eta;
  223. interpolation *= weight;
  224. dval = interpolation;
  225. }
  226. else if ((Y >= Overlap.S) && (Y <= Overlap.N)) { /* (1) */
  227. weight = (General.E - X ) / overlap;
  228. interpolation *= weight;
  229. dval = interpolation;
  230. }
  231. }
  232. else if ((X < Overlap.W) && (X > General.W)) {
  233. if ((Y > Overlap.N) && (Y < General.N)) { /* (4) */
  234. csi = (X - General.W) / overlap;
  235. eta = (General.N - Y) / overlap;
  236. weight = eta * csi;
  237. interpolation *= weight;
  238. dval += interpolation;
  239. }
  240. else if ((Y < Overlap.S) && (Y > General.S)) { /* (2) */
  241. csi = (X - General.W) / overlap;
  242. eta = (Y - General.S) / overlap;
  243. weight = csi * eta;
  244. interpolation *= weight;
  245. dval += interpolation;
  246. }
  247. else if ((Y >= Overlap.S) && (Y <= Overlap.N)) { /* (2) */
  248. weight = (X - General.W) / overlap;
  249. interpolation *= weight;
  250. dval += interpolation;
  251. }
  252. }
  253. else if ((X >= Overlap.W) && (X <= Overlap.E)) {
  254. if ((Y > Overlap.N) && (Y < General.N)) { /* (3) */
  255. weight = (General.N - Y) / overlap;
  256. interpolation *= weight;
  257. dval += interpolation;
  258. }
  259. else if ((Y < Overlap.S) && (Y > General.S)) { /* (1) */
  260. weight = (Y - General.S) / overlap;
  261. interpolation *= weight;
  262. dval = interpolation;
  263. }
  264. }
  265. }
  266. Segment_put(out_seg, &dval, row, col);
  267. }
  268. } /* END COL */
  269. } /* END ROW */
  270. return 1;
  271. }