go - Golang 从坐标列表创建 wkb.Polygon
问题描述
我在一个文件中有坐标列表,我想获取它们的多边形。我使用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)
解决方案
首先,您需要像这样创建一个通用多边形:
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)
推荐阅读
- swift - Firebase Firestore 使用 ID 引用获取数据,然后获取引用
- java - JavaFX fx:id NullPointerException
- python - 使用 python 在 pyfpdf 中写入单元格后,Unicode Bangla Avro Phonetic 的字母被互换。解决办法是什么?
- reactjs - 你如何处理加载时 Typescript 类型的局部状态变量?
- html - 私人聊天气泡相互叠加
- laravel - Lavavel js脚本
- java - 我的这个代码应该返回最常见元素的频率
- python - 使用python将简单矩阵转换为关联矩阵
- swift - Xcode 中 SwiftUI 中的分段 11 非零退出代码错误
- xml - 如何解决此项目构建错误:不可解析的 POM 错误?