PHP查询(包括PostGiS函数)不返回任何结果



我制作了一个脚本,根据点是否在多边形内向我返回ID。目前,当我在DB或Postman上调用脚本时,这是有效的,尽管当我在其他地方调用它时,它不起作用。

我查看了请求后的正文,发现"坐标"不是地理位置,而是编码版本(我的猜测(,我试图在查询中解包,但没有成功。我能够在不同的查询中对其进行解包的唯一方法是st_asgeojson,然后对结果使用json_decode,但我希望在一个查询中完成。

坐标按以下代码传递:

"0101000020E6100000000000000040604000000000000039C0"

我想在查询外部或内部转换它,然后从我的查询中获得结果。我的PHP代码如下:

<?php
require_once('../db.php');
$rawdata = file_get_contents("php://input");
$decoded = json_decode($rawdata, true);
$coordVar = handleCoords($decoded['event']['data']['new']['Coordinates']['coordinates']);
$res = pg_query_params($conn, 'SELECT "ShapeID" FROM "Shapes" WHERE st_within( ST_SetSRID( ST_MakePoint($1, $2), 4326), "Coordinates"::geometry) ', array($coordVar[0], $coordVar[1]));
while ($row = pg_fetch_assoc($res) ){
print_r($row['ShapeID]);
}

function handleCoords($data) {
return array($data[0], $data[1]);
}
?>

在测试我的查询时,我获得了"坐标";在查看结果时,我的一个表是字节码,因为这是一个地理数据类型。我通过使用::几何转换来解决这个问题,考虑到这一点,我遇到的问题是一个简单的解决方案。由于通过POST请求主体传递的坐标也具有相同的数据类型,即地理位置,因此应用相同的转换将允许从任何应用程序调用查询时进行查询。

代码如下:

<?php
require_once('../db.php');
$rawdata = file_get_contents("php://input");
$decoded = json_decode($rawdata, true);
$coordVar = handleCoords($decoded['event']['data']['new']['Coordinates']);
$res = pg_query_params($conn, 'SELECT "ShapeID" FROM "Shapes" WHERE st_within( ST_SetSRID( $1::geometry, 4326), "Coordinates"::geometry) ', array($coordVar));
while ($row = pg_fetch_assoc($res) ){
print_r($row['ShapeID]);
}

?>

最新更新