[go] Having issues getting the bounding CellIDs from a loop / polygon

32 views
Skip to first unread message

Chance Dinkins

unread,
Dec 1, 2020, 11:01:49 PM12/1/20
to s2geometry-io
Hey folks,

First of all, thank you for the library! I appreciate all of the hard work you have put into it. 

Second, I'm having trouble getting the bounding CellIDs. I am looking to obtain the largest possible cell IDs possible, hopefully of various sizes, to completely fill the given polygons. I assume that  CellUnionBound() would do the trick but I'm getting the same output for all polygons. I'm not sure where I'm going wrong or if my assumption on which func to use is accurate. Thank you for taking the time to read this! 

The output I'm getting is: 

1152921504606846976, 3458764513820540928, 5764607523034234880, 8070450532247928832, 10376293541461622784, 12682136550675316736


package main

import (
"encoding/json"
"fmt"
"os"

)

type Location struct {
Geometry json.RawMessage `json:"geometry"`
}

func main() {
f, err := os.Open("./geo.json")
if err != nil {
panic(err)
}
s, _ := f.Stat()
b := make([]byte, s.Size())
f.Read(b)
loc := &Location{}
var g geom.T
err = json.Unmarshal(b, &loc)
if err != nil {
panic(err)
}
err = geojson.Unmarshal(loc.Geometry, &g)
if err != nil {
panic(err)
}
mp := g.(*geom.MultiPolygon)
c := mp.Coords()
for _, polygon := range c {
var xv float64
var xi = -1
points := []s2.Point{}
for _, l := range polygon {
for i := 0; i < len(l)-1; i++ {
p := l[i]
x := p.X()
latlng := s2.LatLngFromDegrees(p.Y(), x)
point := s2.PointFromLatLng(latlng)
points = append(points, point)
if xi == -1 || x > xv {
xv = x
xi = i
}
}
}
if xi == -1 {
continue
}
points = append(points[xi:], points[:xi]...)

loop := s2.LoopFromPoints(points)
printUnionBound(loop.CellUnionBound())

polygon := s2.PolygonFromLoops([]*s2.Loop{loop})
printUnionBound(polygon.CellUnionBound())
}
}

func printUnionBound(cellIDs []s2.CellID) {
l := len(cellIDs)
for i := 0; i < l; i++ {
cellID := cellIDs[i]
if i == l-1 {
fmt.Printf("%d\n", cellID)
} else {
fmt.Printf("%d, ", cellID)
}
}
}

Reply all
Reply to author
Forward
0 new messages