【问题标题】:creating input and output functions for postgresql custom type using postgis types使用 postgis 类型为 postgresql 自定义类型创建输入和输出函数
【发布时间】:2017-11-01 20:26:25
【问题描述】:

我在我的 postgresql 数据库中创建了一个具有这种复合结构的自定义类型:

CREATE TYPE circle_geo AS (
       box geography(POLYGON,4326), 
       center geography(POINT,4326), 
       radius float8
);

所以它使用了一些 postgis 类型(POINT 和 POLYGON)。 现在我希望能够为这个自定义类型创建一个 INPUT 和 OUTPUT 函数,以便我可以传递一个简单的字符串表示(即 (x,y),c 就像 postgresql 本机几何圆类型)来创建一个它的实例

我的输入伪函数如下所示:

input_function(cstring) where cstring is of the form (x,y),r:

radius = r;
center = ST_MakePoint(x,y);
pdist = sqrt(2*radius*radius);
box = ST_MakePolygon(ST_MakeLine(ARRAY[
                ST_Project(center, pdist, radians(45.0)),
                ST_Project(center, pdist, radians(135.0)),
                ST_Project(center, pdist, radians(-135.0)),
                ST_Project(center, pdist, radians(-45.0)),
                ST_Project(center, pdist, radians(45.0))
    ]))

我的 output_function 应该只返回一个由

组成的 cstring
(center.x_lon,center.y_lat),radius

我的问题是我不知道如何编写这些输入和输出函数。它们可以用 PLPgSQL 编写还是必须用 C 编写?如果是这样,我如何在其中调用 postgis 函数(如 ST_MakePoint、ST_MakePolygon、ST_MakeLine、ST_Project ......)? 任何帮助将不胜感激;)

【问题讨论】:

    标签: postgresql postgis plpgsql custom-type


    【解决方案1】:

    类型输入和输出函数必须用C编写,因为它们需要接受或返回类型cstring,这是一个普通的C字符串,不能用SQL处理。

    请参阅the documentation,其中显示了一个示例。

    您可以通过Server Programming Interface (SPI) 从您的函数中调用 PostGIS 函数,这允许您执行 SQL 语句,或者通过直接调用从 PostGIS 导出的 C 函数来更快(在这种情况下,您必须与 PostGIS 链接)。

    【讨论】:

      猜你喜欢
      • 2011-07-30
      • 2013-01-22
      • 2016-07-18
      • 2020-12-11
      • 1970-01-01
      • 2017-10-07
      • 2020-07-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多