Skip to content

Commit

Permalink
geometry.Scan for direct postgis query -> geojson
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmach committed Mar 20, 2015
1 parent 53accc9 commit 6bb5953
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
go.geojson
==========

go.geojson is a library for **encoding and decoding** [GeoJSON](http://geojson.org/) into Go structs.
Go.geojson is a package for **encoding and decoding** [GeoJSON](http://geojson.org/) into Go structs.
Supports both the [json.Marshaler](http://golang.org/pkg/encoding/json/#Marshaler) and [json.Unmarshaler](http://golang.org/pkg/encoding/json/#Unmarshaler)
interfaces as well as helper functions such as `UnmarshalFeatureCollection`, `UnmarshalFeature` and `UnmarshalGeometry`.
interfaces as well as [sql.Scanner](http://golang.org/pkg/database/sql/#Scanner) for directly scanner PostGIS query results.
The package also provides helper functions such as `UnmarshalFeatureCollection`, `UnmarshalFeature` and `UnmarshalGeometry`.

#### To install

Expand Down Expand Up @@ -48,8 +49,7 @@ interfaces as well as helper functions such as `UnmarshalFeatureCollection`, `Un

g.IsPoint() == true
g.Point == []float64{102.0, 0.5}



* #### Marshalling (Go -> JSON)

g := geojson.NewPointGeometry([]float64{1, 2})
Expand All @@ -59,6 +59,13 @@ interfaces as well as helper functions such as `UnmarshalFeatureCollection`, `Un
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.
Expand Down
13 changes: 13 additions & 0 deletions geometry.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,19 @@ func (g *Geometry) UnmarshalJSON(data []byte) error {
return decodeGeometry(g, object)
}

// Scan implements the sql.Scanner interface allowing
// geometry structs to be passed into rows.Scan(...interface{})
// The columns must be received as GeoJSON Geometry.
// When using PostGIS a spatial column would need to be wrapped in ST_AsGeoJSON.
func (g *Geometry) Scan(value interface{}) error {
data, ok := value.([]byte)
if !ok {
return errors.New("unable to parse this type into geojson")
}

return g.UnmarshalJSON(data)
}

func decodeGeometry(g *Geometry, object map[string]interface{}) error {
t, ok := object["type"]
if !ok {
Expand Down
22 changes: 22 additions & 0 deletions geometry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,3 +262,25 @@ func TestUnmarshalGeometryCollection(t *testing.T) {
t.Errorf("should have 2 geometries but got %d", len(g.Geometries))
}
}

func TestGeometryScan(t *testing.T) {
g := &Geometry{}

err := g.Scan(123)
if err == nil {
t.Errorf("should return error if not the correct data type")
}

err = g.Scan([]byte(`{"type":"Point","coordinates":[-93.787988,32.392335]}`))
if err != nil {
t.Fatalf("should parse without error, got %v", err)
}

if !g.IsPoint() {
t.Errorf("should be point, but got %v", g)
}

if g.Point[0] != -93.787988 || g.Point[1] != 32.392335 {
t.Errorf("incorrect point data, got %v", g.Point)
}
}

0 comments on commit 6bb5953

Please sign in to comment.