1 Star 0 Fork 0

jcstone/geojson

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
crsrid.go 1.43 KB
一键复制 编辑 原始数据 按行查看 历史
sjc 提交于 2025-02-17 11:15 +08:00 . 初次提交
package geojson
import (
"fmt"
"strconv"
"strings"
)
type CRS struct {
Type string `json:"type"`
Properties map[string]string `json:"properties,omitempty"`
}
func NewCRS(srid int) *CRS {
properties := make(map[string]string)
properties["name"] = fmt.Sprintf("EPSG:%d", srid)
crs := &CRS{Type: "name", Properties: properties}
return crs
}
func (c *CRS) SetSrid(srid int) {
c.Type = "name"
c.Properties = map[string]string{"name": fmt.Sprintf("EPSG:%d", srid)}
}
// GetSrid extracts the SRID from the CRS name and returns it as an integer.
func (c *CRS) GetSrid() (int, error) {
if c.Type == "name" {
if len(c.Properties) == 1 {
for k, value := range c.Properties {
if k == "name" {
vv := strings.Split(value, ":")
if len(vv) != 2 || !strings.EqualFold(vv[0], "EPSG") {
return -1, fmt.Errorf("crs name is not in the format EPSG:<SRID>")
}
srid, err := strconv.Atoi(vv[1])
if err != nil {
return -1, fmt.Errorf("crs name is not a valid EPSG SRID")
}
return srid, nil
}
}
}
}
return -1, fmt.Errorf("crs type is not 'name' or properties are not as expected")
}
// Verify checks if the crs is valid and returns true if it is, false otherwise and an error if there is a problem.
func (c *CRS) Verify() (bool, error) {
srid, err := c.GetSrid()
if err != nil {
return false, err
}
if srid == -1 {
return false, fmt.Errorf("crs is not valid")
}
return true, nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/jcstone/geojson.git
git@gitee.com:jcstone/geojson.git
jcstone
geojson
geojson
a9c31d4530c6

搜索帮助