CoveragePolygonValidator.h
/**********************************************************************
*
* GEOS - Geometry Engine Open Source
* http://geos.osgeo.org
*
* Copyright (C) 2022 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/noding/BasicSegmentString.h>
#include <geos/geom/LineSegment.h>
#include <geos/algorithm/locate/IndexedPointInAreaLocator.h>
#include <geos/coverage/CoveragePolygon.h>
#include <geos/coverage/CoverageRing.h>
#include <unordered_map>
#include <map>
// Forward declarations
namespace geos {
namespace geom {
class Coordinate;
class Envelope;
class Geometry;
class GeometryFactory;
}
}
namespace geos { // geos
namespace coverage { // geos::coverage
class GEOS_DLL CoveragePolygonValidator {
using Coordinate = geos::geom::Coordinate;
using CoordinateSequence = geos::geom::CoordinateSequence;
using Envelope = geos::geom::Envelope;
using Polygon = geos::geom::Polygon;
using LinearRing = geos::geom::LinearRing;
using Geometry = geos::geom::Geometry;
using GeometryFactory = geos::geom::GeometryFactory;
using LineSegment = geos::geom::LineSegment;
using IndexedPointInAreaLocator = geos::algorithm::locate::IndexedPointInAreaLocator;
private:
class CoverageRingSegment : public LineSegment
{
public:
// Members
CoverageRing* ringForward;
std::size_t indexForward;
CoverageRing* ringOpp;
std::size_t indexOpp;
CoverageRingSegment(
const Coordinate& p_p0, const Coordinate& p_p1,
CoverageRing* p_ring, std::size_t p_index)
: LineSegment(p_p0, p_p1)
, ringForward(nullptr)
, indexForward(0)
, ringOpp(nullptr)
, indexOpp(0)
{
if (p_p1.compareTo(p_p0) < 0) {
reverse();
ringOpp = p_ring;
indexOpp = p_index;
}
else {
ringForward = p_ring;
indexForward = p_index;
}
};
void match(const CoverageRingSegment* seg) {
bool isInvalid = checkInvalid(seg);
if (isInvalid) {
return;
}
//-- record the match
if (ringForward == nullptr) {
ringForward = seg->ringForward;
indexForward = seg->indexForward;
}
else {
ringOpp = seg->ringOpp;
indexOpp = seg->indexOpp;
}
//-- mark ring segments as matched
ringForward->markMatched(indexForward);
ringOpp->markMatched(indexOpp);
}
bool checkInvalid(const CoverageRingSegment* seg) const {
if (ringForward != nullptr && seg->ringForward != nullptr) {
ringForward->markInvalid(indexForward);
seg->ringForward->markInvalid(seg->indexForward);
return true;
}
if (ringOpp != nullptr && seg->ringOpp != nullptr) {
ringOpp->markInvalid(indexOpp);
seg->ringOpp->markInvalid(seg->indexOpp);
return true;
}
return false;
}
struct CoverageRingSegHash {
std::size_t
operator() (CoverageRingSegment const* s) const {
std::size_t h = std::hash<double>{}(s->p0.x);
h ^= (std::hash<double>{}(s->p0.y) << 1);
h ^= (std::hash<double>{}(s->p1.x) << 1);
return h ^ (std::hash<double>{}(s->p1.y) << 1);
}
};
struct CoverageRingSegEq {
bool
operator() (CoverageRingSegment const* lhs, CoverageRingSegment const* rhs) const {
return lhs->p0.x == rhs->p0.x
&& lhs->p0.y == rhs->p0.y
&& lhs->p1.x == rhs->p1.x
&& lhs->p1.y == rhs->p1.y;
}
};
};
// Members
const Geometry* targetGeom;
std::vector<const Geometry*> adjGeoms;
//std::vector<const Polygon*> m_adjPolygons;
const GeometryFactory* geomFactory;
double gapWidth = 0.0;
std::vector<std::unique_ptr<CoveragePolygon>> m_adjCovPolygons;
std::deque<CoverageRing> coverageRingStore;
std::vector<std::unique_ptr<CoordinateSequence>> localCoordinateSequences;
std::deque<CoverageRingSegment> coverageRingSegmentStore;
typedef std::unordered_map<CoverageRingSegment*, CoverageRingSegment*, CoverageRingSegment::CoverageRingSegHash, CoverageRingSegment::CoverageRingSegEq> CoverageRingSegmentMap;
// Declare type as noncopyable
CoveragePolygonValidator(const CoveragePolygonValidator& other) = delete;
CoveragePolygonValidator& operator=(const CoveragePolygonValidator& rhs) = delete;
public:
static std::unique_ptr<Geometry> validate(
const Geometry* targetPolygon,
std::vector<const Geometry*>& adjPolygons);
static std::unique_ptr<Geometry> validate(
const Geometry* targetPolygon,
std::vector<const Geometry*>& adjPolygons,
double gapWidth);
CoveragePolygonValidator(
const Geometry* targetPolygon,
std::vector<const Geometry*>& adjPolygons);
void setGapWidth(double p_gapWidth);
std::unique_ptr<Geometry> validate();
private:
static std::vector<std::unique_ptr<CoveragePolygon>>
toCoveragePolygons(const std::vector<const Polygon*> polygons);
static std::vector<const Polygon*> extractPolygons(std::vector<const Geometry*>& geoms);
/* private */
std::unique_ptr<Geometry> createEmptyResult();
void markMatchedSegments(
std::vector<CoverageRing*>& targetRings,
std::vector<CoverageRing*>& adjRings,
const Envelope& targetEnv);
void markMatchedSegments(
std::vector<CoverageRing*>& rings,
const Envelope& envLimit,
CoverageRingSegmentMap& segmentMap);
CoverageRingSegment* createCoverageRingSegment(
CoverageRing* ring, std::size_t index);
void markInvalidInteractingSegments(
std::vector<CoverageRing*>& targetRings,
std::vector<CoverageRing*>& adjRings,
double distanceTolerance);
void markInvalidInteriorSegments(
std::vector<CoverageRing*>& targetRings,
std::vector<std::unique_ptr<CoveragePolygon>>& adjCovPolygons);
void markInvalidInteriorSection(
CoverageRing& ring,
std::size_t iStart,
std::size_t iEnd,
std::vector<std::unique_ptr<CoveragePolygon>>& adjCovPolygons );
void markInvalidInteriorSegment(
CoverageRing& ring, std::size_t i, CoveragePolygon* adjPoly);
void checkTargetRings(
std::vector<CoverageRing*>& targetRings,
std::vector<CoverageRing*>& adjRngs,
const Envelope& targetEnv);
std::unique_ptr<Geometry> createInvalidLines(std::vector<CoverageRing*>& rings);
std::vector<CoverageRing*> createRings(const Geometry* geom);
std::vector<CoverageRing*> createRings(std::vector<const Polygon*>& polygons);
void createRings(const Polygon* poly, std::vector<CoverageRing*>& rings);
void addRing(
const LinearRing* ring,
bool isShell,
std::vector<CoverageRing*>& rings);
CoverageRing* createRing(const LinearRing* ring, bool isShell);
};
} // namespace geos::coverage
} // namespace geos
geos::algorithm::locate::IndexedPointInAreaLocator
Determines the location of Coordinates relative to an areal geometry, using indexing for efficiency.
Definition IndexedPointInAreaLocator.h:54
geos::coverage::CoveragePolygonValidator::validate
std::unique_ptr< Geometry > validate()
geos::coverage::CoveragePolygonValidator::validate
static std::unique_ptr< Geometry > validate(const Geometry *targetPolygon, std::vector< const Geometry * > &adjPolygons)
geos::coverage::CoveragePolygonValidator::setGapWidth
void setGapWidth(double p_gapWidth)
geos::coverage::CoveragePolygonValidator::CoveragePolygonValidator
CoveragePolygonValidator(const Geometry *targetPolygon, std::vector< const Geometry * > &adjPolygons)
geos::coverage::CoveragePolygonValidator::validate
static std::unique_ptr< Geometry > validate(const Geometry *targetPolygon, std::vector< const Geometry * > &adjPolygons, double gapWidth)
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::LineSegment
Definition LineSegment.h:61
geos::geom::LineSegment::p1
Coordinate p1
Segment start.
Definition LineSegment.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::Polygon
Represents a linear polygon, which may include holes.
Definition Polygon.h:61
geos::geom
Definition Angle.h:26
geos::index
Provides classes for various kinds of spatial indexes.
Definition CleanCoverage.h:39
geos
Basic namespace for all GEOS functionalities.
Definition geos.h:38