Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat : Cells to multipolygon #64

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,15 @@ func ExampleLatLngToCell() {
## Bindings

| C API | Go API |
| ---------------------------- | -------------------------------------------------- |
| ---------------------------- |----------------------------------------------------|
| `latLngToCell` | `LatLngToCell`, `LatLng#Cell` |
| `cellToLatLng` | `CellToLatLng`, `Cell#LatLng` |
| `cellToBoundary` | `CellToBoundary`, `Cell#Boundary` |
| `gridDisk` | `GridDisk`, `Cell#GridDisk` |
| `gridDiskDistances` | `GridDiskDistances`, `Cell#GridDiskDistances` |
| `gridRingUnsafe` | N/A |
| `polygonToCells` | `PolygonToCells`, `GeoPolygon#Cells` |
| `cellsToMultiPolygon` | TODO |
| `cellsToMultiPolygon` | `CellsToMultiPolygon` |
| `degsToRads` | `DegsToRads` |
| `radsToDegs` | `RadsToDegs` |
| `greatCircleDistance` | `GreatCircleDistance* (3/3)` |
Expand Down
33 changes: 30 additions & 3 deletions h3.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,10 @@ type (
}

// LinkedGeoPolygon is a linked-list of GeoPolygons.
// TODO: not implemented.
LinkedGeoPolygon struct{}
LinkedGeoPolygon struct {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In uber/h3, this seems to be doubly-linked: https://github.com/uber/h3/blob/138a7487adb0892715ea7ced785db4e9c38842d1/src/h3lib/include/h3api.h.in#L179

Does that need to be maintained here?

Data GeoPolygon
Next *LinkedGeoPolygon
}
)

func NewLatLng(lat, lng float64) LatLng {
Expand Down Expand Up @@ -246,8 +248,33 @@ func (p GeoPolygon) Cells(resolution int) []Cell {
return PolygonToCells(p, resolution)
}

func CellToGeoPolygon(cell Cell) GeoPolygon {
boundary := CellToBoundary(cell)
loop := make(GeoLoop, len(boundary))

for i, coord := range boundary {
loop[i] = LatLng{Lat: coord.Lat, Lng: coord.Lng}
}

return GeoPolygon{GeoLoop: loop}
}

func CellsToMultiPolygon(cells []Cell) *LinkedGeoPolygon {
panic("not implemented")
var head, current *LinkedGeoPolygon
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd expect this function to wrap cellsToMultiPolygon from the C core library, which handles details like ensuring duplicated internal vertices are not included. https://github.com/uber/h3/blob/138a7487adb0892715ea7ced785db4e9c38842d1/src/h3lib/include/h3api.h.in#L293


for _, cell := range cells {
geoPoly := CellToGeoPolygon(cell)

if head == nil {
head = &LinkedGeoPolygon{Data: geoPoly}
current = head
} else {
current.Next = &LinkedGeoPolygon{Data: geoPoly}
current = current.Next
}
}

return head
}

// PointDistRads returns the "great circle" or "haversine" distance between
Expand Down
115 changes: 115 additions & 0 deletions h3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -845,3 +845,118 @@

return c
}

func TestCellsToMultiPolygon(t *testing.T) {
t.Parallel()

validCellGeoLoop := GeoLoop{
{Lat: 67.22475, Lng: -168.52301},
{Lat: 67.14094, Lng: -168.62691},
{Lat: 67.06725, Lng: -168.49491},
{Lat: 67.07706, Lng: -168.25970},
{Lat: 67.16056, Lng: -168.15480},
{Lat: 67.23456, Lng: -168.28610},
}

lineStartCellGeoLoop := GeoLoop{
{Lat: 37.77201, Lng: -122.41701},
{Lat: 37.77369, Lng: -122.41594},
{Lat: 37.77520, Lng: -122.41720},
{Lat: 37.77502, Lng: -122.41953},
{Lat: 37.77334, Lng: -122.42060},
{Lat: 37.77183, Lng: -122.41934},
}

lineEndCellGeoLoop := GeoLoop{
{Lat: 33.88098, Lng: -118.35439},
{Lat: 33.88267, Lng: -118.35327},
{Lat: 33.88429, Lng: -118.35445},
{Lat: 33.88421, Lng: -118.35676},
{Lat: 33.88251, Lng: -118.35788},
{Lat: 33.88090, Lng: -118.35670},
}

testCases := []struct {
name string
cells []Cell
expected *LinkedGeoPolygon
}{
{
name: "Single Cell",
cells: []Cell{validCell},
expected: &LinkedGeoPolygon{
Data: GeoPolygon{
GeoLoop: validCellGeoLoop,
},
Next: nil,
},
},
{
name: "Multiple Cells",
cells: []Cell{validCell, lineStartCell, lineEndCell},
expected: &LinkedGeoPolygon{
Data: GeoPolygon{
GeoLoop: validCellGeoLoop,
},
Next: &LinkedGeoPolygon{
Data: GeoPolygon{
GeoLoop: lineStartCellGeoLoop,
},
Next: &LinkedGeoPolygon{
Data: GeoPolygon{
GeoLoop: lineEndCellGeoLoop,
},
Next: nil,
},
},
},
},
}

for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
result := CellsToMultiPolygon(tc.cells)
assertLinkedGeoPolygonEqual(t, tc.expected, result)
})
}
}

func assertLinkedGeoPolygonEqual(t *testing.T, expected, actual *LinkedGeoPolygon) {
t.Helper()
for expected != nil && actual != nil {

Check failure on line 928 in h3_test.go

View workflow job for this annotation

GitHub Actions / lint (1.18)

for statements should only be cuddled with assignments used in the iteration (wsl)
assertEqualGeoPolygon(t, expected.Data, actual.Data)
expected, actual = expected.Next, actual.Next
}
if expected != nil || actual != nil {

Check failure on line 932 in h3_test.go

View workflow job for this annotation

GitHub Actions / lint (1.18)

if statements should only be cuddled with assignments (wsl)
t.Errorf("LinkedGeoPolygons length mismatch")
}
}

func assertEqualGeoPolygon(t *testing.T, expected, actual GeoPolygon) {
t.Helper()
assertEqualGeoLoop(t, expected.GeoLoop, actual.GeoLoop)
}

func assertEqualGeoLoop(t *testing.T, expected, actual GeoLoop) {
t.Helper()
const tolerance = 1e-5

if len(expected) != len(actual) {
t.Errorf("GeoLoops length mismatch: expected %d, got %d", len(expected), len(actual))
return
}

for i, e := range expected {
a := actual[i]
if !floatsAreClose(e.Lat, a.Lat, tolerance) || !floatsAreClose(e.Lng, a.Lng, tolerance) {
t.Errorf("GeoLoop vertex mismatch at index %d: expected (%.17f, %.17f), got (%.17f, %.17f)",
i, e.Lat, e.Lng, a.Lat, a.Lng)
}
}
}

func floatsAreClose(a, b, tolerance float64) bool {
return math.Abs(a-b) <= tolerance
}
Loading