我在一個文件中有坐標列表,我想獲取它們的多邊形。我使用wkb庫來加載坐標,但是當我嘗試將它們設置到wkb.Polygon對象中時,出現錯誤:panic: interface conversion: interface {} is [][][]float64, not [][]geom.Coord這是我的代碼:var cc interface {} = collection.Features[0].Geometry.Polygonc := 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)
1 回答
慕森王
TA貢獻1777條經驗 獲得超3個贊
首先,您需要像這樣創建一個通用多邊形:
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)
- 1 回答
- 0 關注
- 136 瀏覽
添加回答
舉報
0/150
提交
取消
