[postgis-devel] Create a new data type for PostGIS in C

Fabiana Zioti fabi_zioti at hotmail.com
Thu Aug 17 10:47:06 PDT 2017


Hello,
I developed some basic extensions for PostgreSQL to study. The extensions contained input and output functions, operators, index (GiST), among others.

Now I would like to create a new data type for PostGIS, but I have some difficulties. Here's what I've done so far:


struct trajectory_elem
{
  int32 id;
  Timestamp time_obj;
  GSERIALIZED *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)

PG_FUNCTION_INFO_V1(trajectory_elem);

Datum
trajectory_elem(PG_FUNCTION_ARGS)
{

  int32 id = PG_GETARG_INT32(0);

  Timestamp timestamp = PG_GETARG_TIMESTAMP(1);

  GSERIALIZED *geom = (GSERIALIZED*)PG_DETOAST_DATUM(PG_GETARG_DATUM(2));

  LWGEOM *lwgeom = lwgeom_from_gserialized(geom);

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

  size_t size;

  /*elog(NOTICE, "trajectory_elem called");*/

  if (lwgeom_is_empty(lwgeom) || lwgeom->type != POINTTYPE)
  {
    elog(NOTICE, "trajectory_elem is NULL, or type is not correct");
  PG_RETURN_NULL();
  }

  trje->id = id;
  trje->time_obj = timestamp;
  trje->geom_elem = gserialized_from_lwgeom(lwgeom, &size);

  elog(NOTICE, "trajectory_elem finish");

  /* Then call free_if_copy on the *varlena* structures you originally get as arguments */
  lwgeom_free(lwgeom);
  PG_FREE_IF_COPY(geom, 0);

  PG_RETURN_TRAJECTELEM_TYPE_P(trje);

}
I also imprinted the output function using lwgeom_to_hexwkb(lwgeom, WKB_EXTENDED, &size);

But in PG_RETURN the connection is closed.

It is wrong the way I'm programming the UDT for PostGIS?

Am I missing too many details? Would you have any examples of creating a new type of data for PostGIS?


Thanks in advance !! (and Sorry for the english)


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/postgis-devel/attachments/20170817/0f2c89ae/attachment.html>


More information about the postgis-devel mailing list