From 852719de423d9db7f91824f42a47ed044f13aaee Mon Sep 17 00:00:00 2001 From: Mojtaba Arezoomand Date: Mon, 12 Aug 2024 23:39:09 +0330 Subject: [PATCH 01/11] feat: added IsValidVertex method --- h3.go | 4 ++++ h3_test.go | 7 +++++++ 2 files changed, 11 insertions(+) diff --git a/h3.go b/h3.go index 77fe71a..3819b65 100644 --- a/h3.go +++ b/h3.go @@ -713,6 +713,10 @@ func LocalIJToCell(origin Cell, ij CoordIJ) Cell { return Cell(out) } +func IsValidVertex(c Cell) bool { + return C.isValidVertex(C.H3Index(c)) == 1 +} + func maxGridDiskSize(k int) int { return 3*k*(k+1) + 1 } diff --git a/h3_test.go b/h3_test.go index c2ab758..476d9d4 100644 --- a/h3_test.go +++ b/h3_test.go @@ -621,6 +621,13 @@ func TestPentagons(t *testing.T) { } } +func TestIsValidVertex(t *testing.T) { + t.Parallel() + + assertFalse(t, IsValidVertex(0)) + assertTrue(t, IsValidVertex(2473183460575936511)) +} + func equalEps(expected, actual float64) bool { return math.Abs(expected-actual) < eps } From 10feb9a05bb6dbe8fa54e105bf651ad6bdf5b7e3 Mon Sep 17 00:00:00 2001 From: Mojtaba Arezoomand Date: Mon, 12 Aug 2024 23:40:13 +0330 Subject: [PATCH 02/11] docs: added IsValidVertex to readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index eaed8ed..29175c5 100644 --- a/README.md +++ b/README.md @@ -126,7 +126,7 @@ func ExampleLatLngToCell() { | `cellToVertex` | TODO | | `cellToVertexes` | TODO | | `vertexToLatLng` | TODO | -| `isValidVertex` | TODO | +| `isValidVertex` | `IsValidVertex` | | `gridDistance` | `GridDistance`, `Cell#GridDistance` | | `gridPathCells` | `GridPath`, `Cell#GridPath` | | `cellToLocalIj` | `CellToLocalIJ` | From a4c797d91b5181f411812966875e5f42ba0c02b8 Mon Sep 17 00:00:00 2001 From: Mojtaba Arezoomand Date: Mon, 12 Aug 2024 23:58:12 +0330 Subject: [PATCH 03/11] fix: fix loop variable res captured by func literal error --- h3_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/h3_test.go b/h3_test.go index 476d9d4..e118228 100644 --- a/h3_test.go +++ b/h3_test.go @@ -609,6 +609,7 @@ func TestPentagons(t *testing.T) { t.Parallel() for _, res := range []int{0, 8, 15} { + res := res t.Run(fmt.Sprintf("res=%d", res), func(t *testing.T) { t.Parallel() pentagons := Pentagons(res) From 2e5075fb2a9e147fd87dc4b5842dc3922c7a6d59 Mon Sep 17 00:00:00 2001 From: Mojtaba Arezoomand Date: Sat, 17 Aug 2024 23:34:48 +0330 Subject: [PATCH 04/11] feat: add cell to vertex function --- README.md | 2 +- h3.go | 7 +++++++ h3_test.go | 10 ++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 29175c5..a2d6c1a 100644 --- a/README.md +++ b/README.md @@ -123,7 +123,7 @@ func ExampleLatLngToCell() { | `directedEdgeToCells` | `DirectedEdge#Cells` | | `originToDirectedEdges` | `Cell#DirectedEdges` | | `directedEdgeToBoundary` | `DirectedEdge#Boundary` | -| `cellToVertex` | TODO | +| `cellToVertex` | `CellToVertex` | | `cellToVertexes` | TODO | | `vertexToLatLng` | TODO | | `isValidVertex` | `IsValidVertex` | diff --git a/h3.go b/h3.go index 3819b65..6d5be28 100644 --- a/h3.go +++ b/h3.go @@ -713,6 +713,13 @@ func LocalIJToCell(origin Cell, ij CoordIJ) Cell { return Cell(out) } +func CellToVertex(c Cell, vertexNum int) Cell { + var out C.H3Index + C.cellToVertex(C.H3Index(c), C.int(vertexNum), &out) + + return Cell(out) +} + func IsValidVertex(c Cell) bool { return C.isValidVertex(C.H3Index(c)) == 1 } diff --git a/h3_test.go b/h3_test.go index e118228..560b7a4 100644 --- a/h3_test.go +++ b/h3_test.go @@ -622,6 +622,16 @@ func TestPentagons(t *testing.T) { } } +func TestCellToVertex(t *testing.T) { + t.Parallel() + + validVertex := CellToVertex(validCell, 0) + invalidVertex := CellToVertex(validCell, 6) + + assertEqual(t, 0x2050dab63fffffff, validVertex) + assertEqual(t, 0, invalidVertex) +} + func TestIsValidVertex(t *testing.T) { t.Parallel() From 011422a6b52d6b28ce4e204adc7a613ea9ba1179 Mon Sep 17 00:00:00 2001 From: Mojtaba Arezoomand Date: Sat, 17 Aug 2024 23:50:35 +0330 Subject: [PATCH 05/11] feat: added cell to vertexes function --- README.md | 2 +- h3.go | 12 ++++++++++-- h3_test.go | 20 ++++++++++++++++++++ 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index a2d6c1a..729df65 100644 --- a/README.md +++ b/README.md @@ -124,7 +124,7 @@ func ExampleLatLngToCell() { | `originToDirectedEdges` | `Cell#DirectedEdges` | | `directedEdgeToBoundary` | `DirectedEdge#Boundary` | | `cellToVertex` | `CellToVertex` | -| `cellToVertexes` | TODO | +| `cellToVertexes` | `cellToVertexes` | | `vertexToLatLng` | TODO | | `isValidVertex` | `IsValidVertex` | | `gridDistance` | `GridDistance`, `Cell#GridDistance` | diff --git a/h3.go b/h3.go index 6d5be28..f3618ac 100644 --- a/h3.go +++ b/h3.go @@ -60,8 +60,9 @@ const ( base16 = 16 bitSize = 64 - numCellEdges = 6 - numEdgeCells = 2 + numCellEdges = 6 + numEdgeCells = 2 + numCellVertexes = 6 DegsToRads = math.Pi / 180.0 RadsToDegs = 180.0 / math.Pi @@ -720,6 +721,13 @@ func CellToVertex(c Cell, vertexNum int) Cell { return Cell(out) } +func CellToVertexes(c Cell) []Cell { + out := make([]C.H3Index, numCellVertexes) + C.cellToVertexes(C.H3Index(c), &out[0]) + + return cellsFromC(out, true, false) +} + func IsValidVertex(c Cell) bool { return C.isValidVertex(C.H3Index(c)) == 1 } diff --git a/h3_test.go b/h3_test.go index 560b7a4..c272953 100644 --- a/h3_test.go +++ b/h3_test.go @@ -632,6 +632,26 @@ func TestCellToVertex(t *testing.T) { assertEqual(t, 0, invalidVertex) } +func TestCellToVertexes(t *testing.T) { + t.Parallel() + + testCases := []struct { + cell Cell + numVertexes int + }{ + {cell: validCell, numVertexes: 6}, + {cell: pentagonCell, numVertexes: 5}, + {cell: -1, numVertexes: 0}, // Invalid cel. + } + + for _, tc := range testCases { + t.Run(fmt.Sprint(tc.numVertexes), func(t *testing.T) { + vertexes := CellToVertexes(tc.cell) + assertEqual(t, tc.numVertexes, len(vertexes)) + }) + } +} + func TestIsValidVertex(t *testing.T) { t.Parallel() From 04a186842eaaa8c78f7f15d0b190491067b75aba Mon Sep 17 00:00:00 2001 From: Mojtaba Arezoomand Date: Sat, 17 Aug 2024 23:51:47 +0330 Subject: [PATCH 06/11] test: improve tests for cell to vertex function --- h3_test.go | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/h3_test.go b/h3_test.go index c272953..6f4ec1b 100644 --- a/h3_test.go +++ b/h3_test.go @@ -625,11 +625,22 @@ func TestPentagons(t *testing.T) { func TestCellToVertex(t *testing.T) { t.Parallel() - validVertex := CellToVertex(validCell, 0) - invalidVertex := CellToVertex(validCell, 6) + testCases := []struct { + cell Cell + expectedVertex Cell + vertexNum int + }{ + {cell: validCell, expectedVertex: 0x2050dab63fffffff, vertexNum: 0}, + {cell: validCell, expectedVertex: 0, vertexNum: 6}, // vertex num should be between 0 and 5 for hexagonal cells. + } + + for i, tc := range testCases { + t.Run(fmt.Sprint(i), func(t *testing.T) { + vertex := CellToVertex(tc.cell, tc.vertexNum) + assertEqual(t, tc.expectedVertex, vertex) + }) + } - assertEqual(t, 0x2050dab63fffffff, validVertex) - assertEqual(t, 0, invalidVertex) } func TestCellToVertexes(t *testing.T) { From 4b62928c06ef7001ded74f8f3305aa21b4f81366 Mon Sep 17 00:00:00 2001 From: Mojtaba Arezoomand Date: Sun, 18 Aug 2024 00:01:46 +0330 Subject: [PATCH 07/11] feat: added vertex to lat lng method --- README.md | 2 +- h3.go | 7 +++++++ h3_test.go | 19 +++++++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 729df65..93ad9c1 100644 --- a/README.md +++ b/README.md @@ -125,7 +125,7 @@ func ExampleLatLngToCell() { | `directedEdgeToBoundary` | `DirectedEdge#Boundary` | | `cellToVertex` | `CellToVertex` | | `cellToVertexes` | `cellToVertexes` | -| `vertexToLatLng` | TODO | +| `vertexToLatLng` | `VertexToLatLng` | | `isValidVertex` | `IsValidVertex` | | `gridDistance` | `GridDistance`, `Cell#GridDistance` | | `gridPathCells` | `GridPath`, `Cell#GridPath` | diff --git a/h3.go b/h3.go index f3618ac..a473af6 100644 --- a/h3.go +++ b/h3.go @@ -728,6 +728,13 @@ func CellToVertexes(c Cell) []Cell { return cellsFromC(out, true, false) } +func VertexToLatLng(vertex Cell) LatLng { + var out C.LatLng + C.vertexToLatLng(C.H3Index(vertex), &out) + + return latLngFromC(out) +} + func IsValidVertex(c Cell) bool { return C.isValidVertex(C.H3Index(c)) == 1 } diff --git a/h3_test.go b/h3_test.go index 6f4ec1b..dbb84cd 100644 --- a/h3_test.go +++ b/h3_test.go @@ -663,6 +663,25 @@ func TestCellToVertexes(t *testing.T) { } } +func TestVertexToLatLng(t *testing.T) { + t.Parallel() + + testCases := []struct { + vertex Cell + expectedLatLng LatLng + }{ + {vertex: CellToVertex(validCell, 0), expectedLatLng: LatLng{Lat: 67.22475, Lng: -168.52301}}, + {vertex: -1, expectedLatLng: LatLng{}}, // Invalid vertex. + } + + for i, tc := range testCases { + t.Run(fmt.Sprint(i), func(t *testing.T) { + latLng := VertexToLatLng(tc.vertex) + assertEqualLatLng(t, tc.expectedLatLng, latLng) + }) + } +} + func TestIsValidVertex(t *testing.T) { t.Parallel() From d5c086e792b8a1359d069e3f96333e35671cb0e0 Mon Sep 17 00:00:00 2001 From: Mojtaba Arezoomand Date: Sun, 18 Aug 2024 00:17:55 +0330 Subject: [PATCH 08/11] fix: added t.Parallel() for subtests --- h3_test.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/h3_test.go b/h3_test.go index dbb84cd..b32c744 100644 --- a/h3_test.go +++ b/h3_test.go @@ -635,7 +635,9 @@ func TestCellToVertex(t *testing.T) { } for i, tc := range testCases { + tc := tc t.Run(fmt.Sprint(i), func(t *testing.T) { + t.Parallel() vertex := CellToVertex(tc.cell, tc.vertexNum) assertEqual(t, tc.expectedVertex, vertex) }) @@ -656,7 +658,9 @@ func TestCellToVertexes(t *testing.T) { } for _, tc := range testCases { + tc := tc t.Run(fmt.Sprint(tc.numVertexes), func(t *testing.T) { + t.Parallel() vertexes := CellToVertexes(tc.cell) assertEqual(t, tc.numVertexes, len(vertexes)) }) @@ -675,7 +679,9 @@ func TestVertexToLatLng(t *testing.T) { } for i, tc := range testCases { + tc := tc t.Run(fmt.Sprint(i), func(t *testing.T) { + t.Parallel() latLng := VertexToLatLng(tc.vertex) assertEqualLatLng(t, tc.expectedLatLng, latLng) }) From d488fcdebb249f50ef10d6ff196023edb8e05478 Mon Sep 17 00:00:00 2001 From: Mojtaba Arezoomand Date: Sun, 18 Aug 2024 00:23:37 +0330 Subject: [PATCH 09/11] fix: fix lint issues --- h3_test.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/h3_test.go b/h3_test.go index b32c744..8dfbd0c 100644 --- a/h3_test.go +++ b/h3_test.go @@ -636,13 +636,13 @@ func TestCellToVertex(t *testing.T) { for i, tc := range testCases { tc := tc + t.Run(fmt.Sprint(i), func(t *testing.T) { t.Parallel() vertex := CellToVertex(tc.cell, tc.vertexNum) assertEqual(t, tc.expectedVertex, vertex) }) } - } func TestCellToVertexes(t *testing.T) { @@ -659,6 +659,7 @@ func TestCellToVertexes(t *testing.T) { for _, tc := range testCases { tc := tc + t.Run(fmt.Sprint(tc.numVertexes), func(t *testing.T) { t.Parallel() vertexes := CellToVertexes(tc.cell) @@ -680,6 +681,7 @@ func TestVertexToLatLng(t *testing.T) { for i, tc := range testCases { tc := tc + t.Run(fmt.Sprint(i), func(t *testing.T) { t.Parallel() latLng := VertexToLatLng(tc.vertex) From 78b8a47757d1b41a03d6e67208506d481fb61cf8 Mon Sep 17 00:00:00 2001 From: Mojtaba Arezoomand Date: Sun, 18 Aug 2024 00:27:13 +0330 Subject: [PATCH 10/11] fix: fix lint issues --- h3_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/h3_test.go b/h3_test.go index 8dfbd0c..a710d9a 100644 --- a/h3_test.go +++ b/h3_test.go @@ -636,9 +636,9 @@ func TestCellToVertex(t *testing.T) { for i, tc := range testCases { tc := tc - t.Run(fmt.Sprint(i), func(t *testing.T) { t.Parallel() + vertex := CellToVertex(tc.cell, tc.vertexNum) assertEqual(t, tc.expectedVertex, vertex) }) @@ -659,9 +659,9 @@ func TestCellToVertexes(t *testing.T) { for _, tc := range testCases { tc := tc - t.Run(fmt.Sprint(tc.numVertexes), func(t *testing.T) { t.Parallel() + vertexes := CellToVertexes(tc.cell) assertEqual(t, tc.numVertexes, len(vertexes)) }) @@ -681,9 +681,9 @@ func TestVertexToLatLng(t *testing.T) { for i, tc := range testCases { tc := tc - t.Run(fmt.Sprint(i), func(t *testing.T) { t.Parallel() + latLng := VertexToLatLng(tc.vertex) assertEqualLatLng(t, tc.expectedLatLng, latLng) }) From fb31b100ccce69a6fc2fa1ce3439c0de2caee76d Mon Sep 17 00:00:00 2001 From: Mojtaba Arezoomand Date: Sun, 18 Aug 2024 00:36:50 +0330 Subject: [PATCH 11/11] fix: fix lint issues --- h3_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/h3_test.go b/h3_test.go index a710d9a..04a3a3c 100644 --- a/h3_test.go +++ b/h3_test.go @@ -636,6 +636,7 @@ func TestCellToVertex(t *testing.T) { for i, tc := range testCases { tc := tc + t.Run(fmt.Sprint(i), func(t *testing.T) { t.Parallel() @@ -681,6 +682,7 @@ func TestVertexToLatLng(t *testing.T) { for i, tc := range testCases { tc := tc + t.Run(fmt.Sprint(i), func(t *testing.T) { t.Parallel()