All Projects → paulmach → Go.geojson

paulmach / Go.geojson

Licence: mit
Encoding and decoding GeoJSON <-> Go

Programming Languages

go
31211 projects - #10 most used programming language
golang
3204 projects

Projects that are alternatives of or similar to Go.geojson

Go Geom
Package geom implements efficient geometry types for geospatial applications.
Stars: ✭ 456 (+165.12%)
Mutual labels:  encoding, decoding, geojson
X509
A PHP library for X.509 public key certificates, attribute certificates, certification requests and certification path validation.
Stars: ✭ 27 (-84.3%)
Mutual labels:  encoding, decoding
Stegify
🔍 Go tool for LSB steganography, capable of hiding any file within an image.
Stars: ✭ 927 (+438.95%)
Mutual labels:  encoding, decoding
Ldpc
C and MATLAB implementation for LDPC encoding and decoding
Stars: ✭ 76 (-55.81%)
Mutual labels:  encoding, decoding
Scodec
Scala combinator library for working with binary data
Stars: ✭ 709 (+312.21%)
Mutual labels:  encoding, decoding
Anycodable
Type-erased wrappers for Encodable, Decodable, and Codable values
Stars: ✭ 811 (+371.51%)
Mutual labels:  encoding, decoding
Fast ber
A C++11 ASN.1 BER Encoding and Decoding Library
Stars: ✭ 54 (-68.6%)
Mutual labels:  encoding, decoding
Flac
Free Lossless Audio Codec
Stars: ✭ 593 (+244.77%)
Mutual labels:  encoding, decoding
Binary
Generic and fast binary serializer for Go
Stars: ✭ 86 (-50%)
Mutual labels:  encoding, decoding
Libbrotli
meta project to build libraries from the brotli source code
Stars: ✭ 110 (-36.05%)
Mutual labels:  encoding, decoding
Vxg.media.sdk.android
Market leading Android SDK with encoding, streaming & playback functionality
Stars: ✭ 119 (-30.81%)
Mutual labels:  encoding, decoding
Encoding
Go package containing implementations of efficient encoding, decoding, and validation APIs.
Stars: ✭ 705 (+309.88%)
Mutual labels:  encoding, decoding
Decodify
Detect and decode encoded strings, recursively.
Stars: ✭ 670 (+289.53%)
Mutual labels:  encoding, decoding
Ffmpeg Video Player
An FFmpeg and SDL Tutorial.
Stars: ✭ 149 (-13.37%)
Mutual labels:  encoding, decoding
Pbf
A low-level, lightweight protocol buffers implementation in JavaScript.
Stars: ✭ 618 (+259.3%)
Mutual labels:  encoding, decoding
Rust Multibase
Multibase in rust
Stars: ✭ 30 (-82.56%)
Mutual labels:  encoding, decoding
Lerc
Limited Error Raster Compression
Stars: ✭ 126 (-26.74%)
Mutual labels:  encoding, decoding
Hashids.net
A small .NET package to generate YouTube-like hashes from one or many numbers. Use hashids when you do not want to expose your database ids to the user.
Stars: ✭ 470 (+173.26%)
Mutual labels:  encoding, decoding
Bitmatch
A Rust crate that allows you to match, bind, and pack the individual bits of integers.
Stars: ✭ 82 (-52.33%)
Mutual labels:  encoding, decoding
Codability
Useful helpers for working with Codable types in Swift
Stars: ✭ 125 (-27.33%)
Mutual labels:  encoding, decoding

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 {
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].