GithubHelp home page GithubHelp logo

go.geojson's Introduction

go.geojson

Go.geojson is a package for encoding and decoding GeoJSON into Go structs. Supports both the json.Marshaler and json.Unmarshaler interfaces as well as sql.Scanner for directly scanning PostGIS query results. The package also provides helper functions such as UnmarshalFeatureCollection, UnmarshalFeature and UnmarshalGeometry.

Important

This package is best for lightweight interaction with GeoJSON. If you want to actually do stuff with the geometry take a look at orb/geojson which decodes the geometries into orb types which you can do all sorts of things with.

To install

go get github.com/paulmach/go.geojson

To use, imports as package name geojson:

import "github.com/paulmach/go.geojson"

Build Status Godoc Reference

Examples

  • Unmarshalling (JSON -> Go)

    go.geojson supports both the json.Marshaler and json.Unmarshaler interfaces as well as helper functions such as UnmarshalFeatureCollection, UnmarshalFeature and UnmarshalGeometry.

      // Feature Collection
      rawFeatureJSON := []byte(`
        { "type": "FeatureCollection",
          "features": [
            { "type": "Feature",
              "geometry": {"type": "Point", "coordinates": [102.0, 0.5]},
              "properties": {"prop0": "value0"}
            }
          ]
        }`)
    
      fc1, err := geojson.UnmarshalFeatureCollection(rawFeatureJSON)
    
      fc2 := geojson.NewFeatureCollection()
      err := json.Unmarshal(rawJSON, fc2)
    
      // Geometry
      rawGeometryJSON := []byte(`{"type": "Point", "coordinates": [102.0, 0.5]}`)
      g, err := geojson.UnmarshalGeometry(rawGeometryJSON)
    
      g.IsPoint() == true
      g.Point == []float64{102.0, 0.5}
    
  • Marshalling (Go -> JSON)

      g := geojson.NewPointGeometry([]float64{1, 2})
      rawJSON, err := g.MarshalJSON()
    
      fc := geojson.NewFeatureCollection()
      fc.AddFeature(geojson.NewPointFeature([]float64{1,2}))
      rawJSON, err := fc.MarshalJSON()
    
  • Scanning PostGIS query results

      row := db.QueryRow("SELECT ST_AsGeoJSON(the_geom) FROM postgis_table)
    
      var geometry *geojson.Geometry
      row.Scan(&geometry)
    
  • Dealing with different Geometry types

    A geometry can be of several types, causing problems in a statically typed language. Thus there is a separate attribute on Geometry for each type. See the Geometry object for more details.

      g := geojson.UnmarshalGeometry([]byte(`
      	{
            "type": "LineString",
            "coordinates": [
              [102.0, 0.0], [103.0, 1.0], [104.0, 0.0], [105.0, 1.0]
            ]
          }`))
    
      switch {
      case g.IsPoint():
      	// do something with g.Point
      case g.IsLineString():
      	// do something with g.LineString
      }
    

Feature Properties

GeoJSON Features can have properties of any type. This can cause issues in a statically typed language such as Go. So, included are some helper methods on the Feature object to ease the pain.

// functions to do the casting for you
func (f Feature) PropertyBool(key string) (bool, error) {
func (f Feature) PropertyInt(key string) (int, error) {
func (f Feature) PropertyFloat64(key string) (float64, error) {
func (f Feature) PropertyString(key string) (string, error) {

// functions that hide the error and let you define default
func (f Feature) PropertyMustBool(key string, def ...bool) bool {
func (f Feature) PropertyMustInt(key string, def ...int) int {
func (f Feature) PropertyMustFloat64(key string, def ...float64) float64 {
func (f Feature) PropertyMustString(key string, def ...string) string {

go.geojson's People

Contributors

paulmach avatar kuznetsovin avatar diligiant avatar adhulipa avatar rubenv avatar samaratrilling avatar pailakka avatar

Stargazers

Walter Zhang avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.