PostGIS  2.2.8dev-r@@SVN_REVISION@@

◆ LWGEOM_to_latlon()

Datum LWGEOM_to_latlon ( PG_FUNCTION_ARGS  )

Definition at line 174 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().

175 {
176  /* Get the parameters */
177  GSERIALIZED *pg_lwgeom = PG_GETARG_GSERIALIZED_P(0);
178  text *format_text = PG_GETARG_TEXT_P(1);
179 
180  LWGEOM *lwgeom;
181  char *format_str = NULL;
182 
183  char * formatted_str;
184  text * formatted_text;
185  char * tmp;
186 
187  /* Only supports points. */
188  uint8_t geom_type = gserialized_get_type(pg_lwgeom);
189  if (POINTTYPE != geom_type)
190  {
191  lwpgerror("Only points are supported, you tried type %s.", lwtype_name(geom_type));
192  }
193  /* Convert to LWGEOM type */
194  lwgeom = lwgeom_from_gserialized(pg_lwgeom);
195 
196  if (format_text == NULL) {
197  lwpgerror("ST_AsLatLonText: invalid format string (null");
198  PG_RETURN_NULL();
199  }
200 
201  format_str = text2cstring(format_text);
202  assert(format_str != NULL);
203 
204  /* The input string supposedly will be in the database encoding,
205  so convert to UTF-8. */
206  tmp = (char *)pg_do_encoding_conversion(
207  (uint8_t *)format_str, strlen(format_str), GetDatabaseEncoding(), PG_UTF8);
208  assert(tmp != NULL);
209  if ( tmp != format_str ) {
210  pfree(format_str);
211  format_str = tmp;
212  }
213 
214  /* Produce the formatted string. */
215  formatted_str = lwpoint_to_latlon((LWPOINT *)lwgeom, format_str);
216  assert(formatted_str != NULL);
217  pfree(format_str);
218 
219  /* Convert the formatted string from UTF-8 back to database encoding. */
220  tmp = (char *)pg_do_encoding_conversion(
221  (uint8_t *)formatted_str, strlen(formatted_str),
222  PG_UTF8, GetDatabaseEncoding());
223  assert(tmp != NULL);
224  if ( tmp != formatted_str) {
225  pfree(formatted_str);
226  formatted_str = tmp;
227  }
228 
229  /* Convert to the postgres output string type. */
230  formatted_text = cstring2text(formatted_str);
231  pfree(formatted_str);
232 
233  PG_RETURN_POINTER(formatted_text);
234 }
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:55
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:393
const char * lwtype_name(uint8_t type)
Return the type name string associated with a type number (e.g.
Definition: lwutil.c:188
char * text2cstring(const text *textptr)
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:70
Here is the call graph for this function:
Here is the caller graph for this function: