How can I insert and select PostGIS geometry types with Gorm?
Nov 05, 2024 am 10:50 AMInserting and Selecting PostGIS Geometry with Gorm
Using Orb and Custom Value Scanning and Marshalling
Inserting and retrieving PostGIS geometry types with Gorm can be challenging. To address this issue, users can leverage the Orb library and implement custom Scan() and Value() methods to convert between Go types and the well-known binary (WKB) format expected by Gorm. Here's an example:
type EWKBGeomPoint geom.Point func (g *EWKBGeomPoint) Scan(input interface{}) error { gt, err := ewkb.Unmarshal(input.([]byte)) if err != nil { return err } g = gt.(*EWKBGeomPoint) return nil } func (g EWKBGeomPoint) Value() (driver.Value, error) { b := geom.Point(g) bp := &b ewkbPt := ewkb.Point{Point: bp.SetSRID(4326)} return ewkbPt.Value() }
By implementing these methods in a struct, Gorm's automated scanning and inserting functions can work seamlessly with geometry types.
Table Setup with Custom Migration
To create the table with the necessary geometry column, a custom migration step can be used to configure the column type:
err = db.Exec(`CREATE TABLE IF NOT EXISTS tracks ( id SERIAL PRIMARY KEY, geom geometry(POINT, 4326) NOT NULL );`).Error if err != nil { return err } err = gormigrate.New(db, gormigrate.DefaultOptions, []*gormigrate.Migration{ { ID: "init", Migrate: func(tx *gorm.DB) error { return tx.CreateTable( Tables..., ).Error }, }, { ID: "tracks_except_geom", Migrate: func(tx *gorm.DB) error { return db.AutoMigrate(Track{}).Error }, }, }).Migrate()
This migration ensures the geometry column is created before the rest of the table structure is set up.
Example Usage
Once the custom Scan(), Value(), and migration steps are implemented, using the geometry type in a Gorm model becomes straightforward:
type Track struct { gorm.Model GeometryPoint EWKBGeomPoint `gorm:"column:geom"` }
With this setup, Gorm can effortlessly insert and select PostGIS geometry types, allowing for seamless usage of geometric data in Go applications.
The above is the detailed content of How can I insert and select PostGIS geometry types with Gorm?. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Go language pack import: What is the difference between underscore and without underscore?

How to implement short-term information transfer between pages in the Beego framework?

How to convert MySQL query result List into a custom structure slice in Go language?

How do I write mock objects and stubs for testing in Go?

How can I define custom type constraints for generics in Go?

How can I use tracing tools to understand the execution flow of my Go applications?

How to write files in Go language conveniently?
