package main
import (
"encoding/json"
"fmt"
"github.com/twpayne/go-geom"
"github.com/twpayne/go-geom/encoding/geojson"
"github.com/twpayne/go-geom/encoding/wkt"
"reflect"
)
func main() {
unitSquare := geom.NewPolygon(geom.XY).MustSetCoords([][]geom.Coord{
{{0, 0}, {1, 0}, {1, 1}, {0, 2}, {0, 0}},
})
unitSquare3 := geom.NewPolygon(geom.XYZ).MustSetCoords([][]geom.Coord{
{{0, 0, 0}, {1, 0, 0}, {1, 1, 0}, {0, 2, 0}, {0, 0, 0}},
})
coords := unitSquare3.Coords()
for _, coord := range coords {
for _, i := range coord {
fmt.Println(i[0])
fmt.Println(i[1])
fmt.Println(i[2])
}
}
unitSquareWkt3, _ := wkt.Marshal(unitSquare3)
fmt.Println(unitSquareWkt3)
fmt.Printf("unitSquare.Area() == %f \n", unitSquare.Area())
// geom -> wkt
unitSquareWkt, _ := wkt.Marshal(unitSquare)
fmt.Println(unitSquareWkt)
// wkt -> geom
if geomFromWkt, err := wkt.Unmarshal("POLYGON ((0 0, 1 0, 1 1, 0 2, 0 0))"); err != nil {
fmt.Println(err)
} else {
if p, ok := geomFromWkt.(*geom.Polygon); ok != true {
fmt.Println(ok)
} else {
coords := p.Coords()
for i, v := range coords {
for j, v2 := range v {
fmt.Printf("arr[%v][%v]=%v \t \n", i, j, v2)
}
fmt.Println()
}
}
fmt.Println(reflect.TypeOf(geomFromWkt).Elem())
}
// geom -> geomJson
bytes, _ := geojson.Marshal(unitSquare)
fmt.Println(string(bytes))
// geoJson -> geom
var geomFromGeojson geom.T;
a := `{"type":"Polygon","coordinates":[[[0,0],[1,0],[1,1],[0,2],[0,0]]]}`
if err := geojson.Unmarshal([]byte(a), &geomFromGeojson); err != nil {
fmt.Println(err)
} else {
if p, ok := geomFromGeojson.(*geom.Polygon); ok != true {
fmt.Println(ok)
} else {
coords := p.Coords()
for i, v := range coords {
for j, v2 := range v {
fmt.Printf("arr[%v][%v]=%v \t \n", i, j, v2)
}
fmt.Println()
}
}
fmt.Println(reflect.TypeOf(geomFromGeojson).Elem())
}
// feature -> featureJson
feature := geojson.Feature{}
feature.Geometry = geomFromGeojson
properties := make(map[string]interface{})
properties["prop0"] = "value0"
properties["prop1"] = "1.2"
feature.Properties = properties
featureJson, _ := feature.MarshalJSON()
fmt.Println(string(featureJson))
// featureJson -> feature
var m map[string]interface{}
json.Unmarshal(featureJson, &m)
marshal, _ := json.Marshal(m["geometry"])
fmt.Println(string(marshal))
// featureCollectionJson
var features [3]*geojson.Feature
features[0] = &feature
features[1] = &feature
features[2] = &feature
featureCollecation1 := geojson.FeatureCollection{Features: features[:2]}
featureCollecationJSON1, _ := featureCollecation1.MarshalJSON()
fmt.Println(string(featureCollecationJSON1))
fmt.Println(len(featureCollecation1.Features))
featureCollecation2 := geojson.FeatureCollection{}
featureCollecation2.Features = append(featureCollecation2.Features, &feature)
featureCollecation2.Features = append(featureCollecation2.Features, &feature)
featureCollecationJSON2, _ := featureCollecation2.MarshalJSON()
fmt.Println(string(featureCollecationJSON2))
fmt.Println(len(featureCollecation2.Features))
point := geom.NewPoint(geom.XY).MustSetCoords(geom.Coord{1.0, 1.0})
pointWkt, _ := wkt.Marshal(point)
fmt.Println(pointWkt)
multipoint := geom.NewMultiPoint(geom.XY).MustSetCoords([]geom.Coord{{1.0, 1.0}, {2, 2}})
multipointWkt, _ := wkt.Marshal(multipoint)
fmt.Println(multipointWkt)
line := geom.NewLineString(geom.XY).MustSetCoords([]geom.Coord{{1.0, 1.0}, {2, 2}})
lineWkt, _ := wkt.Marshal(line)
fmt.Println(lineWkt)
mutiline := geom.NewMultiLineString(geom.XY)
mutiline.Push(line)
mutiline.Push(line)
mutiline.SetSRID(4326)
mutilineWkt, _ := wkt.Marshal(mutiline)
fmt.Println(mutilineWkt)
}
package geoutil
import (
"encoding/json"
"fmt"
"github.com/twpayne/go-geom"
"github.com/twpayne/go-geom/encoding/geojson"
"github.com/twpayne/go-geom/encoding/wkt"
)
func GeomFromWkt(wktStr string) geom.T {
if geometry, err := wkt.Unmarshal(wktStr); err != nil {
fmt.Printf("GeomFromWkt Error:err:[%s],wktStr:[%s]", err, wktStr)
} else {
return geometry
}
return nil
}
func WktFromGeom(geometry geom.T) string {
if wktStr, err := wkt.Marshal(geometry); err != nil {
fmt.Printf("GeomFromWkt Error:err:[%s],wktStr:[%s]", err, wktStr)
} else {
return wktStr
}
return ""
}
func GeomFromJson(jsonStr string) geom.T {
var geometry geom.T
if err := geojson.Unmarshal([]byte(jsonStr), &geometry); err != nil {
fmt.Printf("GeomFromWkt Error:[%s],wktStr:[%s]", err, jsonStr)
} else {
return geometry
}
return nil
}
func JsonFromGeom(geometry geom.T) []byte {
if jsonByte, err := geojson.Marshal(geometry); err != nil {
fmt.Printf("GeomFromWkt Error:[%s],wktStr:[%s]", err, jsonByte)
} else {
return jsonByte
}
return nil
}
func FJsonFromGeom(geometry geom.T, properties map[string]interface{}) geojson.Feature {
feature := geojson.Feature{}
feature.Geometry = geometry
feature.Properties = properties
return feature
}
func GeomFromFJson(featureJson string) geom.T {
var m map[string]interface{}
json.Unmarshal([]byte(featureJson),&m)
marshal, _ := json.Marshal(m["geometry"])
return GeomFromJson(string(marshal))
}
func FCJsonFromFeatures(features []*geojson.Feature) geojson.FeatureCollection {
return geojson.FeatureCollection{Features: features[:]}
}
有疑问加站长微信联系(非本文作者)