PostGis 查询在使用 Go 执行时返回无效的几何错误

我尝试使用 pgx 库从一些带有 Go 的 PostGis (x,y) 1 公里以内id的表中选择它们。steps


我测试了以下返回错误的代码:parse error - invalid geometry (SQLSTATE XX000)


func search(lat float32, lng float32) return (string, error){

    searchQuery   = "SELECT DISTINCT(id) FROM steps WHERE ST_Distance('SRID=4326;POINT($1 $2)'::geography, location) < 1000"


    // GetSession returns *pgxpool.Pool

    rows, err := postgres.GetSession().Query(context.Background(), searchQuery,

    lat,

    lng)

    if err != nil {

        // ERROR: parse error - invalid geometry (SQLSTATE XX000)

        return nil, err

    }

    defer rows.Close()

    ...

}

然后,我刚刚使用ST_SetSRID和ST_MakePoint更改了查询


searchQuery   = "SELECT DISTINCT(id) FROM steps WHERE ST_Distance(ST_SetSRID(ST_MakePoint($1, $2), 4326)::geography, location) < 1000"

... 但 ...


1)我仍然不知道为什么我的第一版查询

"SELECT DISTINCT(id) FROM steps WHERE ST_Distance('SRID=4326;POINT($1 $2)'::geography, location) < 1000"


返回一个几何错误,而当我直接将它测试到pgadmin时它正在工作,用随机坐标值替换 $1 和 $2,例如


"SELECT DISTINCT(id) FROM steps WHERE ST_Distance('SRID=4326;POINT(0.44 3.40)'::geography, location) < 1000"


2) 我不确定将ST_Distance与 ST_SetSRID 和 ST_MakePoint 一起使用是否是检查一个点是否靠近另一个点的最有效方法。


智慧大石
浏览 197回答 1
1回答

米琪卡哇伊

查询问题SELECT DISTINCT(id) FROM steps WHERE ST_Distance('SRID=4326;POINT($1 $2)'::geography, location) < 1000注意 $1 是 lng 而 $2 应该是 lat为什么会发生?'SRID=4326;POINT($1 $2)'是一个字符串文字,postgres 的序数参数占位符不应该在字符串文字中。这不是围棋的问题。这就是 postgres 处理字符串文字的方式。即'$1' != $1,一个是纯字符串,另一个是postgres(不是 Go)将替换为客户端发送到服务器的数据的参数占位符。解决方案..如果你想将字符串文字与参数占位符结合起来,请使用字符串连接:ST_Distance(('SRID=4326;POINT(' || $1::text || ' ' || $2::text || ')')::geometry, location)请注意字符串文字及其连接是如何包裹在额外的括号中的,这只是为了强制执行正确的评估顺序,即强制强制转换::geometry应用于连接后的文本。优化您最有可能使用st_dwithin和利用空间索引,您可以使用您的位置值和地理来构建它,他在帖子的结尾解释道。请参阅blog.cleverelephant.ca/2021/05/indexes-and-queries.html固定 SQL 查询SELECT DISTINCT(id) FROM steps WHERE ST_DWithin(('SRID=4326;POINT(' || $1::text || ' ' || $2::text || ')')::geometry, location, 1000);固定围棋功能func search(lat float32, lng float32) return (string, error){&nbsp; &nbsp; searchQuery&nbsp; &nbsp;= "SELECT DISTINCT(id) FROM steps WHERE ST_DWithin(('SRID=4326;POINT(' || $1::text || ' ' || $2::text || ')')::geometry, location, 1000);"&nbsp; &nbsp; // GetSession returns *pgxpool.Pool&nbsp; &nbsp; rows, err := postgres.GetSession().Query(context.Background(), searchQuery,&nbsp; &nbsp; lng,&nbsp; &nbsp; lat)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; return nil, err&nbsp; &nbsp; }&nbsp; &nbsp; defer rows.Close()&nbsp; &nbsp; ...}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go