我在一个文件中有一个坐标列表,我想要得到他们的多边形。我使用wkb库加载坐标,但是当我试图将它们设置到wkb.Polygon对象中时,我得到一个错误:
panic: interface conversion: interface {} is [][][]float64, not [][]geom.Coord这是我的代码:
var cc interface {} = collection.Features[0].Geometry.Polygon
c := cc.([][]geom.Coord)
po, err := wkb.Polygon{}.SetCoords(c)我也试过了:
c := collection.Features[0].Geometry.Polygon.([][]geom.Coord)但是我得到了:
Invalid type assertion: collection.Features[0].Geometry.Polygon.([][]geom.Coord) (non-interface type [][][]float64 on left)发布于 2020-01-26 03:46:32
首先,您需要创建一个常规多边形,如下所示:
package main
import (
"fmt"
"github.com/twpayne/go-geom"
)
func main() {
unitSquare := geom.NewPolygon(geom.XY).MustSetCoords([][]geom.Coord{
{{0, 0}, {1, 0}, {1, 1}, {0, 1}, {0, 0}},
})
fmt.Printf("unitSquare.Area() == %f", unitSquare.Area())
}然后,您可以将其编组为wkb格式。
// marshal into wkb with litten endian
b, err := wkb.Marshal(unitSquare, wkb.NDR)
if err != nil {
fmt.Printf("wkb marshal error: %s\n", err.Error())
return
}
fmt.Println(b)https://stackoverflow.com/questions/59909825
复制相似问题