forked from libgeos/geos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCoverageValidator.cpp
118 lines (98 loc) · 3.23 KB
/
CoverageValidator.cpp
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
/**********************************************************************
*
* 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.
*
**********************************************************************/
#include <geos/coverage/CoverageValidator.h>
#include <geos/coverage/CoveragePolygonValidator.h>
#include <geos/geom/Envelope.h>
#include <geos/geom/Geometry.h>
#include <geos/geom/GeometryFactory.h>
using geos::geom::Envelope;
using geos::geom::Geometry;
using geos::geom::GeometryFactory;
namespace geos { // geos
namespace coverage { // geos.coverage
/* public static */
bool
CoverageValidator::isValid(std::vector<const Geometry*>& coverage)
{
CoverageValidator v(coverage);
return ! hasInvalidResult(v.validate());
}
/* public static */
bool
CoverageValidator::hasInvalidResult(const std::vector<std::unique_ptr<Geometry>>& validateResult)
{
for (const auto& geom : validateResult) {
if (geom != nullptr)
return true;
}
return false;
}
/* public static */
std::vector<std::unique_ptr<Geometry>>
CoverageValidator::validate(std::vector<const Geometry*>& coverage)
{
CoverageValidator v(coverage);
return v.validate();
}
/* public static */
std::vector<std::unique_ptr<Geometry>>
CoverageValidator::validate(std::vector<const Geometry*>& coverage, double gapWidth)
{
CoverageValidator v(coverage);
v.setGapWidth(gapWidth);
return v.validate();
}
/* public */
std::vector<std::unique_ptr<Geometry>>
CoverageValidator::validate()
{
TemplateSTRtree<const Geometry*> index;
std::vector<std::unique_ptr<Geometry>> invalidLines;
for (auto* geom : m_coverage) {
index.insert(geom);
invalidLines.emplace_back(nullptr);
}
for (std::size_t i = 0; i < m_coverage.size(); i++) {
const Geometry* geom = m_coverage[i];
std::unique_ptr<Geometry> result = validate(geom, index);
invalidLines[i].reset(result.release());
}
return invalidLines;
}
/* private */
std::unique_ptr<Geometry>
CoverageValidator::validate(const Geometry* targetGeom, TemplateSTRtree<const Geometry*>& index)
{
Envelope queryEnv = *(targetGeom->getEnvelopeInternal());
queryEnv.expandBy(m_gapWidth);
// Query the index for nearby geometry and add to the list
std::vector<const Geometry*> nearGeoms;
auto visitor = [&nearGeoms](const Geometry* geom) {
nearGeoms.push_back(geom);
};
index.query(queryEnv, visitor);
//-- the target geometry is returned in the query, so must be removed from the set
auto it = std::find(nearGeoms.begin(), nearGeoms.end(), targetGeom);
if (it != nearGeoms.end()) {
nearGeoms.erase(it);
}
// Geometry[] nearGeoms = GeometryFactory.toGeometryArray(nearGeoms);
std::unique_ptr<Geometry> result = CoveragePolygonValidator::validate(targetGeom, nearGeoms, m_gapWidth);
if (result->isEmpty())
return nullptr;
else
return result;
}
} // namespace geos.coverage
} // namespace geos