1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
|
/******************************************************************************
*
* Project: GDAL
* Purpose: Raster Polygon Enumerator
* Author: Frank Warmerdam, warmerdam@pobox.com
*
******************************************************************************
* Copyright (c) 2008, Frank Warmerdam
* Copyright (c) 2015, Even Rouault <even.rouault at spatialys.com>
*
* SPDX-License-Identifier: MIT
****************************************************************************/
#include "cpl_port.h"
#include "gdal_alg_priv.h"
#include <cstddef>
#include <limits>
#include "cpl_conv.h"
#include "cpl_error.h"
/*! @cond Doxygen_Suppress */
/************************************************************************/
/* GDALRasterPolygonEnumeratorT() */
/************************************************************************/
template <class DataType, class EqualityTest>
GDALRasterPolygonEnumeratorT<
DataType, EqualityTest>::GDALRasterPolygonEnumeratorT(int nConnectednessIn)
: nConnectedness(nConnectednessIn)
{
CPLAssert(nConnectedness == 4 || nConnectedness == 8);
}
/************************************************************************/
/* ~GDALRasterPolygonEnumeratorT() */
/************************************************************************/
template <class DataType, class EqualityTest>
GDALRasterPolygonEnumeratorT<DataType,
EqualityTest>::~GDALRasterPolygonEnumeratorT()
{
Clear();
}
/************************************************************************/
/* Clear() */
/************************************************************************/
template <class DataType, class EqualityTest>
void GDALRasterPolygonEnumeratorT<DataType, EqualityTest>::Clear()
{
CPLFree(panPolyIdMap);
CPLFree(panPolyValue);
panPolyIdMap = nullptr;
panPolyValue = nullptr;
nNextPolygonId = 0;
nPolyAlloc = 0;
}
/************************************************************************/
/* MergePolygon() */
/* */
/* Update the polygon map to indicate the merger of two polygons. */
/************************************************************************/
template <class DataType, class EqualityTest>
void GDALRasterPolygonEnumeratorT<DataType, EqualityTest>::MergePolygon(
int nSrcId, int nDstIdInit)
{
// Figure out the final dest id.
int nDstIdFinal = nDstIdInit;
while (panPolyIdMap[nDstIdFinal] != nDstIdFinal)
nDstIdFinal = panPolyIdMap[nDstIdFinal];
// Map the whole intermediate chain to it.
int nDstIdCur = nDstIdInit;
while (panPolyIdMap[nDstIdCur] != nDstIdCur)
{
int nNextDstId = panPolyIdMap[nDstIdCur];
panPolyIdMap[nDstIdCur] = nDstIdFinal;
nDstIdCur = nNextDstId;
}
// And map the whole source chain to it too (can be done in one pass).
while (panPolyIdMap[nSrcId] != nSrcId)
{
int nNextSrcId = panPolyIdMap[nSrcId];
panPolyIdMap[nSrcId] = nDstIdFinal;
nSrcId = nNextSrcId;
}
panPolyIdMap[nSrcId] = nDstIdFinal;
}
/************************************************************************/
/* NewPolygon() */
/* */
/* Allocate a new polygon id, and reallocate the polygon maps */
/* if needed. */
/************************************************************************/
template <class DataType, class EqualityTest>
int GDALRasterPolygonEnumeratorT<DataType, EqualityTest>::NewPolygon(
DataType nValue)
{
if (nNextPolygonId == std::numeric_limits<int>::max())
{
CPLError(CE_Failure, CPLE_AppDefined,
"GDALRasterPolygonEnumeratorT::NewPolygon(): maximum number "
"of polygons reached");
return -1;
}
if (nNextPolygonId >= nPolyAlloc)
{
int nPolyAllocNew;
if (nPolyAlloc < (std::numeric_limits<int>::max() - 20) / 2)
nPolyAllocNew = nPolyAlloc * 2 + 20;
else
nPolyAllocNew = std::numeric_limits<int>::max();
#if SIZEOF_VOIDP == 4
if (nPolyAllocNew >
static_cast<int>(std::numeric_limits<size_t>::max() /
sizeof(GInt32)) ||
nPolyAllocNew >
static_cast<int>(std::numeric_limits<size_t>::max() /
sizeof(DataType)))
{
CPLError(CE_Failure, CPLE_OutOfMemory,
"GDALRasterPolygonEnumeratorT::NewPolygon(): too many "
"polygons");
return -1;
}
#endif
auto panPolyIdMapNew = static_cast<GInt32 *>(
VSI_REALLOC_VERBOSE(panPolyIdMap, nPolyAllocNew * sizeof(GInt32)));
auto panPolyValueNew = static_cast<DataType *>(VSI_REALLOC_VERBOSE(
panPolyValue, nPolyAllocNew * sizeof(DataType)));
if (panPolyIdMapNew == nullptr || panPolyValueNew == nullptr)
{
VSIFree(panPolyIdMapNew);
VSIFree(panPolyValueNew);
return -1;
}
panPolyIdMap = panPolyIdMapNew;
panPolyValue = panPolyValueNew;
nPolyAlloc = nPolyAllocNew;
}
const int nPolyId = nNextPolygonId;
panPolyIdMap[nPolyId] = nPolyId;
panPolyValue[nPolyId] = nValue;
nNextPolygonId++;
return nPolyId;
}
/************************************************************************/
/* CompleteMerges() */
/* */
/* Make a pass through the maps, ensuring every polygon id */
/* points to the final id it should use, not an intermediate */
/* value. */
/************************************************************************/
template <class DataType, class EqualityTest>
void GDALRasterPolygonEnumeratorT<DataType, EqualityTest>::CompleteMerges()
{
int nFinalPolyCount = 0;
for (int iPoly = 0; iPoly < nNextPolygonId; iPoly++)
{
// Figure out the final id.
int nId = panPolyIdMap[iPoly];
while (nId != panPolyIdMap[nId])
{
nId = panPolyIdMap[nId];
}
// Then map the whole intermediate chain to it.
int nIdCur = panPolyIdMap[iPoly];
panPolyIdMap[iPoly] = nId;
while (nIdCur != panPolyIdMap[nIdCur])
{
int nNextId = panPolyIdMap[nIdCur];
panPolyIdMap[nIdCur] = nId;
nIdCur = nNextId;
}
if (panPolyIdMap[iPoly] == iPoly)
nFinalPolyCount++;
}
CPLDebug("GDALRasterPolygonEnumerator",
"Counted %d polygon fragments forming %d final polygons.",
nNextPolygonId, nFinalPolyCount);
}
/************************************************************************/
/* ProcessLine() */
/* */
/* Assign ids to polygons, one line at a time. */
/************************************************************************/
template <class DataType, class EqualityTest>
bool GDALRasterPolygonEnumeratorT<DataType, EqualityTest>::ProcessLine(
DataType *panLastLineVal, DataType *panThisLineVal, GInt32 *panLastLineId,
GInt32 *panThisLineId, int nXSize)
{
EqualityTest eq;
/* -------------------------------------------------------------------- */
/* Special case for the first line. */
/* -------------------------------------------------------------------- */
if (panLastLineVal == nullptr)
{
for (int i = 0; i < nXSize; i++)
{
if (panThisLineVal[i] == GP_NODATA_MARKER)
{
panThisLineId[i] = -1;
}
else if (i == 0 ||
!(eq.operator()(panThisLineVal[i], panThisLineVal[i - 1])))
{
panThisLineId[i] = NewPolygon(panThisLineVal[i]);
if (panThisLineId[i] < 0)
return false;
}
else
{
panThisLineId[i] = panThisLineId[i - 1];
}
}
return true;
}
/* -------------------------------------------------------------------- */
/* Process each pixel comparing to the previous pixel, and to */
/* the last line. */
/* -------------------------------------------------------------------- */
for (int i = 0; i < nXSize; i++)
{
if (panThisLineVal[i] == GP_NODATA_MARKER)
{
panThisLineId[i] = -1;
}
else if (i > 0 &&
eq.operator()(panThisLineVal[i], panThisLineVal[i - 1]))
{
panThisLineId[i] = panThisLineId[i - 1];
if (eq.operator()(panLastLineVal[i], panThisLineVal[i]) &&
(panPolyIdMap[panLastLineId[i]] !=
panPolyIdMap[panThisLineId[i]]))
{
MergePolygon(panLastLineId[i], panThisLineId[i]);
}
if (nConnectedness == 8 &&
eq.operator()(panLastLineVal[i - 1], panThisLineVal[i]) &&
(panPolyIdMap[panLastLineId[i - 1]] !=
panPolyIdMap[panThisLineId[i]]))
{
MergePolygon(panLastLineId[i - 1], panThisLineId[i]);
}
if (nConnectedness == 8 && i < nXSize - 1 &&
eq.operator()(panLastLineVal[i + 1], panThisLineVal[i]) &&
(panPolyIdMap[panLastLineId[i + 1]] !=
panPolyIdMap[panThisLineId[i]]))
{
MergePolygon(panLastLineId[i + 1], panThisLineId[i]);
}
}
else if (eq.operator()(panLastLineVal[i], panThisLineVal[i]))
{
panThisLineId[i] = panLastLineId[i];
}
else if (i > 0 && nConnectedness == 8 &&
eq.operator()(panLastLineVal[i - 1], panThisLineVal[i]))
{
panThisLineId[i] = panLastLineId[i - 1];
if (i < nXSize - 1 &&
eq.operator()(panLastLineVal[i + 1], panThisLineVal[i]) &&
(panPolyIdMap[panLastLineId[i + 1]] !=
panPolyIdMap[panThisLineId[i]]))
{
MergePolygon(panLastLineId[i + 1], panThisLineId[i]);
}
}
else if (i < nXSize - 1 && nConnectedness == 8 &&
eq.operator()(panLastLineVal[i + 1], panThisLineVal[i]))
{
panThisLineId[i] = panLastLineId[i + 1];
}
else
{
panThisLineId[i] = NewPolygon(panThisLineVal[i]);
if (panThisLineId[i] < 0)
return false;
}
}
return true;
}
template class GDALRasterPolygonEnumeratorT<std::int64_t, IntEqualityTest>;
template class GDALRasterPolygonEnumeratorT<float, FloatEqualityTest>;
template class GDALRasterPolygonEnumeratorT<double, DoubleEqualityTest>;
/*! @endcond */
|