【问题标题】:ActiveRecord::StatementInvalid: PG::InternalError: ERROR: parse error - invalid geometryActiveRecord::StatementInvalid: PG::InternalError: ERROR: parse error - invalid geometry
【发布时间】:2018-05-08 07:30:56
【问题描述】:

我需要找出距离经纬度一定距离的用户。

organisation.users.joins(:location)
            .where("ST_DWithin(ST_GeomFromText('POINT(locations.longitude locations.latitude)', 4326),ST_GeomFromText('POINT(? ?)', 4326), ?)", longitude, latitude, distance)
            .pluck(:id)

我收到此错误 ActiveRecord::StatementInvalid: PG::InternalError: ERROR: parse error - invalid geometry

【问题讨论】:

  • 你错过了逗号:point(locations.longitde, locations.latitude),虽然我不确定是不是这个。
  • @Aarthi 能否分享一下这些变量的内容:locations.longitude locations.latitude?我猜十进制值由, 逗号分隔,这确实会产生错误。尝试使用. 点作为小数分隔符
  • @MarekLipka 你确定 WKT 积分需要用逗号分隔 xy 吗?
  • @JimJones 我不是,这是真的,这只是根据这里的文档猜测:postgresql.org/docs/9.6/static/functions-geometry.html 但我可能是错的。
  • locations.latitude 和 locations.longitude 是浮点值

标签: ruby-on-rails geometry postgis


【解决方案1】:

您指的是字符串中的列名,因此使用此字符串代替预期的坐标。

要克服这个问题,您需要将文本命令 ('point(' 与列值连接起来,最后将文本命令 (')') 连接起来。因此,列名称不在不再是单引号了。

organisation.users.joins(:location)
            .where("ST_DWithin(ST_GeomFromText('POINT(' || locations.longitude  || ' ' ||  locations.latitude ||')', 4326),ST_GeomFromText('POINT(? ?)', 4326), ?)", longitude, latitude, distance)
            .pluck(:id)

【讨论】:

  • 我能得到这个的参考吗,我以前从未见过这个运算符
  • @Aarthi 它用于连接字符串,因此您现在正在使用列内容构建字符串。见运营商文档here
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-13
  • 2012-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-10
相关资源
最近更新 更多