opencell.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181
  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. /* make sure window is set */
  135. G__init_window();
  136. /* Check for reclassification */
  137. reclass_flag = G_get_reclass(name, mapset, &reclass);
  138. switch (reclass_flag) {
  139. case 0:
  140. r_name = name;
  141. r_mapset = mapset;
  142. break;
  143. case 1:
  144. r_name = reclass.name;
  145. r_mapset = reclass.mapset;
  146. if (G_find_cell2(r_name, r_mapset) == NULL) {
  147. G_warning(_("Unable to open raster map <%s@%s> since it is a reclass "
  148. "of raster map <%s@%s> which does not exist"),
  149. name, mapset, r_name, r_mapset);
  150. return -1;
  151. }
  152. break;
  153. default: /* Error reading cellhd/reclass file */
  154. return -1;
  155. }
  156. /* read the cell header */
  157. if (G_get_cellhd(r_name, r_mapset, &cellhd) < 0)
  158. return -1;
  159. /* now check the type */
  160. MAP_TYPE = G_raster_map_type(r_name, r_mapset);
  161. if (MAP_TYPE < 0)
  162. return -1;
  163. if (MAP_TYPE == CELL_TYPE)
  164. /* set the number of bytes for CELL map */
  165. {
  166. CELL_nbytes = cellhd.format + 1;
  167. if (CELL_nbytes < 1) {
  168. G_warning(_("Raster map <%s@%s>: format field in header file invalid"),
  169. r_name, r_mapset);
  170. return -1;
  171. }
  172. }
  173. if (cellhd.proj != G__.window.proj) {
  174. G_warning(_("Raster map <%s@%s> is in different projection than current region. "
  175. "Found raster map <%s@%s>, should be <%s>."),
  176. name, mapset, name, G__projection_name(cellhd.proj),
  177. G__projection_name(G__.window.proj));
  178. return -1;
  179. }
  180. if (cellhd.zone != G__.window.zone) {
  181. G_warning(_("Raster map <%s@%s> is in different zone (%d) than current region (%d)"),
  182. name, mapset, cellhd.zone, G__.window.zone);
  183. return -1;
  184. }
  185. /* when map is int warn if too large cell size */
  186. if (MAP_TYPE == CELL_TYPE && (unsigned int) CELL_nbytes > sizeof(CELL)) {
  187. G_warning(_("Raster map <%s@%s>: bytes per cell (%d) too large"),
  188. name, mapset, CELL_nbytes);
  189. return -1;
  190. }
  191. /* record number of bytes per cell */
  192. if (MAP_TYPE == FCELL_TYPE) {
  193. strcpy(cell_dir, "fcell");
  194. INTERN_SIZE = sizeof(FCELL);
  195. MAP_NBYTES = XDR_FLOAT_NBYTES;
  196. }
  197. else if (MAP_TYPE == DCELL_TYPE) {
  198. strcpy(cell_dir, "fcell");
  199. INTERN_SIZE = sizeof(DCELL);
  200. MAP_NBYTES = XDR_DOUBLE_NBYTES;
  201. }
  202. else { /* integer */
  203. strcpy(cell_dir, "cell");
  204. INTERN_SIZE = sizeof(CELL);
  205. MAP_NBYTES = CELL_nbytes;
  206. }
  207. /* now actually open file for reading */
  208. fd = G_open_old(cell_dir, r_name, r_mapset);
  209. if (fd < 0)
  210. return -1;
  211. fcb = new_fileinfo(fd);
  212. fcb->map_type = MAP_TYPE;
  213. /* Save cell header */
  214. G_copy((char *)&fcb->cellhd, (char *)&cellhd, sizeof(cellhd));
  215. /* allocate null bitstream buffers for reading null rows */
  216. for (i = 0; i < NULL_ROWS_INMEM; i++)
  217. fcb->NULL_ROWS[i] = G__allocate_null_bits(G__.window.cols);
  218. fcb->null_work_buf = G__allocate_null_bits(fcb->cellhd.cols);
  219. /* initialize : no NULL rows in memory */
  220. fcb->min_null_row = (-1) * NULL_ROWS_INMEM;
  221. /* mark closed */
  222. fcb->open_mode = -1;
  223. /* save name and mapset */
  224. {
  225. char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
  226. if (G__name_is_fully_qualified(name, xname, xmapset))
  227. fcb->name = G_store(xname);
  228. else
  229. fcb->name = G_store(name);
  230. }
  231. fcb->mapset = G_store(mapset);
  232. /* mark no data row in memory */
  233. fcb->cur_row = -1;
  234. /* fcb->null_cur_row is not used for reading, only for writing */
  235. fcb->null_cur_row = -1;
  236. /* if reclass, copy reclass structure */
  237. if ((fcb->reclass_flag = reclass_flag))
  238. G_copy(&fcb->reclass, &reclass, sizeof(reclass));
  239. /* check for compressed data format, making initial reads if necessary */
  240. if (G__check_format(fd) < 0) {
  241. close(fd); /* warning issued by check_format() */
  242. return -1;
  243. }
  244. /* create the mapping from cell file to window */
  245. G__create_window_mapping(fd);
  246. /*
  247. * allocate the data buffer
  248. * number of bytes per cell is cellhd.format+1
  249. */
  250. /* for reading fcb->data is allocated to be fcb->cellhd.cols * fcb->nbytes
  251. (= XDR_FLOAT/DOUBLE_NBYTES) and G__.work_buf to be G__.window.cols *
  252. sizeof(CELL or DCELL or FCELL) */
  253. fcb->data = (unsigned char *)G_calloc(fcb->cellhd.cols, MAP_NBYTES);
  254. G__reallocate_work_buf(INTERN_SIZE);
  255. G__reallocate_mask_buf();
  256. G__reallocate_null_buf();
  257. G__reallocate_temp_buf();
  258. /* work_buf is used as intermediate buf for conversions */
  259. /*
  260. * allocate/enlarge the compressed data buffer needed by get_map_row()
  261. */
  262. allocate_compress_buf(fd);
  263. /* initialize/read in quant rules for float point maps */
  264. if (fcb->map_type != CELL_TYPE) {
  265. if (fcb->reclass_flag)
  266. G_read_quant(fcb->reclass.name, fcb->reclass.mapset,
  267. &(fcb->quant));
  268. else
  269. G_read_quant(fcb->name, fcb->mapset, &(fcb->quant));
  270. }
  271. /* now mark open for read: this must follow create_window_mapping() */
  272. fcb->open_mode = OPEN_OLD;
  273. fcb->io_error = 0;
  274. fcb->map_type = MAP_TYPE;
  275. fcb->nbytes = MAP_NBYTES;
  276. fcb->null_file_exists = -1;
  277. if (fcb->map_type != CELL_TYPE)
  278. xdrmem_create(&fcb->xdrstream, (caddr_t) fcb->data,
  279. (u_int) (fcb->nbytes * fcb->cellhd.cols), XDR_DECODE);
  280. return fd;
  281. }
  282. /*****************************************************************/
  283. static int WRITE_NBYTES = sizeof(CELL);
  284. /* bytes per cell for current map */
  285. static int NBYTES = sizeof(CELL);
  286. /* bytes per cell for writing integer maps */
  287. static RASTER_MAP_TYPE WRITE_MAP_TYPE = CELL_TYPE;
  288. /* a type of current map */
  289. static int COMPRESSION_TYPE = 0;
  290. #define FP_NBYTES G__.fp_nbytes
  291. /* bytes per cell for writing floating point maps */
  292. #define FP_TYPE G__.fp_type
  293. /* a type of floating maps to be open */
  294. static int FP_TYPE_SET = 0; /* wether or not the f.p. type was set explicitly
  295. by calling G_set_fp_type() */
  296. static char cell_dir[100];
  297. /*!
  298. \brief Opens a new cell file in a database (compressed)
  299. Opens a new cell file <i>name</i> in the current mapset for writing
  300. by G_put_raster_row().
  301. The file is created and filled with no data it is assumed that the
  302. new cell file is to conform to the current window.
  303. The file must be written sequentially. Use G_open_cell_new_random()
  304. for non sequential writes.
  305. Note: the open actually creates a temporary file G_close_cell() will
  306. move the temporary file to the cell file and write out the necessary
  307. support files (cellhd, cats, hist, etc.).
  308. Diagnostics: warning message printed if open fails
  309. Warning: calls to G_set_window() made after opening a new cell file
  310. may create confusion and should be avoided the new cell file will be
  311. created to conform to the window at the time of the open.
  312. \param name map name
  313. \return open file descriptor ( >= 0) if successful
  314. \return negative integer if error
  315. */
  316. int G_open_cell_new(const char *name)
  317. {
  318. WRITE_MAP_TYPE = CELL_TYPE;
  319. strcpy(cell_dir, "cell");
  320. /* bytes per cell for current map */
  321. WRITE_NBYTES = NBYTES;
  322. return G__open_raster_new(name, OPEN_NEW_COMPRESSED);
  323. }
  324. /*!
  325. \brief Opens a new cell file in a database (random mode)
  326. See also G_open_cell_new().
  327. Used for non sequential writes.
  328. \param name map name
  329. \return open file descriptor ( >= 0) if successful
  330. \return negative integer if error
  331. */
  332. int G_open_cell_new_random(const char *name)
  333. {
  334. WRITE_MAP_TYPE = CELL_TYPE;
  335. /* bytes per cell for current map */
  336. WRITE_NBYTES = NBYTES;
  337. strcpy(cell_dir, "cell");
  338. return G__open_raster_new(name, OPEN_NEW_RANDOM);
  339. }
  340. /*!
  341. \brief Opens a new cell file in a database (uncompressed)
  342. See also G_open_cell_new().
  343. \param name map name
  344. \return open file descriptor ( >= 0) if successful
  345. \return negative integer if error
  346. */
  347. int G_open_cell_new_uncompressed(const char *name)
  348. {
  349. WRITE_MAP_TYPE = CELL_TYPE; /* a type of current map */
  350. strcpy(cell_dir, "cell");
  351. /* bytes per cell for current map */
  352. WRITE_NBYTES = NBYTES;
  353. return G__open_raster_new(name, OPEN_NEW_UNCOMPRESSED);
  354. }
  355. /*!
  356. \brief Save histogram for newly create raster map (cell)
  357. If newly created cell files should have histograms, set flag=1
  358. otherwise set flag=0. Applies to subsequent opens.
  359. \param flag flag indicator
  360. \return 0
  361. */
  362. int G_want_histogram(int flag)
  363. {
  364. G__.want_histogram = flag;
  365. return 0;
  366. }
  367. /*!
  368. \brief Sets the format for subsequent opens on new integer cell files
  369. (uncompressed and random only).
  370. Warning: subsequent put_row calls will only write n+1 bytes
  371. per cell. If the data requires more, the cell file
  372. will be written incorrectly (but with n+1 bytes per cell)
  373. When writing float map: format is -1
  374. \param n format
  375. \return 0
  376. */
  377. int G_set_cell_format(int n)
  378. /* sets the format for integer raster map */
  379. {
  380. if (WRITE_MAP_TYPE == CELL_TYPE) {
  381. NBYTES = n + 1;
  382. if (NBYTES <= 0)
  383. NBYTES = 1;
  384. if ((unsigned int) NBYTES > sizeof(CELL))
  385. NBYTES = sizeof(CELL);
  386. }
  387. return 0;
  388. }
  389. /*!
  390. \brief Get cell value format
  391. \param v cell
  392. \return cell format
  393. */
  394. int G_cellvalue_format(CELL v)
  395. {
  396. unsigned int i;
  397. if (v >= 0)
  398. for (i = 0; i < sizeof(CELL); i++)
  399. if (!(v /= 256))
  400. return i;
  401. return sizeof(CELL) - 1;
  402. }
  403. /*!
  404. \brief Opens new fcell file in a database
  405. Opens a new floating-point map <i>name</i> in the current mapset for
  406. writing. The type of the file (i.e. either double or float) is
  407. determined and fixed at this point. The default is FCELL_TYPE. In
  408. order to change this default
  409. Use G_set_fp_type() where type is one of DCELL_TYPE or FCELL_TYPE.
  410. See warnings and notes for G_open_cell_new().
  411. \param name map name
  412. \return nonnegative file descriptor (int)
  413. \return -1 on error
  414. */
  415. int G_open_fp_cell_new(const char *name)
  416. {
  417. /* use current float. type for writing float point maps */
  418. /* if the FP type was NOT explicitly set by G_set_fp_type()
  419. use environment variable */
  420. if (!FP_TYPE_SET) {
  421. if (getenv("GRASS_FP_DOUBLE")) {
  422. FP_TYPE = DCELL_TYPE;
  423. FP_NBYTES = XDR_DOUBLE_NBYTES;
  424. }
  425. else {
  426. FP_TYPE = FCELL_TYPE;
  427. FP_NBYTES = XDR_FLOAT_NBYTES;
  428. }
  429. }
  430. WRITE_MAP_TYPE = FP_TYPE;
  431. WRITE_NBYTES = FP_NBYTES;
  432. strcpy(cell_dir, "fcell");
  433. return G__open_raster_new(name, OPEN_NEW_COMPRESSED);
  434. }
  435. /*!
  436. \brief Opens new fcell file in a database (uncompressed)
  437. See G_open_fp_cell_new() for details.
  438. \param name map name
  439. \return nonnegative file descriptor (int)
  440. \return -1 on error
  441. */
  442. int G_open_fp_cell_new_uncompressed(const char *name)
  443. {
  444. /* use current float. type for writing float point maps */
  445. if (!FP_TYPE_SET) {
  446. if (getenv("GRASS_FP_DOUBLE")) {
  447. FP_TYPE = DCELL_TYPE;
  448. FP_NBYTES = XDR_DOUBLE_NBYTES;
  449. }
  450. else {
  451. FP_TYPE = FCELL_TYPE;
  452. FP_NBYTES = XDR_FLOAT_NBYTES;
  453. }
  454. }
  455. WRITE_MAP_TYPE = FP_TYPE;
  456. WRITE_NBYTES = FP_NBYTES;
  457. strcpy(cell_dir, "fcell");
  458. return G__open_raster_new(name, OPEN_NEW_UNCOMPRESSED);
  459. }
  460. static int
  461. clean_check_raster_name(const char *inmap, char **outmap, char **outmapset)
  462. {
  463. /* Remove mapset part of name if exists. Also, if mapset
  464. * part exists, make sure it matches current mapset.
  465. */
  466. int status = 0;
  467. char *ptr;
  468. char *buf;
  469. buf = G_store(inmap);
  470. if ((ptr = strpbrk(buf, "@")) != NULL) {
  471. *ptr = '\0';
  472. ptr++;
  473. *outmapset = G_store(G_mapset());
  474. if ((status = strcmp(ptr, *outmapset))) {
  475. G_free(buf);
  476. G_free(*outmapset);
  477. }
  478. else {
  479. *outmap = G_store(buf);
  480. G_free(buf);
  481. }
  482. }
  483. else {
  484. *outmap = buf;
  485. *outmapset = G_store(G_mapset());
  486. }
  487. return status;
  488. }
  489. /* opens a f-cell or cell file depending on WRITE_MAP_TYPE */
  490. static int G__open_raster_new(const char *name, int open_mode)
  491. {
  492. char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
  493. struct fileinfo *fcb;
  494. int i, null_fd, fd;
  495. char *tempname;
  496. char *map;
  497. char *mapset;
  498. /* check for fully-qualfied name */
  499. if (G__name_is_fully_qualified(name, xname, xmapset)) {
  500. if (strcmp(xmapset, G_mapset()) != 0)
  501. G_fatal_error(_("Raster map <%s> is not in the current mapset (%s)"),
  502. name, G_mapset());
  503. name = xname;
  504. }
  505. /* check for legal grass name */
  506. if (G_legal_filename(name) < 0) {
  507. G_warning(_("<%s> is an illegal file name"),
  508. name);
  509. return -1;
  510. }
  511. if (clean_check_raster_name(name, &map, &mapset) != 0) {
  512. G_warning(_("<%s>: bad mapset"), name);
  513. return -1;
  514. }
  515. /* make sure window is set */
  516. G__init_window();
  517. /* open a tempfile name */
  518. tempname = G_tempfile();
  519. fd = creat(tempname, 0666);
  520. if (fd < 0) {
  521. G_warning(_("G__open_raster_new(): no temp files available"));
  522. G_free(tempname);
  523. G_free(map);
  524. G_free(mapset);
  525. return -1;
  526. }
  527. fcb = new_fileinfo(fd);
  528. /*
  529. * since we are bypassing the normal open logic
  530. * must create the cell element
  531. */
  532. G__make_mapset_element(cell_dir);
  533. /* mark closed */
  534. fcb->map_type = WRITE_MAP_TYPE;
  535. fcb->open_mode = -1;
  536. /* for writing fcb->data is allocated to be G__.window.cols *
  537. sizeof(CELL or DCELL or FCELL) and G__.work_buf to be G__.window.cols *
  538. fcb->nbytes (= XDR_FLOAT/DOUBLE_NBYTES) */
  539. fcb->data = (unsigned char *)G_calloc(G__.window.cols,
  540. G_raster_size(fcb->map_type));
  541. G__reallocate_null_buf();
  542. /* we need null buffer to automatically write embeded nulls in put_row */
  543. if (open_mode == OPEN_NEW_COMPRESSED && !COMPRESSION_TYPE)
  544. COMPRESSION_TYPE = getenv("GRASS_INT_ZLIB") ? 2 : 1;
  545. /*
  546. * copy current window into cell header
  547. * set format to cell/supercell
  548. * for compressed writing
  549. * allocate space to hold the row address array
  550. * allocate/enlarge both the compress_buf and the work_buf
  551. */
  552. G_copy((char *)&fcb->cellhd, (char *)&G__.window, sizeof(fcb->cellhd));
  553. if (open_mode == OPEN_NEW_COMPRESSED && fcb->map_type == CELL_TYPE) {
  554. fcb->row_ptr = G_calloc(fcb->cellhd.rows + 1, sizeof(off_t));
  555. G_zero(fcb->row_ptr, (fcb->cellhd.rows + 1) * sizeof(off_t));
  556. G__write_row_ptrs(fd);
  557. fcb->cellhd.compressed = COMPRESSION_TYPE;
  558. allocate_compress_buf(fd);
  559. fcb->nbytes = 1; /* to the minimum */
  560. G__reallocate_work_buf(sizeof(CELL));
  561. G__reallocate_mask_buf();
  562. G__reallocate_temp_buf();
  563. }
  564. else {
  565. fcb->nbytes = WRITE_NBYTES;
  566. if (open_mode == OPEN_NEW_COMPRESSED) {
  567. fcb->row_ptr = G_calloc(fcb->cellhd.rows + 1, sizeof(off_t));
  568. G_zero(fcb->row_ptr, (fcb->cellhd.rows + 1) * sizeof(off_t));
  569. G__write_row_ptrs(fd);
  570. fcb->cellhd.compressed = COMPRESSION_TYPE;
  571. }
  572. else
  573. fcb->cellhd.compressed = 0;
  574. G__reallocate_work_buf(fcb->nbytes);
  575. G__reallocate_mask_buf();
  576. G__reallocate_temp_buf();
  577. if (fcb->map_type != CELL_TYPE) {
  578. G_quant_init(&(fcb->quant));
  579. }
  580. if (open_mode == OPEN_NEW_RANDOM) {
  581. G_warning(_("Unable to write embedded null values "
  582. "for raster map open for random access"));
  583. if (fcb->map_type == CELL_TYPE)
  584. G_write_zeros(fd,
  585. (long)WRITE_NBYTES * fcb->cellhd.cols *
  586. fcb->cellhd.rows);
  587. else if (fcb->map_type == FCELL_TYPE) {
  588. if (G__random_f_initialize_0
  589. (fd, fcb->cellhd.rows, fcb->cellhd.cols) < 0)
  590. return -1;
  591. }
  592. else {
  593. if (G__random_d_initialize_0
  594. (fd, fcb->cellhd.rows, fcb->cellhd.cols) < 0)
  595. return -1;
  596. }
  597. }
  598. }
  599. /* save name and mapset, and tempfile name */
  600. fcb->name = map;
  601. fcb->mapset = mapset;
  602. fcb->temp_name = tempname;
  603. /* next row to be written (in order) is zero */
  604. fcb->cur_row = 0;
  605. /* open a null tempfile name */
  606. tempname = G_tempfile();
  607. null_fd = creat(tempname, 0666);
  608. if (null_fd < 0) {
  609. G_warning(_("G__open_raster_new(): no temp files available"));
  610. G_free(tempname);
  611. G_free(fcb->name);
  612. G_free(fcb->mapset);
  613. G_free(fcb->temp_name);
  614. close(fd);
  615. return -1;
  616. }
  617. fcb->null_temp_name = tempname;
  618. close(null_fd);
  619. /* next row to be written (in order) is zero */
  620. fcb->null_cur_row = 0;
  621. /* allocate null bitstream buffers for writing */
  622. for (i = 0; i < NULL_ROWS_INMEM; i++)
  623. fcb->NULL_ROWS[i] = G__allocate_null_bits(fcb->cellhd.cols);
  624. fcb->min_null_row = (-1) * NULL_ROWS_INMEM;
  625. fcb->null_work_buf = G__allocate_null_bits(fcb->cellhd.cols);
  626. /* init cell stats */
  627. /* now works only for int maps */
  628. if (fcb->map_type == CELL_TYPE)
  629. if ((fcb->want_histogram = G__.want_histogram))
  630. G_init_cell_stats(&fcb->statf);
  631. /* init range and if map is double/float init d/f_range */
  632. G_init_range(&fcb->range);
  633. if (fcb->map_type != CELL_TYPE)
  634. G_init_fp_range(&fcb->fp_range);
  635. /* mark file as open for write */
  636. fcb->open_mode = open_mode;
  637. fcb->io_error = 0;
  638. return fd;
  639. }
  640. /*!
  641. \brief allocate/enlarge the compressed data buffer needed by get_map_row()
  642. and put_map_row()
  643. Note: compressed format is repeat, value:
  644. Repeat takes 1 byte, value takes up to sizeof(CELL)
  645. plus 1 byte header for nbytes needed to store row
  646. */
  647. static int allocate_compress_buf(int fd)
  648. {
  649. struct fileinfo *fcb = &G__.fileinfo[fd];
  650. int n;
  651. n = fcb->cellhd.cols * (sizeof(CELL) + 1) + 1;
  652. if (fcb->cellhd.compressed && fcb->map_type == CELL_TYPE &&
  653. (n > G__.compressed_buf_size)) {
  654. if (G__.compressed_buf_size <= 0)
  655. G__.compressed_buf = (unsigned char *)G_malloc(n);
  656. else
  657. G__.compressed_buf =
  658. (unsigned char *)G_realloc((char *)G__.compressed_buf, n);
  659. G__.compressed_buf_size = n;
  660. }
  661. return 0;
  662. }
  663. /*!
  664. \brief Allocate/enlarge the work data buffer needed by get_map_row and put_map_row()
  665. \param bytes_per_cell number of bytes per cell
  666. \return 0
  667. */
  668. int G__reallocate_work_buf(int bytes_per_cell)
  669. {
  670. int n;
  671. n = G__.window.cols * (bytes_per_cell + 1) + 1;
  672. if (n > G__.work_buf_size) {
  673. if (G__.work_buf_size <= 0)
  674. G__.work_buf = (unsigned char *)G_malloc(n);
  675. else
  676. G__.work_buf =
  677. (unsigned char *)G_realloc((char *)G__.work_buf, n);
  678. G__.work_buf_size = n;
  679. }
  680. return 0;
  681. }
  682. /*!
  683. \brief Allocate/enlarge the null data buffer needed by get_map_row()
  684. and for conversion in put_row
  685. \return 0
  686. */
  687. int G__reallocate_null_buf(void)
  688. {
  689. int n;
  690. n = (G__.window.cols + 1) * sizeof(char);
  691. if (n > G__.null_buf_size) {
  692. if (G__.null_buf_size <= 0)
  693. G__.null_buf = (char *)G_malloc(n);
  694. else
  695. G__.null_buf = (char *)G_realloc(G__.null_buf, n);
  696. G__.null_buf_size = n;
  697. }
  698. return 0;
  699. }
  700. /*!
  701. \brief Allocate/enlarge the mask buffer needed by get_map_row()
  702. \return 0
  703. */
  704. int G__reallocate_mask_buf(void)
  705. {
  706. int n;
  707. n = (G__.window.cols + 1) * sizeof(CELL);
  708. if (n > G__.mask_buf_size) {
  709. if (G__.mask_buf_size <= 0)
  710. G__.mask_buf = (CELL *) G_malloc(n);
  711. else
  712. G__.mask_buf = (CELL *) G_realloc((char *)G__.mask_buf, n);
  713. G__.mask_buf_size = n;
  714. }
  715. return 0;
  716. }
  717. /*!
  718. \brief Allocate/enlarge the temporary buffer needed by G_get_raster_row[_nomask]
  719. \return 0
  720. */
  721. int G__reallocate_temp_buf(void)
  722. {
  723. int n;
  724. n = (G__.window.cols + 1) * sizeof(CELL);
  725. if (n > G__.temp_buf_size) {
  726. if (G__.temp_buf_size <= 0)
  727. G__.temp_buf = (CELL *) G_malloc(n);
  728. else
  729. G__.temp_buf = (CELL *) G_realloc((char *)G__.temp_buf, n);
  730. G__.temp_buf_size = n;
  731. }
  732. return 0;
  733. }
  734. /*!
  735. \brief Set raster map floating-point data format.
  736. This controls the storage type for floating-point maps. It affects
  737. subsequent calls to G_open_fp_map_new(). The <em>type</em>
  738. must be one of FCELL_TYPE (float) or DCELL_TYPE
  739. (double). The use of this routine by applications is discouraged
  740. since its use would override user preferences.
  741. \param type raster data type
  742. \return 1 on success
  743. \return -1 on error
  744. */
  745. int G_set_fp_type(RASTER_MAP_TYPE map_type)
  746. {
  747. FP_TYPE_SET = 1;
  748. if (map_type != FCELL_TYPE && map_type != DCELL_TYPE) {
  749. G_warning(_("G_set_fp_type(): can only be called with FCELL_TYPE or DCELL_TYPE"));
  750. return -1;
  751. }
  752. FP_TYPE = map_type;
  753. if (map_type == DCELL_TYPE)
  754. FP_NBYTES = XDR_DOUBLE_NBYTES;
  755. else
  756. FP_NBYTES = XDR_FLOAT_NBYTES;
  757. return 1;
  758. }
  759. #define FORMAT_FILE "f_format"
  760. /*!
  761. \brief Check if raster map is floating-point
  762. Returns true (1) if raster map <i>name</i> in <i>mapset</i>
  763. is a floating-point dataset; false(0) otherwise.
  764. \param name map name
  765. \param mapset mapset name
  766. \return 1 floating-point
  767. \return 0 int
  768. */
  769. int G_raster_map_is_fp(const char *name, const char *mapset)
  770. {
  771. char path[GPATH_MAX];
  772. if (G_find_cell2(name, mapset) == NULL) {
  773. G_warning(_("Unable to find <%s@%s>"), name, mapset);
  774. return -1;
  775. }
  776. G__file_name(path, "fcell", name, mapset);
  777. if (access(path, 0) == 0)
  778. return 1;
  779. G__file_name(path, "g3dcell", name, mapset);
  780. if (access(path, 0) == 0)
  781. return 1;
  782. return 0;
  783. }
  784. /*!
  785. \brief Determine raster data type
  786. Determines if the raster map is floating point or integer. Returns
  787. DCELL_TYPE for double maps, FCELL_TYPE for float maps, CELL_TYPE for
  788. integer maps, -1 if error has occured
  789. This function just calls G_raster_map_type()!
  790. \param name map name
  791. \param mapset mapset where map <i>name</i> lives
  792. \return raster data type
  793. */
  794. RASTER_MAP_TYPE G_raster_map_type(const char *name, const char *mapset)
  795. {
  796. return G_raster_map_type2(name, mapset);
  797. }
  798. /*!
  799. \brief Determine raster type (look but don't touch)
  800. See G_raster_map_type().
  801. \param name map name
  802. \param mapset mapset where map <i>name</i> lives
  803. \return raster data type
  804. */
  805. RASTER_MAP_TYPE G_raster_map_type2(const char *name, const char *mapset)
  806. {
  807. char path[GPATH_MAX];
  808. if (G_find_cell2(name, mapset) == NULL) {
  809. if (mapset) {
  810. G_warning(_("Raster map <%s> not found in mapset <%s>"), name, mapset);
  811. }
  812. else {
  813. G_warning(_("Raster map <%s> not found"), name);
  814. }
  815. return -1;
  816. }
  817. G__file_name(path, "fcell", name, mapset);
  818. if (access(path, 0) == 0)
  819. return G__check_fp_type(name, mapset);
  820. G__file_name(path, "g3dcell", name, mapset);
  821. if (access(path, 0) == 0)
  822. return DCELL_TYPE;
  823. return CELL_TYPE;
  824. }
  825. /*!
  826. \brief Determine raster type from descriptor
  827. Determines if the raster map is floating point or integer. Returns
  828. DCELL_TYPE for double maps, FCELL_TYPE for float maps, CELL_TYPE for
  829. integer maps, -1 if error has occured
  830. \param fd file descriptor
  831. \return raster data type
  832. */
  833. RASTER_MAP_TYPE G_get_raster_map_type(int fd)
  834. {
  835. struct fileinfo *fcb = &G__.fileinfo[fd];
  836. return fcb->map_type;
  837. }
  838. /*!
  839. \brief Determines whether the floating points cell file has double or float type
  840. \param name map name
  841. \param mapset mapset where map <i>name</i> lives
  842. \return raster type (fcell, dcell)
  843. */
  844. RASTER_MAP_TYPE G__check_fp_type(const char *name, const char *mapset)
  845. {
  846. char path[GPATH_MAX];
  847. struct Key_Value *format_keys;
  848. int in_stat;
  849. char *str, *str1;
  850. RASTER_MAP_TYPE map_type;
  851. G__file_name_misc(path, "cell_misc", FORMAT_FILE, name, mapset);
  852. if (access(path, 0) != 0) {
  853. G_warning(_("Unable to find '%s'"), path);
  854. return -1;
  855. }
  856. format_keys = G_read_key_value_file(path, &in_stat);
  857. if (in_stat != 0) {
  858. G_warning(_("Unable to open '%s'"), path);
  859. return -1;
  860. }
  861. if ((str = G_find_key_value("type", format_keys)) != NULL) {
  862. G_strip(str);
  863. if (strcmp(str, "double") == 0)
  864. map_type = DCELL_TYPE;
  865. else if (strcmp(str, "float") == 0)
  866. map_type = FCELL_TYPE;
  867. else {
  868. G_warning(_("Invalid type: field '%s' in file '%s'"),
  869. str, path);
  870. G_free_key_value(format_keys);
  871. return -1;
  872. }
  873. }
  874. else {
  875. G_free_key_value(format_keys);
  876. return -1;
  877. }
  878. if ((str1 = G_find_key_value("byte_order", format_keys)) != NULL) {
  879. G_strip(str1);
  880. if (strcmp(str1, "xdr") != 0)
  881. G_warning(_("Raster map <%s> is not xdr: byte_order: %s"),
  882. name, str);
  883. /* here read and translate byte order if not using xdr */
  884. }
  885. G_free_key_value(format_keys);
  886. return map_type;
  887. }
  888. /*!
  889. \brief Opens a new raster map
  890. Opens a new raster map of type <i>wr_type</i>
  891. See warnings and notes for G_open_cell_new().
  892. Supported data types:
  893. - CELL_TYPE
  894. - FCELL_TYPE
  895. - DCELL_TYPE
  896. On CELL_TYPE calls G_open_cell_new() otherwise G_open_fp_cell_new().
  897. \param name map name
  898. \param wr_type raster data type
  899. \return nonnegative file descriptor (int)
  900. \return -1 on error
  901. */
  902. int G_open_raster_new(const char *name, RASTER_MAP_TYPE wr_type)
  903. {
  904. if (wr_type == CELL_TYPE)
  905. return G_open_cell_new(name);
  906. G_set_fp_type(wr_type);
  907. return G_open_fp_cell_new(name);
  908. }
  909. /*!
  910. \brief Opens a new raster map (uncompressed)
  911. See G_open_raster_new().
  912. \param name map name
  913. \param wr_type raster data type
  914. \return nonnegative file descriptor (int)
  915. \return -1 on error
  916. */
  917. int G_open_raster_new_uncompressed(const char *name, RASTER_MAP_TYPE wr_type)
  918. {
  919. if (wr_type == CELL_TYPE)
  920. return G_open_cell_new_uncompressed(name);
  921. G_set_fp_type(wr_type);
  922. return G_open_fp_cell_new_uncompressed(name);
  923. }
  924. /*!
  925. \brief Sets quant translation rules for raster map opened for
  926. reading.
  927. Returned by G_open_cell_old(). After calling this function,
  928. G_get_c_raster_row() and G_get_map_row() will use rules defined by q
  929. (instead of using rules defined in map's quant file) to convert floats to
  930. ints.
  931. \param fd file descriptor (cell file)
  932. \param q pointer to Quant structure
  933. \return 0 success
  934. \return -1 failure
  935. */
  936. int G_set_quant_rules(int fd, struct Quant *q)
  937. {
  938. struct fileinfo *fcb = &G__.fileinfo[fd];
  939. CELL cell;
  940. DCELL dcell;
  941. struct Quant_table *p;
  942. if (fcb->open_mode != OPEN_OLD) {
  943. G_warning(_("G_set_quant_rules() can be called only for "
  944. "raster maps opened for reading"));
  945. return -1;
  946. }
  947. /* copy all info from q to fcb->quant) */
  948. G_quant_init(&fcb->quant);
  949. if (q->truncate_only) {
  950. G_quant_truncate(&fcb->quant);
  951. return 0;
  952. }
  953. for (p = &(q->table[q->nofRules - 1]); p >= q->table; p--)
  954. G_quant_add_rule(&fcb->quant, p->dLow, p->dHigh, p->cLow, p->cHigh);
  955. if (G_quant_get_neg_infinite_rule(q, &dcell, &cell) > 0)
  956. G_quant_set_neg_infinite_rule(&fcb->quant, dcell, cell);
  957. if (G_quant_get_pos_infinite_rule(q, &dcell, &cell) > 0)
  958. G_quant_set_pos_infinite_rule(&fcb->quant, dcell, cell);
  959. return 0;
  960. }