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

Definition at line 912 of file rt_pg.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().

913 {
914  int i = 0;
915  int len = 0;
916  char *sql = NULL;
917  int spi_result;
918  TupleDesc tupdesc;
919  SPITupleTable *tuptable = NULL;
920  HeapTuple tuple;
921  char *tmp = NULL;
922  char *srs = NULL;
923 
924 /*
925 SELECT
926  CASE
927  WHEN (upper(auth_name) = 'EPSG' OR upper(auth_name) = 'EPSGA') AND length(COALESCE(auth_srid::text, '')) > 0
928  THEN upper(auth_name) || ':' || auth_srid
929  WHEN length(COALESCE(auth_name, '') || COALESCE(auth_srid::text, '')) > 0
930  THEN COALESCE(auth_name, '') || COALESCE(auth_srid::text, '')
931  ELSE ''
932  END,
933  proj4text,
934  srtext
935 FROM spatial_ref_sys
936 WHERE srid = X
937 LIMIT 1
938 */
939 
940  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);
941  sql = (char *) palloc(len);
942  if (NULL == sql) {
943  elog(ERROR, "rtpg_getSR: Could not allocate memory for sql\n");
944  return NULL;
945  }
946 
947  spi_result = SPI_connect();
948  if (spi_result != SPI_OK_CONNECT) {
949  pfree(sql);
950  elog(ERROR, "rtpg_getSR: Could not connect to database using SPI\n");
951  return NULL;
952  }
953 
954  /* execute query */
955  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);
956  POSTGIS_RT_DEBUGF(4, "SRS query: %s", sql);
957  spi_result = SPI_execute(sql, TRUE, 0);
958  SPI_pfree(sql);
959  if (spi_result != SPI_OK_SELECT || SPI_tuptable == NULL || SPI_processed != 1) {
960  if (SPI_tuptable) SPI_freetuptable(tuptable);
961  SPI_finish();
962  elog(ERROR, "rtpg_getSR: Cannot find SRID (%d) in spatial_ref_sys", srid);
963  return NULL;
964  }
965 
966  tupdesc = SPI_tuptable->tupdesc;
967  tuptable = SPI_tuptable;
968  tuple = tuptable->vals[0];
969 
970  /* which column to use? */
971  for (i = 1; i < 4; i++) {
972  tmp = SPI_getvalue(tuple, tupdesc, i);
973 
974  /* value AND GDAL supports this SR */
975  if (
976  SPI_result != SPI_ERROR_NOATTRIBUTE &&
977  SPI_result != SPI_ERROR_NOOUTFUNC &&
978  tmp != NULL &&
979  strlen(tmp) &&
981  ) {
982  POSTGIS_RT_DEBUGF(4, "Value for column %d is %s", i, tmp);
983 
984  len = strlen(tmp) + 1;
985  srs = SPI_palloc(sizeof(char) * len);
986  if (NULL == srs) {
987  pfree(tmp);
988  if (SPI_tuptable) SPI_freetuptable(tuptable);
989  SPI_finish();
990  elog(ERROR, "rtpg_getSR: Could not allocate memory for spatial reference text\n");
991  return NULL;
992  }
993  strncpy(srs, tmp, len);
994  pfree(tmp);
995 
996  break;
997  }
998 
999  if (tmp != NULL)
1000  pfree(tmp);
1001  continue;
1002  }
1003 
1004  if (SPI_tuptable) SPI_freetuptable(tuptable);
1005  SPI_finish();
1006 
1007  /* unable to get SR info */
1008  if (srs == NULL) {
1009  if (SPI_tuptable) SPI_freetuptable(tuptable);
1010  SPI_finish();
1011  elog(ERROR, "rtpg_getSR: Could not find a viable spatial reference for SRID (%d)", srid);
1012  return NULL;
1013  }
1014 
1015  return srs;
1016 }
int rt_util_gdal_supported_sr(const char *srs)
Definition: rt_api.c:354
#define POSTGIS_RT_DEBUGF(level, msg,...)
Definition: rt_pg.h:62
#define MAX_INT_CHARLEN
Definition: rt_pg.c:65
#define TRUE
Definition: dbfopen.c:170

Here is the call graph for this function:

Here is the caller graph for this function: