【发布时间】: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 函数接口:
- 将 PostGIS 功能转换为 PostgreSQL 基本地理类型
- 将结果传递给我的 C 库
- 适用于那些非常基础的类型(失去强大的 PostGIS 库的任何支持)
- 将结果发送到转换回 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