PostGIS 3.7.0dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches

◆ LWGEOM_to_latlon()

Datum LWGEOM_to_latlon ( PG_FUNCTION_ARGS  )

Definition at line 214 of file lwgeom_inout.c.

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

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

Here is the call graph for this function: