我尝试使用 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 一起使用是否是检查一个点是否靠近另一个点的最有效方法。
米琪卡哇伊
相关分类