geojson

package module
v0.0.0-...-a9c31d4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 17, 2025 License: Apache-2.0 Imports: 5 Imported by: 0

README

geojson

介绍

go 语言geojson解析(Marshal与Unmarshal)、CRS(仅支持EPSG:{srid})、WKT生成和解析。

软件架构

软件架构说明

安装教程
  1. xxxx
  2. xxxx
  3. xxxx
使用说明
  1. xxxx
  2. xxxx
  3. xxxx
参与贡献
  1. Fork 本仓库
  2. 新建 Feat_xxx 分支
  3. 提交代码
  4. 新建 Pull Request
特技
  1. 使用 Readme_XXX.md 来支持不同的语言,例如 Readme_en.md, Readme_zh.md
  2. Gitee 官方博客 blog.gitee.com
  3. 你可以 https://gitee.com/explore 这个地址来了解 Gitee 上的优秀开源项目
  4. GVP 全称是 Gitee 最有价值开源项目,是综合评定出的优秀开源项目
  5. Gitee 官方提供的使用手册 https://gitee.com/help
  6. Gitee 封面人物是一档用来展示 Gitee 会员风采的栏目 https://gitee.com/gitee-stars/

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FormatWKT

func FormatWKT(wkt string) string

func UnmarshalGeoJSON

func UnmarshalGeoJSON(data []byte) (string, interface{}, error)

解析 GeoJSON,根据Type返回相应类型和结构体(FeatureCollection或者Feature),便于调用者类型转换 调用示例 geojsonType,f,err := UnmarshalGeoJSON(data)

if err != nil {
	return nil, err
}

switch geojsonType { case "FeatureCollection":

fc := f.(*FeatureCollection)
// 处理FeatureCollection

case "Feature":

f := f.(*Feature)
// 处理Feature

Types

type CRS

type CRS struct {
	Type       string            `json:"type"`
	Properties map[string]string `json:"properties,omitempty"`
}

func NewCRS

func NewCRS(srid int) *CRS

func (*CRS) GetSrid

func (c *CRS) GetSrid() (int, error)

GetSrid extracts the SRID from the CRS name and returns it as an integer.

func (*CRS) SetSrid

func (c *CRS) SetSrid(srid int)

func (*CRS) Verify

func (c *CRS) Verify() (bool, error)

Verify checks if the crs is valid and returns true if it is, false otherwise and an error if there is a problem.

type Feature

type Feature struct {
	ID         interface{}            `json:"id,omitempty"`
	Type       string                 `json:"type"`
	Geometry   Geometry               `json:"geometry"`
	Properties map[string]interface{} `json:"properties"`
}

A Feature corresponds to GeoJSON feature object

func UnmarshalFeature

func UnmarshalFeature(data []byte) (*Feature, error)

解析Feature,根据 Geometry 字段选择相应的结构体

func (*Feature) PropertyBool

func (f *Feature) PropertyBool(key string) (bool, error)

PropertyBool type asserts a property to `bool`.

func (*Feature) PropertyFloat64

func (f *Feature) PropertyFloat64(key string) (float64, error)

PropertyFloat64 type asserts a property to `float64`.

func (*Feature) PropertyInt

func (f *Feature) PropertyInt(key string) (int, error)

PropertyInt type asserts a property to `int`.

func (*Feature) PropertyMustBool

func (f *Feature) PropertyMustBool(key string, def ...bool) bool

PropertyMustBool guarantees the return of a `bool` (with optional default)

useful when you explicitly want a `bool` in a single value return context:

myFunc(f.PropertyMustBool("param1"), f.PropertyMustBool("optional_param", true))

func (*Feature) PropertyMustFloat64

func (f *Feature) PropertyMustFloat64(key string, def ...float64) float64

PropertyMustFloat64 guarantees the return of a `float64` (with optional default)

useful when you explicitly want a `float64` in a single value return context:

myFunc(f.PropertyMustFloat64("param1"), f.PropertyMustFloat64("optional_param", 10.1))

func (*Feature) PropertyMustInt

func (f *Feature) PropertyMustInt(key string, def ...int) int

PropertyMustInt guarantees the return of a `int` (with optional default)

useful when you explicitly want a `int` in a single value return context:

myFunc(f.PropertyMustInt("param1"), f.PropertyMustInt("optional_param", 123))

func (*Feature) PropertyMustString

func (f *Feature) PropertyMustString(key string, def ...string) string

PropertyMustString guarantees the return of a `string` (with optional default)

useful when you explicitly want a `string` in a single value return context:

myFunc(f.PropertyMustString("param1"), f.PropertyMustString("optional_param", "default"))

func (*Feature) PropertyString

func (f *Feature) PropertyString(key string) (string, error)

PropertyString type asserts a property to `string`.

func (*Feature) SetProperty

func (f *Feature) SetProperty(key string, value interface{})

SetProperty provides the inverse of all the property functions and is here for consistency.

type FeatureCollection

type FeatureCollection struct {
	Type     GeoJsonType `json:"type"`
	Features []*Feature  `json:"features"`
	CRS      *CRS        `json:"crs,omitempty"` // Coordinate Reference System Objects are not currently supported
}

A FeatureCollection correlates to a GeoJSON feature collection.

func UnmarshalFeatureCollection

func UnmarshalFeatureCollection(data []byte) (*FeatureCollection, error)

解析 Geojson到FeatureCollection,如geojson类型为Feature,则创建FeatureCollection,将Feature加入到Features中,CRS默认4326

func UnmarshalFeatureCollectionOnly

func UnmarshalFeatureCollectionOnly(data []byte) (*FeatureCollection, error)

解析Geojson到FeatureCollection(类型必须为FeatureCollection,如geojson类型不为FeatureCollection则报错)

func (*FeatureCollection) MarshalToGeoJSON

func (fc *FeatureCollection) MarshalToGeoJSON() (string, error)

Serialize a FeatureCollection to JSON

type GeoJsonType

type GeoJsonType string
const (
	FeatureCollectionType GeoJsonType = "FeatureCollection"
	FeatureType           GeoJsonType = "Feature"
)

type Geometry

type Geometry interface {
	GetType() GeometryType
	GetCoordinates() interface{}
	GetWkt() string
}

Define Geometry interface, all geometric types must implement this interface

func ParseWKT

func ParseWKT(wkt string) (Geometry, string, error)

type GeometryType

type GeometryType string

Define GeometryType enum

const (
	PointType           GeometryType = "Point"
	MultiPointType      GeometryType = "MultiPoint"
	LineStringType      GeometryType = "LineString"
	MultiLineStringType GeometryType = "MultiLineString"
	PolygonType         GeometryType = "Polygon"
	MultiPolygonType    GeometryType = "MultiPolygon"
)

func UnmarshalGeometry

func UnmarshalGeometry(geometryGeoJson []byte) (GeometryType, interface{}, error)

type LineString

type LineString struct {
	Type        GeometryType `json:"type"`
	Coordinates [][]float64  `json:"coordinates"`
}

func (LineString) GetCoordinates

func (l LineString) GetCoordinates() interface{}

func (LineString) GetType

func (l LineString) GetType() GeometryType

func (LineString) GetWkt

func (l LineString) GetWkt() string

type MultiLineString

type MultiLineString struct {
	Type        GeometryType  `json:"type"`
	Coordinates [][][]float64 `json:"coordinates"`
}

func (MultiLineString) GetCoordinates

func (m MultiLineString) GetCoordinates() interface{}

func (MultiLineString) GetType

func (m MultiLineString) GetType() GeometryType

func (MultiLineString) GetWkt

func (m MultiLineString) GetWkt() string

type MultiPoint

type MultiPoint struct {
	Type        GeometryType `json:"type"`
	Coordinates [][]float64  `json:"coordinates"`
}

func (MultiPoint) GetCoordinates

func (m MultiPoint) GetCoordinates() interface{}

func (MultiPoint) GetType

func (m MultiPoint) GetType() GeometryType

func (MultiPoint) GetWkt

func (m MultiPoint) GetWkt() string

type MultiPolygon

type MultiPolygon struct {
	Type        GeometryType    `json:"type"`
	Coordinates [][][][]float64 `json:"coordinates"`
}

func (MultiPolygon) GetCoordinates

func (m MultiPolygon) GetCoordinates() interface{}

func (MultiPolygon) GetType

func (m MultiPolygon) GetType() GeometryType

func (MultiPolygon) GetWkt

func (m MultiPolygon) GetWkt() string

type Point

type Point struct {
	Type        GeometryType `json:"type"`
	Coordinates []float64    `json:"coordinates"`
}

Define specific geometry types

func (Point) GetCoordinates

func (p Point) GetCoordinates() interface{}

func (Point) GetType

func (p Point) GetType() GeometryType

func (Point) GetWkt

func (p Point) GetWkt() string

type Polygon

type Polygon struct {
	Type        GeometryType  `json:"type"`
	Coordinates [][][]float64 `json:"coordinates"`
}

func (Polygon) GetCoordinates

func (p Polygon) GetCoordinates() interface{}

func (Polygon) GetType

func (p Polygon) GetType() GeometryType

func (Polygon) GetWkt

func (p Polygon) GetWkt() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL