map3.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  1. #include <stdlib.h>
  2. #include <limits.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <grass/gis.h>
  6. #include <grass/raster.h>
  7. #include <grass/raster.h>
  8. #include <grass/raster3d.h>
  9. #include <grass/btree.h>
  10. #include <grass/glocale.h>
  11. #include <grass/calc.h>
  12. #include "mapcalc.h"
  13. #include "globals.h"
  14. #include "globals3.h"
  15. /****************************************************************************/
  16. RASTER3D_Region current_region3;
  17. void setup_region(void)
  18. {
  19. Rast3d_init_defaults();
  20. Rast3d_get_window(&current_region3);
  21. rows = current_region3.rows;
  22. columns = current_region3.cols;
  23. depths = current_region3.depths;
  24. calc_init(columns);
  25. }
  26. /****************************************************************************/
  27. typedef struct map
  28. {
  29. const char *name;
  30. const char *mapset;
  31. int have_cats;
  32. int have_colors;
  33. int min_row, max_row;
  34. void *handle;
  35. int fd;
  36. struct Categories cats;
  37. struct Colors colors;
  38. BTREE btree;
  39. } map;
  40. /****************************************************************************/
  41. static map *maps;
  42. static int num_maps;
  43. static int max_maps;
  44. static void **omaps;
  45. static int num_omaps;
  46. static int max_omaps;
  47. static unsigned char *red, *grn, *blu;
  48. static unsigned char *set;
  49. static int min_row = INT_MAX;
  50. static int max_row = -INT_MAX;
  51. static int min_col = INT_MAX;
  52. static int max_col = -INT_MAX;
  53. /****************************************************************************/
  54. static void read_row(void *handle, char *buf, int type, int depth, int row)
  55. {
  56. int i;
  57. switch (type) {
  58. case CELL_TYPE:
  59. for (i = 0; i < columns; i++) {
  60. double x;
  61. Rast3d_get_value(handle, i, row, depth, (char *)&x, DCELL_TYPE);
  62. if (Rast3d_is_null_value_num(&x, DCELL_TYPE))
  63. SET_NULL_C(&((CELL *) buf)[i]);
  64. else
  65. ((CELL *) buf)[i] = (CELL) x;
  66. }
  67. break;
  68. case FCELL_TYPE:
  69. for (i = 0; i < columns; i++) {
  70. float x;
  71. Rast3d_get_value(handle, i, row, depth, (char *)&x, FCELL_TYPE);
  72. if (Rast3d_is_null_value_num(&x, FCELL_TYPE))
  73. SET_NULL_F(&((FCELL *) buf)[i]);
  74. else
  75. ((FCELL *) buf)[i] = x;
  76. }
  77. break;
  78. case DCELL_TYPE:
  79. for (i = 0; i < columns; i++) {
  80. double x;
  81. Rast3d_get_value(handle, i, row, depth, (char *)&x, DCELL_TYPE);
  82. if (Rast3d_is_null_value_num(&x, DCELL_TYPE))
  83. SET_NULL_D(&((DCELL *) buf)[i]);
  84. else
  85. ((DCELL *) buf)[i] = x;
  86. }
  87. break;
  88. }
  89. }
  90. static void write_row(void *handle, const char *buf, int type, int depth,
  91. int row)
  92. {
  93. int i;
  94. switch (type) {
  95. case CELL_TYPE:
  96. for (i = 0; i < columns; i++) {
  97. double x;
  98. if (IS_NULL_C(&((CELL *) buf)[i]))
  99. Rast3d_set_null_value(&x, 1, DCELL_TYPE);
  100. else
  101. x = ((CELL *) buf)[i];
  102. if (Rast3d_put_value(handle, i, row, depth, (char *)&x, DCELL_TYPE) <
  103. 0)
  104. G_fatal_error(_("Error writing data"));
  105. }
  106. break;
  107. case FCELL_TYPE:
  108. for (i = 0; i < columns; i++) {
  109. float x;
  110. if (IS_NULL_F(&((FCELL *) buf)[i]))
  111. Rast3d_set_null_value(&x, 1, FCELL_TYPE);
  112. else
  113. x = ((FCELL *) buf)[i];
  114. if (Rast3d_put_value(handle, i, row, depth, (char *)&x, FCELL_TYPE) <
  115. 0)
  116. G_fatal_error(_("Error writing data"));
  117. }
  118. break;
  119. case DCELL_TYPE:
  120. for (i = 0; i < columns; i++) {
  121. double x;
  122. if (IS_NULL_D(&((DCELL *) buf)[i]))
  123. Rast3d_set_null_value(&x, 1, DCELL_TYPE);
  124. else
  125. x = ((DCELL *) buf)[i];
  126. if (Rast3d_put_value(handle, i, row, depth, (char *)&x, DCELL_TYPE) <
  127. 0)
  128. G_fatal_error(_("Error writing data"));
  129. }
  130. break;
  131. }
  132. }
  133. /****************************************************************************/
  134. static int compare_ints(const void *a, const void *b)
  135. {
  136. return *(const int *)a - *(const int *)b;
  137. }
  138. static void init_colors(map * m)
  139. {
  140. if (!red)
  141. red = G_malloc(columns);
  142. if (!grn)
  143. grn = G_malloc(columns);
  144. if (!blu)
  145. blu = G_malloc(columns);
  146. if (!set)
  147. set = G_malloc(columns);
  148. if (Rast3d_read_colors((char *)m->name, (char *)m->mapset, &m->colors) < 0)
  149. G_fatal_error(_("Unable to read color file for raster map <%s@%s>"),
  150. m->name, m->mapset);
  151. m->have_colors = 1;
  152. }
  153. static void init_cats(map * m)
  154. {
  155. if (Rast3d_read_cats((char *)m->name, (char *)m->mapset, &m->cats) < 0)
  156. G_fatal_error(_("Unable to read category file of raster map <%s@%s>"),
  157. m->name, m->mapset);
  158. if (!btree_create(&m->btree, compare_ints, 1))
  159. G_fatal_error(_("Unable to create btree for raster map <%s@%s>"),
  160. m->name, m->mapset);
  161. m->have_cats = 1;
  162. }
  163. static void translate_from_colors(map * m, DCELL * rast, CELL * cell,
  164. int ncols, int mod)
  165. {
  166. int i;
  167. Rast_lookup_d_colors(rast, red, grn, blu, set, ncols, &m->colors);
  168. switch (mod) {
  169. case 'r':
  170. for (i = 0; i < ncols; i++)
  171. cell[i] = red[i];
  172. break;
  173. case 'g':
  174. for (i = 0; i < ncols; i++)
  175. cell[i] = grn[i];
  176. break;
  177. case 'b':
  178. for (i = 0; i < ncols; i++)
  179. cell[i] = blu[i];
  180. break;
  181. case '#': /* grey (backwards compatible) */
  182. /* old weightings: R=0.177, G=0.813, B=0.011 */
  183. for (i = 0; i < ncols; i++)
  184. cell[i] =
  185. (181 * red[i] + 833 * grn[i] + 11 * blu[i] + 512) / 1024;
  186. break;
  187. case 'y': /* grey (NTSC) */
  188. /* NTSC weightings: R=0.299, G=0.587, B=0.114 */
  189. for (i = 0; i < ncols; i++)
  190. cell[i] =
  191. (306 * red[i] + 601 * grn[i] + 117 * blu[i] + 512) / 1024;
  192. break;
  193. case 'i': /* grey (equal weight) */
  194. for (i = 0; i < ncols; i++)
  195. cell[i] = (red[i] + grn[i] + blu[i]) / 3;
  196. break;
  197. case 'M':
  198. case '@':
  199. default:
  200. G_fatal_error(_("Invalid map modifier: '%c'"), mod);
  201. break;
  202. }
  203. }
  204. /* convert cell values to double based on the values in the
  205. * category file.
  206. *
  207. * This requires performing sscanf() of the category label
  208. * and only do it it for new categories. Must maintain
  209. * some kind of maps of already scaned values.
  210. *
  211. * This maps is a hybrid tree, where the data in each node
  212. * of the tree is an array of, for example, 64 values, and
  213. * the key of the tree is the category represented by the
  214. * first index of the data
  215. *
  216. * To speed things up a little, use shifts instead of divide or multiply
  217. * to compute the key and the index
  218. *
  219. * This uses the BTREE library to manage the tree itself
  220. * btree structure must already be initialized
  221. * pcats structure must already contain category labels
  222. */
  223. #define SHIFT 6
  224. #define NCATS (1<<SHIFT)
  225. static void translate_from_cats(map * m, CELL * cell, DCELL * xcell,
  226. int ncols)
  227. {
  228. struct Categories *pcats;
  229. BTREE *btree;
  230. int i, idx;
  231. CELL cat, key;
  232. double vbuf[1 << SHIFT];
  233. double *values;
  234. void *ptr;
  235. char *label;
  236. btree = &m->btree;
  237. pcats = &m->cats;
  238. for (; ncols-- > 0; cell++, xcell++) {
  239. cat = *cell;
  240. if (IS_NULL_C(cell)) {
  241. SET_NULL_D(xcell);
  242. continue;
  243. }
  244. /* compute key as cat/NCATS * NCATS, adjusting down for negatives
  245. * and idx so that key+idx == cat
  246. */
  247. if (cat < 0)
  248. key = -(((-cat - 1) >> SHIFT) << SHIFT) - NCATS;
  249. else
  250. key = (cat >> SHIFT) << SHIFT;
  251. idx = cat - key;
  252. /* If key not already in the tree, sscanf() all cats for this key
  253. * and put them into the tree
  254. */
  255. if (!btree_find(btree, &key, &ptr)) {
  256. values = vbuf;
  257. int cat = i + key;
  258. for (i = 0; i < NCATS; i++) {
  259. if ((label = Rast_get_c_cat((CELL *) &cat, pcats)) == NULL
  260. || sscanf(label, "%lf", values) != 1)
  261. SET_NULL_D(values);
  262. values++;
  263. }
  264. values = vbuf;
  265. btree_update(btree, &key, sizeof(key), vbuf, sizeof(vbuf));
  266. }
  267. else
  268. values = ptr;
  269. /* and finally lookup the translated value */
  270. if (IS_NULL_D(&values[idx]))
  271. SET_NULL_D(xcell);
  272. else
  273. *xcell = values[idx];
  274. }
  275. }
  276. static void setup_map(map * m)
  277. {
  278. }
  279. static void read_map(map * m, void *buf, int res_type, int depth, int row,
  280. int col)
  281. {
  282. CELL *ibuf = buf;
  283. FCELL *fbuf = buf;
  284. DCELL *dbuf = buf;
  285. if (row < 0 || row >= rows || depth < 0 || depth >= depths) {
  286. int i;
  287. switch (res_type) {
  288. case CELL_TYPE:
  289. for (i = 0; i < columns; i++)
  290. SET_NULL_C(&ibuf[i]);
  291. break;
  292. case FCELL_TYPE:
  293. for (i = 0; i < columns; i++)
  294. SET_NULL_F(&fbuf[i]);
  295. break;
  296. case DCELL_TYPE:
  297. for (i = 0; i < columns; i++)
  298. SET_NULL_D(&dbuf[i]);
  299. break;
  300. default:
  301. G_fatal_error(_("Unknown type: %d"), res_type);
  302. break;
  303. }
  304. return;
  305. }
  306. read_row(m->handle, buf, res_type, depth, row);
  307. if (col)
  308. column_shift(buf, res_type, col);
  309. }
  310. static void close_map(map * m)
  311. {
  312. if (!m->handle)
  313. return;
  314. if (!Rast3d_close(m->handle))
  315. G_fatal_error(_("Unable to close raster map <%s@%s>"),
  316. m->name, m->mapset);
  317. if (m->have_cats) {
  318. btree_free(&m->btree);
  319. Rast_free_cats(&m->cats);
  320. m->have_cats = 0;
  321. }
  322. if (m->have_colors) {
  323. Rast_free_colors(&m->colors);
  324. m->have_colors = 0;
  325. }
  326. }
  327. /****************************************************************************/
  328. int map_type(const char *name, int mod)
  329. {
  330. const char *mapset;
  331. char *tmpname;
  332. int result;
  333. switch (mod) {
  334. case 'M':
  335. tmpname = G_store((char *)name);
  336. mapset = G_find_raster3d(tmpname, "");
  337. if (mapset) {
  338. void *handle;
  339. setup_region(); /* TODO: setup_region should be called by evaluate() ? */
  340. handle = Rast3d_open_cell_old(tmpname, mapset, &current_region3,
  341. RASTER3D_TILE_SAME_AS_FILE, RASTER3D_NO_CACHE);
  342. result = (Rast3d_file_type_map(handle) == FCELL_TYPE)
  343. ? FCELL_TYPE : DCELL_TYPE;
  344. Rast3d_close(handle);
  345. }
  346. else
  347. result = -1;
  348. G_free(tmpname);
  349. return result;
  350. case '@':
  351. return DCELL_TYPE;
  352. case 'r':
  353. case 'g':
  354. case 'b':
  355. case '#':
  356. case 'y':
  357. case 'i':
  358. return CELL_TYPE;
  359. default:
  360. G_fatal_error(_("Invalid map modifier: '%c'"), mod);
  361. return -1;
  362. }
  363. }
  364. int open_map(const char *name, int mod, int row, int col)
  365. {
  366. int i;
  367. const char *mapset;
  368. char *tmpname;
  369. int use_cats = 0;
  370. int use_colors = 0;
  371. map *m;
  372. if (row < min_row)
  373. min_row = row;
  374. if (row > max_row)
  375. max_row = row;
  376. if (col < min_col)
  377. min_col = col;
  378. if (col > max_col)
  379. max_col = col;
  380. tmpname = G_store((char *)name);
  381. mapset = G_find_raster3d(tmpname, "");
  382. G_free(tmpname);
  383. if (!mapset)
  384. G_fatal_error("open_map: map [%s] not found", name);
  385. switch (mod) {
  386. case 'M':
  387. break;
  388. case '@':
  389. use_cats = 1;
  390. break;
  391. case 'r':
  392. case 'g':
  393. case 'b':
  394. case '#':
  395. case 'y':
  396. case 'i':
  397. use_colors = 1;
  398. break;
  399. default:
  400. G_fatal_error(_("Invalid map modifier: '%c'"), mod);
  401. break;
  402. }
  403. for (i = 0; i < num_maps; i++) {
  404. m = &maps[i];
  405. if (strcmp(m->name, name) != 0 || strcmp(m->mapset, mapset) != 0)
  406. continue;
  407. if (row < m->min_row)
  408. m->min_row = row;
  409. if (row > m->max_row)
  410. m->max_row = row;
  411. if (use_cats && !m->have_cats)
  412. init_cats(m);
  413. if (use_colors && !m->have_colors)
  414. init_colors(m);
  415. return i;
  416. }
  417. if (num_maps >= max_maps) {
  418. max_maps += 10;
  419. maps = G_realloc(maps, max_maps * sizeof(map));
  420. }
  421. m = &maps[num_maps];
  422. m->name = name;
  423. m->mapset = mapset;
  424. m->have_cats = 0;
  425. m->have_colors = 0;
  426. m->min_row = row;
  427. m->max_row = row;
  428. if (use_cats)
  429. init_cats(m);
  430. if (use_colors)
  431. init_colors(m);
  432. m->handle = Rast3d_open_cell_old((char *)name, (char *)mapset,
  433. &current_region3, DCELL_TYPE,
  434. RASTER3D_USE_CACHE_DEFAULT);
  435. if (!m->handle)
  436. G_fatal_error(_("Unable to open raster map <%s>"), name);
  437. return num_maps++;
  438. }
  439. void setup_maps(void)
  440. {
  441. int i;
  442. /* We need to reduce the number of worker threads to one, to
  443. * avoid that several threads access a single map for reading
  444. * at the same time. The raster3d library is not thread safe.
  445. * */
  446. putenv("WORKERS=0");
  447. for (i = 0; i < num_maps; i++)
  448. setup_map(&maps[i]);
  449. }
  450. void get_map_row(int idx, int mod, int depth, int row, int col, void *buf,
  451. int res_type)
  452. {
  453. static CELL *ibuf;
  454. static DCELL *fbuf;
  455. map *m = &maps[idx];
  456. switch (mod) {
  457. case 'M':
  458. read_map(m, buf, res_type, depth, row, col);
  459. break;
  460. case '@':
  461. if (!ibuf)
  462. ibuf = G_malloc(columns * sizeof(CELL));
  463. read_map(m, ibuf, CELL_TYPE, depth, row, col);
  464. translate_from_cats(m, ibuf, buf, columns);
  465. break;
  466. case 'r':
  467. case 'g':
  468. case 'b':
  469. case '#':
  470. case 'y':
  471. case 'i':
  472. if (!fbuf)
  473. fbuf = G_malloc(columns * sizeof(DCELL));
  474. read_map(m, fbuf, DCELL_TYPE, depth, row, col);
  475. translate_from_colors(m, fbuf, buf, columns, mod);
  476. break;
  477. default:
  478. G_fatal_error(_("Invalid map modifier: '%c'"), mod);
  479. break;
  480. }
  481. }
  482. void close_maps(void)
  483. {
  484. int i;
  485. for (i = 0; i < num_maps; i++)
  486. close_map(&maps[i]);
  487. num_maps = 0;
  488. }
  489. void list_maps(FILE *fp, const char *sep)
  490. {
  491. int i;
  492. for (i = 0; i < num_maps; i++) {
  493. const struct map *m = &maps[i];
  494. fprintf(fp, "%s%s@%s", i ? sep : "", m->name, m->mapset);
  495. }
  496. }
  497. /****************************************************************************/
  498. int check_output_map(const char *name)
  499. {
  500. return !!G_find_raster3d(name, G_mapset());
  501. }
  502. int open_output_map(const char *name, int res_type)
  503. {
  504. void *handle;
  505. Rast3d_set_file_type(res_type == FCELL_TYPE ? FCELL_TYPE : DCELL_TYPE);
  506. handle = Rast3d_open_new_opt_tile_size((char *)name, RASTER3D_USE_CACHE_XYZ, &current_region3, res_type == FCELL_TYPE ? FCELL_TYPE : DCELL_TYPE, 32);
  507. if (!handle)
  508. G_fatal_error(_("Unable to create raster map <%s>"), name);
  509. if (num_omaps >= max_omaps) {
  510. max_omaps += 10;
  511. omaps = G_realloc(omaps, max_omaps * sizeof(void *));
  512. }
  513. omaps[num_omaps] = handle;
  514. return num_omaps++;
  515. }
  516. void put_map_row(int fd, void *buf, int res_type)
  517. {
  518. void *handle = omaps[fd];
  519. write_row(handle, buf, res_type, current_depth, current_row);
  520. }
  521. void close_output_map(int fd)
  522. {
  523. void *handle = omaps[fd];
  524. if (!Rast3d_close(handle))
  525. G_fatal_error(_("Unable to close output raster map"));
  526. }
  527. void unopen_output_map(int fd)
  528. {
  529. close_output_map(fd);
  530. }
  531. /****************************************************************************/
  532. void copy_cats(const char *dst, int idx)
  533. {
  534. const map *m = &maps[idx];
  535. struct Categories cats;
  536. if (Rast3d_read_cats((char *)m->name, (char *)m->mapset, &cats) < 0)
  537. return;
  538. Rast3d_write_cats((char *)dst, &cats);
  539. Rast_free_cats(&cats);
  540. }
  541. void copy_colors(const char *dst, int idx)
  542. {
  543. const map *m = &maps[idx];
  544. struct Colors colr;
  545. if (Rast3d_read_colors((char *)m->name, (char *)m->mapset, &colr) <= 0)
  546. return;
  547. Rast3d_write_colors((char *)dst, G_mapset(), &colr);
  548. Rast_free_colors(&colr);
  549. }
  550. void copy_history(const char *dst, int idx)
  551. {
  552. }
  553. void create_history(const char *dst, expression * e)
  554. {
  555. }
  556. /****************************************************************************/