我不是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函数接口:
只需简单地了解一下我现在在做什么:
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并将其添加到导入中?有什么更好的吗?
发布于 2020-12-18 00:01:11
PostGIS似乎没有公共的C,因此您将无法直接链接到PostGIS库。
我的建议是使用PostGIS调用C代码中的服务器编程接口(SPI) SQL函数。另一种方法是使用PostGIS中的函数调用接口直接调用include/fmgr.h
函数,这样会更快,但需要首先在目录中查找函数。
https://stackoverflow.com/questions/65350812
复制相似问题