| 1918 | } |
| 1919 | |
| 1920 | bool CalculateCoverage(const OGRGeometry *poGeom, |
| 1921 | const OGREnvelope &oSnappedGeomExtent, int nXSize, |
| 1922 | int nYSize, GByte *pabyCoverageBuf) const |
| 1923 | { |
| 1924 | #if GEOS_GRID_INTERSECTION_AVAILABLE |
| 1925 | if (m_options.pixels == GDALZonalStatsOptions::FRACTIONAL) |
| 1926 | { |
| 1927 | std::memset(pabyCoverageBuf, 0, |
| 1928 | static_cast<size_t>(nXSize) * nYSize * |
| 1929 | GDALGetDataTypeSizeBytes(GDT_Float32)); |
| 1930 | GEOSGeometry *poGeosGeom = |
| 1931 | poGeom->exportToGEOS(m_geosContext, true); |
| 1932 | if (!poGeosGeom) |
| 1933 | { |
| 1934 | CPLError(CE_Failure, CPLE_AppDefined, |
| 1935 | "Failed to convert geometry to GEOS."); |
| 1936 | return false; |
| 1937 | } |
| 1938 | |
| 1939 | const bool bRet = CPL_TO_BOOL(GEOSGridIntersectionFractions_r( |
| 1940 | m_geosContext, poGeosGeom, oSnappedGeomExtent.MinX, |
| 1941 | oSnappedGeomExtent.MinY, oSnappedGeomExtent.MaxX, |
| 1942 | oSnappedGeomExtent.MaxY, nXSize, nYSize, |
| 1943 | reinterpret_cast<float *>(pabyCoverageBuf))); |
| 1944 | if (!bRet) |
| 1945 | { |
| 1946 | CPLError(CE_Failure, CPLE_AppDefined, |
| 1947 | "Failed to calculate pixel intersection fractions."); |
| 1948 | } |
| 1949 | GEOSGeom_destroy_r(m_geosContext, poGeosGeom); |
| 1950 | |
| 1951 | return bRet; |
| 1952 | } |
| 1953 | else |
| 1954 | #endif |
| 1955 | { |
| 1956 | GDALGeoTransform oCoverageGT; |
| 1957 | oCoverageGT.xorig = oSnappedGeomExtent.MinX; |
| 1958 | oCoverageGT.xscale = m_srcGT.xscale; |
| 1959 | oCoverageGT.xrot = 0; |
| 1960 | |
| 1961 | oCoverageGT.yorig = m_srcGT.yscale < 0 ? oSnappedGeomExtent.MaxY |
| 1962 | : oSnappedGeomExtent.MinY; |
| 1963 | oCoverageGT.yscale = m_srcGT.yscale; |
| 1964 | oCoverageGT.yrot = 0; |
| 1965 | |
| 1966 | // Create a memory dataset that wraps the coverage buffer so that |
| 1967 | // we can invoke GDALRasterize |
| 1968 | std::unique_ptr<MEMDataset> poMemDS(MEMDataset::Create( |
| 1969 | "", nXSize, nYSize, 0, m_coverageDataType, nullptr)); |
| 1970 | poMemDS->SetGeoTransform(oCoverageGT); |
| 1971 | constexpr double dfBurnValue = 255.0; |
| 1972 | constexpr int nBand = 1; |
| 1973 | |
| 1974 | MEMRasterBand *poCoverageBand = |
| 1975 | new MEMRasterBand(poMemDS.get(), 1, pabyCoverageBuf, |
| 1976 | m_coverageDataType, 0, 0, false, nullptr); |
| 1977 | poMemDS->AddMEMBand(poCoverageBand); |
no test coverage detected