range.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  1. /*!
  2. * \file lib/raster/range.c
  3. *
  4. * \brief Raster Library - Raster range file management
  5. *
  6. * (C) 2001-2009 GRASS Development Team
  7. *
  8. * This program is free software under the GNU General Public License
  9. * (>=v2). Read the file COPYING that comes with GRASS for details.
  10. *
  11. * \author Original author CERL
  12. */
  13. #include <unistd.h>
  14. #include <grass/raster.h>
  15. #include <grass/glocale.h>
  16. #include "R.h"
  17. #define DEFAULT_CELL_MIN 1
  18. #define DEFAULT_CELL_MAX 255
  19. static void init_rstats(struct R_stats *);
  20. /*!
  21. \brief Remove floating-point range
  22. Note: For internal use only.
  23. \param name map name
  24. */
  25. void Rast__remove_fp_range(const char *name)
  26. {
  27. G_remove_misc("cell_misc", "f_range", name);
  28. }
  29. /*!
  30. * \brief Construct default range
  31. *
  32. * Sets the integer range to [1,255]
  33. *
  34. * \param[out] r pointer to Range structure which holds range info
  35. */
  36. void Rast_construct_default_range(struct Range *range)
  37. {
  38. Rast_update_range(DEFAULT_CELL_MIN, range);
  39. Rast_update_range(DEFAULT_CELL_MAX, range);
  40. }
  41. /*!
  42. * \brief Read floating-point range
  43. *
  44. * Read the floating point range file <i>drange</i>. This file is
  45. * written in binary using XDR format.
  46. *
  47. * An empty range file indicates that the min, max are undefined. This
  48. * is a valid case, and the result should be an initialized range
  49. * struct with no defined min/max. If the range file is missing and
  50. * the map is a floating-point map, this function will create a
  51. * default range by calling G_construct_default_range().
  52. *
  53. * \param name map name
  54. * \param mapset mapset name
  55. * \param drange pointer to FPRange structure which holds fp range
  56. *
  57. * \return 1 on success
  58. * \return 2 range is empty
  59. * \return -1 on error
  60. */
  61. int Rast_read_fp_range(const char *name, const char *mapset,
  62. struct FPRange *drange)
  63. {
  64. struct Range range;
  65. int fd;
  66. char xdr_buf[2][XDR_DOUBLE_NBYTES];
  67. DCELL dcell1, dcell2;
  68. Rast_init();
  69. Rast_init_fp_range(drange);
  70. if (Rast_map_type(name, mapset) == CELL_TYPE) {
  71. /* if map is integer
  72. read integer range and convert it to double */
  73. if (Rast_read_range(name, mapset, &range) >= 0) {
  74. /* if the integer range is empty */
  75. if (range.first_time)
  76. return 2;
  77. Rast_update_fp_range((DCELL) range.min, drange);
  78. Rast_update_fp_range((DCELL) range.max, drange);
  79. return 1;
  80. }
  81. return -1;
  82. }
  83. fd = -1;
  84. if (G_find_file2_misc("cell_misc", "f_range", name, mapset)) {
  85. fd = G_open_old_misc("cell_misc", "f_range", name, mapset);
  86. if (fd < 0) {
  87. G_warning(_("Unable to read fp range file for <%s>"),
  88. G_fully_qualified_name(name, mapset));
  89. return -1;
  90. }
  91. if (read(fd, xdr_buf, sizeof(xdr_buf)) != sizeof(xdr_buf)) {
  92. /* if the f_range file exists, but empty file, meaning Nulls */
  93. close(fd);
  94. G_debug(1, "Empty fp range file meaning Nulls for <%s>",
  95. G_fully_qualified_name(name, mapset));
  96. return 2;
  97. }
  98. G_xdr_get_double(&dcell1, xdr_buf[0]);
  99. G_xdr_get_double(&dcell2, xdr_buf[1]);
  100. Rast_update_fp_range(dcell1, drange);
  101. Rast_update_fp_range(dcell2, drange);
  102. close(fd);
  103. }
  104. return 1;
  105. }
  106. /*!
  107. * \brief Read raster range (CELL)
  108. *
  109. * This routine reads the range information for the raster map
  110. * <i>name</i> in <i>mapset</i> into the <i>range</i> structure.
  111. *
  112. * A diagnostic message is printed and -1 is returned if there is an error
  113. * reading the range file. Otherwise, 0 is returned.
  114. *
  115. * Old range file (those with 4 numbers) should treat zeros in this
  116. * file as NULL-values. New range files (those with just 2 numbers)
  117. * should treat these numbers as real data (zeros are real data in
  118. * this case). An empty range file indicates that the min, max are
  119. * undefined. This is a valid case, and the result should be an
  120. * initialized range struct with no defined min/max. If the range file
  121. * is missing and the map is a floating-point map, this function will
  122. * create a default range by calling G_construct_default_range().
  123. *
  124. * \param name map name
  125. * \param mapset mapset name
  126. * \param[out] range pointer to Range structure which holds range info
  127. *
  128. * \return -1 on error
  129. * \return 1 on success
  130. * \return 2 if range is empty
  131. * \return 3 if raster map is floating-point, get range from quant rules
  132. */
  133. int Rast_read_range(const char *name, const char *mapset, struct Range *range)
  134. {
  135. FILE *fd;
  136. CELL x[4];
  137. char buf[200];
  138. int n, count;
  139. struct Quant quant;
  140. struct FPRange drange;
  141. Rast_init_range(range);
  142. fd = NULL;
  143. /* if map is not integer, read quant rules, and get limits */
  144. if (Rast_map_type(name, mapset) != CELL_TYPE) {
  145. DCELL dmin, dmax;
  146. if (Rast_read_quant(name, mapset, &quant) < 0) {
  147. G_warning(_("Unable to read quant rules for raster map <%s>"),
  148. G_fully_qualified_name(name, mapset));
  149. return -1;
  150. }
  151. if (Rast_quant_is_truncate(&quant) || Rast_quant_is_round(&quant)) {
  152. if (Rast_read_fp_range(name, mapset, &drange) >= 0) {
  153. Rast_get_fp_range_min_max(&drange, &dmin, &dmax);
  154. if (Rast_quant_is_truncate(&quant)) {
  155. x[0] = (CELL) dmin;
  156. x[1] = (CELL) dmax;
  157. }
  158. else { /* round */
  159. if (dmin > 0)
  160. x[0] = (CELL) (dmin + .5);
  161. else
  162. x[0] = (CELL) (dmin - .5);
  163. if (dmax > 0)
  164. x[1] = (CELL) (dmax + .5);
  165. else
  166. x[1] = (CELL) (dmax - .5);
  167. }
  168. }
  169. else
  170. return -1;
  171. }
  172. else
  173. Rast_quant_get_limits(&quant, &dmin, &dmax, &x[0], &x[1]);
  174. Rast_update_range(x[0], range);
  175. Rast_update_range(x[1], range);
  176. return 3;
  177. }
  178. if (G_find_file2_misc("cell_misc", "range", name, mapset)) {
  179. fd = G_fopen_old_misc("cell_misc", "range", name, mapset);
  180. if (!fd) {
  181. G_warning(_("Unable to read range file for <%s>"),
  182. G_fully_qualified_name(name, mapset));
  183. return -1;
  184. }
  185. /* if range file exists but empty */
  186. if (!fgets(buf, sizeof buf, fd)) {
  187. if (fd)
  188. fclose(fd);
  189. return 2;
  190. }
  191. x[0] = x[1] = x[2] = x[3] = 0;
  192. count = sscanf(buf, "%d%d%d%d", &x[0], &x[1], &x[2], &x[3]);
  193. /* if wrong format */
  194. if (count <= 0) {
  195. if (fd)
  196. fclose(fd);
  197. G_warning(_("Unable to read range file for <%s>"),
  198. G_fully_qualified_name(name, mapset));
  199. return -1;
  200. }
  201. for (n = 0; n < count; n++) {
  202. /* if count==4, the range file is old (4.1) and 0's in it
  203. have to be ignored */
  204. if (count < 4 || x[n])
  205. Rast_update_range((CELL) x[n], range);
  206. }
  207. fclose(fd);
  208. }
  209. return 1;
  210. }
  211. /*!
  212. * \brief Read raster stats
  213. *
  214. * Read the stats file <i>stats</i>. This file is
  215. * written in binary using XDR format.
  216. *
  217. * An empty stats file indicates that all cells are NULL. This
  218. * is a valid case, and the result should be an initialized rstats
  219. * struct with no defined stats. If the stats file is missing
  220. * this function will create a default stats with count = 0.
  221. *
  222. * \param name map name
  223. * \param mapset mapset name
  224. * \param rstats pointer to R_stats structure which holds raster stats
  225. *
  226. * \return 1 on success
  227. * \return 2 stats is empty
  228. * \return -1 on error or stats file does not exist
  229. */
  230. int Rast_read_rstats(const char *name, const char *mapset,
  231. struct R_stats *rstats)
  232. {
  233. int fd;
  234. char xdr_buf[2][XDR_DOUBLE_NBYTES];
  235. DCELL dcell1, dcell2;
  236. unsigned char cc[8];
  237. char nbytes;
  238. int i;
  239. off_t count;
  240. Rast_init();
  241. init_rstats(rstats);
  242. fd = -1;
  243. if (!G_find_file2_misc("cell_misc", "stats", name, mapset)) {
  244. G_debug(1, "Stats file does not exist");
  245. return -1;
  246. }
  247. fd = G_open_old_misc("cell_misc", "stats", name, mapset);
  248. if (fd < 0) {
  249. G_warning(_("Unable to read stats file for <%s>"),
  250. G_fully_qualified_name(name, mapset));
  251. return -1;
  252. }
  253. if (read(fd, xdr_buf, sizeof(xdr_buf)) != sizeof(xdr_buf)) {
  254. /* if the stats file exists, but empty file, meaning Nulls */
  255. close(fd);
  256. G_debug(1, "Empty stats file meaning Nulls for <%s>",
  257. G_fully_qualified_name(name, mapset));
  258. return 2;
  259. }
  260. G_xdr_get_double(&dcell1, xdr_buf[0]);
  261. G_xdr_get_double(&dcell2, xdr_buf[1]);
  262. rstats->sum = dcell1;
  263. rstats->sumsq = dcell2;
  264. /* count; see cell_values_int() in get_row.c */
  265. nbytes = 1;
  266. if (read(fd, &nbytes, 1) != 1) {
  267. /* if the stats file exists, but empty file, meaning Nulls */
  268. close(fd);
  269. G_debug(1, "Unable to read byte count in stats file for <%s>",
  270. G_fully_qualified_name(name, mapset));
  271. return -1;
  272. }
  273. if (nbytes < 1 || nbytes > sizeof(off_t)) {
  274. close(fd);
  275. G_debug(1, "Invalid byte count in stats file for <%s>",
  276. G_fully_qualified_name(name, mapset));
  277. return -1;
  278. }
  279. if (read(fd, cc, nbytes) != nbytes) {
  280. /* incorrect number of bytes for count */
  281. close(fd);
  282. G_debug(1, "Unable to read count in stats file for <%s>",
  283. G_fully_qualified_name(name, mapset));
  284. return -1;
  285. }
  286. count = 0;
  287. /* copy byte by byte */
  288. for (i = nbytes - 1; i >= 0; i--) {
  289. count = (count << 8);
  290. count = count + cc[i];
  291. }
  292. rstats->count = count;
  293. close(fd);
  294. return 1;
  295. }
  296. /*!
  297. * \brief Write raster range file
  298. *
  299. * This routine writes the range information for the raster map
  300. * <i>name</i> in the current mapset from the <i>range</i> structure.
  301. * A diagnostic message is printed and -1 is returned if there is an
  302. * error writing the range file. Otherwise, 0 is returned.
  303. *
  304. * This routine only writes 2 numbers (min,max) to the range
  305. * file, instead of the 4 (pmin,pmax,nmin,nmax) previously written.
  306. * If there is no defined min,max, an empty file is written.
  307. *
  308. * \param name map name
  309. * \param range pointer to Range structure which holds range info
  310. */
  311. void Rast_write_range(const char *name, const struct Range *range)
  312. {
  313. FILE *fp;
  314. Rast_write_rstats(name, &(range->rstats));
  315. if (Rast_map_type(name, G_mapset()) != CELL_TYPE) {
  316. G_remove_misc("cell_misc", "range", name); /* remove the old file with this name */
  317. G_fatal_error(_("Unable to write range file for <%s>"), name);
  318. }
  319. fp = G_fopen_new_misc("cell_misc", "range", name);
  320. if (!fp) {
  321. G_remove_misc("cell_misc", "range", name); /* remove the old file with this name */
  322. G_fatal_error(_("Unable to write range file for <%s>"), name);
  323. }
  324. /* if range has been updated */
  325. if (!range->first_time)
  326. fprintf(fp, "%ld %ld\n", (long)range->min, (long)range->max);
  327. fclose(fp);
  328. }
  329. /*!
  330. * \brief Write raster range file (floating-point)
  331. *
  332. * Write the floating point range file <tt>f_range</tt>. This file is
  333. * written in binary using XDR format. If there is no defined min/max
  334. * in <em>range</em>, an empty <tt>f_range</tt> file is created.
  335. *
  336. * \param name map name
  337. * \param range pointer to FPRange which holds fp range info
  338. */
  339. void Rast_write_fp_range(const char *name, const struct FPRange *range)
  340. {
  341. int fd;
  342. char xdr_buf[2][XDR_DOUBLE_NBYTES];
  343. Rast_init();
  344. Rast_write_rstats(name, &(range->rstats));
  345. fd = G_open_new_misc("cell_misc", "f_range", name);
  346. if (fd < 0) {
  347. G_remove_misc("cell_misc", "f_range", name);
  348. G_fatal_error(_("Unable to write range file for <%s>"), name);
  349. }
  350. /* if range hasn't been updated, write empty file meaning Nulls */
  351. if (range->first_time) {
  352. close(fd);
  353. return;
  354. }
  355. G_xdr_put_double(xdr_buf[0], &range->min);
  356. G_xdr_put_double(xdr_buf[1], &range->max);
  357. if (write(fd, xdr_buf, sizeof(xdr_buf)) != sizeof(xdr_buf)) {
  358. G_remove_misc("cell_misc", "f_range", name);
  359. G_fatal_error(_("Unable to write range file for <%s>"), name);
  360. }
  361. close(fd);
  362. }
  363. /*!
  364. * \brief Write raster stats file
  365. *
  366. * Write the stats file <tt>stats</tt>. This file is
  367. * written in binary using XDR format. If the count is < 1
  368. * in <em>rstats</em>, an empty <tt>stats</tt> file is created.
  369. *
  370. * \param name map name
  371. * \param rstats pointer to R_stats which holds stats info
  372. */
  373. void Rast_write_rstats(const char *name, const struct R_stats *rstats)
  374. {
  375. int fd;
  376. char xdr_buf[2][XDR_DOUBLE_NBYTES];
  377. unsigned char cc[8];
  378. char nbytes;
  379. int i;
  380. off_t count;
  381. Rast_init();
  382. fd = G_open_new_misc("cell_misc", "stats", name);
  383. if (fd < 0) {
  384. G_remove_misc("cell_misc", "stats", name);
  385. G_fatal_error(_("Unable to write stats file for <%s>"), name);
  386. }
  387. /* if count is zero, write empty file meaning Nulls */
  388. if (rstats->count < 1) {
  389. close(fd);
  390. return;
  391. }
  392. G_xdr_put_double(xdr_buf[0], &rstats->sum);
  393. G_xdr_put_double(xdr_buf[1], &rstats->sumsq);
  394. if (write(fd, xdr_buf, sizeof(xdr_buf)) != sizeof(xdr_buf)) {
  395. G_remove_misc("cell_misc", "stats", name);
  396. G_fatal_error(_("Unable to write stats file for <%s>"), name);
  397. }
  398. /* count; see convert_int() in put_row.c */
  399. count = rstats->count;
  400. nbytes = 0;
  401. /* copy byte by byte */
  402. for (i = 0; i < sizeof(off_t); i++) {
  403. cc[i] = count & 0xff;
  404. count >>= 8;
  405. if (cc[i])
  406. nbytes = i;
  407. }
  408. nbytes++;
  409. /* number of bytes needed for count */
  410. if (write(fd, &nbytes, 1) != 1) {
  411. G_remove_misc("cell_misc", "stats", name);
  412. G_fatal_error(_("Unable to write stats file for <%s>"), name);
  413. }
  414. if (write(fd, cc, nbytes) != nbytes) {
  415. G_remove_misc("cell_misc", "stats", name);
  416. G_fatal_error(_("Unable to write stats file for <%s>"), name);
  417. }
  418. close(fd);
  419. }
  420. /*!
  421. * \brief Update range structure (CELL)
  422. *
  423. * Compares the <i>cat</i> value with the minimum and maximum values
  424. * in the <i>range</i> structure, modifying the range if <i>cat</i>
  425. * extends the range.
  426. *
  427. * NULL-values must be detected and ignored.
  428. *
  429. * \param cat raster value
  430. * \param range pointer to Range structure which holds range info
  431. */
  432. void Rast_update_range(CELL cat, struct Range *range)
  433. {
  434. if (!Rast_is_c_null_value(&cat)) {
  435. if (range->first_time) {
  436. range->first_time = 0;
  437. range->min = cat;
  438. range->max = cat;
  439. return;
  440. }
  441. if (cat < range->min)
  442. range->min = cat;
  443. if (cat > range->max)
  444. range->max = cat;
  445. }
  446. }
  447. /*!
  448. * \brief Update range structure (floating-point)
  449. *
  450. * Compares the <i>cat</i> value with the minimum and maximum values
  451. * in the <i>range</i> structure, modifying the range if <i>cat</i>
  452. * extends the range.
  453. *
  454. * NULL-values must be detected and ignored.
  455. *
  456. * \param val raster value
  457. * \param range pointer to Range structure which holds range info
  458. */
  459. void Rast_update_fp_range(DCELL val, struct FPRange *range)
  460. {
  461. if (!Rast_is_d_null_value(&val)) {
  462. if (range->first_time) {
  463. range->first_time = 0;
  464. range->min = val;
  465. range->max = val;
  466. return;
  467. }
  468. if (val < range->min)
  469. range->min = val;
  470. if (val > range->max)
  471. range->max = val;
  472. }
  473. }
  474. /*!
  475. * \brief Update range structure based on raster row (CELL)
  476. *
  477. * This routine updates the <i>range</i> data just like
  478. * Rast_update_range(), but for <i>n</i> values from the <i>cell</i>
  479. * array.
  480. *
  481. * \param cell raster values
  482. * \param n number of values
  483. * \param range pointer to Range structure which holds range info
  484. */
  485. void Rast_row_update_range(const CELL * cell, int n, struct Range *range)
  486. {
  487. Rast__row_update_range(cell, n, range, 0);
  488. }
  489. /*!
  490. * \brief Update range structure based on raster row
  491. *
  492. * Note: for internal use only.
  493. *
  494. * \param cell raster values
  495. * \param n number of values
  496. * \param range pointer to Range structure which holds range info
  497. * \param ignore_zeros ignore zeros
  498. */
  499. void Rast__row_update_range(const CELL * cell, int n,
  500. struct Range *range, int ignore_zeros)
  501. {
  502. CELL cat;
  503. while (n-- > 0) {
  504. cat = *cell++;
  505. if (Rast_is_c_null_value(&cat) || (ignore_zeros && !cat))
  506. continue;
  507. if (range->first_time) {
  508. range->first_time = 0;
  509. range->min = cat;
  510. range->max = cat;
  511. range->rstats.sum = cat;
  512. range->rstats.sumsq = (DCELL) cat * cat;
  513. range->rstats.count = 1;
  514. continue;
  515. }
  516. if (cat < range->min)
  517. range->min = cat;
  518. if (cat > range->max)
  519. range->max = cat;
  520. range->rstats.sum += cat;
  521. range->rstats.sumsq += (DCELL) cat * cat;
  522. range->rstats.count += 1;
  523. }
  524. }
  525. /*!
  526. * \brief Update range structure based on raster row (floating-point)
  527. *
  528. * This routine updates the <i>range</i> data just like
  529. * Rast_update_range(), but for <i>n</i> values from the <i>cell</i>
  530. * array.
  531. *
  532. * \param cell raster values
  533. * \param n number of values
  534. * \param range pointer to Range structure which holds range info
  535. * \param data_type raster type (CELL, FCELL, DCELL)
  536. */
  537. void Rast_row_update_fp_range(const void *rast, int n,
  538. struct FPRange *range,
  539. RASTER_MAP_TYPE data_type)
  540. {
  541. size_t size = Rast_cell_size(data_type);
  542. DCELL val = 0.0;
  543. while (n-- > 0) {
  544. switch (data_type) {
  545. case CELL_TYPE:
  546. val = (DCELL) * ((CELL *) rast);
  547. break;
  548. case FCELL_TYPE:
  549. val = (DCELL) * ((FCELL *) rast);
  550. break;
  551. case DCELL_TYPE:
  552. val = *((DCELL *) rast);
  553. break;
  554. }
  555. if (Rast_is_null_value(rast, data_type)) {
  556. rast = G_incr_void_ptr(rast, size);
  557. continue;
  558. }
  559. if (range->first_time) {
  560. range->first_time = 0;
  561. range->min = val;
  562. range->max = val;
  563. range->rstats.sum = val;
  564. range->rstats.sumsq = val * val;
  565. range->rstats.count = 1;
  566. }
  567. else {
  568. if (val < range->min)
  569. range->min = val;
  570. if (val > range->max)
  571. range->max = val;
  572. range->rstats.sum += val;
  573. range->rstats.sumsq += val * val;
  574. range->rstats.count += 1;
  575. }
  576. rast = G_incr_void_ptr(rast, size);
  577. }
  578. }
  579. /*!
  580. * \brief Initialize range structure
  581. *
  582. * Initializes the <i>range</i> structure for updates by
  583. * Rast_update_range() and Rast_row_update_range().
  584. *
  585. * Must set a flag in the range structure that indicates that no
  586. * min/max have been defined - probably a <tt>"first"</tt> boolean
  587. * flag.
  588. *
  589. * \param range pointer to Range structure which holds range info
  590. */
  591. void Rast_init_range(struct Range *range)
  592. {
  593. Rast_set_c_null_value(&(range->min), 1);
  594. Rast_set_c_null_value(&(range->max), 1);
  595. init_rstats(&range->rstats);
  596. range->first_time = 1;
  597. }
  598. /*!
  599. * \brief Get range min and max
  600. *
  601. * The minimum and maximum CELL values are extracted from the
  602. * <i>range</i> structure.
  603. *
  604. * If the range structure has no defined min/max (first!=0) there will
  605. * not be a valid range. In this case the min and max returned must be
  606. * the NULL-value.
  607. *
  608. * \param range pointer to Range structure which holds range info
  609. * \param[out] min minimum value
  610. * \param[out] max maximum value
  611. */
  612. void Rast_get_range_min_max(const struct Range *range, CELL * min, CELL * max)
  613. {
  614. if (range->first_time) {
  615. Rast_set_c_null_value(min, 1);
  616. Rast_set_c_null_value(max, 1);
  617. }
  618. else {
  619. if (Rast_is_c_null_value(&(range->min)))
  620. Rast_set_c_null_value(min, 1);
  621. else
  622. *min = range->min;
  623. if (Rast_is_c_null_value(&(range->max)))
  624. Rast_set_c_null_value(max, 1);
  625. else
  626. *max = range->max;
  627. }
  628. }
  629. /*!
  630. * \brief Initialize fp range
  631. *
  632. * Must set a flag in the range structure that indicates that no
  633. * min/max have been defined - probably a <tt>"first"</tt> boolean
  634. * flag.
  635. *
  636. * \param range pointer to FPRange which holds fp range info
  637. */
  638. void Rast_init_fp_range(struct FPRange *range)
  639. {
  640. Rast_set_d_null_value(&(range->min), 1);
  641. Rast_set_d_null_value(&(range->max), 1);
  642. init_rstats(&range->rstats);
  643. range->first_time = 1;
  644. }
  645. /*!
  646. * \brief Get minimum and maximum value from fp range
  647. *
  648. * Extract the min/max from the range structure <i>range</i>. If the
  649. * range structure has no defined min/max (first!=0) there will not be
  650. * a valid range. In this case the min and max returned must be the
  651. * NULL-value.
  652. *
  653. * \param range pointer to FPRange which holds fp range info
  654. * \param[out] min minimum value
  655. * \param[out] max maximum value
  656. */
  657. void Rast_get_fp_range_min_max(const struct FPRange *range,
  658. DCELL * min, DCELL * max)
  659. {
  660. if (range->first_time) {
  661. Rast_set_d_null_value(min, 1);
  662. Rast_set_d_null_value(max, 1);
  663. }
  664. else {
  665. if (Rast_is_d_null_value(&(range->min)))
  666. Rast_set_d_null_value(min, 1);
  667. else
  668. *min = range->min;
  669. if (Rast_is_d_null_value(&(range->max)))
  670. Rast_set_d_null_value(max, 1);
  671. else
  672. *max = range->max;
  673. }
  674. }
  675. static void init_rstats(struct R_stats *rstats)
  676. {
  677. Rast_set_d_null_value(&(rstats->sum), 1);
  678. Rast_set_d_null_value(&(rstats->sumsq), 1);
  679. rstats->count = 0;
  680. }