如何使用PostGIS类型实现PostgreSQL扩展

时间:2017-09-01 12:14:11

标签: c postgresql postgis

我想为PostGIS创建一个新的数据类型,但是我遇到了一些困难。这是我到目前为止所做的:

/* PostgreSQL */
#include <postgres.h>
#include <liblwgeom.h>
#include <lwgeom_pg.h>
#include <utils/timestamp.h>


struct trajectory_elem
{
  // int32 dummy;    /* Padding to make it double aligned. */
  Timestamp time_obj;
  LWGEOM *geom_elem;       /* Geometry Object - */
};

#define DatumGetTrajectoryElem(X)      ((struct trajectory_elem*) PG_DETOAST_DATUM(X))
#define PG_GETARG_TRAJECTELEM_TYPE_P(n)  DatumGetTrajectoryElem(PG_GETARG_DATUM(n))
#define PG_RETURN_TRAJECTELEM_TYPE_P(x)  PG_RETURN_POINTER(x)


extern Datum create_trajectory_elem(PG_FUNCTION_ARGS);
extern Datum trajectory_elem_in(PG_FUNCTION_ARGS);
extern Datum trajectory_elem_out(PG_FUNCTION_ARGS);

.....
PG_FUNCTION_INFO_V1(create_trajectory_elem);

Datum
create_trajectory_elem(PG_FUNCTION_ARGS)
 {
   Timestamp time_geom = PG_GETARG_TIMESTAMP(0);
   GSERIALIZED *geom = (GSERIALIZED*)PG_DETOAST_DATUM(PG_GETARG_DATUM(1));

   struct trajectory_elem *trje = (struct trajectory_elem *) palloc(sizeof(struct trajectory_elem));

   trje->time_obj = time_geom;

   /*get lwgeom from GSERIALIZED varlena*/
   trje->geom_elem = lwgeom_from_gserialized(geom);

   if ( lwgeom_is_empty(trje->geom_elem))
   {
     PG_FREE_IF_COPY(geom, 0);
     PG_RETURN_NULL();
   }

   PG_FREE_IF_COPY(geom, 0);

   PG_RETURN_TRAJECTELEM_TYPE_P(trje);

 }

我创建轨迹_elem结构的方式是否正确?

我可以简单地指向LWGEO类型吗?或者..我应该创建一个像GSERIALIZED这样的新类型吗?

此类型的输入和输出功能如何?我可以使用已经由PostGIS实现的功能吗?

我测试了I / O函数的一些实现,但是当它到达PG_RETURN_TRAJECTELEM_TYPE_P(create_trajectory_elem)时,服务器就会丢弃。所以他们可能错了

0 个答案:

没有答案