vrt.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*!
  2. \file lib/raster/vrt.c
  3. \brief Raster Library - virtual GRASS raster maps.
  4. (C) 2010 by the GRASS Development Team
  5. This program is free software under the GNU General Public License
  6. (>=v2). Read the file COPYING that comes with GRASS for details.
  7. \author Markus Metz
  8. */
  9. #include <grass/gis.h>
  10. #include <grass/raster.h>
  11. #include <grass/gprojects.h>
  12. #include <grass/glocale.h>
  13. #include "R.h"
  14. int cmp_wnd(const void *a, const void *b)
  15. {
  16. struct Cell_head *cellhda = &((struct tileinfo *) a)->cellhd;
  17. struct Cell_head *cellhdb = &((struct tileinfo *) b)->cellhd;
  18. /* sort from descending N to S, then ascending from W to E */
  19. if (cellhda->south > cellhdb->south)
  20. return -1;
  21. if (cellhda->south < cellhdb->south)
  22. return 1;
  23. if (cellhda->north > cellhdb->north)
  24. return -1;
  25. if (cellhda->north < cellhdb->north)
  26. return 1;
  27. if (cellhda->west < cellhdb->west)
  28. return -1;
  29. if (cellhda->west > cellhdb->west)
  30. return 1;
  31. if (cellhda->east < cellhdb->east)
  32. return -1;
  33. if (cellhda->east > cellhdb->east)
  34. return 1;
  35. return 0;
  36. }
  37. struct R_vrt *Rast_get_vrt(const char *vname, const char *vmapset)
  38. {
  39. FILE *fp;
  40. int talloc, tilecount;
  41. struct tileinfo *ti;
  42. struct R_vrt *vrt;
  43. struct Cell_head *rd_window = &R__.rd_window;
  44. struct ilist *tlist;
  45. tilecount = 0;
  46. ti = NULL;
  47. if (!G_find_raster2(vname, vmapset))
  48. return NULL;
  49. fp = G_fopen_old_misc("cell_misc", "vrt", vname, vmapset);
  50. if (!fp)
  51. return NULL;
  52. tlist = G_new_ilist();
  53. talloc = 0;
  54. while (1) {
  55. char buf[GNAME_MAX];
  56. char *name;
  57. const char *mapset;
  58. struct tileinfo *p;
  59. if (!G_getl2(buf, sizeof(buf), fp))
  60. break;
  61. /* Ignore empty lines */
  62. if (!*buf)
  63. continue;
  64. name = buf;
  65. if ((mapset = G_find_raster(name, "")) == NULL)
  66. G_fatal_error(_("Tile raster map <%s> not found"), name);
  67. if (strcmp(name, vname) == 0)
  68. G_fatal_error(_("A virtual raster can not contain itself"));
  69. if (tilecount >= talloc) {
  70. talloc += 100;
  71. ti = G_realloc(ti, talloc * sizeof(struct tileinfo));
  72. }
  73. p = &ti[tilecount];
  74. p->name = G_store(name);
  75. p->mapset = G_store(mapset);
  76. Rast_get_cellhd(p->name, p->mapset, &(p->cellhd));
  77. p->clist = NULL;
  78. if (rd_window->proj == PROJECTION_LL) {
  79. while (p->cellhd.west >= rd_window->east) {
  80. p->cellhd.west -= 360.0;
  81. p->cellhd.east -= 360.0;
  82. }
  83. while (p->cellhd.east <= rd_window->west) {
  84. p->cellhd.west += 360.0;
  85. p->cellhd.east += 360.0;
  86. }
  87. }
  88. if (p->cellhd.north > rd_window->south &&
  89. p->cellhd.south <= rd_window->north &&
  90. p->cellhd.west < rd_window->east &&
  91. p->cellhd.east >= rd_window->west) {
  92. int col;
  93. double east;
  94. G_ilist_add(tlist, tilecount);
  95. p->clist = G_new_ilist();
  96. for (col = 0; col < rd_window->cols; col++) {
  97. east = rd_window->west + rd_window->ew_res * (col + 0.5);
  98. if (rd_window->proj == PROJECTION_LL) {
  99. while (east > p->cellhd.east)
  100. east -= 360;
  101. while (east < p->cellhd.west)
  102. east += 360;
  103. }
  104. if (east >= p->cellhd.west && east < p->cellhd.east)
  105. G_ilist_add(p->clist, col);
  106. }
  107. }
  108. tilecount++;
  109. }
  110. if (tilecount > 1)
  111. qsort(ti, tilecount, sizeof(struct tileinfo), cmp_wnd);
  112. fclose(fp);
  113. vrt = G_calloc(1, sizeof(struct R_vrt));
  114. vrt->tilecount = tilecount;
  115. vrt->tileinfo = ti;
  116. vrt->tlist = tlist;
  117. return vrt;
  118. }
  119. void Rast_close_vrt(struct R_vrt *vrt)
  120. {
  121. int i;
  122. for (i = 0; i < vrt->tilecount; i++) {
  123. struct tileinfo *p;
  124. p = &(vrt->tileinfo[i]);
  125. G_free(p->name);
  126. G_free(p->mapset);
  127. if (p->clist)
  128. G_free_ilist(p->clist);
  129. }
  130. G_free(vrt->tileinfo);
  131. G_free_ilist(vrt->tlist);
  132. G_free(vrt);
  133. }
  134. /* must only be called by get_map_row_nomask()
  135. * move to get_row.c as read_data_vrt() ? */
  136. int Rast_get_vrt_row(int fd, void *buf, int row, RASTER_MAP_TYPE data_type)
  137. {
  138. struct fileinfo *fcb = &R__.fileinfo[fd];
  139. struct R_vrt *vrt = fcb->vrt;
  140. struct tileinfo *ti = vrt->tileinfo;
  141. struct Cell_head *rd_window = &R__.rd_window;
  142. double rown, rows;
  143. int i, j;
  144. int have_tile;
  145. void *tmpbuf;
  146. size_t size = Rast_cell_size(data_type);
  147. rown = rd_window->north - rd_window->ns_res * row;
  148. rows = rd_window->north - rd_window->ns_res * (row + 1);
  149. Rast_set_null_value(buf, rd_window->cols, data_type);
  150. tmpbuf = Rast_allocate_input_buf(data_type);
  151. have_tile = 0;
  152. for (i = 0; i < vrt->tlist->n_values; i++) {
  153. struct tileinfo *p = &ti[vrt->tlist->value[i]];
  154. if (p->cellhd.north > rows && p->cellhd.south <= rown) {
  155. int tfd;
  156. void *p1, *p2;
  157. /* recurse into get_map_row(), collect data for all tiles
  158. * a mask is applied to the collected data
  159. * after this function returns */
  160. Rast_set_null_value(tmpbuf, rd_window->cols, data_type);
  161. /* avoid Rast__check_for_auto_masking() */
  162. tfd = Rast__open_old(p->name, p->mapset);
  163. Rast_get_row_nomask(tfd, tmpbuf, row, data_type);
  164. Rast_unopen(tfd);
  165. p1 = buf;
  166. p2 = tmpbuf;
  167. /* restrict to start and end col ? */
  168. for (j = 0; j < p->clist->n_values; j++) {
  169. p1 = (unsigned char *)buf + size * p->clist->value[j];
  170. p2 = (unsigned char *)tmpbuf + size * p->clist->value[j];
  171. if (!Rast_is_null_value(p2, data_type)) {
  172. switch (data_type) {
  173. case CELL_TYPE:
  174. *(CELL *) p1 = *(CELL *) p2;
  175. break;
  176. case FCELL_TYPE:
  177. *(FCELL *) p1 = *(FCELL *) p2;
  178. break;
  179. case DCELL_TYPE:
  180. *(DCELL *) p1 = *(DCELL *) p2;
  181. break;
  182. default:
  183. break;
  184. }
  185. }
  186. }
  187. have_tile = 1;
  188. }
  189. }
  190. G_free(tmpbuf);
  191. return have_tile;
  192. }