【问题标题】:PostGIS functions integrated in php example?php 示例中集成的 PostGIS 功能?
【发布时间】:2012-11-19 07:41:51
【问题描述】:

我正在为具有 PostGIS 功能的 postgreSQL 数据库编写客户端。这个客户端当然是通过 php 进行通信的。但是,我正在寻找可以从我的 php 文件中调用的可用 postGIS 函数的适当文档。

如果我能得到一个简单的例子来说明 postGIS 函数是如何集成到 php 中的,我将不胜感激。

干杯:)

【问题讨论】:

标签: php function postgresql geocoding postgis


【解决方案1】:

Postgis 是在您的 postgresql 数据库中的某些模式中可用的存储过程。 如果您有 PDO,您可以编写调用然后非常接近对象的过程。 有关可用的获取模式,请参阅 PHP PDO。

<?php
require_once 'constants.php';
class Postgis extends PDO
{
  public function __construct()
  {
    $this->pg = new PDO( PDO_DB_DSN );
    $this->pg->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
  }
  public function makePoint( $x, $y )
  {
    $preparedStatement = $this->pg->prepare( 'SELECT public.st_makepoint( ?, ? )' );
    $preparedStatement->execute( array( $x, $y ) );
    return $preparedStatement->fetchColumn();
  }
}

try
{
  $postgis = new Postgis();
  $point = $postgis->makePoint( 34, 44 ); //point holds the binary string of a point without srid
  var_dump( $point );
}
catch ( PDOException $e )
{
  // any errors here
  var_dump( $e->getMessage() );
}
?>

【讨论】:

    【解决方案2】:

    PostGIS 在其网站上列出了它的所有类型和功能,例如这里是Version 1.5 reference documentation。您可以将这些函数调用为 SQL,就像从 PHP 调用任何其他 SQL 一样。

    因此,使用 PHP 的内置 PostgreSQL 函数,您可以执行以下操作:

    $result = pg_query_params('
        SELECT ST_Distance( location, ST_SetSRID(ST_Point($1, $2), 27700) )
        FROM table WHERE id=$3', array(400000, 300000, 123) );
    

    获取从英国网格参考系统中的 (400,000, 300,000) 到数据库表中第 123 行位置列的距离(以米为单位)。

    可能有围绕 PostGIS 函数的 PHP 包装器,但恐怕我不知道任何

    【讨论】:

      猜你喜欢
      • 2022-01-12
      • 2020-04-13
      • 1970-01-01
      • 1970-01-01
      • 2012-08-01
      • 2021-06-15
      • 1970-01-01
      • 2013-11-01
      • 2020-12-09
      相关资源
      最近更新 更多