From 22412b0cfc9bd23dc605346603efb844dfad9fa9 Mon Sep 17 00:00:00 2001 From: Robert Brown Date: Wed, 1 Dec 2021 16:04:19 +0000 Subject: [PATCH] SetToLinkedGeo bug and update to latest C library (#49) * Correctly set next for multipolygon SetToLinkedGeo * Update h3 C library to v3.7.2 --- v3/H3_VERSION | 2 +- v3/h3.go | 5 +++++ v3/h3_algos.c | 5 ++++- v3/h3_test.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 53 insertions(+), 2 deletions(-) diff --git a/v3/H3_VERSION b/v3/H3_VERSION index 1885263..2272683 100644 --- a/v3/H3_VERSION +++ b/v3/H3_VERSION @@ -1 +1 @@ -v3.7.1 +v3.7.2 diff --git a/v3/h3.go b/v3/h3.go index 00753e3..1b8a93a 100644 --- a/v3/h3.go +++ b/v3/h3.go @@ -609,6 +609,11 @@ func linkedGeoPolygonFromC(cg *C.LinkedGeoPolygon) LinkedGeoPolygon { g.First = linkedGeoLoopFromC(cg.first) g.Last = linkedGeoLoopFromC(cg.last) + if cg.next != nil { + next := linkedGeoPolygonFromC(cg.next) + g.Next = &next + } + return g } diff --git a/v3/h3_algos.c b/v3/h3_algos.c index 769a1c8..ac0a596 100644 --- a/v3/h3_algos.c +++ b/v3/h3_algos.c @@ -317,7 +317,10 @@ H3Index h3NeighborRotations(H3Index origin, Direction dir, int* rotations) { } else { Direction oldDigit = H3_GET_INDEX_DIGIT(out, r + 1); Direction nextDir; - if (isResClassIII(r + 1)) { + if (oldDigit == INVALID_DIGIT) { + // Only possible on invalid input + return H3_NULL; + } else if (isResClassIII(r + 1)) { H3_SET_INDEX_DIGIT(out, r + 1, NEW_DIGIT_II[oldDigit][dir]); nextDir = NEW_ADJUSTMENT_II[oldDigit][dir]; } else { diff --git a/v3/h3_test.go b/v3/h3_test.go index 25a9bfb..721163b 100644 --- a/v3/h3_test.go +++ b/v3/h3_test.go @@ -147,6 +147,32 @@ var ( 0x872f5a374ffffff, 0x872f5a329ffffff, }, + { + 0x89194ad36a3ffff, + 0x89194ad3263ffff, + 0x89194ad326fffff, + 0x89194ad149bffff, + 0x89194ad1493ffff, + 0x89194ad322bffff, + 0x89194ad3277ffff, + 0x89194ad338bffff, + 0x89194ad3313ffff, + 0x89194ad3317ffff, + 0x89194ad33bbffff, + 0x89194ad3387ffff, + 0x89194ad3383ffff, + 0x89194ad3397ffff, + 0x89194ad33b3ffff, + 0x89194ad314fffff, + 0x89194ad3143ffff, + 0x89194ad315bffff, + 0x88194ad369fffff, + 0x88194ad361fffff, + 0x88194ad363fffff, + 0x88194ad30dfffff, + 0x88194ad347fffff, + 0x88194ad345fffff, + }, } validGeoCoordB = GeoCoord{ @@ -501,6 +527,23 @@ func TestSetToLinkedGeo(t *testing.T) { assert.Equal(t, float64(35.68805899195678), polygon.Last.First.Vertex.Latitude) assert.Equal(t, float64(139.7268573711303), polygon.Last.First.Vertex.Longitude) }) + t.Run("Multipolygon", func(t *testing.T) { + t.Parallel() + polygon := SetToLinkedGeo(validLinkedIndexes[2]) + + assert.NotNil(t, polygon.First) + assert.NotNil(t, polygon.Last) + assert.NotNil(t, polygon.Next) + + assert.Equal(t, float64(51.49710134101195), polygon.First.First.Vertex.Latitude) + assert.Equal(t, float64(-0.09036943363144687), polygon.First.First.Vertex.Longitude) + + assert.Equal(t, float64(51.49794867871118), polygon.Last.First.Vertex.Latitude) + assert.Equal(t, float64(-0.07191264615040037), polygon.Last.First.Vertex.Longitude) + + assert.Equal(t, float64(51.53058149905774), polygon.Next.First.First.Vertex.Latitude) + assert.Equal(t, float64(-0.07297959470277668), polygon.Next.First.First.Vertex.Longitude) + }) } func TestLine(t *testing.T) {