Commit 7bca95c7 by dliangx

update

parent 76c8e915
...@@ -438,7 +438,7 @@ func GetRelatedAdmArea(obj model.GeoJson) ([]model.GeoJson, error) { ...@@ -438,7 +438,7 @@ func GetRelatedAdmArea(obj model.GeoJson) ([]model.GeoJson, error) {
return results, err return results, err
} }
func CalcAreasCenter(ids []string) (float64, float64, error) { func CalcAreasCenter(ids []string, opt int) (float64, float64, error) {
db, err := connectDB() db, err := connectDB()
if err != nil { if err != nil {
return 0, 0, fmt.Errorf("database connection failed: %v", err) return 0, 0, fmt.Errorf("database connection failed: %v", err)
...@@ -449,14 +449,29 @@ func CalcAreasCenter(ids []string) (float64, float64, error) { ...@@ -449,14 +449,29 @@ func CalcAreasCenter(ids []string) (float64, float64, error) {
idList := strings.Join(ids, ",") idList := strings.Join(ids, ",")
var wktPoint string var wktPoint string
err = db.QueryRow(` var query string
SELECT
ST_AsText(ST_Centroid(ST_Collect(geom))) AS overall_centroid if opt == 1 {
FROM query = `
geojson_data SELECT ST_AsText(ST_Centroid(ST_Collect(geometry))) AS overall_centroid
WHERE id in (` + idList + `) FROM opt_routes WHERE id in (` + idList + `)`
`).Scan(&wktPoint) } else if opt == 2 {
query = `
SELECT ST_AsText(ST_Centroid(ST_Collect(geometry))) AS overall_centroid
FROM opt_fly_routes WHERE id in (` + idList + `)`
} else if opt == 3 {
query = `
SELECT ST_AsText(ST_Centroid(ST_Collect(geometry))) AS overall_centroid
FROM opt_areas WHERE id in (` + idList + `)`
} else if opt == 4 {
query = `
SELECT ST_AsText(ST_Centroid(ST_Collect(geom))) AS overall_centroid
FROM geojson_data WHERE id in (` + idList + `)`
} else {
return 0, 0, errors.New("opt value is not allow! ")
}
err = db.QueryRow(query).Scan(&wktPoint)
if err != nil { if err != nil {
if err == sql.ErrNoRows { if err == sql.ErrNoRows {
return 0, 0, fmt.Errorf("no areas found") return 0, 0, fmt.Errorf("no areas found")
......
...@@ -183,19 +183,26 @@ func CalcAreasCenter(ctx context.Context, c *app.RequestContext) { ...@@ -183,19 +183,26 @@ func CalcAreasCenter(ctx context.Context, c *app.RequestContext) {
// 获取 ids 参数 // 获取 ids 参数
idsStr := c.Query("ids") idsStr := c.Query("ids")
if idsStr == "" { if idsStr == "" {
c.JSON(consts.StatusBadRequest, utils.H{"message": "ids parameter required"}) c.JSON(consts.StatusBadRequest, utils.H{"code": 10001, "message": "ids parameter required"})
return
}
// 获取 opt 参数
opt, err := strconv.Atoi(c.Query("opt"))
if err != nil {
c.JSON(consts.StatusBadRequest, utils.H{"code": 10003, "message": "opt parameter required"})
return return
} }
// 分割 ids 字符串为数组 // 分割 ids 字符串为数组
ids := strings.Split(idsStr, ",") ids := strings.Split(idsStr, ",")
if len(ids) == 0 { if len(ids) == 0 {
c.JSON(consts.StatusBadRequest, utils.H{"message": "invalid ids format"}) c.JSON(consts.StatusBadRequest, utils.H{"code": 10001, "message": "invalid ids format"})
return return
} }
// 调用 geo.CalcAreasCenter 计算中心点 // 调用 geo.CalcAreasCenter 计算中心点,传入 opt 参数
lat, lon, err := geo.CalcAreasCenter(ids) lat, lon, err := geo.CalcAreasCenter(ids, opt)
if err != nil { if err != nil {
c.JSON(consts.StatusOK, utils.H{"code": 10012, "message": err.Error()}) c.JSON(consts.StatusOK, utils.H{"code": 10012, "message": err.Error()})
return return
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment