<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css" style="display:none;"><!-- P {margin-top:0;margin-bottom:0;} --></style>
</head>
<body dir="ltr">
<div id="divtagdefaultwrapper" style="font-size:12pt;color:#000000;font-family:Calibri,Helvetica,sans-serif;" dir="ltr">
<p></p>
<div>Hello,</div>
<div>I developed some basic extensions for PostgreSQL to study. The extensions contained input and output functions, operators, index (GiST), among others.</div>
<div><br>
</div>
<div>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:</div>
<br>
<p></p>
<p></p>
<div>struct trajectory_elem</div>
<div>{</div>
<div>  int32 id;</div>
<div>  Timestamp time_obj;</div>
<div>  GSERIALIZED *geom_elem;       /* Geometry Object */</div>
<div>};</div>
<div><br>
</div>
<div>#define DatumGetTrajectoryElem(X)      ((struct trajectory_elem*) PG_DETOAST_DATUM(X))</div>
<div>#define PG_GETARG_TRAJECTELEM_TYPE_P(n)  DatumGetTrajectoryElem(PG_GETARG_DATUM(n))</div>
<div>#define PG_RETURN_TRAJECTELEM_TYPE_P(x)  PG_RETURN_POINTER(x)</div>
<div><br>
</div>
<div>
<div>PG_FUNCTION_INFO_V1(trajectory_elem);</div>
<div><br>
</div>
<div>Datum</div>
<div>trajectory_elem(PG_FUNCTION_ARGS)</div>
<div>{</div>
<div><br>
</div>
<div>  int32 id = PG_GETARG_INT32(0);</div>
<div><br>
</div>
<div>  Timestamp timestamp = PG_GETARG_TIMESTAMP(1);</div>
<div><br>
</div>
<div>  GSERIALIZED *geom = (GSERIALIZED*)PG_DETOAST_DATUM(PG_GETARG_DATUM(2));</div>
<div><br>
</div>
<div>  LWGEOM *lwgeom = lwgeom_from_gserialized(geom);</div>
<div><br>
</div>
<div>  struct trajectory_elem *trje = (struct trajectory_elem *) palloc(sizeof(struct trajectory_elem));</div>
<div><br>
</div>
<div><span style="font-size: 12pt;">  size_t size;</span><br>
</div>
<div><br>
</div>
<div>  /*elog(NOTICE, "trajectory_elem called");*/</div>
<div><br>
</div>
<div>  if (lwgeom_is_empty(lwgeom) || lwgeom->type != POINTTYPE)</div>
<div>  {</div>
<div>    elog(NOTICE, "trajectory_elem is NULL, or type is not correct");</div>
<div>  <span style="white-space:pre"></span>PG_RETURN_NULL();</div>
<div>  }</div>
<div><br>
</div>
<div>  trje->id = id;</div>
<div>  trje->time_obj = timestamp;</div>
<div>  trje->geom_elem = gserialized_from_lwgeom(lwgeom, &size);</div>
<div><br>
</div>
<div>  elog(NOTICE, "trajectory_elem finish");</div>
<div><br>
</div>
<div>  /* Then call free_if_copy on the *varlena* structures you originally get as arguments */</div>
<div>  lwgeom_free(lwgeom);</div>
<div>  PG_FREE_IF_COPY(geom, 0);</div>
<div><span style="font-size: 12pt;"><br>
</span></div>
<div><span style="font-size: 12pt;">  PG_RETURN_TRAJECTELEM_TYPE_P(trje);</span><br>
</div>
<div></div>
<div><br>
</div>
<div><span style="font-size: 12pt;">}</span><br>
</div>
<div><span style="font-size: 12pt;"><span>I also imprinted the output function using </span></span><span style="font-size: 12pt;">lwgeom_to_hexwkb(lwgeom, WKB_EXTENDED, &size);</span></div>
<div><span style="font-size: 12pt;"><span>
<div>
<div><br>
</div>
<div><span>But in PG_RETURN the connection is closed.</span></div>
</div>
<div><span><br>
</span></div>
<div><span>
<div style="font-family: Calibri, Helvetica, sans-serif, serif, EmojiFont; font-size: 16px;">
It is wrong the way I'm programming the UDT for PostGIS?</div>
<div style="font-family: Calibri, Helvetica, sans-serif, serif, EmojiFont; font-size: 16px;">
<br>
</div>
<div style="font-family: Calibri, Helvetica, sans-serif, serif, EmojiFont; font-size: 16px;">
Thanks in advance !! (and <span>Sorry for the english)</span></div>
<br>
</span></div>
<br>
</span></span></div>
<br>
</div>
<br>
<p></p>
</div>
</body>
</html>