Count the number of times provided value(s) occur in the band.
1613 {
1616 uint8_t *
data = NULL;
1617 double nodata = 0;
1618
1619 int scale = 0;
1620 int doround = 0;
1621 double tmpd = 0;
1622 uint32_t i = 0;
1623
1626 int rtn;
1627 double pxlval;
1628 int isnodata = 0;
1629 double rpxlval;
1630 uint32_t total = 0;
1631 uint32_t vcnts_count = 0;
1632 uint32_t new_valuecount = 0;
1633
1634#if POSTGIS_DEBUG_LEVEL > 0
1635 clock_t start, stop;
1636 double elapsed = 0;
1637#endif
1638
1640#if POSTGIS_DEBUG_LEVEL > 0
1641 start = clock();
1642#endif
1643
1644 assert(NULL != band);
1645 assert(NULL != rtn_count);
1646
1648 if (data == NULL) {
1649 rterror(
"rt_band_get_summary_stats: Cannot get band data");
1650 return NULL;
1651 }
1652
1653 pixtype =
band->pixtype;
1654
1658 }
1659 else {
1660 exclude_nodata_value = 0;
1662 }
1663
1664 RASTER_DEBUGF(3,
"exclude_nodata_value = %d", exclude_nodata_value);
1665
1666
1667 if (roundto < 0 ||
FLT_EQ(roundto, 0.0)) {
1668 roundto = 0;
1669 scale = 0;
1670 }
1671
1672 else if (roundto < 1) {
1673 switch (pixtype) {
1674
1684 roundto = 0;
1685 break;
1686
1690 for (scale = 0; scale <= 20; scale++)
1691 {
1692 tmpd = roundto * pow(10, scale);
1693 if (
FLT_EQ((tmpd - ((
int)tmpd)), 0.0))
1694 break;
1695 }
1696 break;
1698 break;
1699 }
1700 }
1701
1702 else {
1703 for (scale = 0; scale >= -20; scale--) {
1704 tmpd = roundto * pow(10, scale);
1705 if (tmpd < 1 ||
FLT_EQ(tmpd, 1.0)) {
1706 if (scale == 0) doround = 1;
1707 break;
1708 }
1709 }
1710 }
1711
1712 if (scale != 0 || doround)
1713 doround = 1;
1714 else
1715 doround = 0;
1718
1719
1720 if (search_values_count > 0 && NULL != search_values) {
1722 if (NULL == vcnts) {
1723 rterror(
"rt_band_get_count_of_values: Could not allocate memory for value counts");
1724 *rtn_count = 0;
1725 return NULL;
1726 }
1727
1728 for (i = 0; i < search_values_count; i++) {
1731 if (!doround)
1732 vcnts[i].
value = search_values[i];
1733 else
1734 vcnts[i].
value =
ROUND(search_values[i], scale);
1735 }
1736 vcnts_count = i;
1737 }
1738 else
1739 search_values_count = 0;
1740 RASTER_DEBUGF(3,
"search_values_count = %d", search_values_count);
1741
1742
1744 if (exclude_nodata_value) {
1745 rtwarn(
"All pixels of band have the NODATA value");
1746 return NULL;
1747 }
1748 else {
1749 if (search_values_count > 0) {
1750
1751 for (i = 0; i < search_values_count; i++) {
1752 if (!doround)
1753 tmpd = nodata;
1754 else
1755 tmpd =
ROUND(nodata, scale);
1756
1757 if (
FLT_NEQ(tmpd, vcnts[i].value))
1758 continue;
1759
1761 if (NULL != rtn_total) *rtn_total = vcnts[i].
count;
1763 }
1764
1765 *rtn_count = vcnts_count;
1766 }
1767
1768 else {
1770 if (NULL == vcnts) {
1771 rterror(
"rt_band_get_count_of_values: Could not allocate memory for value counts");
1772 *rtn_count = 0;
1773 return NULL;
1774 }
1775
1776 vcnts->
value = nodata;
1778 if (NULL != rtn_total) *rtn_total = vcnts[i].
count;
1780
1781 *rtn_count = 1;
1782 }
1783
1784 return vcnts;
1785 }
1786 }
1787
1788 for (x = 0;
x <
band->width;
x++) {
1789 for (y = 0;
y <
band->height;
y++) {
1791
1792
1794 continue;
1795
1796 if (!exclude_nodata_value || (exclude_nodata_value && !isnodata)) {
1797 total++;
1798 if (doround) {
1799 rpxlval =
ROUND(pxlval, scale);
1800 }
1801 else
1802 rpxlval = pxlval;
1803 RASTER_DEBUGF(5,
"(pxlval, rpxlval) => (%0.6f, %0.6f)", pxlval, rpxlval);
1804
1805 new_valuecount = 1;
1806
1807 for (i = 0; i < vcnts_count; i++) {
1808
1809 if (
FLT_EQ(vcnts[i].value, rpxlval)) {
1811 new_valuecount = 0;
1812 RASTER_DEBUGF(5,
"(value, count) => (%0.6f, %d)", vcnts[i].value, vcnts[i].count);
1813 break;
1814 }
1815 }
1816
1817
1818
1819
1820
1821
1822 if (!new_valuecount || search_values_count > 0) continue;
1823
1824
1826 if (NULL == vcnts) {
1827 rterror(
"rt_band_get_count_of_values: Could not allocate memory for value counts");
1828 *rtn_count = 0;
1829 return NULL;
1830 }
1831
1832 vcnts[vcnts_count].
value = rpxlval;
1833 vcnts[vcnts_count].
count = 1;
1834 vcnts[vcnts_count].
percent = 0;
1835 RASTER_DEBUGF(5,
"(value, count) => (%0.6f, %d)", vcnts[vcnts_count].value, vcnts[vcnts_count].count);
1836 vcnts_count++;
1837 }
1838 }
1839 }
1840
1841#if POSTGIS_DEBUG_LEVEL > 0
1842 stop = clock();
1843 elapsed = ((double) (stop - start)) / CLOCKS_PER_SEC;
1845#endif
1846
1847 for (i = 0; i < vcnts_count; i++) {
1848 vcnts[i].
percent = (double) vcnts[i].count / total;
1849 RASTER_DEBUGF(5,
"(value, count) => (%0.6f, %d)", vcnts[i].value, vcnts[i].count);
1850 }
1851
1853 if (NULL != rtn_total) *rtn_total = total;
1854 *rtn_count = vcnts_count;
1855 return vcnts;
1856}
void rterror(const char *fmt,...) __attribute__((format(printf
Wrappers used for reporting errors and info.
void * rtalloc(size_t size)
Wrappers used for managing memory.
#define RASTER_DEBUG(level, msg)
#define RASTER_DEBUGF(level, msg,...)
void void void rtwarn(const char *fmt,...) __attribute__((format(printf
int rt_band_get_hasnodata_flag(rt_band band)
Get hasnodata flag value.
void * rt_band_get_data(rt_band band)
Get pointer to raster band data.
rt_errorstate rt_band_get_pixel(rt_band band, int x, int y, double *value, int *nodata)
Get pixel value.
int rt_band_get_isnodata_flag(rt_band band)
Get isnodata flag value.
void * rtrealloc(void *mem, size_t size)
struct rt_valuecount_t * rt_valuecount
rt_errorstate rt_band_get_nodata(rt_band band, double *nodata)
Get NODATA value.