Skip to content

Commit a45f697

Browse files
committed
On Cell::AddRing, close the LinearRing if the last point and the first are not the same
1 parent 2e1e43b commit a45f697

File tree

4 files changed

+24
-11
lines changed

4 files changed

+24
-11
lines changed

include/fields2cover/types/LinearRing.h

+2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ struct LinearRing : public Geometries<LinearRing, OGRLinearRing, wkbLinearRing,
4646
const Point startPoint() const;
4747
const Point endPoint() const;
4848
bool isClockwise() const;
49+
bool isClosed() const;
50+
LinearRing& closeRing();
4951

5052
Point closestPointTo(const Point& p) const;
5153
};

src/fields2cover/types/Cell.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ Cell Cell::convexHull() const {
122122

123123
void Cell::addRing(const LinearRing& t) {
124124
auto r = t.clone();
125-
this->data_->addRing(r.get());
125+
this->data_->addRing(r.closeRing().get());
126126
}
127127

128128
void Cell::addGeometry(const LinearRing& ring) {

src/fields2cover/types/LinearRing.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,17 @@ bool LinearRing::isClockwise() const {
122122
return data_->isClockwise();
123123
}
124124

125+
bool LinearRing::isClosed() const {
126+
return data_->IsEmpty() || data_->get_IsClosed();
127+
}
128+
129+
LinearRing& LinearRing::closeRing() {
130+
if (!this->isClosed()) {
131+
this->addPoint(this->startPoint());
132+
}
133+
return *this;
134+
}
135+
125136
Point LinearRing::closestPointTo(const Point& p) const {
126137
std::vector<double> dist;
127138
std::vector<Point> ps;

src/fields2cover/utils/parser.cpp

+10-10
Original file line numberDiff line numberDiff line change
@@ -88,17 +88,17 @@ F2CPoint getPointFromJson(const json& ps) {
8888
}
8989

9090
F2CCell getCellFromJson(const json& imported_cell) {
91-
F2CLinearRing outer_ring;
92-
for (auto&& ps : imported_cell["geometry"]["coordinates"][0]) {
93-
outer_ring.addPoint(getPointFromJson(ps));
94-
}
95-
F2CCell cell(outer_ring);
96-
for (int i = 1; i < imported_cell["geometry"]["coordinates"].size(); ++i) {
97-
F2CLinearRing inner_ring;
98-
for (auto&& ps : imported_cell["geometry"]["coordinates"][i]) {
99-
inner_ring.addPoint(getPointFromJson(ps));
91+
auto jsonToF2CRing = [] (const json& json_ring) {
92+
F2CLinearRing ring;
93+
for (auto&& ps : json_ring) {
94+
ring.addPoint(getPointFromJson(ps));
10095
}
101-
cell.addRing(inner_ring);
96+
return ring;
97+
};
98+
F2CCell cell;
99+
auto json_rings = imported_cell["geometry"]["coordinates"];
100+
for (int i = 0; i < json_rings.size(); ++i) {
101+
cell.addRing(jsonToF2CRing(json_rings[i]));
102102
}
103103
return cell;
104104
}

0 commit comments

Comments
 (0)