PostGIS  2.1.10dev-r@@SVN_REVISION@@
Datum LWGEOM_to_latlon ( PG_FUNCTION_ARGS  )

Definition at line 162 of file lwgeom_inout.c.

References gserialized_get_type(), lwerror(), lwgeom_from_gserialized(), lwpoint_to_latlon(), lwtype_name(), POINTTYPE, and text2cstring().

163 {
164  /* Get the parameters */
165  GSERIALIZED *pg_lwgeom = (GSERIALIZED *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
166  text *format_text = PG_GETARG_TEXT_P(1);
167 
168  LWGEOM *lwgeom;
169  char *format_str = NULL;
170 
171  char * formatted_str;
172  text * formatted_text;
173  char * tmp;
174 
175  /* Only supports points. */
176  uint8_t geom_type = gserialized_get_type(pg_lwgeom);
177  if (POINTTYPE != geom_type)
178  {
179  lwerror("Only points are supported, you tried type %s.", lwtype_name(geom_type));
180  }
181  /* Convert to LWGEOM type */
182  lwgeom = lwgeom_from_gserialized(pg_lwgeom);
183 
184  if (format_text == NULL) {
185  lwerror("ST_AsLatLonText: invalid format string (null");
186  PG_RETURN_NULL();
187  }
188 
189  format_str = text2cstring(format_text);
190  assert(format_str != NULL);
191 
192  /* The input string supposedly will be in the database encoding,
193  so convert to UTF-8. */
194  tmp = (char *)pg_do_encoding_conversion(
195  (uint8_t *)format_str, strlen(format_str), GetDatabaseEncoding(), PG_UTF8);
196  assert(tmp != NULL);
197  if ( tmp != format_str ) {
198  pfree(format_str);
199  format_str = tmp;
200  }
201 
202  /* Produce the formatted string. */
203  formatted_str = lwpoint_to_latlon((LWPOINT *)lwgeom, format_str);
204  assert(formatted_str != NULL);
205  pfree(format_str);
206 
207  /* Convert the formatted string from UTF-8 back to database encoding. */
208  tmp = (char *)pg_do_encoding_conversion(
209  (uint8_t *)formatted_str, strlen(formatted_str),
210  PG_UTF8, GetDatabaseEncoding());
211  assert(tmp != NULL);
212  if ( tmp != formatted_str) {
213  pfree(formatted_str);
214  formatted_str = tmp;
215  }
216 
217  /* Convert to the postgres output string type. */
218  formatted_text = cstring2text(formatted_str);
219  pfree(formatted_str);
220 
221  PG_RETURN_POINTER(formatted_text);
222 }
uint32_t gserialized_get_type(const GSERIALIZED *s)
Extract the geometry type from the serialized form (it hides in the anonymous data area...
Definition: g_serialized.c:56
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
char * lwpoint_to_latlon(const LWPOINT *p, const char *format)
Definition: lwprint.c:391
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:67
const char * lwtype_name(uint8_t type)
Return the type name string associated with a type number (e.g.
Definition: lwutil.c:164
char * text2cstring(const text *textptr)
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:60

Here is the call graph for this function: