PostGIS  3.0.6dev-r@@SVN_REVISION@@

◆ LWGEOM_to_latlon()

Datum LWGEOM_to_latlon ( PG_FUNCTION_ARGS  )

Definition at line 200 of file lwgeom_inout.c.

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

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

Here is the call graph for this function: