PostGIS  2.3.7dev-r@@SVN_REVISION@@
char* rtpg_getSR ( int  srid)

Definition at line 282 of file rtpg_internal.c.

References MAX_INT_CHARLEN, POSTGIS_RT_DEBUGF, rt_util_gdal_supported_sr(), rtgdalraster::sql, and TRUE.

Referenced by RASTER_asGDALRaster(), RASTER_asRaster(), and RASTER_GDALWarp().

282  {
283  int i = 0;
284  int len = 0;
285  char *sql = NULL;
286  int spi_result;
287  TupleDesc tupdesc;
288  SPITupleTable *tuptable = NULL;
289  HeapTuple tuple;
290  char *tmp = NULL;
291  char *srs = NULL;
292 
293 /*
294 SELECT
295  CASE
296  WHEN (upper(auth_name) = 'EPSG' OR upper(auth_name) = 'EPSGA') AND length(COALESCE(auth_srid::text, '')) > 0
297  THEN upper(auth_name) || ':' || auth_srid
298  WHEN length(COALESCE(auth_name, '') || COALESCE(auth_srid::text, '')) > 0
299  THEN COALESCE(auth_name, '') || COALESCE(auth_srid::text, '')
300  ELSE ''
301  END,
302  proj4text,
303  srtext
304 FROM spatial_ref_sys
305 WHERE srid = X
306 LIMIT 1
307 */
308 
309  len = sizeof(char) * (strlen("SELECT CASE WHEN (upper(auth_name) = 'EPSG' OR upper(auth_name) = 'EPSGA') AND length(COALESCE(auth_srid::text, '')) > 0 THEN upper(auth_name) || ':' || auth_srid WHEN length(COALESCE(auth_name, '') || COALESCE(auth_srid::text, '')) > 0 THEN COALESCE(auth_name, '') || COALESCE(auth_srid::text, '') ELSE '' END, proj4text, srtext FROM spatial_ref_sys WHERE srid = LIMIT 1") + MAX_INT_CHARLEN + 1);
310  sql = (char *) palloc(len);
311  if (NULL == sql) {
312  elog(ERROR, "rtpg_getSR: Could not allocate memory for sql\n");
313  return NULL;
314  }
315 
316  spi_result = SPI_connect();
317  if (spi_result != SPI_OK_CONNECT) {
318  pfree(sql);
319  elog(ERROR, "rtpg_getSR: Could not connect to database using SPI\n");
320  return NULL;
321  }
322 
323  /* execute query */
324  snprintf(sql, len, "SELECT CASE WHEN (upper(auth_name) = 'EPSG' OR upper(auth_name) = 'EPSGA') AND length(COALESCE(auth_srid::text, '')) > 0 THEN upper(auth_name) || ':' || auth_srid WHEN length(COALESCE(auth_name, '') || COALESCE(auth_srid::text, '')) > 0 THEN COALESCE(auth_name, '') || COALESCE(auth_srid::text, '') ELSE '' END, proj4text, srtext FROM spatial_ref_sys WHERE srid = %d LIMIT 1", srid);
325  POSTGIS_RT_DEBUGF(4, "SRS query: %s", sql);
326  spi_result = SPI_execute(sql, TRUE, 0);
327  SPI_pfree(sql);
328  if (spi_result != SPI_OK_SELECT || SPI_tuptable == NULL || SPI_processed != 1) {
329  if (SPI_tuptable) SPI_freetuptable(tuptable);
330  SPI_finish();
331  elog(ERROR, "rtpg_getSR: Cannot find SRID (%d) in spatial_ref_sys", srid);
332  return NULL;
333  }
334 
335  tupdesc = SPI_tuptable->tupdesc;
336  tuptable = SPI_tuptable;
337  tuple = tuptable->vals[0];
338 
339  /* which column to use? */
340  for (i = 1; i < 4; i++) {
341  tmp = SPI_getvalue(tuple, tupdesc, i);
342 
343  /* value AND GDAL supports this SR */
344  if (
345  SPI_result != SPI_ERROR_NOATTRIBUTE &&
346  SPI_result != SPI_ERROR_NOOUTFUNC &&
347  tmp != NULL &&
348  strlen(tmp) &&
350  ) {
351  POSTGIS_RT_DEBUGF(4, "Value for column %d is %s", i, tmp);
352 
353  len = strlen(tmp) + 1;
354  srs = SPI_palloc(sizeof(char) * len);
355  if (NULL == srs) {
356  pfree(tmp);
357  if (SPI_tuptable) SPI_freetuptable(tuptable);
358  SPI_finish();
359  elog(ERROR, "rtpg_getSR: Could not allocate memory for spatial reference text\n");
360  return NULL;
361  }
362  strncpy(srs, tmp, len);
363  pfree(tmp);
364 
365  break;
366  }
367 
368  if (tmp != NULL)
369  pfree(tmp);
370  continue;
371  }
372 
373  if (SPI_tuptable) SPI_freetuptable(tuptable);
374  SPI_finish();
375 
376  /* unable to get SR info */
377  if (srs == NULL) {
378  if (SPI_tuptable) SPI_freetuptable(tuptable);
379  SPI_finish();
380  elog(ERROR, "rtpg_getSR: Could not find a viable spatial reference for SRID (%d)", srid);
381  return NULL;
382  }
383 
384  return srs;
385 }
#define POSTGIS_RT_DEBUGF(level, msg,...)
Definition: rtpostgis.h:57
#define MAX_INT_CHARLEN
Definition: rtpostgis.h:68
int rt_util_gdal_supported_sr(const char *srs)
Definition: rt_util.c:243
#define TRUE
Definition: dbfopen.c:169

Here is the call graph for this function:

Here is the caller graph for this function: