twpayne / go-geom

Package geom implements efficient geometry types for geospatial applications.
BSD 2-Clause "Simplified" License
839 stars 104 forks source link

geom.Point must implement Encoder or be converted to a string #210

Closed anstosa closed 2 years ago

anstosa commented 2 years ago

I'm new to go and gis so forgive probably a lot of ignorance. I'm using gorm as my ORM and trying to insert a 3D ECEF point into PostGIS like so

type Example struct {
  Location    *geom.Point    `json:"location" gorm:"type:geometry(POINTZ, 4978)"`
  // ...
}

type Input struct {
  Location    []float64
}

func (db *PostgresDB) LogExample(input *Input) error {
point, err := geom.NewPoint(geom.XYZ).SetSRID(4978).SetCoords(
    geom.Coord{input.Location[0], input.Location[1], input.Location[2]})
// handle error
obj := &Example{
    Location:    point,
}
return db.Create(&obj).Error

When I do this I get Cannot encode geom.Point into oid 18001 - geom.Point must implement Encoder or be converted to a string

What am I missing?

twpayne commented 2 years ago

I'm not familiar with GORM, but if you want to interface with PostGIS then you'll need to convert the geometry to/from EWKB or hex-encoded EWKB, depending on the exact query you're using. See the PostGIS example for a full example.

anstosa commented 2 years ago

Thanks for the pointer!