open.c 28 KB

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