2026-01-07 16:07:51 +00:00
|
|
|
package h3utils
|
2025-11-13 20:01:15 +00:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
2025-11-15 21:33:01 +00:00
|
|
|
"strings"
|
2025-11-13 20:01:15 +00:00
|
|
|
|
2025-11-21 17:44:29 +00:00
|
|
|
"github.com/Gleipnir-Technology/go-geojson2h3/v2"
|
2025-11-13 20:01:15 +00:00
|
|
|
"github.com/tidwall/geojson"
|
2025-11-13 23:48:41 +00:00
|
|
|
"github.com/uber/h3-go/v4"
|
2025-11-13 20:01:15 +00:00
|
|
|
)
|
|
|
|
|
|
2025-11-19 15:21:06 +00:00
|
|
|
/*
|
|
|
|
|
func h3ToBoundsGeoJSON(c h3.Cell) (string, error) {
|
|
|
|
|
b, err := h3.CellToBoundary(c)
|
2025-11-13 20:01:15 +00:00
|
|
|
if err != nil {
|
2025-11-19 15:21:06 +00:00
|
|
|
respondError(w, "Failed to get cell boundary", err, http.StatusInternalServerError)
|
|
|
|
|
return
|
2025-11-13 20:01:15 +00:00
|
|
|
}
|
2025-11-19 15:21:06 +00:00
|
|
|
features, err := geojson2h3.ToFeatureCollection(b)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", fmt.Errorf("Failed to convert boundary to
|
2025-11-13 20:01:15 +00:00
|
|
|
}
|
2025-11-19 15:21:06 +00:00
|
|
|
*/
|
2025-11-15 21:33:01 +00:00
|
|
|
|
2026-01-07 16:07:51 +00:00
|
|
|
func ToCell(s string) (h3.Cell, error) {
|
2026-01-05 23:25:16 +00:00
|
|
|
c := h3.CellFromString(s)
|
|
|
|
|
if !c.IsValid() {
|
|
|
|
|
return c, fmt.Errorf("Invalid cell definition '%s'", s)
|
|
|
|
|
}
|
|
|
|
|
return c, nil
|
|
|
|
|
}
|
2026-01-07 16:07:51 +00:00
|
|
|
func H3ToGeoJSON(indexes []h3.Cell) (interface{}, error) {
|
2025-11-15 21:33:01 +00:00
|
|
|
featureCollection, err := geojson2h3.ToFeatureCollection(indexes)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", fmt.Errorf("Failed to get feature collection: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return featureCollection.JSON(), nil
|
|
|
|
|
}
|
2025-11-19 15:21:06 +00:00
|
|
|
|
2025-11-13 20:01:15 +00:00
|
|
|
func main2() {
|
|
|
|
|
resolution := 9
|
|
|
|
|
object, err := geojson.Parse(`{"type":"FeatureCollection","features":[{"type":"Feature","properties":{"shape":"Polygon","name":"Unnamed Layer","category":"default"},"geometry":{"type":"Polygon","coordinates":[[[-73.901303,40.756892],[-73.893924,40.743755],[-73.871476,40.756278],[-73.863378,40.764175],[-73.871444,40.768467],[-73.879852,40.760014],[-73.885515,40.764045],[-73.891522,40.761054],[-73.901303,40.756892]]]},"id":"a6ca1b7e-9ddf-4425-ad07-8a895f7d6ccf"}]}`, nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
indexes, err := geojson2h3.ToH3(resolution, object)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
for _, index := range indexes {
|
2025-11-13 23:48:41 +00:00
|
|
|
fmt.Printf("h3index: %s\n", index.String())
|
2025-11-13 20:01:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
featureCollection, err := geojson2h3.ToFeatureCollection(indexes)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
fmt.Println("Polyfill:")
|
|
|
|
|
fmt.Println(featureCollection.JSON())
|
|
|
|
|
}
|
2025-11-13 23:48:41 +00:00
|
|
|
|
|
|
|
|
// Given a cell at a smaller resolution remap it to the larger resolution
|
|
|
|
|
func scaleCell(cell h3.Cell, resolution int) (h3.Cell, error) {
|
|
|
|
|
r := cell.Resolution()
|
|
|
|
|
if r == resolution {
|
|
|
|
|
return cell, nil
|
|
|
|
|
}
|
|
|
|
|
latLong, err := cell.LatLng()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return 0, fmt.Errorf("Failed to get latlng: %w", err)
|
|
|
|
|
}
|
|
|
|
|
scaled, err := h3.LatLngToCell(latLong, resolution)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return 0, fmt.Errorf("Failed to create latlng: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return scaled, nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-08 15:00:30 +00:00
|
|
|
func GetCell(x, y float64, resolution int) (h3.Cell, error) {
|
2025-11-13 23:48:41 +00:00
|
|
|
latLng := h3.NewLatLng(y, x)
|
|
|
|
|
return h3.LatLngToCell(latLng, resolution)
|
|
|
|
|
}
|
2025-11-15 21:33:01 +00:00
|
|
|
|
2026-01-07 16:07:51 +00:00
|
|
|
func CellToPostgisGeometry(c h3.Cell) (string, error) {
|
2025-11-15 21:33:01 +00:00
|
|
|
boundary, err := h3.CellToBoundary(c)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", fmt.Errorf("Failed to get cell boundary: %w", err)
|
|
|
|
|
}
|
|
|
|
|
var sb strings.Builder
|
|
|
|
|
|
|
|
|
|
for i, p := range boundary {
|
|
|
|
|
if i > 0 {
|
|
|
|
|
sb.WriteString(",")
|
|
|
|
|
}
|
|
|
|
|
fmt.Fprintf(&sb, "%g %g", p.Lng, p.Lat)
|
|
|
|
|
}
|
|
|
|
|
// add the first point on to the end to close the polygon
|
|
|
|
|
sb.WriteString(",")
|
|
|
|
|
fmt.Fprintf(&sb, "%g %g", boundary[0].Lng, boundary[0].Lat)
|
|
|
|
|
|
|
|
|
|
return fmt.Sprintf("POLYGON((%s))", sb.String()), nil
|
|
|
|
|
}
|