package shp import ( "context" "fmt" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/net/ghttp" "github.com/tidwall/gjson" shp2 "github.com/tiger1103/gfast/v3/api/v1/common/shp" "io" "net/http" "regexp" "strings" ) const MaxNeighborsLen = 8 //最大邻居个数 const SOURCE = "static/source/" const Gisfile = "gisfile/" // 84的投影文件 const WGS84_PRJ = "GEOGCS[\"GCS_WGS_1984\",DATUM[\"D_WGS_1984\",SPHEROID[\"WGS_1984\",6378137.0,298.257223563]],PRIMEM[\"Greenwich\",0.0],UNIT[\"Degree\",0.0174532925199433]]" func InitShp(group *ghttp.RouterGroup) { group.Group("/shp", func(group *ghttp.RouterGroup) { group.Bind(new(SHP)) }) } type SHP struct { } type SHPLoadReq struct { g.Meta `path:"load" summary:"cesium加载shp" method:"get" tags:"shp相关" ` //SourceID string `json:"source_id" dc:"资源id" v:"required"` Path string `json:"path" dc:"路径" v:"required"` } type SHPLoadRes struct { shp2.ShpObj } //func (receiver SHP) LoadSHP(ctx context.Context, req *SHPLoadReq) (res *SHPLoadRes, err error) { // err, obj := shp2.ReadShp(req.Path) // if err != nil { // return nil, err // } // res = &SHPLoadRes{} // res.Points = obj.Points // res.Polylines = obj.Polylines // res.Polygons = obj.Polygons // return res, err //} type Range struct { MinX float64 `json:"min_x"` MinY float64 `json:"min_y"` MaxX float64 `json:"max_x"` MaxY float64 `json:"max_y"` } type Position struct { X float64 `json:"x"` Y float64 `json:"y"` Z float64 `json:"z"` Attr map[string]interface{} `json:"attr"` } type Text struct { X float64 `json:"x"` Y float64 `json:"y"` Text string `json:"text"` } type Circle struct { X float64 `json:"x"` Y float64 `json:"y"` Z float64 `json:"z"` Radius float64 `json:"radius"` } type Polyline struct { Positions []Position `json:"positions"` Attr map[string]interface{} `json:"attr"` Range Range `json:"range"` } type Polygon struct { Positions []Position `json:"positions"` Attr map[string]interface{} `json:"attr"` Range Range `json:"range"` } type MultiPolygon struct { Polygons []Polygon `json:"polygons"` Attr map[string]interface{} `json:"attr"` Range Range `json:"range"` } type MultiPolyline struct { Polylines []Polyline `json:"polylines"` Attr map[string]interface{} `json:"attr"` Range Range `json:"range"` } type LayerData struct { LayerName string `json:"layer_name"` Proj4 string `json:"proj4"` Texts []Text `json:"texts"` Circles []Circle `json:"circles"` Points []Position `json:"points"` Polylines []Polyline `json:"polylines"` Polygons []Polygon `json:"polygons"` MultiPolygons []MultiPolygon `json:"multi_polygons"` MultiPolylines []MultiPolyline `json:"multi_polylines"` } type Directory struct { Name string `json:"name"` IsDir bool `json:"is_dir"` Data []LayerData `json:"data"` Children []Directory `json:"children"` } type Response struct { Code int `json:"code"` Data Directory `json:"data"` } // JoinLoadSHP 原本的LoadSHP有问题,直接调用远程的接口 func (receiver SHP) JoinLoadSHP(ctx context.Context, req *SHPLoadReq) (res *SHPLoadRes, err error) { res = new(SHPLoadRes) if req.Path[0] == '/' { req.Path = req.Path[1:] } url := "http://192.168.1.177:8921/yjearth5/api/v1/vector/load?path=/project/zmkg/" + req.Path reqs, err := http.Get(url) if err != nil { return nil, err } defer reqs.Body.Close() body, err := io.ReadAll(reqs.Body) if err != nil { return nil, err } if gjson.Get(string(body), "message").String() == "资源不存在" { return nil, fmt.Errorf("资源不存在") } var list shp2.ShpObj processShapes(body, "data.children.0.data.0.polylines", &list) processShapes(body, "data.children.0.data.0.polygons", &list) processShapes(body, "data.children.0.data", &list) if list.Polylines == nil { list.Polylines = []shp2.Polyline{} } if list.Polygons == nil { list.Polygons = []shp2.Polygon{} } if list.Points == nil { list.Points = []shp2.Point{} } res.Polygons = list.Polygons res.Polylines = list.Polylines res.Points = list.Points return res, err } func Adasda(path string) (res *SHPLoadRes) { fmt.Println("加载shp文件", path) res = new(SHPLoadRes) if path[0] == '/' { path = path[1:] } url := "http://192.168.1.177:8921/yjearth5/api/v1/vector/load?path=/project/zmkg/" + path reqs, err := http.Get(url) if err != nil { fmt.Println("请求数据失败") } defer reqs.Body.Close() body, err := io.ReadAll(reqs.Body) if err != nil { fmt.Errorf("读取资源失败") } if gjson.Get(string(body), "message").String() == "资源不存在" { fmt.Errorf("资源不存在") } var list = shp2.ShpObj{ Polylines: []shp2.Polyline{}, Polygons: []shp2.Polygon{}, Points: []shp2.Point{}, } processShapes(body, "data.children.0.data.0.polylines", &list) processShapes(body, "data.children.0.data.0.polygons", &list) processShapes(body, "data.children.0.data", &list) res.Polygons = list.Polygons res.Polylines = list.Polylines res.Points = list.Points return } func processShapes(data []byte, path string, shapes *shp2.ShpObj) { gjson.GetBytes(data, path).ForEach(func(key, shape gjson.Result) bool { // 面 var pointsList []shp2.Point psGet := shape.Get("positions") if psGet.Exists() { psGet.ForEach(func(posKey, value gjson.Result) bool { longitude := value.Get("x").Float() latitude := value.Get("y").Float() altitude := value.Get("z").Float() pointsList = append(pointsList, shp2.Point{ Lng: longitude, Lat: latitude, Alt: altitude, }) return true }) if len(pointsList) > 0 { shapes.Polylines = append(shapes.Polylines, shp2.Polyline{ Positions: pointsList, Width: "5", Color: "#f00", }) } if shape.Get("attr").Exists() { aName := shape.Get("attr.NAME").String() fmt.Println("!!! ", aName) shapes.Polylines[len(shapes.Polylines)-1].Property = shp2.Property{ Name: aName, } } if shape.Get("range").Exists() { minX := shape.Get("range.min_x").Float() minY := shape.Get("range.min_y").Float() maxX := shape.Get("range.max_x").Float() maxY := shape.Get("range.max_y").Float() shapes.Polylines[len(shapes.Polylines)-1].Range = shp2.Box{ MinX: minX, MinY: minY, MaxX: maxX, MaxY: maxY, } } } else { //fmt.Println("!!! ", shape.Get("points")) // 点 var point []shp2.Point shape.Get("points").ForEach(func(posKey, value gjson.Result) bool { aName := value.Get("attr.NAME") if value.Get("attr.NAME").Exists() { //排除nc 和 空 isPureNumber, _ := regexp.MatchString(`^\d+$`, aName.String()) if strings.Contains(aName.String(), "NC") || strings.TrimSpace(aName.String()) == "" || isPureNumber { return true } longitude := value.Get("x").Float() latitude := value.Get("y").Float() altitude := value.Get("z").Float() point = append(point, shp2.Point{ Lng: longitude, Lat: latitude, Alt: altitude, Property: shp2.Property{ Name: aName.String(), }, }) } return true }) if len(point) > 0 { shapes.Points = append(shapes.Points, point...) } } return true }) }