Skip to content

Commit

Permalink
fix plumbing around deduped overlapping rings
Browse files Browse the repository at this point in the history
PDOK-16135
  • Loading branch information
roelarents committed Feb 15, 2024
1 parent 3114976 commit 5a5d6b7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 47 deletions.
54 changes: 13 additions & 41 deletions snap/snap.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ func addPointsAndSnap(ix *PointIndex, polygon geom.Polygon, levels []Level) map[

newPolygons := make(map[Level][][][][2]float64, len(levels))
for l := range levelMap {
newOuters[l], newInners[l] = dedupeAndSortBySizeInnersOuters(newOuters[l], newInners[l])
newPolygonsForLevel := matchInnersToPolygons(newPolygonsForLevel, newInners[l], len(polygon) > 1)
newOuters[l], newInners[l] = dedupeInnersOuters(newOuters[l], newInners[l])
newPolygonsForLevel := matchInnersToPolygons(outersToPolygons(newOuters[l]), newInners[l], len(polygon) > 1)
if len(newPolygonsForLevel) > 1 {
newPolygons[l] = newPolygonsForLevel
}
Expand All @@ -153,14 +153,22 @@ func addPointsAndSnap(ix *PointIndex, polygon geom.Polygon, levels []Level) map[
// points and lines at the end, as outer rings
for level, pointsAndLines := range newPointsAndLines {
for _, pointOrLine := range pointsAndLines {
newOuters[level] = append(newOuters[level], [][][2]float64{pointOrLine})
newPolygons[level] = append(newPolygons[level], [][][2]float64{pointOrLine})
}
}
return floatPolygonsToGeomPolygonsForAllLevels(newOuters)
return floatPolygonsToGeomPolygonsForAllLevels(newPolygons)
}

func outersToPolygons(outers [][][2]float64) [][][][2]float64 {
polygons := make([][][][2]float64, len(outers))
for i := 0; i < len(outers); i++ {
polygons[i] = [][][2]float64{outers[i]}
}
return polygons
}

// lang=python
func dedupeAndSortBySizeInnersOuters(outers [][][2]float64, inners [][][2]float64) ([][][2]float64, [][][2]float64) {
func dedupeInnersOuters(outers [][][2]float64, inners [][][2]float64) ([][][2]float64, [][][2]float64) {
// ToDo: optimize by deleting rings from allRings on the fly
allRings := append(outers, inners...)

Check failure on line 173 in snap/snap.go

View workflow job for this annotation

GitHub Actions / lint

appendAssign: append result not assigned to the same slice (gocritic)
lenOuters := len(outers)
Expand Down Expand Up @@ -296,42 +304,6 @@ func sortPolyIdxsByOuterAreaDesc(polygons [][][][2]float64) []int {
return areas.Keys()
}

// helper for matchInnersToPolygons to delete duplicate polygons
// comparing them only by their outers and asserting that a deleted polygon didn't have inner rings appended yet
// yes it's implemented as ~O(n^2),
// but it's expected that the (outer) rings are usually different even from the first point,
// making it still more efficient than using a hashmap of the entire rings
func dedupePolygonsByOuters(polygons [][][][2]float64) (int, [][][][2]float64) {
numPolygons := len(polygons)
numDeleted := 0
for i := 0; i < numPolygons; i++ {
ring := polygons[i][0] // only check the outer
compareToOther:
for j := i + 1; j < numPolygons; j++ {
ringLen := len(ring)
other := polygons[j][0]
otherLen := len(other)
if ringLen != otherLen {
continue
}
for k := 0; k < min(ringLen, otherLen); k++ {
if ring[k] != other[k] {
continue compareToOther
}
}
// delete
if len(polygons[j]) > 1 {
panicDeletedPolygonHasInnerRing(polygons[j])
}
polygons = append(polygons[:j], polygons[j+1:]...)
j--
numPolygons--
numDeleted++
}
}
return numDeleted, polygons
}

// from paulmach/orb, modified to also return whether it's on the boundary
// ringContains returns true if the point is inside the ring.
// Points on the boundary are also considered in. In which case the second returned var is true too.
Expand Down
7 changes: 1 addition & 6 deletions snap/snap_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package snap

import (
"fmt"
"testing"

"github.com/go-spatial/geom/encoding/wkt"
Expand Down Expand Up @@ -622,16 +621,12 @@ func TestSnap_snapPolygon(t *testing.T) {
},
want: map[tms20.TMID][]geom.Polygon{
// 1 big outer with 2 holes. and 2 new outers/polygons (inside those holes from the big outer) each with their own hole.
// a duplicate inner/outer pair from the extra C is removed
1: {
{
{{4.0, 124.0}, {4.0, 4.0}, {60.0, 4.0}, {60.0, 124.0}}, // ccw
{{28.0, 52.0}, {52.0, 52.0}, {52.0, 12.0}, {12.0, 12.0}, {12.0, 52.0}}, // cw
{{12.0, 116.0}, {52.0, 116.0}, {52.0, 76.0}, {28.0, 76.0}, {12.0, 76.0}}, // cw
}, {
{{28.0, 52.0}, {12.0, 52.0}, {12.0, 12.0}, {52.0, 12.0}, {52.0, 52.0}, {28.0, 52.0}}, // ccw
{{28.0, 52.0}, {52.0, 52.0}, {52.0, 12.0}, {12.0, 12.0}, {12.0, 52.0}, {28.0, 52.0}}, // cw
// TODO order of separate outer rings OK. does this overlap the deepest nested square?
// TODO deduplicate this, remove totally?
}, {
{{28.0, 44.0}, {20.0, 44.0}, {20.0, 20.0}, {44.0, 20.0}, {44.0, 44.0}}, // ccw
{{28.0, 36.0}, {36.0, 36.0}, {36.0, 28.0}, {28.0, 28.0}}, // cw
Expand Down

0 comments on commit 5a5d6b7

Please sign in to comment.