PostGIS  3.4.0dev-r@@SVN_REVISION@@

◆ LWGEOM_to_latlon()

Datum LWGEOM_to_latlon ( PG_FUNCTION_ARGS  )

Definition at line 212 of file lwgeom_inout.c.

213 {
214  /* Get the parameters */
215  GSERIALIZED *pg_lwgeom = PG_GETARG_GSERIALIZED_P(0);
216  text *format_text = PG_GETARG_TEXT_P(1);
217 
218  LWGEOM *lwgeom;
219  char *format_str = NULL;
220 
221  char * formatted_str;
222  text * formatted_text;
223  char * tmp;
224 
225  /* Only supports points. */
226  uint8_t geom_type = gserialized_get_type(pg_lwgeom);
227  if (POINTTYPE != geom_type)
228  {
229  lwpgerror("Only points are supported, you tried type %s.", lwtype_name(geom_type));
230  }
231  /* Convert to LWGEOM type */
232  lwgeom = lwgeom_from_gserialized(pg_lwgeom);
233 
234  if (format_text == NULL) {
235  lwpgerror("ST_AsLatLonText: invalid format string (null");
236  PG_RETURN_NULL();
237  }
238 
239  if (!lwgeom_isfinite(lwgeom)) {
240  lwpgerror("ST_AsLatLonText: invalid coordinate");
241  PG_RETURN_NULL();
242  }
243 
244  format_str = text_to_cstring(format_text);
245  assert(format_str != NULL);
246 
247  /* The input string supposedly will be in the database encoding,
248  so convert to UTF-8. */
249  tmp = (char *)pg_do_encoding_conversion(
250  (uint8_t *)format_str, strlen(format_str), GetDatabaseEncoding(), PG_UTF8);
251  assert(tmp != NULL);
252  if ( tmp != format_str ) {
253  pfree(format_str);
254  format_str = tmp;
255  }
256 
257  /* Produce the formatted string. */
258  formatted_str = lwpoint_to_latlon((LWPOINT *)lwgeom, format_str);
259  assert(formatted_str != NULL);
260  pfree(format_str);
261 
262  /* Convert the formatted string from UTF-8 back to database encoding. */
263  tmp = (char *)pg_do_encoding_conversion(
264  (uint8_t *)formatted_str, strlen(formatted_str),
265  PG_UTF8, GetDatabaseEncoding());
266  assert(tmp != NULL);
267  if ( tmp != formatted_str) {
268  pfree(formatted_str);
269  formatted_str = tmp;
270  }
271 
272  /* Convert to the postgres output string type. */
273  formatted_text = cstring_to_text(formatted_str);
274  pfree(formatted_str);
275 
276  PG_RETURN_POINTER(formatted_text);
277 }
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
Definition: gserialized.c:239
uint32_t gserialized_get_type(const GSERIALIZED *g)
Extract the geometry type from the serialized form (it hides in the anonymous data area,...
Definition: gserialized.c:89
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:102
int lwgeom_isfinite(const LWGEOM *lwgeom)
Check if a LWGEOM has any non-finite (NaN or Inf) coordinates.
Definition: lwgeom.c:2681
char * lwpoint_to_latlon(const LWPOINT *p, const char *format)
Definition: lwprint.c:431
const char * lwtype_name(uint8_t type)
Return the type name string associated with a type number (e.g.
Definition: lwutil.c:216

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

Here is the call graph for this function: