【问题标题】:Variables inside PostgreSQL/PostGIS query in PHP filePHP文件中PostgreSQL/PostGIS查询中的变量
【发布时间】:2014-04-10 20:12:58
【问题描述】:

我正在尝试在 PHP 文件中使用 PostgreSQL/PostGIS 查询中的变量。

变量需要将 lat 和 lng 值放在 POINT 几何图形中,但在我的代码中有些东西不能正常工作,我收到错误消息:

警告:pg_query():查询失败:错误:解析错误 - 无效 geometry HINT: "POINT("

当我运行具有 POINT 值(而不是变量)的查询时

SELECT ST_AsText (the_geom) as location, name, intptlat, intptlon FROM tiger_match WHERE ST_INTERSECTS (the_geom, ST_Buffer((ST_GeomFromText('POINT(-85.1043 34.315)',4326)), 0.1));

一切正常。

我在网上看了一些文章和帖子,但我找不到解决办法:

我在 php 文件中使用的整个代码如下:

<?php
$host = "localhost"; 
$user = "postgres"; 
$pass = "2907"; 
$db = "postgis";

$intptlon = pg_escape_string($_GET['intptlon']);
$intptlat = pg_escape_string($_GET['intptlat']);

function parseToXML($htmlStr) 
{ 
$xmlStr=str_replace('<','&lt;',$htmlStr); 
$xmlStr=str_replace('>','&gt;',$xmlStr); 
$xmlStr=str_replace('"','&quot;',$xmlStr); 
$xmlStr=str_replace("'",'&#39;',$xmlStr); 
$xmlStr=str_replace("&",'&amp;',$xmlStr);
return $xmlStr; 
} 

// Opens a connection to a PostgreSQL
$con = pg_connect("host=$host dbname=$db user=$user password=$pass") or die ("Could not connect to server\n"); 

$query = "select ST_AsText (the_geom) as location, name, intptlat, intptlon FROM tiger_match WHERE ST_INTERSECTS (the_geom, ST_Buffer((ST_GeomFromText('POINT('$intptlon' '$intptlat')',4326)), 0.1))";
$rs = pg_query($con, $query) or die("Cannot execute query: $query\n");

header("Content-type: text/xml");
// Start XML file, echo parent node
echo '<markers>';

// Iterate through the rows, printing XML nodes for each
while ($row = @pg_fetch_assoc($rs)){
  // ADD TO XML DOCUMENT NODE
  echo '<marker ';
  echo 'name="' . parseToXML($row['name']) . '" ';
  echo 'intptlat="' . $row['intptlat'] . '" ';
  echo 'intptlon="' . $row['intptlon'] . '" ';
  echo 'the_geom="' . $row['location'] . '" ';
  echo '/>';
}
echo '</markers>';
?>

【问题讨论】:

  • 尝试将 POINT('$intptlon' '$intptlat') 更改为 POINT($intptlon $intptlat) 或更好 - 使用参数进行查询。
  • 参见参数化查询手册:php.net/manual/en/function.pg-query-params.php
  • 我尝试使用参数进行查询,并将我的查询更改为 $rs = pg_query_params($con,"select ST_AsText (the_geom) as location, name, intptlat, intptlon FROM tiger_match WHERE ST_INTERSECTS (the_geom, ST_Buffer((ST_GeomFromText('POINT($intptlon $intptlat)',4326)), 0.1))"); ,但我现在收到错误 pg_query_params() expects parameter 1 to be string 。这部分代码是不是也需要改一下$intptlon = pg_escape_string($_GET['intptlon']);才能使用参数查询?

标签: php postgresql postgis


【解决方案1】:

Don't mix parameters for WKT with SQL。使用传统的字符串实用程序创建 WKT(T for text),然后将该字符串参数提供给查询,或者使用采用数字参数的替代几何构造函数,例如ST_MakePoint(x, y).

查询应该如下所示:

SELECT ST_AsText(the_geom) as location, name, intptlat, intptlon
FROM tiger_match
WHERE ST_DWithin(the_geom, ST_SetSRID(ST_MakePoint($intptlon, $intptlat), 4326), 0.1);

一般来说,不要缓冲几何图形来进行邻近搜索。在这种情况下,这是一个点,但有些人也对整张桌子这样做,这是不必要的昂贵。此外,您的距离是 0.1 度,这是一个无意义的距离单位。如果您使用geography 类型,那么它将使用公制距离。

【讨论】:

  • 谢谢 Mike T,这行得通,我将查询更改为 $query = "SELECT ST_AsText(the_geom) as location, name, statefp, intptlat, intptlon FROM tiger_match WHERE ST_DWithin (the_geom, ST_SetSRID(ST_MakePoint('$intptlon', '$intptlat'), 4326), 0.01)";,并将 $intptlon = pg_escape_string($_GET['intptlon']); $intptlat = pg_escape_string($_GET['intptlat']); 更改为 $intptlat = $_GET["intptlat"]; $intptlon = $_GET["intptlon"];,一切正常 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-04
  • 1970-01-01
  • 2019-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多