postgresql - 使用 pgx 从 Postgres 扫描 PostGIS 点时,接口(interface) {} 是字符串,而不是 []uint8

标签 postgresql go postgis pgx

我有一个带有 buildings 的数据库包含 coordinate 的表GEOMETRY(POINT, 4326) 类型的列.带着希望能读懂坐标,我写了如下代码:

    rows, err := db.pool.Query(context.Background(), `select "uuid", "coordinate" from "building"`)
    defer rows.Close()

    for rows.Next() {
        var uuid pgtype.UUID
        var coordinate postgis.Point
        err := rows.Scan(&uuid, &coordinate)
        if err != nil {
            return err
        }
        log.Println("~~~", coordinate.X, coordinate.Y)
    }
然后,我收到以下错误:
2020/08/22 18:32:32 [Recovery] 2020/08/22 - 18:32:32 panic recovered:
POST /get-state HTTP/1.1
Host: localhost:3000
Accept: */*
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Length: 0
Postman-Token: 4e6f4dc7-9dc4-4cd4-a328-8da2bc86d43d
User-Agent: PostmanRuntime/7.26.3


interface conversion: interface {} is string, not []uint8
/usr/local/Cellar/go/1.14.6/libexec/src/runtime/iface.go:260 (0x100bd11)
    panicdottypeE: panic(&TypeAssertionError{iface, have, want, ""})
/Users/virtumonde/go/pkg/mod/github.com/cridenour/go-postgis@v1.0.0/decode.go:21 (0x10fda31)
    decode: ewkb, err := hex.DecodeString(string(value.([]byte)))
/Users/virtumonde/go/pkg/mod/github.com/cridenour/go-postgis@v1.0.0/point.go:48 (0x10fde58)
    (*Point).Scan: reader, err := decode(value)
/Users/virtumonde/go/pkg/mod/github.com/jackc/pgtype@v1.4.2/pgtype.go:590 (0x120ff39)
    scanPlanSQLScanner.Scan: return scanner.Scan(string(src))
/Users/virtumonde/go/pkg/mod/github.com/jackc/pgx/v4@v4.8.1/rows.go:220 (0x1627b2c)
    (*connRows).Scan: err := rows.scanPlans[i].Scan(ci, fieldDescriptions[i].DataTypeOID, fieldDescriptions[i].Format, values[i], dst)
/Users/virtumonde/go/pkg/mod/github.com/jackc/pgx/v4@v4.8.1/pgxpool/rows.go:70 (0x1634cd4)
    (*poolRows).Scan: err := rows.r.Scan(dest...)
/Users/virtumonde/Desktop/dev/terminus/terminus-server/db.go:49 (0x16388f3)
    Database.GetPlayers: err := rows.Scan(&uuid, &coordinate)
/Users/virtumonde/Desktop/dev/terminus/terminus-server/main.go:116 (0x163b26b)
    main.func4: players, err := db.GetPlayers()
/Users/virtumonde/go/pkg/mod/github.com/gin-gonic/gin@v1.6.3/context.go:161 (0x15a0eba)
    (*Context).Next: c.handlers[c.index](c)
/Users/virtumonde/go/pkg/mod/github.com/gin-gonic/gin@v1.6.3/recovery.go:83 (0x15b45bf)
    RecoveryWithWriter.func1: c.Next()
/Users/virtumonde/go/pkg/mod/github.com/gin-gonic/gin@v1.6.3/context.go:161 (0x15a0eba)
    (*Context).Next: c.handlers[c.index](c)
/Users/virtumonde/go/pkg/mod/github.com/gin-gonic/gin@v1.6.3/logger.go:241 (0x15b36f0)
    LoggerWithConfig.func1: c.Next()
/Users/virtumonde/go/pkg/mod/github.com/gin-gonic/gin@v1.6.3/context.go:161 (0x15a0eba)
    (*Context).Next: c.handlers[c.index](c)
/Users/virtumonde/go/pkg/mod/github.com/gin-gonic/gin@v1.6.3/gin.go:409 (0x15aac95)
    (*Engine).handleHTTPRequest: c.Next()
/Users/virtumonde/go/pkg/mod/github.com/gin-gonic/gin@v1.6.3/gin.go:367 (0x15aa3ac)
    (*Engine).ServeHTTP: engine.handleHTTPRequest(c)
/usr/local/Cellar/go/1.14.6/libexec/src/net/http/server.go:2836 (0x13a2692)
    serverHandler.ServeHTTP: handler.ServeHTTP(rw, req)
/usr/local/Cellar/go/1.14.6/libexec/src/net/http/server.go:1924 (0x139dffb)
    (*conn).serve: serverHandler{c.server}.ServeHTTP(w, w.req)
/usr/local/Cellar/go/1.14.6/libexec/src/runtime/asm_amd64.s:1373 (0x10640c0)
    goexit: BYTE    $0x90   // NOP
先感谢您。任何的意见都将会有帮助。

最佳答案

import ("github.com/paulmach/orb/encoding/wkb")
err := rows.Scan(&uuid, wkb.Scanner(&coordinate))

关于postgresql - 使用 pgx 从 Postgres 扫描 PostGIS 点时,接口(interface) {} 是字符串,而不是 []uint8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63538069/

相关文章:

python - pandas.read_sql()中的chunksize参数如何避免将数据加载到内存中

google-app-engine - 使用 App Engine 进行 Go 的 I18n 策略

php - 位移 : Can someone explain what this code does?

go - 如何通过字符串变量调用方法?

python - Django 测试 : error creating the test database: permission denied to copy database "template_postgis"

postgresql - 使用一个docker-compose.yml启动两个Wiki js容器,并通过traefik分离数据库(postgres)

postgresql - 如何在 pgAdmin III 中为多个表生成 CREATE 脚本?

sql - PostgreSQL:根据日期识别回访者 - 连接或窗口函数?

postgresql - 在 postgresql 中使用 st_contains 在几何上连接两个大型 postgresql 表的最快方法是什么?

Postgresql 空间查询太慢