opencell.c 29 KB

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