PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ LWGEOM_to_latlon()

Datum LWGEOM_to_latlon ( PG_FUNCTION_ARGS  )

Definition at line 198 of file lwgeom_inout.c.

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

Referenced by LWGEOM_in().

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