3

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

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


struct trajectory_elem
{
  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)
{
  GSERIALIZED *geom = PG_GETARG_GSERIALIZED_P(0);

  LWGEOM *lwgeom = lwgeom_from_gserialized(geom);

  Timestamp timestamp = PG_GETARG_TIMESTAMP(1);

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

  elog(NOTICE, "trajectory_elem called");

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

  trje->lwgeom = lwgeom;

  if(trje->lwgeom->type == POINTTYPE){
    elog(NOTICE, "eh ponto");
  }

  trje->time_obj = timestamp;

  elog(NOTICE, "trajectory_elem finish");

  PG_RETURN_TRAJECTELEM_TYPE_P(trje);


 }

Is the way I'm creating my trajectory_elem structure correct?

Can I simply point to the LWGEO type? Or.. Should I create a new type like the GSERIALIZED ?

How would the input and output functions for this type be? Could I use the functions already implemented by PostGIS and lwgeom?

I tested some implementations for the I / O functions but when it arrives in PG_RETURN_TRAJECTELEM_TYPE_P (create_trajectory_elem function), the server drops. So they were probably wrong ?

  • Welcome to GIS SE! As a new user be sure to take the Tour where you will see that there should be only one question asked per question under our focussed Q&A format. – PolyGeo Sep 3 '17 at 22:03
  • 1
    This is more of a software engineering question than a GIS one. – Vince Sep 4 '17 at 2:55
  • 1
    ... and sufficiently specialist that you might have more luck on the PostGIS mailing lists. It requires postgres guts knowledge and PostGIS specifics. I hack on postgres for a living, and I'd have to do some studying before I could help you... – Craig Ringer Sep 4 '17 at 5:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.