open.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947
  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. return fd;
  289. }
  290. /*!
  291. \brief Opens a new cell file in a database (compressed)
  292. Opens a new cell file <i>name</i> in the current mapset for writing
  293. by Rast_put_row().
  294. The file is created and filled with no data it is assumed that the
  295. new cell file is to conform to the current window.
  296. The file must be written sequentially. Use Rast_open_new_random()
  297. for non sequential writes.
  298. Note: the open actually creates a temporary file Rast_close() will
  299. move the temporary file to the cell file and write out the necessary
  300. support files (cellhd, cats, hist, etc.).
  301. Diagnostics: warning message printed if open fails
  302. Warning: calls to Rast_set_window() made after opening a new cell file
  303. may create confusion and should be avoided the new cell file will be
  304. created to conform to the window at the time of the open.
  305. \param name map name
  306. \return open file descriptor ( >= 0) if successful
  307. \return negative integer if error
  308. */
  309. int Rast_open_c_new(const char *name)
  310. {
  311. return open_raster_new(name, OPEN_NEW_COMPRESSED, CELL_TYPE);
  312. }
  313. /*!
  314. \brief Opens a new cell file in a database (uncompressed)
  315. See also Rast_open_new().
  316. \param name map name
  317. \return open file descriptor ( >= 0) if successful
  318. \return negative integer if error
  319. */
  320. int Rast_open_c_new_uncompressed(const char *name)
  321. {
  322. return open_raster_new(name, OPEN_NEW_UNCOMPRESSED, CELL_TYPE);
  323. }
  324. /*!
  325. \brief Save histogram for newly create raster map (cell)
  326. If newly created cell files should have histograms, set flag=1
  327. otherwise set flag=0. Applies to subsequent opens.
  328. \param flag flag indicator
  329. */
  330. void Rast_want_histogram(int flag)
  331. {
  332. R__.want_histogram = flag;
  333. }
  334. /*!
  335. \brief Sets the format for subsequent opens on new integer cell files
  336. (uncompressed and random only).
  337. Warning: subsequent put_row calls will only write n+1 bytes per
  338. cell. If the data requires more, the cell file will be written
  339. incorrectly (but with n+1 bytes per cell)
  340. When writing float map: format is -1
  341. \param n format
  342. */
  343. void Rast_set_cell_format(int n)
  344. /* sets the format for integer raster map */
  345. {
  346. R__.nbytes = n + 1;
  347. if (R__.nbytes <= 0)
  348. R__.nbytes = 1;
  349. if (R__.nbytes > sizeof(CELL))
  350. R__.nbytes = sizeof(CELL);
  351. }
  352. /*!
  353. \brief Get cell value format
  354. \param v cell
  355. \return cell format
  356. */
  357. int Rast_get_cell_format(CELL v)
  358. {
  359. unsigned int i;
  360. if (v >= 0)
  361. for (i = 0; i < sizeof(CELL); i++)
  362. if (!(v /= 256))
  363. return i;
  364. return sizeof(CELL) - 1;
  365. }
  366. /*!
  367. \brief Opens new fcell file in a database
  368. Opens a new floating-point map <i>name</i> in the current mapset for
  369. writing. The type of the file (i.e. either double or float) is
  370. determined and fixed at this point. The default is FCELL_TYPE. In
  371. order to change this default
  372. Use Rast_set_fp_type() where type is one of DCELL_TYPE or FCELL_TYPE.
  373. See warnings and notes for Rast_open_new().
  374. \param name map name
  375. \return nonnegative file descriptor (int)
  376. */
  377. int Rast_open_fp_new(const char *name)
  378. {
  379. return open_raster_new(name, OPEN_NEW_COMPRESSED, R__.fp_type);
  380. }
  381. /*!
  382. \brief Opens new fcell file in a database (uncompressed)
  383. See Rast_open_fp_new() for details.
  384. \param name map name
  385. \return nonnegative file descriptor (int)
  386. */
  387. int Rast_open_fp_new_uncompressed(const char *name)
  388. {
  389. return open_raster_new(name, OPEN_NEW_UNCOMPRESSED, R__.fp_type);
  390. }
  391. #ifdef HAVE_GDAL
  392. static int open_raster_new_gdal(char *map, char *mapset,
  393. RASTER_MAP_TYPE map_type)
  394. {
  395. int fd;
  396. struct fileinfo *fcb;
  397. fd = new_fileinfo();
  398. fcb = &R__.fileinfo[fd];
  399. fcb->data_fd = -1;
  400. /* mark closed */
  401. fcb->map_type = map_type;
  402. fcb->open_mode = -1;
  403. fcb->gdal = Rast_create_gdal_link(map, map_type);
  404. if (!fcb->gdal)
  405. G_fatal_error(_("Unable to create GDAL link"));
  406. fcb->cellhd = R__.wr_window;
  407. fcb->cellhd.compressed = 0;
  408. fcb->nbytes = Rast_cell_size(fcb->map_type);
  409. /* for writing fcb->data is allocated to be R__.wr_window.cols *
  410. sizeof(CELL or DCELL or FCELL) */
  411. fcb->data = G_calloc(R__.wr_window.cols, fcb->nbytes);
  412. fcb->name = map;
  413. fcb->mapset = mapset;
  414. fcb->cur_row = 0;
  415. fcb->row_ptr = NULL;
  416. fcb->temp_name = NULL;
  417. fcb->null_temp_name = NULL;
  418. fcb->null_cur_row = 0;
  419. fcb->null_bits = NULL;
  420. fcb->null_fd = -1;
  421. if (fcb->map_type != CELL_TYPE)
  422. Rast_quant_init(&(fcb->quant));
  423. /* init cell stats */
  424. /* now works only for int maps */
  425. if (fcb->map_type == CELL_TYPE)
  426. if ((fcb->want_histogram = R__.want_histogram))
  427. Rast_init_cell_stats(&fcb->statf);
  428. /* init range and if map is double/float init d/f_range */
  429. Rast_init_range(&fcb->range);
  430. if (fcb->map_type != CELL_TYPE)
  431. Rast_init_fp_range(&fcb->fp_range);
  432. /* mark file as open for write */
  433. fcb->open_mode = OPEN_NEW_UNCOMPRESSED;
  434. fcb->io_error = 0;
  435. return fd;
  436. }
  437. #endif /* HAVE_GDAL */
  438. static int open_raster_new(const char *name, int open_mode,
  439. RASTER_MAP_TYPE map_type)
  440. {
  441. char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
  442. struct fileinfo *fcb;
  443. int fd, cell_fd;
  444. char *tempname;
  445. char *map;
  446. char *mapset;
  447. const char *cell_dir;
  448. int nbytes;
  449. Rast__init();
  450. switch (map_type) {
  451. case CELL_TYPE:
  452. cell_dir = "cell";
  453. nbytes = R__.nbytes;
  454. break;
  455. case FCELL_TYPE:
  456. nbytes = XDR_FLOAT_NBYTES;
  457. cell_dir = "fcell";
  458. break;
  459. case DCELL_TYPE:
  460. nbytes = XDR_DOUBLE_NBYTES;
  461. cell_dir = "fcell";
  462. break;
  463. default:
  464. G_fatal_error(_("Invalid map type <%d>"), map_type);
  465. break;
  466. }
  467. if (G_unqualified_name(name, G_mapset(), xname, xmapset) < 0)
  468. G_fatal_error(_("Raster map <%s> is not in the current mapset (%s)"),
  469. name, G_mapset());
  470. map = G_store(xname);
  471. mapset = G_store(xmapset);
  472. /* check for legal grass name */
  473. if (G_legal_filename(map) < 0)
  474. G_fatal_error(_("<%s> is an illegal file name"), map);
  475. #ifdef HAVE_GDAL
  476. if (G_find_file2("", "GDAL", G_mapset()))
  477. return open_raster_new_gdal(map, mapset, map_type);
  478. #endif
  479. /* open a tempfile name */
  480. tempname = G_tempfile();
  481. cell_fd = creat(tempname, 0666);
  482. if (cell_fd < 0) {
  483. G_free(mapset);
  484. G_free(tempname);
  485. G_free(map);
  486. G_fatal_error(_("No temp files available"));
  487. }
  488. fd = new_fileinfo();
  489. fcb = &R__.fileinfo[fd];
  490. fcb->data_fd = cell_fd;
  491. /*
  492. * since we are bypassing the normal open logic
  493. * must create the cell element
  494. */
  495. G__make_mapset_element(cell_dir);
  496. /* mark closed */
  497. fcb->map_type = map_type;
  498. fcb->open_mode = -1;
  499. fcb->gdal = NULL;
  500. /* for writing fcb->data is allocated to be R__.wr_window.cols *
  501. sizeof(CELL or DCELL or FCELL) */
  502. fcb->data = (unsigned char *)G_calloc(R__.wr_window.cols,
  503. Rast_cell_size(fcb->map_type));
  504. /*
  505. * copy current window into cell header
  506. * set format to cell/supercell
  507. * for compressed writing
  508. * allocate space to hold the row address array
  509. */
  510. fcb->cellhd = R__.wr_window;
  511. if (open_mode == OPEN_NEW_COMPRESSED && fcb->map_type == CELL_TYPE) {
  512. fcb->row_ptr = G_calloc(fcb->cellhd.rows + 1, sizeof(off_t));
  513. G_zero(fcb->row_ptr, (fcb->cellhd.rows + 1) * sizeof(off_t));
  514. Rast__write_row_ptrs(fd);
  515. fcb->cellhd.compressed = R__.compression_type;
  516. fcb->nbytes = 1; /* to the minimum */
  517. }
  518. else {
  519. fcb->nbytes = nbytes;
  520. if (open_mode == OPEN_NEW_COMPRESSED) {
  521. fcb->row_ptr = G_calloc(fcb->cellhd.rows + 1, sizeof(off_t));
  522. G_zero(fcb->row_ptr, (fcb->cellhd.rows + 1) * sizeof(off_t));
  523. Rast__write_row_ptrs(fd);
  524. fcb->cellhd.compressed = R__.compression_type;
  525. }
  526. else
  527. fcb->cellhd.compressed = 0;
  528. if (fcb->map_type != CELL_TYPE) {
  529. Rast_quant_init(&(fcb->quant));
  530. }
  531. }
  532. /* save name and mapset, and tempfile name */
  533. fcb->name = map;
  534. fcb->mapset = mapset;
  535. fcb->temp_name = tempname;
  536. /* next row to be written (in order) is zero */
  537. fcb->cur_row = 0;
  538. /* open a null tempfile name */
  539. tempname = G_tempfile();
  540. fcb->null_fd = creat(tempname, 0666);
  541. if (fcb->null_fd < 0) {
  542. G_free(tempname);
  543. G_free(fcb->name);
  544. G_free(fcb->mapset);
  545. G_free(fcb->temp_name);
  546. close(cell_fd);
  547. G_fatal_error(_("no temp files available"));
  548. }
  549. fcb->null_temp_name = tempname;
  550. /* next row to be written (in order) is zero */
  551. fcb->null_cur_row = 0;
  552. /* allocate null bitstream buffer for writing */
  553. fcb->null_bits = Rast__allocate_null_bits(fcb->cellhd.cols);
  554. /* init cell stats */
  555. /* now works only for int maps */
  556. if (fcb->map_type == CELL_TYPE)
  557. if ((fcb->want_histogram = R__.want_histogram))
  558. Rast_init_cell_stats(&fcb->statf);
  559. /* init range and if map is double/float init d/f_range */
  560. Rast_init_range(&fcb->range);
  561. if (fcb->map_type != CELL_TYPE)
  562. Rast_init_fp_range(&fcb->fp_range);
  563. /* mark file as open for write */
  564. fcb->open_mode = open_mode;
  565. fcb->io_error = 0;
  566. return fd;
  567. }
  568. /*!
  569. \brief Set raster map floating-point data format.
  570. This controls the storage type for floating-point maps. It affects
  571. subsequent calls to G_open_fp_map_new(). The <i>type</i> must be
  572. one of FCELL_TYPE (float) or DCELL_TYPE (double). The use of this
  573. routine by applications is discouraged since its use would override
  574. user preferences.
  575. \param type raster data type
  576. \return void
  577. */
  578. void Rast_set_fp_type(RASTER_MAP_TYPE map_type)
  579. {
  580. Rast__init();
  581. switch (map_type) {
  582. case FCELL_TYPE:
  583. case DCELL_TYPE:
  584. R__.fp_type = map_type;
  585. break;
  586. default:
  587. G_fatal_error(_("Rast_set_fp_type(): can only be called with FCELL_TYPE or DCELL_TYPE"));
  588. break;
  589. }
  590. }
  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 Rast_map_is_fp(const char *name, const char *mapset)
  601. {
  602. char path[GPATH_MAX];
  603. const char *xmapset;
  604. xmapset = G_find_raster2(name, mapset);
  605. if (!xmapset)
  606. G_fatal_error(_("Raster map <%s> not found"),
  607. G_fully_qualified_name(name, mapset));
  608. G_file_name(path, "fcell", name, xmapset);
  609. if (access(path, 0) == 0)
  610. return 1;
  611. G_file_name(path, "g3dcell", name, xmapset);
  612. if (access(path, 0) == 0)
  613. return 1;
  614. return 0;
  615. }
  616. /*!
  617. \brief Determine raster data type
  618. Determines if the raster map is floating point or integer. Returns
  619. DCELL_TYPE for double maps, FCELL_TYPE for float maps, CELL_TYPE for
  620. integer maps, -1 if error has occurred
  621. \param name map name
  622. \param mapset mapset where map <i>name</i> lives
  623. \return raster data type
  624. */
  625. RASTER_MAP_TYPE Rast_map_type(const char *name, const char *mapset)
  626. {
  627. char path[GPATH_MAX];
  628. const char *xmapset;
  629. xmapset = G_find_raster2(name, mapset);
  630. if (!xmapset) {
  631. if (mapset && *mapset)
  632. G_fatal_error(_("Raster map <%s> not found in mapset <%s>"),
  633. name, mapset);
  634. else
  635. G_fatal_error(_("Raster map <%s> not found"), name);
  636. }
  637. G_file_name(path, "fcell", name, xmapset);
  638. if (access(path, 0) == 0)
  639. return Rast__check_fp_type(name, xmapset);
  640. G_file_name(path, "g3dcell", name, xmapset);
  641. if (access(path, 0) == 0)
  642. return DCELL_TYPE;
  643. return CELL_TYPE;
  644. }
  645. /*!
  646. \brief Determine raster type from descriptor
  647. Determines if the raster map is floating point or integer. Returns
  648. DCELL_TYPE for double maps, FCELL_TYPE for float maps, CELL_TYPE for
  649. integer maps, -1 if error has occurred
  650. \param fd file descriptor
  651. \return raster data type
  652. */
  653. RASTER_MAP_TYPE Rast_get_map_type(int fd)
  654. {
  655. struct fileinfo *fcb = &R__.fileinfo[fd];
  656. return fcb->map_type;
  657. }
  658. /*!
  659. \brief Determines whether the floating points cell file has double or float type
  660. \param name map name
  661. \param mapset mapset where map <i>name</i> lives
  662. \return raster type (fcell, dcell)
  663. */
  664. RASTER_MAP_TYPE Rast__check_fp_type(const char *name, const char *mapset)
  665. {
  666. char path[GPATH_MAX];
  667. struct Key_Value *format_keys;
  668. const char *str, *str1;
  669. RASTER_MAP_TYPE map_type;
  670. const char *xmapset;
  671. xmapset = G_find_raster2(name, mapset);
  672. if (!xmapset)
  673. G_fatal_error(_("Raster map <%s> not found"),
  674. G_fully_qualified_name(name, mapset));
  675. G_file_name_misc(path, "cell_misc", FORMAT_FILE, name, xmapset);
  676. if (access(path, 0) != 0)
  677. G_fatal_error(_("Unable to find '%s'"), path);
  678. format_keys = G_read_key_value_file(path);
  679. if ((str = G_find_key_value("type", format_keys)) != NULL) {
  680. if (strcmp(str, "double") == 0)
  681. map_type = DCELL_TYPE;
  682. else if (strcmp(str, "float") == 0)
  683. map_type = FCELL_TYPE;
  684. else {
  685. G_free_key_value(format_keys);
  686. G_fatal_error(_("Invalid type: field '%s' in file '%s'"), str, path);
  687. }
  688. }
  689. else {
  690. G_free_key_value(format_keys);
  691. G_fatal_error(_("Missing type: field in file '%s'"), path);
  692. }
  693. if ((str1 = G_find_key_value("byte_order", format_keys)) != NULL) {
  694. if (strcmp(str1, "xdr") != 0)
  695. G_warning(_("Raster map <%s> is not xdr: byte_order: %s"),
  696. name, str);
  697. /* here read and translate byte order if not using xdr */
  698. }
  699. G_free_key_value(format_keys);
  700. return map_type;
  701. }
  702. /*!
  703. \brief Opens a new raster map
  704. Opens a new raster map of type <i>wr_type</i>
  705. See warnings and notes for Rast_open_new().
  706. Supported data types:
  707. - CELL_TYPE
  708. - FCELL_TYPE
  709. - DCELL_TYPE
  710. On CELL_TYPE calls Rast_open_new() otherwise Rast_open_fp_new().
  711. \param name map name
  712. \param wr_type raster data type
  713. \return nonnegative file descriptor (int)
  714. */
  715. int Rast_open_new(const char *name, RASTER_MAP_TYPE wr_type)
  716. {
  717. return open_raster_new(name, OPEN_NEW_COMPRESSED, wr_type);
  718. }
  719. /*!
  720. \brief Opens a new raster map (uncompressed)
  721. See Rast_open_new().
  722. \param name map name
  723. \param wr_type raster data type
  724. \return nonnegative file descriptor (int)
  725. */
  726. int Rast_open_new_uncompressed(const char *name, RASTER_MAP_TYPE wr_type)
  727. {
  728. return open_raster_new(name, OPEN_NEW_UNCOMPRESSED, wr_type);
  729. }
  730. /*!
  731. \brief Sets quant translation rules for raster map opened for
  732. reading.
  733. Returned by Rast_open_old(). After calling this function,
  734. Rast_get_c_row() and Rast_get_c_row() will use rules defined by q
  735. (instead of using rules defined in map's quant file) to convert floats to
  736. ints.
  737. \param fd file descriptor (cell file)
  738. \param q pointer to Quant structure
  739. \return void
  740. */
  741. void Rast_set_quant_rules(int fd, struct Quant *q)
  742. {
  743. struct fileinfo *fcb = &R__.fileinfo[fd];
  744. CELL cell;
  745. DCELL dcell;
  746. struct Quant_table *p;
  747. if (fcb->open_mode != OPEN_OLD)
  748. G_fatal_error(_("Rast_set_quant_rules() can be called only for "
  749. "raster maps opened for reading"));
  750. /* copy all info from q to fcb->quant) */
  751. Rast_quant_init(&fcb->quant);
  752. if (q->truncate_only) {
  753. Rast_quant_truncate(&fcb->quant);
  754. return;
  755. }
  756. for (p = &(q->table[q->nofRules - 1]); p >= q->table; p--)
  757. Rast_quant_add_rule(&fcb->quant, p->dLow, p->dHigh, p->cLow,
  758. p->cHigh);
  759. if (Rast_quant_get_neg_infinite_rule(q, &dcell, &cell) > 0)
  760. Rast_quant_set_neg_infinite_rule(&fcb->quant, dcell, cell);
  761. if (Rast_quant_get_pos_infinite_rule(q, &dcell, &cell) > 0)
  762. Rast_quant_set_pos_infinite_rule(&fcb->quant, dcell, cell);
  763. }