【问题标题】:How develop PostgreSQL extension inluding PostGIS library如何开发包含 PostGIS 库的 PostgreSQL 扩展
【发布时间】:2020-12-18 01:58:33
【问题描述】:

我不是 C 专家。 据说使用或不使用 C 构建 PostgreSQL 扩展非常强大。

99% 的时间我也使用 PostGIS 和扩展,但问题是: 无法正确包含需要的内容,我玩过

#include "../postgis_config.h"
#include "liblwgeom.h"
#include "lwgeom_pg.h"

但是如果不重新编译整个 PostGIS 就无法获得正确的结果...因此创建派生版本。

PostGIS 应该是我想到的库的依赖项,所以我需要一些链接到它的东西,如果存在或捕获错误,同时编译我的库...

目前我正在使用 PostgreSQL 的 #include <utils/geo_decls.h> 进行解析 因此创建 PostgreSQL 函数接口:

  1. 将 PostGIS 功能转换为 PostgreSQL 基本地理类型
  2. 将结果传递给我的 C 库
  3. 适用于那些非常基础的类型(失去强大的 PostGIS 库的任何支持)
  4. 将结果发送到转换回 PostGIS 的函数。

请简单介绍一下我现在在做什么:
mylib.c

#include "postgres.h"           
#include "fmgr.h"
#include <utils/geo_decls.h>    
#include "utils/builtins.h"

Datum pg_xytile_to_box(PG_FUNCTION_ARGS)
{
    int x, y, zoom;
    struct tile_box box;
    BOX *pg_box = (BOX *) palloc(sizeof(BOX));

    x = PG_GETARG_INT64(0);
    y = PG_GETARG_INT64(1);
    zoom = PG_GETARG_INT32(2);

    if ( xytile_to_box(&box, x, y, zoom) != 0 )
    {
        pfree(pg_box);
        PG_RETURN_NULL();
    }

    pg_box->low.x = box.w;
    pg_box->low.y = box.s;
    pg_box->high.x = box.e;
    pg_box->high.y = box.n;

    PG_RETURN_BOX_P(pg_box);
}

PG_FUNCTION_INFO_V1(pg_xytile_to_box);

extension_code.sql

CREATE OR REPLACE FUNCTION _xytile_to_box(x int, y int, zoom int default 11) RETURNS box
    AS 'MODULE_PATHNAME', 'pg_xytile_to_box'
 LANGUAGE C IMMUTABLE STRICT;

CREATE OR REPLACE FUNCTION xtile_to_geom(xtile int, ytile int, zoom int default 11) returns geometry(polygon, 4326)
language plpgsql security definer as $FUNC$
BEGIN
return (
select st_setsrid((ST_MakeBox2D(b[0]::geometry(point), b[1]::geometry(point)))::geometry(polygon), 4326) as t
from _xytile_to_box(xtile, ytile, zoom) as b
);
END; $FUNC$;

有一种方法可以指示 Makefile 查找 PostGIS 并将其添加到导入中吗? 有更好的吗?

【问题讨论】:

    标签: c postgresql makefile postgis


    【解决方案1】:

    PostGIS 似乎没有公共 C API,因此您将无法直接链接到 PostGIS 库。

    我的建议是使用Server Programming Interface (SPI) 从您的 C 代码中调用 PostGIS SQL 函数。另一种方法是直接使用include/fmgr.h中的函数调用接口调用PostGIS函数,这样会更快,但需要您先在目录中查找函数。

    【讨论】:

      猜你喜欢
      • 2014-05-08
      • 2019-06-02
      • 2015-02-17
      • 2020-06-25
      • 1970-01-01
      • 1970-01-01
      • 2019-04-01
      • 2015-01-15
      • 1970-01-01
      相关资源
      最近更新 更多