Categorygithub.com/paulmach/go.geojson
modulepackage
1.5.0
Repository: https://github.com/paulmach/go.geojson.git
Documentation: pkg.go.dev

# README

go.geojson CI Godoc Reference

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.

Deprecated, use orb/geojson

The orb package, and its subpackages, provide all the features here and more.

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 {

# Functions

NewCollectionFeature creates and initializes a GeoJSON feature with a geometry collection geometry using the given geometries.
NewCollectionGeometry creates and initializes a geometry collection geometry with the given geometries.
NewFeature creates and initializes a GeoJSON feature given the required attributes.
NewFeatureCollection creates and initializes a new feature collection.
NewLineStringFeature creates and initializes a GeoJSON feature with a line string geometry using the given coordinates.
NewLineStringGeometry creates and initializes a line string geometry with the given coordinates.
NewMultiLineStringFeature creates and initializes a GeoJSON feature with a multi-line string geometry using the given lines.
NewMultiLineStringGeometry creates and initializes a multi-line string geometry with the given lines.
NewMultiPointFeature creates and initializes a GeoJSON feature with a multi-point geometry using the given coordinates.
NewMultiPointGeometry creates and initializes a multi-point geometry with the given coordinates.
NewMultiPolygonFeature creates and initializes a GeoJSON feature with a multi-polygon geometry using the given polygons.
NewMultiPolygonGeometry creates and initializes a multi-polygon geometry with the given polygons.
NewPointFeature creates and initializes a GeoJSON feature with a point geometry using the given coordinate.
NewPointGeometry creates and initializes a point geometry with the give coordinate.
NewPolygonFeature creates and initializes a GeoJSON feature with a polygon geometry using the given polygon.
NewPolygonGeometry creates and initializes a polygon geometry with the given polygon.
UnmarshalFeature decodes the data into a GeoJSON feature.
UnmarshalFeatureCollection decodes the data into a GeoJSON feature collection.
UnmarshalGeometry decodes the data into a GeoJSON geometry.

# Constants

The geometry types supported by GeoJSON 1.0.
The geometry types supported by GeoJSON 1.0.
The geometry types supported by GeoJSON 1.0.
The geometry types supported by GeoJSON 1.0.
The geometry types supported by GeoJSON 1.0.
The geometry types supported by GeoJSON 1.0.
The geometry types supported by GeoJSON 1.0.

# Structs

A Feature corresponds to GeoJSON feature object.
A FeatureCollection correlates to a GeoJSON feature collection.
A Geometry correlates to a GeoJSON geometry object.

# Type aliases

A GeometryType serves to enumerate the different GeoJSON geometry types.