PostGIS  2.5.7dev-r@@SVN_REVISION@@

◆ LWGEOM_to_latlon()

Datum LWGEOM_to_latlon ( PG_FUNCTION_ARGS  )

Definition at line 199 of file lwgeom_inout.c.

200 {
201  /* Get the parameters */
202  GSERIALIZED *pg_lwgeom = PG_GETARG_GSERIALIZED_P(0);
203  text *format_text = PG_GETARG_TEXT_P(1);
204 
205  LWGEOM *lwgeom;
206  char *format_str = NULL;
207 
208  char * formatted_str;
209  text * formatted_text;
210  char * tmp;
211 
212  /* Only supports points. */
213  uint8_t geom_type = gserialized_get_type(pg_lwgeom);
214  if (POINTTYPE != geom_type)
215  {
216  lwpgerror("Only points are supported, you tried type %s.", lwtype_name(geom_type));
217  }
218  /* Convert to LWGEOM type */
219  lwgeom = lwgeom_from_gserialized(pg_lwgeom);
220 
221  if (format_text == NULL) {
222  lwpgerror("ST_AsLatLonText: invalid format string (null");
223  PG_RETURN_NULL();
224  }
225 
226  format_str = text_to_cstring(format_text);
227  assert(format_str != NULL);
228 
229  /* The input string supposedly will be in the database encoding,
230  so convert to UTF-8. */
231  tmp = (char *)pg_do_encoding_conversion(
232  (uint8_t *)format_str, strlen(format_str), GetDatabaseEncoding(), PG_UTF8);
233  assert(tmp != NULL);
234  if ( tmp != format_str ) {
235  pfree(format_str);
236  format_str = tmp;
237  }
238 
239  /* Produce the formatted string. */
240  formatted_str = lwpoint_to_latlon((LWPOINT *)lwgeom, format_str);
241  assert(formatted_str != NULL);
242  pfree(format_str);
243 
244  /* Convert the formatted string from UTF-8 back to database encoding. */
245  tmp = (char *)pg_do_encoding_conversion(
246  (uint8_t *)formatted_str, strlen(formatted_str),
247  PG_UTF8, GetDatabaseEncoding());
248  assert(tmp != NULL);
249  if ( tmp != formatted_str) {
250  pfree(formatted_str);
251  formatted_str = tmp;
252  }
253 
254  /* Convert to the postgres output string type. */
255  formatted_text = cstring_to_text(formatted_str);
256  pfree(formatted_str);
257 
258  PG_RETURN_POINTER(formatted_text);
259 }
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
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:86
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:85
char * lwpoint_to_latlon(const LWPOINT *p, const char *format)
Definition: lwprint.c:426
const char * lwtype_name(uint8_t type)
Return the type name string associated with a type number (e.g.
Definition: lwutil.c:218
char * text_to_cstring(const text *textptr)
unsigned char uint8_t
Definition: uthash.h:79

References gserialized_get_type(), lwgeom_from_gserialized(), lwpoint_to_latlon(), lwtype_name(), POINTTYPE, and text_to_cstring().

Here is the call graph for this function: