32 #include <executor/spi.h>
57 const char *oldstr,
const char *newstr,
60 const char *tmp =
str;
63 int64_t length, reslen;
64 size_t oldlen = strlen(oldstr);
65 size_t newlen = strlen(newstr);
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);
103 for (j = strlen(
str) - 1; j >= 0; 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 memcpy(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)
289 SPITupleTable *tuptable = NULL;
310 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);
311 sql = (
char *) palloc(len);
313 elog(ERROR,
"rtpg_getSR: Could not allocate memory for sql\n");
317 spi_result = SPI_connect();
318 if (spi_result != SPI_OK_CONNECT) {
320 elog(ERROR,
"rtpg_getSR: Could not connect to database using SPI\n");
325 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);
327 spi_result = SPI_execute(
sql,
TRUE, 0);
329 if (spi_result != SPI_OK_SELECT || SPI_tuptable == NULL || SPI_processed != 1) {
330 if (SPI_tuptable) SPI_freetuptable(tuptable);
332 elog(ERROR,
"rtpg_getSR: Cannot find SRID (%d) in spatial_ref_sys", srid);
336 tupdesc = SPI_tuptable->tupdesc;
337 tuptable = SPI_tuptable;
338 tuple = tuptable->vals[0];
341 for (i = 1; i < 4; i++) {
342 tmp = SPI_getvalue(tuple, tupdesc, i);
346 SPI_result != SPI_ERROR_NOATTRIBUTE &&
347 SPI_result != SPI_ERROR_NOOUTFUNC &&
354 len = strlen(tmp) + 1;
355 srs = SPI_palloc(
sizeof(
char) * len);
358 if (SPI_tuptable) SPI_freetuptable(tuptable);
360 elog(ERROR,
"rtpg_getSR: Could not allocate memory for spatial reference text\n");
363 strncpy(srs, tmp, len);
374 if (SPI_tuptable) SPI_freetuptable(tuptable);
379 if (SPI_tuptable) SPI_freetuptable(tuptable);
381 elog(ERROR,
"rtpg_getSR: Could not find a viable spatial reference for SRID (%d)", srid);
int rt_util_gdal_supported_sr(const char *srs)
char * rtpg_removespaces(char *str)
char * rtpg_getSR(int32_t srid)
char * rtpg_strtoupper(char *str)
char * rtpg_chartrim(const char *input, char *remove)
char * rtpg_strrstr(const char *s1, const char *s2)
char * rtpg_trim(const char *input)
char ** rtpg_strsplit(const char *str, const char *delimiter, uint32_t *n)
char * rtpg_strreplace(const char *str, const char *oldstr, const char *newstr, int *count)
#define POSTGIS_RT_DEBUGF(level, msg,...)