sql >> Base de Datos >  >> RDS >> Mysql

Trabajando con datos espaciales con Gorm y MySQL

Aquí hay otro enfoque; usar codificación binaria.

Según este doc , MySQL almacena valores de geometría utilizando 4 bytes para indicar el SRID (ID de referencia espacial) seguido de la representación WKB (Binario bien conocido) del valor.

Entonces, un tipo puede usar la codificación WKB y agregar y eliminar el prefijo de cuatro bytes en las funciones Value() y Scan(). La biblioteca go-geom que se encuentra en otras respuestas tiene un paquete de codificación WKB, github.com/twpayne/go-geom/encoding/wkb.

Por ejemplo:

type MyPoint struct {
    Point wkb.Point
}

func (m *MyPoint) Value() (driver.Value, error) {
    value, err := m.Point.Value()
    if err != nil {
        return nil, err
    }

    buf, ok := value.([]byte)
    if !ok {
        return nil, fmt.Errorf("did not convert value: expected []byte, but was %T", value)
    }

    mysqlEncoding := make([]byte, 4)
    binary.LittleEndian.PutUint32(mysqlEncoding, 4326)
    mysqlEncoding = append(mysqlEncoding, buf...)

    return mysqlEncoding, err
}

func (m *MyPoint) Scan(src interface{}) error {
    if src == nil {
        return nil
    }

    mysqlEncoding, ok := src.([]byte)
    if !ok {
        return fmt.Errorf("did not scan: expected []byte but was %T", src)
    }

    var srid uint32 = binary.LittleEndian.Uint32(mysqlEncoding[0:4])

    err := m.Point.Scan(mysqlEncoding[4:])

    m.Point.SetSRID(int(srid))

    return err
}

Definición de una etiqueta usando el tipo MyPoint:

type Tag struct {
    Name string   `gorm:"type:varchar(50);primary_key"`
    Loc  *MyPoint `gorm:"column:loc"`
}

func (t Tag) String() string {
    return fmt.Sprintf("%s @ Point(%f, %f)", t.Name, t.Loc.Point.Coords().X(), t.Loc.Point.Coords().Y())
}

Creando una etiqueta usando el tipo:

tag := &Tag{
    Name: "London",
    Loc: &MyPoint{
        wkb.Point{
            geom.NewPoint(geom.XY).MustSetCoords([]float64{0.1275, 51.50722}).SetSRID(4326),
        },
    },
}

err = db.Create(&tag).Error
if err != nil {
    log.Fatalf("create: %v", err)
}

Resultados de MySQL:

mysql> describe tag;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| name  | varchar(50) | NO   | PRI | NULL    |       |
| loc   | geometry    | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+


mysql> select name, st_astext(loc) from tag;
+--------+------------------------+
| name   | st_astext(loc)         |
+--------+------------------------+
| London | POINT(0.1275 51.50722) |
+--------+------------------------+
  • (ArcGIS dice 4326 es la referencia espacial más común para almacenar datos de referencia en todo el mundo. Sirve como predeterminado tanto para la base de datos espacial PostGIS como para el estándar GeoJSON. También se usa de forma predeterminada en la mayoría de las bibliotecas de mapas web).