golang geom wkt

golang才是未来 · · 1044 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

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[:]}
}






有疑问加站长微信联系(非本文作者)

本文来自:简书

感谢作者:golang才是未来

查看原文:golang geom wkt

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

1044 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传