If a user has some input coordinates, it is quite onerous to get a polygon object.
For example, the following program has a hardcoded list of coordinates, constructs a polygon from those coordinates, and then calculates the centroid of the polygon.
func main() {
coords := []float64{
144.9324481856214,
-37.80831791530031,
151.1946474301501,
-33.85326738062629,
153.011491595451,
-27.465643838083587,
130.84533948192757,
-12.45094813855576,
115.8443383313529,
-31.96739822401411,
144.9324481856214,
-37.80831791530031,
}
seq := geom.NewSequence(coords, geom.DimXY)
ring, err := geom.NewLineString(seq)
if err != nil {
log.Fatalf("could not build ring: %v", err)
}
poly, err := geom.NewPolygon([]geom.LineString{ring})
if err != nil {
log.Fatalf("could not build polygon: %v", err)
}
log.Printf("Center: %v", poly.Centroid().AsText())
}
It would be nice if there were alternate (easier/terser) ways to create geometry objects. Potentially this could live in a separate package (named gbuild
in this example, but that's just a placeholder). For example, it might look like this:
func main() {
coords := []float64{
144.9324481856214,
-37.80831791530031,
151.1946474301501,
-33.85326738062629,
153.011491595451,
-27.465643838083587,
130.84533948192757,
-12.45094813855576,
115.8443383313529,
-31.96739822401411,
144.9324481856214,
-37.80831791530031,
}
poly, err := gbuild.PolygonFromCoords([][]float64{coords})
log.Fatalf("could not build polygon: %v", err)
}
log.Printf("Center: %v", poly.Centroid().AsText())
}
feature