opencell.c 25 KB

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