open.c 25 KB

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