Identify features for shared-edge detection by layer, segment, and seq

Features don't necessarily have IDs, and sequence numbers are only unique
within a layer and a reading segment: the flatgeobuf, geobuf, and CSV
readers start every segment's sequence at the same offset and parse in
parallel, so two different features can have the same seq. Record all
three in each vertex record, and only count an edge as shared if it is
part of polygons that differ in any of them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CQ6vQUJtmGZnqz7RmVYQaJ
This commit is contained in:
Claude
2026-11-30 21:38:40 +00:00
parent 4352b1498f
commit c062c4a240
3 changed files with 42 additions and 19 deletions
+10 -10
View File
@@ -1231,10 +1231,10 @@ int vertexcmp(const void *void1, const void *void2) {
return 1;
}
if (v1->feature < v2->feature) {
if (v1->feature_less(*v2)) {
return -1;
}
if (v1->feature > v2->feature) {
if (v2->feature_less(*v1)) {
return 1;
}
@@ -2070,26 +2070,26 @@ std::pair<int, metadata> read_input(std::vector<source> &sources, char *fname, i
// All the vertices with the same central point, which are adjacent after sorting
std::vector<vertex> group;
// The polygon neighbors of the central point, and which feature each is from
std::vector<std::pair<draw, long long>> neighbors;
// The polygon neighbors of the central point, and which feature (vertex) each is from
std::vector<std::pair<draw, vertex const *>> neighbors;
auto finish_group = [&]() {
// A neighbor that is connected to the central point by more than one
// polygon feature is the other end of a shared edge
neighbors.clear();
for (auto const &g : group) {
if (g.feature >= 0) {
neighbors.emplace_back(g.p1, g.feature);
if (g.is_polygon()) {
neighbors.emplace_back(g.p1, &g);
if (g.p2 != g.p1) {
neighbors.emplace_back(g.p2, g.feature);
neighbors.emplace_back(g.p2, &g);
}
}
}
std::sort(neighbors.begin(), neighbors.end(), [](std::pair<draw, long long> const &a, std::pair<draw, long long> const &b) {
std::sort(neighbors.begin(), neighbors.end(), [](std::pair<draw, vertex const *> const &a, std::pair<draw, vertex const *> const &b) {
if (a.first < b.first) {
return true;
} else if (a.first == b.first) {
return a.second < b.second;
return a.second->feature_less(*b.second);
}
return false;
});
@@ -2097,7 +2097,7 @@ std::pair<int, metadata> read_input(std::vector<source> &sources, char *fname, i
size_t l;
bool multiple = false;
for (l = k + 1; l < neighbors.size() && neighbors[l].first == neighbors[k].first; l++) {
if (neighbors[l].second != neighbors[k].second) {
if (!neighbors[l].second->same_feature(*neighbors[k].second)) {
multiple = true;
}
}
+1 -1
View File
@@ -526,7 +526,7 @@ int serialize_feature(struct serialization_state *sst, serial_feature &sf, std::
scaled_geometry[(k - i + 0) % (j - i - 1) + i],
scaled_geometry[(k - i + 1) % (j - i - 1) + i],
scaled_geometry[(k - i + 2) % (j - i - 1) + i],
*(sst->layer_seq));
*(sst->layer_seq), sf.layer, sf.segment);
fwrite_check((char *) &v, sizeof(struct vertex), 1, r->vertexfile, &r->vertexpos, sst->fname);
}
+31 -8
View File
@@ -284,12 +284,19 @@ struct vertex {
draw mid;
draw p2;
// Which polygon feature this vertex is part of, or -1 if it is part of a line,
// so that edges shared between different polygon features can be identified
long long feature = -1;
// Which polygon feature this vertex is part of, so that edges shared
// between different polygon features can be identified. Features do not
// necessarily have IDs, and sequence numbers are only unique within a layer
// and a reading segment, so a feature is identified by all three.
// `seq` is -1 if the vertex is part of a line rather than a polygon.
long long seq = -1;
int layer = -1;
int segment = -1;
vertex(draw one, draw joint, draw two, long long feature_ = -1) {
feature = feature_;
vertex(draw one, draw joint, draw two, long long seq_ = -1, int layer_ = -1, int segment_ = -1) {
seq = seq_;
layer = layer_;
segment = segment_;
if (one < two) {
p1 = one;
@@ -302,6 +309,24 @@ struct vertex {
mid = joint;
}
bool is_polygon() const {
return seq >= 0;
}
bool same_feature(const vertex &v) const {
return seq == v.seq && layer == v.layer && segment == v.segment;
}
bool feature_less(const vertex &v) const {
if (layer != v.layer) {
return layer < v.layer;
}
if (segment != v.segment) {
return segment < v.segment;
}
return seq < v.seq;
}
bool operator<(const vertex &v) const {
if (mid < v.mid) {
return true;
@@ -312,9 +337,7 @@ struct vertex {
if (p2 < v.p2) {
return true;
} else if (p2 == v.p2) {
if (feature < v.feature) {
return true;
}
return feature_less(v);
}
}
}