open.c 28 KB

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