package solaranalyzer import ( "encoding/json" "os/exec" "path/filepath" "strings" "github.com/tidwall/gjson" ) const ( drillingHole = "hole" // 钻孔 pillar = "pile" // 桩基 bracket = "shelves" // 支架 solarPanel = "pho" // 光伏板 ) type Point struct { Points []XYPosition // 光伏板的坐标点 ID int // pv_module 表中的自增ID } type XYPosition struct { X float64 `json:"x"` Y float64 `json:"y"` } // getSolarPanelRanges 解析光伏板 JSON func getSolarPanelRanges(file string) []Point { var points []Point gjson.Parse(file).ForEach(func(_, record gjson.Result) bool { record.Get("detail").ForEach(func(_, detail gjson.Result) bool { id := record.Get("id").Int() var longitudeAndLatitude []XYPosition gjson.Get(detail.String(), "positions").ForEach(func(_, position gjson.Result) bool { longitude := position.Get("lng").Float() latitude := position.Get("lat").Float() longitudeAndLatitude = append(longitudeAndLatitude, XYPosition{X: longitude, Y: latitude}) return true }) points = append(points, Point{ID: int(id), Points: longitudeAndLatitude}) return true }) return true }) return points } // convertToLongitudeAndLatitude 根据 tif 文件将坐标转换为经纬度 func convertToLongitudeAndLatitude(tifFilePath string, coordinatePoints []XYPosition) []XYPosition { positionsJSON, err := json.Marshal(coordinatePoints) if err != nil { panic(err) } convertPath, err := filepath.Abs("./convert") if err != nil { panic(err) } output, err := exec.Command(convertPath, filepath.Clean(tifFilePath), string(positionsJSON)).CombinedOutput() if err != nil { panic(err) } outputLines := strings.Split(string(output), "\n") lastLine := outputLines[len(outputLines)-2] // 获取最后一行 // 解析二进制返回的 JSON, 并将其转换为 XYPosition 结构 var longitudeAndLatitude []XYPosition gjson.Get(lastLine, "data").ForEach(func(_, position gjson.Result) bool { longitudeAndLatitude = append(longitudeAndLatitude, XYPosition{ X: position.Get("y").Float(), Y: position.Get("x").Float(), }) return true }) return longitudeAndLatitude }