Files
zmkgC/internal/app/system/controller/qianqi_pingchang.go

289 lines
8.8 KiB
Go
Raw Normal View History

2025-07-07 20:11:59 +08:00
// ==========================================================================
// GFast自动生成controller操作代码。
// 生成日期2023-08-08 10:08:24
// 生成路径: internal/app/system/controller/qianqi_pingchang.go
// 生成人gfast
// desc:平场数据
// company:云南奇讯科技有限公司
// ==========================================================================
package controller
import (
"context"
"encoding/json"
"errors"
"github.com/gogf/gf/v2/crypto/gmd5"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
"github.com/tiger1103/gfast/v3/api/v1/common/globe"
"github.com/tiger1103/gfast/v3/api/v1/common/shp"
"github.com/tiger1103/gfast/v3/api/v1/common/tool"
"github.com/tiger1103/gfast/v3/api/v1/system"
"github.com/tiger1103/gfast/v3/internal/app/system/dao"
"github.com/tomchavakis/turf-go/constants"
"github.com/tomchavakis/turf-go/measurement"
"math"
"strings"
)
const MaxNeighborsLen = 8 //最大邻居个数
type qianqiPingchangController struct {
BaseController
}
var QianqiPingchang = new(qianqiPingchangController)
func (c *qianqiPingchangController) PingchangImport(ctx context.Context, req *system.PingchangImportReq) (res *system.UsualRes, err error) {
r := ghttp.RequestFromCtx(ctx)
files := r.GetUploadFiles("file")
names, err := files.Save(globe.SOURCE)
if err != nil {
return nil, err
}
//fmt.Println(names)
prefix := strings.Split(names[0], ".")[0]
shpfile := globe.SOURCE + "/" + prefix + ".shp"
err, s := shp.ReadShp(shpfile)
if err != nil {
return nil, err
}
var pcs []system.PC
//name := strings.Split(shpfile, ".")[0]
//导入平场区域时,只处理线部分
//marshal, err := json.Marshal(s.Polylines)
//if err != nil {
// return nil, err
//}
for _, v := range s.Polylines {
pc := system.PC{
ProjectID: req.ProjectID,
PCName: v.Name,
GridWidth: 3,
SHP: shpfile,
}
marshal, err := json.Marshal(v.Positions)
if err != nil {
continue
}
pc.Range = string(marshal)
pc.PcID = gmd5.MustEncryptString(string(marshal))
count, err := dao.QianqiPingchang.Ctx(ctx).Where(dao.QianqiPingchang.Columns().PcId, pc.PcID).Count()
if err != nil {
continue
}
if count == 0 {
pcs = append(pcs, pc)
}
}
if len(pcs) > 0 {
dao.QianqiPingchang.Ctx(ctx).Insert(&pcs)
}
return
}
func (c *qianqiPingchangController) PingchangUpdate(ctx context.Context, req *system.PingchangUpdateReq) (res *system.UsualRes, err error) {
var pc system.PC
if len(req.Points) > 0 {
marshal, err := json.Marshal(req.Points)
if err != nil {
return nil, err
}
pc.Points = string(marshal)
}
pc.GridWidth = req.GridWidth
pc.PCName = req.PCName
dao.QianqiPingchang.Ctx(ctx).Where("pc_id", req.PcID).Update(&gdb.Map{
dao.QianqiPingchang.Columns().PcName: req.PCName,
dao.QianqiPingchang.Columns().GridWidth: req.GridWidth,
dao.QianqiPingchang.Columns().Points: pc.Points,
})
return
}
func (c *qianqiPingchangController) PingChangList(ctx context.Context, req *system.PingChangListReq) (res *system.PingChangListRes, err error) {
var pcs []system.PC
dao.QianqiPingchang.Ctx(ctx).Where("project_id", req.ProjectID).Offset((req.Page - 1) * req.PageSize).Limit(req.PageSize).Order("pc_name asc,created_at desc").Scan(&pcs)
res = &system.PingChangListRes{}
for _, v := range pcs {
pc1 := system.PC1{
ProjectID: v.ProjectID,
PCName: v.PCName,
Progress: v.Progress,
GridWidth: v.GridWidth,
PcID: v.PcID,
Cut: v.Cut,
Fill: v.Fill,
Total: v.Total,
Area: v.Area,
Shp: v.SHP,
}
if len(v.Range) > 0 {
err1 := json.Unmarshal([]byte(v.Range), &pc1.Range)
if err1 != nil {
continue
}
}
pc1.Points = []system.Point{}
if len(v.Points) > 0 {
err2 := json.Unmarshal([]byte(v.Points), &pc1.Points)
if err2 != nil {
continue
}
}
pc1.SmoothPoints = []system.Point{}
if len(v.SmoothPoints) > 0 {
err3 := json.Unmarshal([]byte(v.SmoothPoints), &pc1.SmoothPoints)
if err3 != nil {
continue
}
}
res.Range = append(res.Range, pc1)
}
return
}
func (c *qianqiPingchangController) PingChangAdd(ctx context.Context, req *system.PingChangAddReq) (res *system.UsualRes, err error) {
marshal, errs := json.Marshal(req.Range)
if errs != nil {
return nil, errs
}
dao.QianqiPingchang.Ctx(ctx).Insert(&system.PC{
ProjectID: req.ProjectID,
PcID: tool.GetUuid(),
PCName: req.PCName,
GridWidth: req.GridWidth,
Range: string(marshal),
})
return
}
func (c *qianqiPingchangController) PingchangDel(ctx context.Context, req *system.PingchangDelReq) (res *system.UsualRes, err error) {
_, err = dao.QianqiPingchang.Ctx(ctx).Unscoped().Delete(dao.QianqiPingchang.Columns().PcId, req.PcID)
return
}
func (c *qianqiPingchangController) PingchangCalculate(ctx context.Context, req *system.PingchangCalculateReq) (res *system.UsualRes, err error) {
pc := &system.PC{}
err = dao.QianqiPingchang.Ctx(ctx).Where(dao.QianqiPingchang.Columns().PcId, req.PcID).Scan(pc)
if err != nil {
return nil, err
}
if pc == nil {
return nil, errors.New("资源不存在")
}
widthNeighbors := CalNeighbors(req.Locations, float64(pc.GridWidth*math.Sqrt(float64(2)))+2)
/* res = &SHPCreateRes{Points: widthNeighbors}
filename := pcs.PCName + "_" + strconv.Itoa(int(req.GridWidth)) + "m精度"
CreateSHP(widthNeighbors, filename)*/
//CreateSHP(widthNeighbors, "test")
var smoothPoints []system.Point
for i, v := range widthNeighbors {
var s_p system.Point
s_p.Alt = v.Avrage
s_p.Lng = v.Lng
s_p.Lat = v.Lat
smoothPoints = append(smoothPoints, s_p)
widthNeighbors[i].Neighbors = []system.Point{}
}
marshal, err := json.Marshal(smoothPoints)
if err != nil {
return nil, err
}
locs, err := json.Marshal(req.Locations)
if err != nil {
return nil, err
}
/* _, err = dao.QianqiPingchang.Ctx(ctx).Where(dao.QianqiPingchang.Columns().PcId, req.PcID).Update(&system.PC{
SmoothPoints: string(marshal),
Points: string(locs),
})*/
_, err = dao.QianqiPingchang.Ctx(ctx).Where(dao.QianqiPingchang.Columns().PcId, req.PcID).Update(g.Map{
"smooth_points": string(marshal),
"points": string(locs),
})
return
}
type Point struct {
system.Point
//WeightMap gmap.ListMap `json:"weight_map" dc:"权重表"`
Neighbors []system.Point `json:"neighbors" dc:"相邻的点"`
Avrage float64 `json:"avrage" dc:"区域内的平均高度"`
Total float64 `json:"total" dc:"总高度"`
}
func CalNeighbors(locations []system.Point, dis float64) []*Point {
var widthNeighbors []*Point
for i, v := range locations {
var point Point
point.Lng = v.Lng
point.Lat = v.Lat
point.Alt = v.Alt
for j, v2 := range locations {
if i != j {
//如果已经最多找到了8个临近点就不再进行寻找减少计算量
if len(point.Neighbors) == MaxNeighborsLen {
break
}
//排除自己,计算与其他点的权重
distance, err := measurement.Distance(v.Lng, v.Lat, v2.Lng, v2.Lat, constants.UnitMeters)
if err != nil {
continue
}
//如果距离小于网格距离的2倍代表是邻居
if distance < dis {
point.Total += v2.Alt
point.Neighbors = append(point.Neighbors, v2)
}
}
}
widthNeighbors = append(widthNeighbors, &point)
}
for i, v := range widthNeighbors {
num := len(v.Neighbors)
if num > 0 {
widthNeighbors[i].Avrage = v.Total / float64(num)
//fmt.Println(i+1, "个点", "总高度", v.Total, "平均高度", widthNeighbors[i].Avrage, "邻居数量", num)
}
}
return widthNeighbors
}
//
//// List 列表
//func (c *qianqiPingchangController) List(ctx context.Context, req *system.QianqiPingchangSearchReq) (res *system.QianqiPingchangSearchRes, err error) {
// res, err = service.QianqiPingchang().List(ctx, req)
// return
//}
//
//// Get 获取平场数据
//func (c *qianqiPingchangController) Get(ctx context.Context, req *system.QianqiPingchangGetReq) (res *system.QianqiPingchangGetRes, err error) {
// res = new(system.QianqiPingchangGetRes)
// res.QianqiPingchangInfoRes, err = service.QianqiPingchang().GetById(ctx, req.Id)
// return
//}
//
//// Add 添加平场数据
//func (c *qianqiPingchangController) Add(ctx context.Context, req *system.QianqiPingchangAddReq) (res *system.QianqiPingchangAddRes, err error) {
// err = service.QianqiPingchang().Add(ctx, req)
// return
//}
//
//// Edit 修改平场数据
//func (c *qianqiPingchangController) Edit(ctx context.Context, req *system.QianqiPingchangEditReq) (res *system.QianqiPingchangEditRes, err error) {
// err = service.QianqiPingchang().Edit(ctx, req)
// return
//}
//
//// Delete 删除平场数据
//func (c *qianqiPingchangController) Delete(ctx context.Context, req *system.QianqiPingchangDeleteReq) (res *system.QianqiPingchangDeleteRes, err error) {
// err = service.QianqiPingchang().Delete(ctx, req.Ids)
// return
//}