open.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  1. /*!
  2. * \file lib/raster/open.c
  3. *
  4. * \brief Raster Library - Open raster file
  5. *
  6. * (C) 1999-2009 by the GRASS Development Team
  7. *
  8. * This program is free software under the GNU General Public
  9. * License (>=v2). Read the file COPYING that comes with GRASS
  10. * for details.
  11. *
  12. * \author USACERL and many others
  13. */
  14. #include <unistd.h>
  15. #include <string.h>
  16. #include <sys/types.h>
  17. #include <sys/stat.h>
  18. #include <fcntl.h>
  19. #include <errno.h>
  20. #include <grass/config.h>
  21. #include <grass/gis.h>
  22. #include <grass/raster.h>
  23. #include <grass/glocale.h>
  24. #include "R.h"
  25. #define FORMAT_FILE "f_format"
  26. #define NULL_FILE "null"
  27. /* cmpressed null file */
  28. #define NULLC_FILE "nullcmpr"
  29. static int new_fileinfo(void)
  30. {
  31. int oldsize = R__.fileinfo_count;
  32. int newsize = oldsize;
  33. int i;
  34. for (i = 0; i < oldsize; i++)
  35. if (R__.fileinfo[i].open_mode <= 0) {
  36. memset(&R__.fileinfo[i], 0, sizeof(struct fileinfo));
  37. R__.fileinfo[i].open_mode = -1;
  38. return i;
  39. }
  40. if (newsize < 20)
  41. newsize += 20;
  42. else
  43. newsize *= 2;
  44. R__.fileinfo = G_realloc(R__.fileinfo, newsize * sizeof(struct fileinfo));
  45. /* Mark all cell files as closed */
  46. for (i = oldsize; i < newsize; i++) {
  47. memset(&R__.fileinfo[i], 0, sizeof(struct fileinfo));
  48. R__.fileinfo[i].open_mode = -1;
  49. }
  50. R__.fileinfo_count = newsize;
  51. return oldsize;
  52. }
  53. /*!
  54. * \brief Open raster file
  55. *
  56. * Arrange for the NULL-value bitmap to be read as well as the raster
  57. * map. If no NULL-value bitmap exists, arrange for the production of
  58. * NULL-values based on zeros in the raster map. If the map is
  59. * floating-point, arrange for quantization to integer for
  60. * Rast_get_c_row(), et. al., by reading the quantization rules
  61. * for the map using Rast_read_quant(). If the programmer wants to read
  62. * the floating point map using uing quant rules other than the ones
  63. * stored in map's quant file, he/she should call Rast_set_quant_rules()
  64. * after the call to Rast_open_old().
  65. *
  66. * \param name map name
  67. * \param open_mode mode
  68. * \param map_type map type (CELL, FCELL, DCELL)
  69. *
  70. * \return open file descriptor ( >= 0) if successful
  71. */
  72. static int open_raster_new(const char *name, int open_mode,
  73. RASTER_MAP_TYPE map_type);
  74. /*!
  75. \brief Open an existing integer raster map (cell)
  76. Opens the existing cell file <i>name</i> in the <i>mapset</i> for
  77. reading by Rast_get_row() with mapping into the current window.
  78. This routine opens the raster map <i>name</i> in <i>mapset</i> for
  79. reading. A nonnegative file descriptor is returned if the open is
  80. successful. Otherwise a diagnostic message is printed and a negative
  81. value is returned. This routine does quite a bit of work. Since
  82. GRASS users expect that all raster maps will be resampled into the
  83. current region, the resampling index for the raster map is prepared
  84. by this routine after the file is opened. The resampling is based on
  85. the active module region (see also \ref The_Region}. Preparation
  86. required for reading the various raster file formats (see \ref
  87. Raster_File_Format for an explanation of the various raster file
  88. formats) is also done.
  89. Diagnostics: warning message printed if open fails.
  90. \param name map name
  91. \param mapset mapset name where raster map <i>name</i> lives
  92. \return nonnegative file descriptor (int)
  93. */
  94. int Rast_open_old(const char *name, const char *mapset)
  95. {
  96. int fd = Rast__open_old(name, mapset);
  97. /* turn on auto masking, if not already on */
  98. Rast__check_for_auto_masking();
  99. /*
  100. if(R__.auto_mask <= 0)
  101. R__.mask_buf = Rast_allocate_c_buf();
  102. now we don't ever free it!, so no need to allocate it (Olga)
  103. */
  104. /* mask_buf is used for reading MASK file when mask is set and
  105. for reading map rows when the null file doesn't exist */
  106. return fd;
  107. }
  108. /*! \brief Lower level function, open cell files, supercell files,
  109. and the MASK file.
  110. Actions:
  111. - opens the named cell file, following reclass reference if
  112. named layer is a reclass layer.
  113. - creates the required mapping between the data and the window
  114. for use by the get_map_row family of routines.
  115. Diagnostics: Errors other than actual open failure will cause a
  116. diagnostic to be delivered through G_warning() open failure messages
  117. are left to the calling routine since the masking logic will want to
  118. issue a different warning.
  119. Note: This routine does NOT open the MASK layer. If it did we would
  120. get infinite recursion. This routine is called to open the mask by
  121. Rast__check_for_auto_masking() which is called by Rast_open_old().
  122. \param name map name
  123. \param mapset mapset of cell file to be opened
  124. \return open file descriptor
  125. */
  126. int Rast__open_old(const char *name, const char *mapset)
  127. {
  128. struct fileinfo *fcb;
  129. int cell_fd, fd;
  130. char *cell_dir;
  131. const char *r_name;
  132. const char *r_mapset;
  133. struct Cell_head cellhd;
  134. int CELL_nbytes = 0; /* bytes per cell in CELL map */
  135. int INTERN_SIZE;
  136. int reclass_flag;
  137. int MAP_NBYTES;
  138. RASTER_MAP_TYPE MAP_TYPE;
  139. struct Reclass reclass;
  140. char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
  141. struct GDAL_link *gdal;
  142. struct R_vrt *vrt;
  143. Rast__init();
  144. G_unqualified_name(name, mapset, xname, xmapset);
  145. name = xname;
  146. mapset = xmapset;
  147. if (!G_find_raster2(name, mapset))
  148. G_fatal_error(_("Raster map <%s> not found"),
  149. G_fully_qualified_name(name, mapset));
  150. /* Check for reclassification */
  151. reclass_flag = Rast_get_reclass(name, mapset, &reclass);
  152. switch (reclass_flag) {
  153. case 0:
  154. r_name = name;
  155. r_mapset = mapset;
  156. break;
  157. case 1:
  158. r_name = reclass.name;
  159. r_mapset = reclass.mapset;
  160. if (!G_find_raster2(r_name, r_mapset))
  161. G_fatal_error(_("Unable to open raster map <%s@%s> since it is a reclass "
  162. "of raster map <%s@%s> which does not exist"),
  163. name, mapset, r_name, r_mapset);
  164. break;
  165. default: /* Error reading cellhd/reclass file */
  166. G_fatal_error(_("Error reading reclass file for raster map <%s>"),
  167. G_fully_qualified_name(name, mapset));
  168. break;
  169. }
  170. /* read the cell header */
  171. Rast_get_cellhd(r_name, r_mapset, &cellhd);
  172. /* now check the type */
  173. MAP_TYPE = Rast_map_type(r_name, r_mapset);
  174. if (MAP_TYPE < 0)
  175. G_fatal_error(_("Error reading map type for raster map <%s>"),
  176. G_fully_qualified_name(name, mapset));
  177. if (MAP_TYPE == CELL_TYPE)
  178. /* set the number of bytes for CELL map */
  179. {
  180. CELL_nbytes = cellhd.format + 1;
  181. if (CELL_nbytes < 1)
  182. G_fatal_error(_("Raster map <%s@%s>: format field in header file invalid"),
  183. r_name, r_mapset);
  184. }
  185. /* compressor */
  186. if (MAP_TYPE != CELL_TYPE) {
  187. /* fp maps do not use RLE */
  188. /* previously, compressed simply meant yes (ZLIB) or no
  189. * now compressed encodes compressor type
  190. * 0: not compressed
  191. * 1, 2: ZLIB
  192. * 3: LZ4
  193. * 4: BZIP2
  194. * etc */
  195. if (cellhd.compressed == 1)
  196. cellhd.compressed = 2;
  197. }
  198. /* test if compressor type is supported */
  199. if (!G_check_compressor(cellhd.compressed)) {
  200. G_fatal_error(_("Compression with %s is not supported in this GRASS GIS installation"), G_compressor_name(cellhd.compressed));
  201. }
  202. if (cellhd.proj != R__.rd_window.proj)
  203. G_fatal_error(_("Raster map <%s> is in different projection than current region. "
  204. "Found <%s>, should be <%s>."),
  205. G_fully_qualified_name(name, mapset),
  206. G_projection_name(cellhd.proj),
  207. G_projection_name(R__.rd_window.proj));
  208. if (cellhd.zone != R__.rd_window.zone)
  209. G_fatal_error(_("Raster map <%s> is in different zone (%d) than current region (%d)"),
  210. G_fully_qualified_name(name, mapset), cellhd.zone, R__.rd_window.zone);
  211. /* when map is int warn if too large cell size */
  212. if (MAP_TYPE == CELL_TYPE && (unsigned int)CELL_nbytes > sizeof(CELL))
  213. G_fatal_error(_("Raster map <%s>: bytes per cell (%d) too large"),
  214. G_fully_qualified_name(name, mapset), CELL_nbytes);
  215. /* record number of bytes per cell */
  216. if (MAP_TYPE == FCELL_TYPE) {
  217. cell_dir = "fcell";
  218. INTERN_SIZE = sizeof(FCELL);
  219. MAP_NBYTES = XDR_FLOAT_NBYTES;
  220. }
  221. else if (MAP_TYPE == DCELL_TYPE) {
  222. cell_dir = "fcell";
  223. INTERN_SIZE = sizeof(DCELL);
  224. MAP_NBYTES = XDR_DOUBLE_NBYTES;
  225. }
  226. else { /* integer */
  227. cell_dir = "cell";
  228. INTERN_SIZE = sizeof(CELL);
  229. MAP_NBYTES = CELL_nbytes;
  230. }
  231. gdal = Rast_get_gdal_link(r_name, r_mapset);
  232. vrt = Rast_get_vrt(r_name, r_mapset);
  233. cell_fd = -1;
  234. if (gdal) {
  235. #ifdef HAVE_GDAL
  236. cell_fd = -1;
  237. #else
  238. G_fatal_error(_("Raster map <%s@%s> is a GDAL link but GRASS is compiled without GDAL support"),
  239. r_name, r_mapset);
  240. #endif
  241. }
  242. else if (vrt) {
  243. cell_fd = -1;
  244. }
  245. else {
  246. /* now actually open file for reading */
  247. cell_fd = G_open_old(cell_dir, r_name, r_mapset);
  248. if (cell_fd < 0)
  249. G_fatal_error(_("Unable to open %s file for raster map <%s@%s>"),
  250. cell_dir, r_name, r_mapset);
  251. }
  252. fd = new_fileinfo();
  253. fcb = &R__.fileinfo[fd];
  254. fcb->data_fd = cell_fd;
  255. fcb->map_type = MAP_TYPE;
  256. /* Save cell header */
  257. fcb->cellhd = cellhd;
  258. /* allocate null bitstream buffers for reading null rows */
  259. fcb->null_fd = -1;
  260. fcb->null_cur_row = -1;
  261. fcb->null_bits = Rast__allocate_null_bits(cellhd.cols);
  262. /* mark closed */
  263. fcb->open_mode = -1;
  264. /* save name and mapset */
  265. fcb->name = G_store(name);
  266. fcb->mapset = G_store(mapset);
  267. /* mark no data row in memory */
  268. fcb->cur_row = -1;
  269. /* if reclass, copy reclass structure */
  270. if ((fcb->reclass_flag = reclass_flag))
  271. fcb->reclass = reclass;
  272. fcb->gdal = gdal;
  273. fcb->vrt = vrt;
  274. if (!gdal && !vrt) {
  275. /* check for compressed data format, making initial reads if necessary */
  276. if (Rast__check_format(fd) < 0) {
  277. close(cell_fd); /* warning issued by check_format() */
  278. G_fatal_error(_("Error reading format for <%s@%s>"),
  279. r_name, r_mapset);
  280. }
  281. }
  282. if (!vrt) {
  283. /* create the mapping from cell file to window */
  284. Rast__create_window_mapping(fd);
  285. }
  286. /*
  287. * allocate the data buffer
  288. * number of bytes per cell is cellhd.format+1
  289. */
  290. /* for reading fcb->data is allocated to be fcb->cellhd.cols * fcb->nbytes
  291. (= XDR_FLOAT/DOUBLE_NBYTES) */
  292. fcb->data = (unsigned char *)G_calloc(fcb->cellhd.cols, MAP_NBYTES);
  293. /* initialize/read in quant rules for float point maps */
  294. if (fcb->map_type != CELL_TYPE) {
  295. if (fcb->reclass_flag)
  296. Rast_read_quant(fcb->reclass.name, fcb->reclass.mapset,
  297. &(fcb->quant));
  298. else
  299. Rast_read_quant(fcb->name, fcb->mapset, &(fcb->quant));
  300. }
  301. /* now mark open for read: this must follow create_window_mapping() */
  302. fcb->open_mode = OPEN_OLD;
  303. fcb->io_error = 0;
  304. fcb->map_type = MAP_TYPE;
  305. fcb->nbytes = MAP_NBYTES;
  306. fcb->null_row_ptr = NULL;
  307. if (!gdal && !vrt) {
  308. /* First, check for compressed null file */
  309. fcb->null_fd = G_open_old_misc("cell_misc", NULL_FILE, r_name, r_mapset);
  310. if (fcb->null_fd < 0) {
  311. fcb->null_fd = G_open_old_misc("cell_misc", NULLC_FILE, r_name, r_mapset);
  312. if (fcb->null_fd >= 0) {
  313. fcb->null_row_ptr = G_calloc(fcb->cellhd.rows + 1, sizeof(off_t));
  314. if (Rast__read_null_row_ptrs(fd, fcb->null_fd) < 0) {
  315. close(fcb->null_fd);
  316. fcb->null_fd = -1;
  317. G_free(fcb->null_row_ptr);
  318. fcb->null_row_ptr = NULL;
  319. }
  320. }
  321. }
  322. fcb->null_file_exists = fcb->null_fd >= 0;
  323. }
  324. return fd;
  325. }
  326. /*!
  327. \brief Opens a new cell file in a database (compressed)
  328. Opens a new cell file <i>name</i> in the current mapset for writing
  329. by Rast_put_row().
  330. The file is created and filled with no data it is assumed that the
  331. new cell file is to conform to the current window.
  332. The file must be written sequentially. Use Rast_open_new_random()
  333. for non sequential writes.
  334. Note: the open actually creates a temporary file Rast_close() will
  335. move the temporary file to the cell file and write out the necessary
  336. support files (cellhd, cats, hist, etc.).
  337. Diagnostics: warning message printed if open fails
  338. Warning: calls to Rast_set_window() made after opening a new cell file
  339. may create confusion and should be avoided the new cell file will be
  340. created to conform to the window at the time of the open.
  341. \param name map name
  342. \return open file descriptor ( >= 0) if successful
  343. \return negative integer if error
  344. */
  345. int Rast_open_c_new(const char *name)
  346. {
  347. return open_raster_new(name, OPEN_NEW_COMPRESSED, CELL_TYPE);
  348. }
  349. /*!
  350. \brief Opens a new cell file in a database (uncompressed)
  351. See also Rast_open_new().
  352. \param name map name
  353. \return open file descriptor ( >= 0) if successful
  354. \return negative integer if error
  355. */
  356. int Rast_open_c_new_uncompressed(const char *name)
  357. {
  358. return open_raster_new(name, OPEN_NEW_UNCOMPRESSED, CELL_TYPE);
  359. }
  360. /*!
  361. \brief Save histogram for newly create raster map (cell)
  362. If newly created cell files should have histograms, set flag=1
  363. otherwise set flag=0. Applies to subsequent opens.
  364. \param flag flag indicator
  365. */
  366. void Rast_want_histogram(int flag)
  367. {
  368. R__.want_histogram = flag;
  369. }
  370. /*!
  371. \brief Sets the format for subsequent opens on new integer cell files
  372. (uncompressed and random only).
  373. Warning: subsequent put_row calls will only write n+1 bytes per
  374. cell. If the data requires more, the cell file will be written
  375. incorrectly (but with n+1 bytes per cell)
  376. When writing float map: format is -1
  377. \param n format
  378. */
  379. void Rast_set_cell_format(int n)
  380. /* sets the format for integer raster map */
  381. {
  382. R__.nbytes = n + 1;
  383. if (R__.nbytes <= 0)
  384. R__.nbytes = 1;
  385. if (R__.nbytes > sizeof(CELL))
  386. R__.nbytes = sizeof(CELL);
  387. }
  388. /*!
  389. \brief Get cell value format
  390. \param v cell
  391. \return cell format
  392. */
  393. int Rast_get_cell_format(CELL v)
  394. {
  395. unsigned int i;
  396. if (v >= 0)
  397. for (i = 0; i < sizeof(CELL); i++)
  398. if (!(v /= 256))
  399. return i;
  400. return sizeof(CELL) - 1;
  401. }
  402. /*!
  403. \brief Opens new fcell file in a database
  404. Opens a new floating-point map <i>name</i> in the current mapset for
  405. writing. The type of the file (i.e. either double or float) is
  406. determined and fixed at this point. The default is FCELL_TYPE. In
  407. order to change this default
  408. Use Rast_set_fp_type() where type is one of DCELL_TYPE or FCELL_TYPE.
  409. See warnings and notes for Rast_open_new().
  410. \param name map name
  411. \return nonnegative file descriptor (int)
  412. */
  413. int Rast_open_fp_new(const char *name)
  414. {
  415. return open_raster_new(name, OPEN_NEW_COMPRESSED, R__.fp_type);
  416. }
  417. /*!
  418. \brief Opens new fcell file in a database (uncompressed)
  419. See Rast_open_fp_new() for details.
  420. \param name map name
  421. \return nonnegative file descriptor (int)
  422. */
  423. int Rast_open_fp_new_uncompressed(const char *name)
  424. {
  425. return open_raster_new(name, OPEN_NEW_UNCOMPRESSED, R__.fp_type);
  426. }
  427. #ifdef HAVE_GDAL
  428. static int open_raster_new_gdal(char *map, char *mapset,
  429. RASTER_MAP_TYPE map_type)
  430. {
  431. int fd;
  432. struct fileinfo *fcb;
  433. fd = new_fileinfo();
  434. fcb = &R__.fileinfo[fd];
  435. fcb->data_fd = -1;
  436. /* mark closed */
  437. fcb->map_type = map_type;
  438. fcb->open_mode = -1;
  439. fcb->gdal = Rast_create_gdal_link(map, map_type);
  440. if (!fcb->gdal)
  441. G_fatal_error(_("Unable to create GDAL link"));
  442. fcb->cellhd = R__.wr_window;
  443. fcb->cellhd.compressed = 0;
  444. fcb->nbytes = Rast_cell_size(fcb->map_type);
  445. /* for writing fcb->data is allocated to be R__.wr_window.cols *
  446. sizeof(CELL or DCELL or FCELL) */
  447. fcb->data = G_calloc(R__.wr_window.cols, fcb->nbytes);
  448. fcb->name = map;
  449. fcb->mapset = mapset;
  450. fcb->cur_row = 0;
  451. fcb->row_ptr = NULL;
  452. fcb->temp_name = NULL;
  453. fcb->null_temp_name = NULL;
  454. fcb->null_cur_row = 0;
  455. fcb->null_bits = NULL;
  456. fcb->null_fd = -1;
  457. fcb->null_row_ptr = NULL;
  458. if (fcb->map_type != CELL_TYPE)
  459. Rast_quant_init(&(fcb->quant));
  460. /* init cell stats */
  461. /* now works only for int maps */
  462. if (fcb->map_type == CELL_TYPE)
  463. if ((fcb->want_histogram = R__.want_histogram))
  464. Rast_init_cell_stats(&fcb->statf);
  465. /* init range and if map is double/float init d/f_range */
  466. Rast_init_range(&fcb->range);
  467. if (fcb->map_type != CELL_TYPE)
  468. Rast_init_fp_range(&fcb->fp_range);
  469. /* mark file as open for write */
  470. fcb->open_mode = OPEN_NEW_UNCOMPRESSED;
  471. fcb->io_error = 0;
  472. return fd;
  473. }
  474. #endif /* HAVE_GDAL */
  475. static int open_raster_new(const char *name, int open_mode,
  476. RASTER_MAP_TYPE map_type)
  477. {
  478. char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
  479. struct fileinfo *fcb;
  480. int fd, cell_fd;
  481. char *tempname;
  482. char *map;
  483. char *mapset;
  484. const char *cell_dir;
  485. int nbytes;
  486. Rast__init();
  487. switch (map_type) {
  488. case CELL_TYPE:
  489. cell_dir = "cell";
  490. nbytes = R__.nbytes;
  491. break;
  492. case FCELL_TYPE:
  493. nbytes = XDR_FLOAT_NBYTES;
  494. cell_dir = "fcell";
  495. break;
  496. case DCELL_TYPE:
  497. nbytes = XDR_DOUBLE_NBYTES;
  498. cell_dir = "fcell";
  499. break;
  500. default:
  501. G_fatal_error(_("Invalid map type <%d>"), map_type);
  502. break;
  503. }
  504. if (G_unqualified_name(name, G_mapset(), xname, xmapset) < 0)
  505. G_fatal_error(_("Raster map <%s> is not in the current mapset (%s)"),
  506. name, G_mapset());
  507. map = G_store(xname);
  508. mapset = G_store(xmapset);
  509. /* check for legal grass name */
  510. if (G_legal_filename(map) < 0)
  511. G_fatal_error(_("<%s> is an illegal file name"), map);
  512. #ifdef HAVE_GDAL
  513. if (G_find_file2("", "GDAL", G_mapset()))
  514. return open_raster_new_gdal(map, mapset, map_type);
  515. #endif
  516. /* open a tempfile name */
  517. tempname = G_tempfile();
  518. cell_fd = creat(tempname, 0666);
  519. if (cell_fd < 0) {
  520. int err = errno;
  521. G_free(mapset);
  522. G_free(tempname);
  523. G_free(map);
  524. G_fatal_error(_("No temp files available: %s"), strerror(err));
  525. }
  526. fd = new_fileinfo();
  527. fcb = &R__.fileinfo[fd];
  528. fcb->data_fd = cell_fd;
  529. /*
  530. * since we are bypassing the normal open logic
  531. * must create the cell element
  532. */
  533. G_make_mapset_object_group(cell_dir);
  534. /* mark closed */
  535. fcb->map_type = map_type;
  536. fcb->open_mode = -1;
  537. fcb->gdal = NULL;
  538. fcb->vrt = NULL;
  539. /* for writing fcb->data is allocated to be R__.wr_window.cols *
  540. sizeof(CELL or DCELL or FCELL) */
  541. fcb->data = (unsigned char *)G_calloc(R__.wr_window.cols,
  542. Rast_cell_size(fcb->map_type));
  543. /*
  544. * copy current window into cell header
  545. * set format to cell/supercell
  546. * for compressed writing
  547. * allocate space to hold the row address array
  548. */
  549. fcb->cellhd = R__.wr_window;
  550. /* change open_mode to OPEN_NEW_UNCOMPRESSED if R__.compression_type == 0 ? */
  551. if (open_mode == OPEN_NEW_COMPRESSED && fcb->map_type == CELL_TYPE) {
  552. fcb->row_ptr = G_calloc(fcb->cellhd.rows + 1, sizeof(off_t));
  553. G_zero(fcb->row_ptr, (fcb->cellhd.rows + 1) * sizeof(off_t));
  554. Rast__write_row_ptrs(fd);
  555. fcb->cellhd.compressed = R__.compression_type;
  556. fcb->nbytes = 1; /* to the minimum */
  557. }
  558. else {
  559. fcb->nbytes = nbytes;
  560. if (open_mode == OPEN_NEW_COMPRESSED) {
  561. fcb->row_ptr = G_calloc(fcb->cellhd.rows + 1, sizeof(off_t));
  562. G_zero(fcb->row_ptr, (fcb->cellhd.rows + 1) * sizeof(off_t));
  563. Rast__write_row_ptrs(fd);
  564. fcb->cellhd.compressed = R__.compression_type;
  565. }
  566. else
  567. fcb->cellhd.compressed = 0;
  568. if (fcb->map_type != CELL_TYPE) {
  569. Rast_quant_init(&(fcb->quant));
  570. }
  571. }
  572. if (open_mode == OPEN_NEW_COMPRESSED && fcb->map_type != CELL_TYPE &&
  573. fcb->cellhd.compressed == 1) {
  574. /* fp maps do not use RLE */
  575. fcb->cellhd.compressed = 2;
  576. }
  577. /* save name and mapset, and tempfile name */
  578. fcb->name = map;
  579. fcb->mapset = mapset;
  580. fcb->temp_name = tempname;
  581. /* next row to be written (in order) is zero */
  582. fcb->cur_row = 0;
  583. /* open a null tempfile name */
  584. tempname = G_tempfile();
  585. fcb->null_fd = creat(tempname, 0666);
  586. if (fcb->null_fd < 0) {
  587. int err = errno;
  588. G_free(tempname);
  589. G_free(fcb->name);
  590. G_free(fcb->mapset);
  591. G_free(fcb->temp_name);
  592. close(cell_fd);
  593. G_fatal_error(_("No temp files available: %s"), strerror(err));
  594. }
  595. fcb->null_temp_name = tempname;
  596. fcb->null_row_ptr = NULL;
  597. if (R__.compress_nulls) {
  598. fcb->null_row_ptr = G_calloc(fcb->cellhd.rows + 1, sizeof(off_t));
  599. G_zero(fcb->null_row_ptr, (fcb->cellhd.rows + 1) * sizeof(off_t));
  600. Rast__write_null_row_ptrs(fd, fcb->null_fd);
  601. }
  602. /* next row to be written (in order) is zero */
  603. fcb->null_cur_row = 0;
  604. /* allocate null bitstream buffer for writing */
  605. fcb->null_bits = Rast__allocate_null_bits(fcb->cellhd.cols);
  606. /* init cell stats */
  607. /* now works only for int maps */
  608. if (fcb->map_type == CELL_TYPE)
  609. if ((fcb->want_histogram = R__.want_histogram))
  610. Rast_init_cell_stats(&fcb->statf);
  611. /* init range and if map is double/float init d/f_range */
  612. Rast_init_range(&fcb->range);
  613. if (fcb->map_type != CELL_TYPE)
  614. Rast_init_fp_range(&fcb->fp_range);
  615. /* mark file as open for write */
  616. fcb->open_mode = open_mode;
  617. fcb->io_error = 0;
  618. return fd;
  619. }
  620. int Rast__open_null_write(const char *name)
  621. {
  622. char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
  623. struct fileinfo *fcb;
  624. int fd;
  625. char *tempname;
  626. char *map;
  627. char *mapset;
  628. Rast__init();
  629. if (!G_find_raster2(name, G_mapset()))
  630. G_fatal_error(_("Raster map <%s> does not exist in the current mapset (%s)"),
  631. name, G_mapset());
  632. if (G_unqualified_name(name, G_mapset(), xname, xmapset) < 0)
  633. G_fatal_error(_("Raster map <%s> is not in the current mapset (%s)"),
  634. name, G_mapset());
  635. map = G_store(xname);
  636. mapset = G_store(xmapset);
  637. fd = new_fileinfo();
  638. fcb = &R__.fileinfo[fd];
  639. G_zero(fcb, sizeof(*fcb));
  640. fcb->name = map;
  641. fcb->mapset = mapset;
  642. Rast_get_cellhd(map, mapset, &fcb->cellhd);
  643. /* open a null tempfile name */
  644. tempname = G_tempfile();
  645. fcb->null_fd = creat(tempname, 0666);
  646. if (fcb->null_fd < 0) {
  647. int err = errno;
  648. G_free(tempname);
  649. G_free(fcb->name);
  650. G_free(fcb->mapset);
  651. G_fatal_error(_("No temp files available: %s"), strerror(err));
  652. }
  653. fcb->null_temp_name = tempname;
  654. if (R__.compress_nulls) {
  655. fcb->null_row_ptr = G_calloc(fcb->cellhd.rows + 1, sizeof(off_t));
  656. G_zero(fcb->null_row_ptr, (fcb->cellhd.rows + 1) * sizeof(off_t));
  657. Rast__write_null_row_ptrs(fd, fcb->null_fd);
  658. }
  659. /* allocate null bitstream buffer for writing */
  660. fcb->null_bits = Rast__allocate_null_bits(fcb->cellhd.cols);
  661. return fd;
  662. }
  663. /*!
  664. \brief Set raster map floating-point data format.
  665. This controls the storage type for floating-point maps. It affects
  666. subsequent calls to G_open_fp_map_new(). The <i>type</i> must be
  667. one of FCELL_TYPE (float) or DCELL_TYPE (double). The use of this
  668. routine by applications is discouraged since its use would override
  669. user preferences.
  670. \param type raster data type
  671. \return void
  672. */
  673. void Rast_set_fp_type(RASTER_MAP_TYPE map_type)
  674. {
  675. Rast__init();
  676. switch (map_type) {
  677. case FCELL_TYPE:
  678. case DCELL_TYPE:
  679. R__.fp_type = map_type;
  680. break;
  681. default:
  682. G_fatal_error(_("Rast_set_fp_type(): can only be called with FCELL_TYPE or DCELL_TYPE"));
  683. break;
  684. }
  685. }
  686. /*!
  687. \brief Check if raster map is floating-point
  688. Returns true (1) if raster map <i>name</i> in <i>mapset</i>
  689. is a floating-point dataset; false(0) otherwise.
  690. \param name map name
  691. \param mapset mapset name
  692. \return 1 floating-point
  693. \return 0 int
  694. */
  695. int Rast_map_is_fp(const char *name, const char *mapset)
  696. {
  697. char path[GPATH_MAX];
  698. const char *xmapset;
  699. xmapset = G_find_raster2(name, mapset);
  700. if (!xmapset)
  701. G_fatal_error(_("Raster map <%s> not found"),
  702. G_fully_qualified_name(name, mapset));
  703. G_file_name(path, "fcell", name, xmapset);
  704. if (access(path, 0) == 0)
  705. return 1;
  706. G_file_name(path, "g3dcell", name, xmapset);
  707. if (access(path, 0) == 0)
  708. return 1;
  709. return 0;
  710. }
  711. /*!
  712. \brief Determine raster data type
  713. Determines if the raster map is floating point or integer. Returns
  714. DCELL_TYPE for double maps, FCELL_TYPE for float maps, CELL_TYPE for
  715. integer maps, -1 if error has occurred
  716. \param name map name
  717. \param mapset mapset where map <i>name</i> lives
  718. \return raster data type
  719. */
  720. RASTER_MAP_TYPE Rast_map_type(const char *name, const char *mapset)
  721. {
  722. char path[GPATH_MAX];
  723. const char *xmapset;
  724. xmapset = G_find_raster2(name, mapset);
  725. if (!xmapset) {
  726. if (mapset && *mapset)
  727. G_fatal_error(_("Raster map <%s> not found in mapset <%s>"),
  728. name, mapset);
  729. else
  730. G_fatal_error(_("Raster map <%s> not found"), name);
  731. }
  732. G_file_name(path, "fcell", name, xmapset);
  733. if (access(path, 0) == 0)
  734. return Rast__check_fp_type(name, xmapset);
  735. G_file_name(path, "g3dcell", name, xmapset);
  736. if (access(path, 0) == 0)
  737. return DCELL_TYPE;
  738. return CELL_TYPE;
  739. }
  740. /*!
  741. \brief Determine raster type from descriptor
  742. Determines if the raster map is floating point or integer. Returns
  743. DCELL_TYPE for double maps, FCELL_TYPE for float maps, CELL_TYPE for
  744. integer maps, -1 if error has occurred
  745. \param fd file descriptor
  746. \return raster data type
  747. */
  748. RASTER_MAP_TYPE Rast_get_map_type(int fd)
  749. {
  750. struct fileinfo *fcb = &R__.fileinfo[fd];
  751. return fcb->map_type;
  752. }
  753. /*!
  754. \brief Determines whether the floating points cell file has double or float type
  755. \param name map name
  756. \param mapset mapset where map <i>name</i> lives
  757. \return raster type (fcell, dcell)
  758. */
  759. RASTER_MAP_TYPE Rast__check_fp_type(const char *name, const char *mapset)
  760. {
  761. char path[GPATH_MAX];
  762. struct Key_Value *format_keys;
  763. const char *str, *str1;
  764. RASTER_MAP_TYPE map_type;
  765. const char *xmapset;
  766. xmapset = G_find_raster2(name, mapset);
  767. if (!xmapset)
  768. G_fatal_error(_("Raster map <%s> not found"),
  769. G_fully_qualified_name(name, mapset));
  770. G_file_name_misc(path, "cell_misc", FORMAT_FILE, name, xmapset);
  771. if (access(path, 0) != 0)
  772. G_fatal_error(_("Unable to find '%s'"), path);
  773. format_keys = G_read_key_value_file(path);
  774. if ((str = G_find_key_value("type", format_keys)) != NULL) {
  775. if (strcmp(str, "double") == 0)
  776. map_type = DCELL_TYPE;
  777. else if (strcmp(str, "float") == 0)
  778. map_type = FCELL_TYPE;
  779. else {
  780. G_free_key_value(format_keys);
  781. G_fatal_error(_("Invalid type: field '%s' in file '%s'"), str, path);
  782. }
  783. }
  784. else {
  785. G_free_key_value(format_keys);
  786. G_fatal_error(_("Missing type: field in file '%s'"), path);
  787. }
  788. if ((str1 = G_find_key_value("byte_order", format_keys)) != NULL) {
  789. if (strcmp(str1, "xdr") != 0)
  790. G_warning(_("Raster map <%s> is not xdr: byte_order: %s"),
  791. name, str);
  792. /* here read and translate byte order if not using xdr */
  793. }
  794. G_free_key_value(format_keys);
  795. return map_type;
  796. }
  797. /*!
  798. \brief Opens a new raster map
  799. Opens a new raster map of type <i>wr_type</i>
  800. See warnings and notes for Rast_open_new().
  801. Supported data types:
  802. - CELL_TYPE
  803. - FCELL_TYPE
  804. - DCELL_TYPE
  805. On CELL_TYPE calls Rast_open_new() otherwise Rast_open_fp_new().
  806. \param name map name
  807. \param wr_type raster data type
  808. \return nonnegative file descriptor (int)
  809. */
  810. int Rast_open_new(const char *name, RASTER_MAP_TYPE wr_type)
  811. {
  812. return open_raster_new(name, OPEN_NEW_COMPRESSED, wr_type);
  813. }
  814. /*!
  815. \brief Opens a new raster map (uncompressed)
  816. See Rast_open_new().
  817. \param name map name
  818. \param wr_type raster data type
  819. \return nonnegative file descriptor (int)
  820. */
  821. int Rast_open_new_uncompressed(const char *name, RASTER_MAP_TYPE wr_type)
  822. {
  823. return open_raster_new(name, OPEN_NEW_UNCOMPRESSED, wr_type);
  824. }
  825. /*!
  826. \brief Sets quant translation rules for raster map opened for
  827. reading.
  828. Returned by Rast_open_old(). After calling this function,
  829. Rast_get_c_row() and Rast_get_c_row() will use rules defined by q
  830. (instead of using rules defined in map's quant file) to convert floats to
  831. ints.
  832. \param fd file descriptor (cell file)
  833. \param q pointer to Quant structure
  834. \return void
  835. */
  836. void Rast_set_quant_rules(int fd, struct Quant *q)
  837. {
  838. struct fileinfo *fcb = &R__.fileinfo[fd];
  839. CELL cell;
  840. DCELL dcell;
  841. struct Quant_table *p;
  842. if (fcb->open_mode != OPEN_OLD)
  843. G_fatal_error(_("Rast_set_quant_rules() can be called only for "
  844. "raster maps opened for reading"));
  845. /* copy all info from q to fcb->quant) */
  846. Rast_quant_init(&fcb->quant);
  847. if (q->truncate_only) {
  848. Rast_quant_truncate(&fcb->quant);
  849. return;
  850. }
  851. for (p = &(q->table[q->nofRules - 1]); p >= q->table; p--)
  852. Rast_quant_add_rule(&fcb->quant, p->dLow, p->dHigh, p->cLow,
  853. p->cHigh);
  854. if (Rast_quant_get_neg_infinite_rule(q, &dcell, &cell) > 0)
  855. Rast_quant_set_neg_infinite_rule(&fcb->quant, dcell, cell);
  856. if (Rast_quant_get_pos_infinite_rule(q, &dcell, &cell) > 0)
  857. Rast_quant_set_pos_infinite_rule(&fcb->quant, dcell, cell);
  858. }