【问题标题】:Postgis ST_DWithin function in laravel(lumen) querylaravel(lumen) 查询中的 Postgis ST_DWithin 函数
【发布时间】:2019-02-23 05:48:24
【问题描述】:

我发现 laravel(lumen) 查询生成器中的 postgis 函数存在问题。

流明版本:5.6

Postgres:9.6.9 与 postGIS

我有一个有效的代码:

$sql = "ST_DWithin(location ,'POINT($lat $lon)', $distance)";
$query->whereRaw($sql);

这可行,但我想通过参数绑定传递参数:

$sql = "ST_DWithin(location ,'POINT(? ?)', ?)";
$query->whereRaw($sql, [$lat, $lon, $distance]);

乍一看看起来不错,但它返回一个错误: Invalid parameter number: parameter was not defined (SQL: select * from "my_table" where ST_DWithin(location ,'POINT(123 123)', 1000)

我尝试了其他组合并且有效:

$point = 'POINT($lat $lon)';
$sql = "ST_DWithin(location ,?, ?)";
$query->whereRaw($sql, [$point, $distance]);

所以问题似乎出在POINT 函数上

【问题讨论】:

  • 转换为几何:'POINT(? ?)'::geometry
  • 它不起作用。还是一样的错误
  • 我注意到的其他事情:1)WKT定义坐标顺序为[x,y],因此您需要使用'POINT(<lon> <lat>)' --> 更好地使用SetSRID(ST_MakePoint(<lon>, <lat>), 4326); 2) 纬度不能超过+-90°; 3) 如果您打算使用米作为距离,请将两个输入几何图形都转换为geography,否则距离参数将被视为degrees; 4)检查是否必须在字符串文字中转义参数

标签: php laravel postgresql postgis


【解决方案1】:

此处的几何图形应作为 WKB 而非 WKT 格式传递。

select * from "my_table" where ST_DWithin(location ,ST_GeomFromText('POINT(123 123)') , 1000)

希望这会有所帮助。

【讨论】:

  • 还是同样的错误。我使用 ST_MakePoint(?,?) 而不是 POINT(? ?) 解决了它,但我仍然不知道为什么以前的查询不起作用。看起来 laravel 查询构建器/php PDO 在传递带有 '' 符号的参数时发疯了
  • 该查询在 postgresql 中运行正常吗?
  • 是的,从 "my_table" 中选择 * ST_DWithin(location ,'POINT(123 123)', 1000) 在 postgresql 中工作
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-02-25
  • 1970-01-01
  • 1970-01-01
  • 2016-12-27
  • 2015-10-20
  • 2020-07-29
  • 1970-01-01
相关资源
最近更新 更多