CoverageCleaner.h
/**********************************************************************
*
* GEOS - Geometry Engine Open Source
* http://geos.osgeo.org
*
* Copyright (c) 2025 Martin Davis
* Copyright (C) 2025 Paul Ramsey <pramsey@cleverelephant.ca>
*
* This is free software; you can redistribute and/or modify it under
* the terms of the GNU Lesser General Public Licence as published
* by the Free Software Foundation.
* See the COPYING file for more information.
*
**********************************************************************/
#pragma once
#include <geos/index/strtree/TemplateSTRtree.h>
#include <geos/coverage/CleanCoverage.h>
#include <geos/geom/Envelope.h>
#include <geos/constants.h>
#include <geos/export.h>
#include <vector>
#include <memory>
#include <map>
// Forward declarations
namespace geos {
namespace geom {
class Coordinate;
class CoordinateSequence;
class Geometry;
class GeometryFactory;
class LineString;
class LinearRing;
class Polygon;
}
namespace index {
}
namespace noding {
class SegmentString;
}
namespace coverage {
class CleanCoverage;
}
}
namespace geos { // geos.
namespace coverage { // geos.coverage
class GEOS_DLL CoverageCleaner {
using Coordinate = geos::geom::Coordinate;
using CoordinateSequence = geos::geom::CoordinateSequence;
using Geometry = geos::geom::Geometry;
using GeometryFactory = geos::geom::GeometryFactory;
using Point = geos::geom::Point;
using Polygon = geos::geom::Polygon;
using LineString = geos::geom::LineString;
using LinearRing = geos::geom::LinearRing;
using Envelope = geos::geom::Envelope;
using SegmentString = geos::noding::SegmentString;
public:
static constexpr int MERGE_LONGEST_BORDER = 0;
static constexpr int MERGE_MAX_AREA = 1;
static constexpr int MERGE_MIN_AREA = 2;
static constexpr int MERGE_MIN_INDEX = 3;
private:
std::vector<const Geometry*> coverage;
const GeometryFactory* geomFactory;
double snappingDistance;
double gapMaximumWidth = 0.0;
int overlapMergeStrategy = MERGE_LONGEST_BORDER;
std::unique_ptr<index::strtree::TemplateSTRtree<std::size_t>> covIndex;
std::vector<std::unique_ptr<Polygon>> resultants;
std::unique_ptr<CleanCoverage> cleanCov;
std::map<std::size_t, std::vector<std::size_t>> overlapParentMap;
std::vector<const Polygon*> overlaps;
std::vector<const Polygon*> gaps;
std::vector<const Polygon*> mergableGaps;
static constexpr double DEFAULT_SNAPPING_FACTOR = 1.0e8;
static double computeDefaultSnappingDistance(
std::vector<const Geometry*>& geoms);
static Envelope extent(std::vector<const Geometry*>& geoms);
void mergeOverlaps(
std::map<std::size_t, std::vector<std::size_t>>& overlapMap);
std::unique_ptr<CleanCoverage::MergeStrategy> mergeStrategy(
int mergeStrategyId);
void computeResultants(double tolerance);
void createCoverageIndex();
void classifyResult(std::vector<std::unique_ptr<Polygon>>& rs);
void classifyResultant(std::size_t resultIndex, const Polygon* resPoly);
static bool covers(const Geometry* poly, const Point* intPt);
std::vector<const Polygon*> findMergableGaps(
std::vector<const Polygon*> gaps);
bool isMergableGap(const Polygon* gap);
static std::vector<std::unique_ptr<geom::Polygon>> polygonize(
const Geometry* cleanEdges);
static bool isPolygonal(const Geometry* geom);
static std::vector<const Polygon*> toPolygonArray(
const Geometry* geom);
public:
CoverageCleaner(std::vector<const Geometry*>& coverage);
static std::vector<std::unique_ptr<Geometry>> clean(
std::vector<const Geometry*>& coverage,
double snapDistance,
int mergeStrategy,
double maxWidth);
static std::vector<std::unique_ptr<Geometry>> clean(
std::vector<const Geometry*>& coverage,
double snapDistance,
double maxWidth);
static std::vector<std::unique_ptr<Geometry>> cleanOverlapGap(
std::vector<const Geometry*>& coverage,
int mergeStrategy,
double maxWidth);
static std::vector<std::unique_ptr<Geometry>> cleanGapWidth(
std::vector<const Geometry*>& coverage,
double maxWidth);
void setSnappingDistance(double snapDistance);
void setOverlapMergeStrategy(int mergeStrategy);
void setGapMaximumWidth(double maxWidth);
void clean();
std::vector<std::unique_ptr<Geometry>> getResult();
std::vector<const Polygon*> getOverlaps();
std::vector<const Polygon*> getMergedGaps();
std::unique_ptr<Geometry> toGeometry(
std::vector<SegmentString*>& segStrings,
const GeometryFactory* geomFact);
std::unique_ptr<Geometry> node(
std::vector<const Geometry*>& coverage,
double snapDistance);
CoverageCleaner(const CoverageCleaner&) = delete;
CoverageCleaner& operator=(const CoverageCleaner&) = delete;
};
} // namespace geos.coverage
} // namespace geos
geos::coverage::CoverageCleaner::MERGE_MAX_AREA
static constexpr int MERGE_MAX_AREA
Definition CoverageCleaner.h:146
geos::coverage::CoverageCleaner::setGapMaximumWidth
void setGapMaximumWidth(double maxWidth)
geos::coverage::CoverageCleaner::MERGE_MIN_AREA
static constexpr int MERGE_MIN_AREA
Definition CoverageCleaner.h:148
geos::coverage::CoverageCleaner::clean
static std::vector< std::unique_ptr< Geometry > > clean(std::vector< const Geometry * > &coverage, double snapDistance, double maxWidth)
geos::coverage::CoverageCleaner::getMergedGaps
std::vector< const Polygon * > getMergedGaps()
geos::coverage::CoverageCleaner::clean
void clean()
geos::coverage::CoverageCleaner::MERGE_LONGEST_BORDER
static constexpr int MERGE_LONGEST_BORDER
Definition CoverageCleaner.h:144
geos::coverage::CoverageCleaner::clean
static std::vector< std::unique_ptr< Geometry > > clean(std::vector< const Geometry * > &coverage, double snapDistance, int mergeStrategy, double maxWidth)
geos::coverage::CoverageCleaner::CoverageCleaner
CoverageCleaner(std::vector< const Geometry * > &coverage)
geos::coverage::CoverageCleaner::MERGE_MIN_INDEX
static constexpr int MERGE_MIN_INDEX
Definition CoverageCleaner.h:150
geos::coverage::CoverageCleaner::CoverageCleaner
CoverageCleaner(const CoverageCleaner &)=delete
geos::coverage::CoverageCleaner::setOverlapMergeStrategy
void setOverlapMergeStrategy(int mergeStrategy)
geos::coverage::CoverageCleaner::getOverlaps
std::vector< const Polygon * > getOverlaps()
geos::coverage::CoverageCleaner::cleanGapWidth
static std::vector< std::unique_ptr< Geometry > > cleanGapWidth(std::vector< const Geometry * > &coverage, double maxWidth)
geos::coverage::CoverageCleaner::cleanOverlapGap
static std::vector< std::unique_ptr< Geometry > > cleanOverlapGap(std::vector< const Geometry * > &coverage, int mergeStrategy, double maxWidth)
geos::coverage::CoverageCleaner::getResult
std::vector< std::unique_ptr< Geometry > > getResult()
geos::coverage::CoverageCleaner::setSnappingDistance
void setSnappingDistance(double snapDistance)
geos::geom::CoordinateSequence
The internal representation of a list of coordinates inside a Geometry.
Definition CoordinateSequence.h:56
geos::geom::Coordinate
Coordinate is the lightweight class used to store coordinates.
Definition Coordinate.h:217
geos::geom::Envelope
An Envelope defines a rectangulare region of the 2D coordinate plane.
Definition Envelope.h:59
geos::geom::GeometryFactory
Supplies a set of utility methods for building Geometry objects from CoordinateSequence or other Geom...
Definition GeometryFactory.h:70
geos::geom::Geometry
Basic implementation of Geometry, constructed and destructed by GeometryFactory.
Definition Geometry.h:196
geos::geom::LineString
Definition LineString.h:66
geos::geom::LinearRing
Models an OGC SFS LinearRing. A LinearRing is a LineString which is both closed and simple.
Definition LinearRing.h:54
geos::geom::Point
Definition Point.h:61
geos::geom::Polygon
Represents a linear polygon, which may include holes.
Definition Polygon.h:61
geos::noding::SegmentString
An interface for classes which represent a sequence of contiguous line segments.
Definition SegmentString.h:47
geos::geom
Definition Angle.h:26
geos::index
Provides classes for various kinds of spatial indexes.
Definition CleanCoverage.h:39
geos::noding
Classes to compute nodings for arrangements of line segments and line segment sequences.
Definition CoverageCleaner.h:42
geos
Basic namespace for all GEOS functionalities.
Definition geos.h:38