32 #include <executor/spi.h> 57 const char *oldstr,
const char *newstr,
60 const char *tmp = str;
64 int oldlen = strlen(oldstr);
65 int newlen = strlen(newstr);
66 int limit = (count != NULL && *count > 0) ? *count : -1;
69 while ((tmp = strstr(tmp, oldstr)) != NULL && found != limit)
70 found++, tmp += oldlen;
72 length = strlen(str) + found * (newlen - oldlen);
73 if ((result = (
char *) palloc(length + 1)) == NULL) {
74 fprintf(stderr,
"Not enough memory\n");
83 while ((limit-- > 0) && (tmp = strstr(tmp, oldstr)) != NULL) {
85 strncpy(result + reslen, str, length);
86 strcpy(result + (reslen += length), newstr);
92 strcpy(result + reslen, str);
95 if (count != NULL) *count = found;
103 for (j = strlen(str) - 1; j >= 0; j--)
104 str[j] = toupper(str[j]);
118 return (
char *) input;
121 while (strchr(
remove, *input) != NULL)
125 ptr = ((
char *) input) + strlen(input);
126 while (strchr(
remove, *--ptr) != NULL)
129 rtn = palloc(
sizeof(
char) * (strlen(input) - offset + 1));
131 fprintf(stderr,
"Not enough memory\n");
134 strncpy(rtn, input, strlen(input) - offset);
135 rtn[strlen(input) - offset] =
'\0';
152 tmp = palloc(
sizeof(
char) * (strlen(str) + 1));
154 fprintf(stderr,
"Not enough memory\n");
159 if (!strlen(tmp) || !delimiter || !strlen(delimiter)) {
161 rtn = (
char **) palloc(*n *
sizeof(
char *));
163 fprintf(stderr,
"Not enough memory\n");
166 rtn[0] = (
char *) palloc(
sizeof(
char) * (strlen(tmp) + 1));
167 if (NULL == rtn[0]) {
168 fprintf(stderr,
"Not enough memory\n");
176 token = strtok(tmp, delimiter);
177 while (token != NULL) {
179 rtn = (
char **) palloc(
sizeof(
char *));
182 rtn = (
char **) repalloc(rtn, (*n + 1) *
sizeof(
char *));
185 fprintf(stderr,
"Not enough memory\n");
190 rtn[*n] = (
char *) palloc(
sizeof(
char) * (strlen(token) + 1));
191 if (NULL == rtn[*n]) {
192 fprintf(stderr,
"Not enough memory\n");
196 strcpy(rtn[*n], token);
199 token = strtok(NULL, delimiter);
235 return (
char *) input;
238 while (isspace(*input) && *input !=
'\0')
242 inputlen = strlen(input);
244 ptr = ((
char *) input) + inputlen;
245 while (isspace(*--ptr))
249 rtn = palloc(
sizeof(
char) * (inputlen - offset + 1));
251 fprintf(stderr,
"Not enough memory\n");
254 strncpy(rtn, input, inputlen - offset);
255 rtn[inputlen - offset] =
'\0';
266 int s1len = strlen(s1);
267 int s2len = strlen(s2);
273 s = (
char *) (s1 + s1len - s2len);
275 if (strncmp(s, s2, s2len) == 0)
288 SPITupleTable *tuptable = NULL;
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);
312 elog(ERROR,
"rtpg_getSR: Could not allocate memory for sql\n");
316 spi_result = SPI_connect();
317 if (spi_result != SPI_OK_CONNECT) {
319 elog(ERROR,
"rtpg_getSR: Could not connect to database using SPI\n");
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);
326 spi_result = SPI_execute(sql,
TRUE, 0);
328 if (spi_result != SPI_OK_SELECT || SPI_tuptable == NULL || SPI_processed != 1) {
329 if (SPI_tuptable) SPI_freetuptable(tuptable);
331 elog(ERROR,
"rtpg_getSR: Cannot find SRID (%d) in spatial_ref_sys", srid);
335 tupdesc = SPI_tuptable->tupdesc;
336 tuptable = SPI_tuptable;
337 tuple = tuptable->vals[0];
340 for (i = 1; i < 4; i++) {
341 tmp = SPI_getvalue(tuple, tupdesc, i);
345 SPI_result != SPI_ERROR_NOATTRIBUTE &&
346 SPI_result != SPI_ERROR_NOOUTFUNC &&
353 len = strlen(tmp) + 1;
354 srs = SPI_palloc(
sizeof(
char) * len);
357 if (SPI_tuptable) SPI_freetuptable(tuptable);
359 elog(ERROR,
"rtpg_getSR: Could not allocate memory for spatial reference text\n");
362 strncpy(srs, tmp, len);
373 if (SPI_tuptable) SPI_freetuptable(tuptable);
378 if (SPI_tuptable) SPI_freetuptable(tuptable);
380 elog(ERROR,
"rtpg_getSR: Could not find a viable spatial reference for SRID (%d)", srid);
char ** rtpg_strsplit(const char *str, const char *delimiter, int *n)
char * rtpg_strreplace(const char *str, const char *oldstr, const char *newstr, int *count)
#define POSTGIS_RT_DEBUGF(level, msg,...)
int rt_util_gdal_supported_sr(const char *srs)
char * rtpg_trim(const char *input)
char * rtpg_chartrim(const char *input, char *remove)
char * rtpg_removespaces(char *str)
char * rtpg_getSR(int srid)
char * rtpg_strrstr(const char *s1, const char *s2)
char * rtpg_strtoupper(char *str)