PostGIS  3.2.2dev-r@@SVN_REVISION@@
rtpostgis.c
Go to the documentation of this file.
1 /*
2  *
3  * WKTRaster - Raster Types for PostGIS
4  * http://trac.osgeo.org/postgis/wiki/WKTRaster
5  *
6  * Copyright (C) 2011-2013 Regents of the University of California
7  * <bkpark@ucdavis.edu>
8  * Copyright (C) 2010-2011 Jorge Arevalo <jorge.arevalo@deimos-space.com>
9  * Copyright (C) 2010-2011 David Zwarg <dzwarg@azavea.com>
10  * Copyright (C) 2009-2011 Pierre Racine <pierre.racine@sbf.ulaval.ca>
11  * Copyright (C) 2009-2011 Mateusz Loskot <mateusz@loskot.net>
12  * Copyright (C) 2008-2009 Sandro Santilli <strk@kbt.io>
13  *
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License
16  * as published by the Free Software Foundation; either version 2
17  * of the License, or (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software Foundation,
26  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27  *
28  */
29 
30 /***************************************************************
31  * Some rules for returning NOTICE or ERROR...
32  *
33  * Send an ERROR like:
34  *
35  * elog(ERROR, "RASTER_out: Could not deserialize raster");
36  *
37  * only when:
38  *
39  * -something wrong happen with memory,
40  * -a function got an invalid argument ('3BUI' as pixel type) so that no row can
41  * be processed
42  *
43  * *** IMPORTANT: elog(ERROR, ...) does NOT return to calling function ***
44  *
45  * Send a NOTICE like:
46  *
47  * elog(NOTICE, "Invalid band index (must use 1-based). Returning NULL");
48  *
49  * when arguments (e.g. x, y, band) are NULL or out of range so that some or
50  * most rows can be processed anyway
51  *
52  * in this case,
53  * for SET functions or function normally returning a modified raster, return
54  * the original raster
55  * for GET functions, return NULL
56  * try to deduce a valid parameter value if it makes sence (e.g. out of range
57  * index for addBand)
58  *
59  * Do not put the name of the faulty function for NOTICEs, only with ERRORs.
60  *
61  ****************************************************************/
62 
63 /******************************************************************************
64  * Some notes on memory management...
65  *
66  * Every time a SQL function is called, PostgreSQL creates a new memory context.
67  * So, all the memory allocated with palloc/repalloc in that context is
68  * automatically free'd at the end of the function. If you want some data to
69  * live between function calls, you have 2 options:
70  *
71  * - Use fcinfo->flinfo->fn_mcxt contex to store the data (by pointing the
72  * data you want to keep with fcinfo->flinfo->fn_extra)
73  * - Use SRF funcapi, and storing the data at multi_call_memory_ctx (by pointing
74  * the data you want to keep with funcctx->user_fctx. funcctx is created by
75  * funcctx = SPI_FIRSTCALL_INIT()). Recommended way in functions returning rows,
76  * like RASTER_dumpAsPolygons (see section 34.9.9 at
77  * http://www.postgresql.org/docs/8.4/static/xfunc-c.html).
78  *
79  * But raster code follows the same philosophy than the rest of PostGIS: keep
80  * memory as clean as possible. So, we free all allocated memory.
81  *
82  * TODO: In case of functions returning NULL, we should free the memory too.
83  *****************************************************************************/
84 
85 /******************************************************************************
86  * Notes for use of PG_DETOAST_DATUM(), PG_DETOAST_DATUM_SLICE()
87  * and PG_DETOAST_DATUM_COPY()
88  *
89  * When ONLY getting raster (not band) metadata, use PG_DETOAST_DATUM_SLICE()
90  * as it is generally quicker to get only the chunk of memory that contains
91  * the raster metadata.
92  *
93  * Example: PG_DETOAST_DATUM_SLICE(PG_GETARG_DATUM(0), 0,
94  * sizeof(struct rt_raster_serialized_t))
95  *
96  * When ONLY setting raster or band(s) metadata OR reading band data, use
97  * PG_DETOAST_DATUM() as rt_raster_deserialize() allocates local memory
98  * for the raster and band(s) metadata.
99  *
100  * Example: PG_DETOAST_DATUM(PG_GETARG_DATUM(0))
101  *
102  * When SETTING band pixel values, use PG_DETOAST_DATUM_COPY(). This is
103  * because band data (not metadata) is just a pointer to the correct
104  * memory location in the detoasted datum. What is returned from
105  * PG_DETOAST_DATUM() may or may not be a copy of the input datum.
106  *
107  * From the comments in postgresql/src/include/fmgr.h...
108  *
109  * pg_detoast_datum() gives you either the input datum (if not toasted)
110  * or a detoasted copy allocated with palloc().
111  *
112  * From the mouth of Tom Lane...
113  * http://archives.postgresql.org/pgsql-hackers/2002-01/msg01289.php
114  *
115  * PG_DETOAST_DATUM_COPY guarantees to give you a copy, even if the
116  * original wasn't toasted. This allows you to scribble on the input,
117  * in case that happens to be a useful way of forming your result.
118  * Without a forced copy, a routine for a pass-by-ref datatype must
119  * NEVER, EVER scribble on its input ... because very possibly it'd
120  * be scribbling on a valid tuple in a disk buffer, or a valid entry
121  * in the syscache.
122  *
123  * The key detail above is that the raster datatype is a varlena, a
124  * passed by reference datatype.
125  *
126  * Example: PG_DETOAST_DATUM_COPY(PG_GETARG_DATUM(0))
127  *
128  * If in doubt, use PG_DETOAST_DATUM_COPY() as that guarantees that the input
129  * datum is copied for use.
130  *****************************************************************************/
131 
132 #include <postgres.h> /* for palloc */
133 #include <fmgr.h> /* for PG_MODULE_MAGIC */
134 #include "utils/guc.h"
135 #include "utils/memutils.h"
136 
137 #include "../../postgis_config.h"
138 #include "lwgeom_pg.h"
139 
140 #include "rtpostgis.h"
141 #include "rtpg_internal.h"
142 #include "stringlist.h"
143 #include "optionlist.h"
144 
145 #ifndef __GNUC__
146 # define __attribute__ (x)
147 #endif
148 
149 /*
150  * This is required for builds against pgsql
151  */
153 
154 /* Module load callback */
155 void _PG_init(void);
156 
157 /* Module unload callback */
158 void _PG_fini(void);
159 
160 #define RT_MSG_MAXLEN 256
161 
162 
163 /* ---------------------------------------------------------------- */
164 /* Memory allocation / error reporting hooks */
165 /* ---------------------------------------------------------------- */
166 
167 static void *
168 rt_pg_alloc(size_t size)
169 {
170  void * result;
171 
172  POSTGIS_RT_DEBUGF(5, "rt_pgalloc(%ld) called", (long int) size);
173 
174  result = palloc(size);
175 
176  return result;
177 }
178 
179 static void *
180 rt_pg_realloc(void *mem, size_t size)
181 {
182  void * result;
183 
184  POSTGIS_RT_DEBUGF(5, "rt_pg_realloc(%ld) called", (long int) size);
185 
186  if (mem)
187  result = repalloc(mem, size);
188 
189  else
190  result = palloc(size);
191 
192  return result;
193 }
194 
195 static void
196 rt_pg_free(void *ptr)
197 {
198  POSTGIS_RT_DEBUG(5, "rt_pfree called");
199  pfree(ptr);
200 }
201 
202 static void rt_pg_error(const char *fmt, va_list ap)
203  __attribute__(( format(printf,1,0) ));
204 
205 static void
206 rt_pg_error(const char *fmt, va_list ap)
207 {
208  char errmsg[RT_MSG_MAXLEN+1];
209 
210  vsnprintf (errmsg, RT_MSG_MAXLEN, fmt, ap);
211 
212  errmsg[RT_MSG_MAXLEN]='\0';
213  ereport(ERROR, (errmsg_internal("%s", errmsg)));
214 }
215 
216 static void rt_pg_notice(const char *fmt, va_list ap)
217  __attribute__(( format(printf,1,0) ));
218 
219 static void
220 rt_pg_notice(const char *fmt, va_list ap)
221 {
222  char msg[RT_MSG_MAXLEN+1];
223 
224  vsnprintf (msg, RT_MSG_MAXLEN, fmt, ap);
225 
226  msg[RT_MSG_MAXLEN]='\0';
227  ereport(NOTICE, (errmsg_internal("%s", msg)));
228 }
229 
230 static void rt_pg_debug(const char *fmt, va_list ap)
231  __attribute__(( format(printf,1,0) ));
232 
233 static void
234 rt_pg_debug(const char *fmt, va_list ap)
235 {
236  char msg[RT_MSG_MAXLEN+1];
237 
238  vsnprintf (msg, RT_MSG_MAXLEN, fmt, ap);
239 
240  msg[RT_MSG_MAXLEN]='\0';
241  ereport(DEBUG1, (errmsg_internal("%s", msg)));
242 }
243 
244 static char *
245 rt_pg_options(const char* varname)
246 {
247  char optname[128];
248  char *optvalue;
249  snprintf(optname, 128, "postgis.%s", varname);
250  /* GetConfigOptionByName(name, found_name, missing_ok) */
251  optvalue = GetConfigOptionByName(optname, NULL, true);
252  if (optvalue && strlen(optvalue) == 0)
253  return NULL;
254  else
255  return optvalue;
256 }
257 
258 /* ---------------------------------------------------------------- */
259 /* GDAL allowed config options for VSI filesystems */
260 /* ---------------------------------------------------------------- */
261 
263 
264 
265 #if POSTGIS_GDAL_VERSION < 23
266 
267 /*
268 * For older versions of GDAL we have extracted the list of options
269 * that were available at the 2.2 release and use that
270 * as our set of allowed VSI network file options.
271 */
272 static void
274 {
275  const char * gdaloption;
276  const char * const gdaloptions[] = {
277  "aws_access_key_id",
278  "aws_https",
279  "aws_max_keys",
280  "aws_s3_endpoint",
281  "aws_region",
282  "aws_request_payer",
283  "aws_secret_access_key",
284  "aws_session_token",
285  "aws_timestamp",
286  "aws_virtual_hosting",
287  "cpl_gs_timestamp",
288  "cpl_gs_endpoint",
289  "gs_secret_access_key",
290  "gs_access_key_id",
291  "goa2_client_id",
292  "goa2_client_secret",
293  "cpl_curl_enable_vsimem",
294  "cpl_curl_gzip",
295  "cpl_curl_verbose",
296  "gdal_http_auth",
297  "gdal_http_connecttimeout",
298  "gdal_http_cookie",
299  "gdal_http_header_file",
300  "gdal_http_low_speed_time",
301  "gdal_http_low_speed_limit",
302  "gdal_http_max_retry",
303  "gdal_http_netrc",
304  "gdal_http_proxy",
305  "gdal_http_proxyuserpwd",
306  "gdal_http_retry_delay",
307  "gdal_http_userpwd",
308  "gdal_http_timeout",
309  "gdal_http_unsafessl",
310  "gdal_http_useragent",
311  "gdal_disable_readdir_on_open",
312  "gdal_proxy_auth",
313  "curl_ca_bundle",
314  "ssl_cert_file",
315  "vsi_cache_size",
316  "cpl_vsil_curl_use_head",
317  "cpl_vsil_curl_use_s3_redirect",
318  "cpl_vsil_curl_max_ranges",
319  "cpl_vsil_curl_use_cache",
320  "cpl_vsil_curl_allowed_filename",
321  "cpl_vsil_curl_allowed_extensions",
322  "cpl_vsil_curl_slow_get_size",
323  "vsi_cache",
324  "vsis3_chunk_size",
325  NULL
326  };
327  const char * const * gdaloptionsptr = gdaloptions;
328 
330  while((gdaloption = *gdaloptionsptr++))
331  {
333  }
335 }
336 
337 #else /* POSTGIS_GDAL_VERSION < 23 */
338 
339 /*
340 * For newer versions of GDAL the VSIGetFileSystemOptions() call returns
341 * all the allowed options for each VSI network file type, and we just have
342 * to keep the list of VSI types statically in rt_pg_vsi_load_all_options().
343 */
344 static void
345 rt_pg_vsi_load_options(const char* vsiname, stringlist_t *s)
346 {
347  CPLXMLNode *root, *optNode;
348  const char *xml = VSIGetFileSystemOptions(vsiname);
349  if (!xml) return;
350 
351  root = CPLParseXMLString(xml);
352  if (!root) {
353  elog(ERROR, "%s: Unable to read options for VSI %s", __func__, vsiname);
354  return;
355  }
356  optNode = CPLSearchXMLNode(root, "Option");
357  if (!optNode) {
358  CPLDestroyXMLNode(root);
359  elog(ERROR, "%s: Unable to find <Option> in VSI XML %s", __func__, vsiname);
360  return;
361  }
362  while(optNode)
363  {
364  const char *option = CPLGetXMLValue(optNode, "name", NULL);
365  if (option) {
366  char *optionstr = pstrdup(option);
367  char *ptr = optionstr;
368  /* The options parser used in rt_util_gdal_open()
369  lowercases keys, so we'll lower case our list
370  of options before storing them in the stringlist. */
371  while (*ptr) {
372  *ptr = tolower(*ptr);
373  ptr++;
374  }
375  elog(DEBUG4, "GDAL %s option: %s", vsiname, optionstr);
376  stringlist_add_string_nosort(s, optionstr);
377  }
378  optNode = optNode->psNext;
379  }
380  CPLDestroyXMLNode(root);
381 }
382 
383 static void
385 {
386  const char * vsiname;
387  const char * const vsilist[] = {
388  "/vsicurl/",
389  "/vsis3/",
390  "/vsigs/",
391  "/vsiaz/",
392  "/vsioss/",
393  "/vsihdfs/",
394  "/vsiwebhdfs/",
395  "/vsiswift/",
396  "/vsiadls/",
397  NULL
398  };
399  const char * const * vsilistptr = vsilist;
400 
402  while((vsiname = *vsilistptr++))
403  {
405  }
407 }
408 
409 #endif /* POSTGIS_GDAL_VERSION < 23 */
410 
411 
412 static bool
413 rt_pg_vsi_check_options(char **newval, void **extra, GucSource source)
414 {
415  size_t olist_sz, i;
416  char *olist[OPTION_LIST_SIZE];
417  const char *found = NULL;
418  char *newoptions;
419 
420  memset(olist, 0, sizeof(olist));
421  if (!newval || !*newval)
422  return false;
423  newoptions = pstrdup(*newval);
424 
425  /* Cache the legal options if they aren't already loaded */
428 
429  elog(DEBUG5, "%s: processing VSI options: %s", __func__, newoptions);
430  option_list_parse(newoptions, olist);
431  olist_sz = option_list_length(olist);
432  if (olist_sz % 2 != 0)
433  return false;
434 
435  for (i = 0; i < olist_sz; i += 2)
436  {
437  found = stringlist_find(vsi_option_stringlist, olist[i]);
438  if (!found)
439  {
440  elog(WARNING, "'%s' is not a legal VSI network file option", olist[i]);
441  pfree(newoptions);
442  return false;
443  }
444  }
445  return true;
446 }
447 
448 
449 /* ---------------------------------------------------------------- */
450 /* PostGIS raster GUCs */
451 /* ---------------------------------------------------------------- */
452 
453 static char *gdal_datapath = NULL;
454 static char *gdal_vsi_options = NULL;
455 extern char *gdal_enabled_drivers;
456 extern bool enable_outdb_rasters;
457 
458 /* ---------------------------------------------------------------- */
459 /* Useful variables */
460 /* ---------------------------------------------------------------- */
461 
465 
466 /* postgis.gdal_datapath */
467 static void
468 rtpg_assignHookGDALDataPath(const char *newpath, void *extra) {
469  POSTGIS_RT_DEBUGF(4, "newpath = %s", newpath);
470  POSTGIS_RT_DEBUGF(4, "gdaldatapath = %s", gdal_datapath);
471 
472  /* clear finder cache */
473  CPLFinderClean();
474 
475  /* clear cached OSR */
476  OSRCleanup();
477 
478  /* set GDAL_DATA */
479  CPLSetConfigOption("GDAL_DATA", newpath);
480  POSTGIS_RT_DEBUGF(4, "GDAL_DATA = %s", CPLGetConfigOption("GDAL_DATA", ""));
481 }
482 
483 /* postgis.gdal_enabled_drivers */
484 static void
485 rtpg_assignHookGDALEnabledDrivers(const char *enabled_drivers, void *extra) {
486  int enable_all = 0;
487  int disable_all = 0;
488  int vsicurl = 0;
489 
490  char **enabled_drivers_array = NULL;
491  uint32_t enabled_drivers_count = 0;
492  bool *enabled_drivers_found = NULL;
493  char *gdal_skip = NULL;
494 
495  uint32_t i;
496  uint32_t j;
497 
498  POSTGIS_RT_DEBUGF(4, "GDAL_SKIP = %s", CPLGetConfigOption("GDAL_SKIP", ""));
499  POSTGIS_RT_DEBUGF(4, "enabled_drivers = %s", enabled_drivers);
500 
501  /* if NULL, nothing to do */
502  if (enabled_drivers == NULL)
503  return;
504 
505  elog(DEBUG4, "Enabling GDAL drivers: %s", enabled_drivers);
506 
507  /* destroy the driver manager */
508  /* this is the only way to ensure GDAL_SKIP is recognized */
509  GDALDestroyDriverManager();
510  CPLSetConfigOption("GDAL_SKIP", NULL);
511 
512  /* force wrapper function to call GDALAllRegister() */
514 
515  enabled_drivers_array = rtpg_strsplit(enabled_drivers, " ", &enabled_drivers_count);
516  enabled_drivers_found = palloc(sizeof(bool) * enabled_drivers_count);
517  memset(enabled_drivers_found, FALSE, sizeof(bool) * enabled_drivers_count);
518 
519  /* scan for keywords DISABLE_ALL and ENABLE_ALL */
520  disable_all = 0;
521  enable_all = 0;
522  if (strstr(enabled_drivers, GDAL_DISABLE_ALL) != NULL) {
523  for (i = 0; i < enabled_drivers_count; i++) {
524  if (strstr(enabled_drivers_array[i], GDAL_DISABLE_ALL) != NULL) {
525  enabled_drivers_found[i] = TRUE;
526  disable_all = 1;
527  }
528  }
529  }
530  else if (strstr(enabled_drivers, GDAL_ENABLE_ALL) != NULL) {
531  for (i = 0; i < enabled_drivers_count; i++) {
532  if (strstr(enabled_drivers_array[i], GDAL_ENABLE_ALL) != NULL) {
533  enabled_drivers_found[i] = TRUE;
534  enable_all = 1;
535  }
536  }
537  }
538  else if (strstr(enabled_drivers, GDAL_VSICURL) != NULL) {
539  for (i = 0; i < enabled_drivers_count; i++) {
540  if (strstr(enabled_drivers_array[i], GDAL_VSICURL) != NULL) {
541  enabled_drivers_found[i] = TRUE;
542  vsicurl = 1;
543  }
544  }
545  }
546 
547  if (!enable_all) {
548  int found = 0;
549  uint32_t drv_count = 0;
550  rt_gdaldriver drv_set = rt_raster_gdal_drivers(&drv_count, 0);
551 
552  POSTGIS_RT_DEBUGF(4, "driver count = %d", drv_count);
553 
554  /* all other drivers than those in new drivers are added to GDAL_SKIP */
555  for (i = 0; i < drv_count; i++) {
556  found = 0;
557 
558  if (!disable_all) {
559  /* gdal driver found in enabled_drivers, continue to thorough search */
560  if (strstr(enabled_drivers, drv_set[i].short_name) != NULL) {
561  /* thorough search of enabled_drivers */
562  for (j = 0; j < enabled_drivers_count; j++) {
563  /* driver found */
564  if (strcmp(enabled_drivers_array[j], drv_set[i].short_name) == 0) {
565  enabled_drivers_found[j] = TRUE;
566  found = 1;
567  }
568  }
569  }
570  }
571 
572  /* driver found, continue */
573  if (found)
574  continue;
575 
576  /* driver not found, add to gdal_skip */
577  if (gdal_skip == NULL) {
578  gdal_skip = palloc(sizeof(char) * (strlen(drv_set[i].short_name) + 1));
579  gdal_skip[0] = '\0';
580  }
581  else {
582  gdal_skip = repalloc(
583  gdal_skip,
584  sizeof(char) * (
585  strlen(gdal_skip) + 1 + strlen(drv_set[i].short_name) + 1
586  )
587  );
588  strcat(gdal_skip, " ");
589  }
590  strcat(gdal_skip, drv_set[i].short_name);
591  }
592 
593  for (i = 0; i < drv_count; i++) {
594  pfree(drv_set[i].short_name);
595  pfree(drv_set[i].long_name);
596  pfree(drv_set[i].create_options);
597  }
598  if (drv_count) pfree(drv_set);
599 
600  }
601 
602  for (i = 0; i < enabled_drivers_count; i++) {
603  if (enabled_drivers_found[i])
604  continue;
605 
606  if (disable_all)
607  elog(WARNING, "%s set. Ignoring GDAL driver: %s", GDAL_DISABLE_ALL, enabled_drivers_array[i]);
608  else if (enable_all)
609  elog(WARNING, "%s set. Ignoring GDAL driver: %s", GDAL_ENABLE_ALL, enabled_drivers_array[i]);
610  else
611  elog(WARNING, "Unknown GDAL driver: %s", enabled_drivers_array[i]);
612  }
613 
614  if (vsicurl)
615  elog(WARNING, "%s set.", GDAL_VSICURL);
616 
617  /* destroy the driver manager */
618  /* this is the only way to ensure GDAL_SKIP is recognized */
619  GDALDestroyDriverManager();
620 
621  /* set GDAL_SKIP */
622  POSTGIS_RT_DEBUGF(4, "gdal_skip = %s", gdal_skip);
623  CPLSetConfigOption("GDAL_SKIP", gdal_skip);
624  if (gdal_skip != NULL) pfree(gdal_skip);
625 
626  /* force wrapper function to call GDALAllRegister() */
628 
629  pfree(enabled_drivers_array);
630  pfree(enabled_drivers_found);
631  POSTGIS_RT_DEBUGF(4, "GDAL_SKIP = %s", CPLGetConfigOption("GDAL_SKIP", ""));
632 }
633 
634 /* postgis.enable_outdb_rasters */
635 static void
636 rtpg_assignHookEnableOutDBRasters(bool enable, void *extra) {
637  /* do nothing for now */
638 }
639 
640 
641 /* Module load callback */
642 void
643 _PG_init(void) {
644 
645  bool boot_postgis_enable_outdb_rasters = false;
646  MemoryContext old_context;
647 
648  /*
649  * Change to context for memory allocation calls like palloc() in the
650  * extension initialization routine
651  */
652  old_context = MemoryContextSwitchTo(TopMemoryContext);
653 
654  /*
655  use POSTGIS_GDAL_ENABLED_DRIVERS to set the bootValue
656  of GUC postgis.gdal_enabled_drivers
657  */
658  env_postgis_gdal_enabled_drivers = getenv("POSTGIS_GDAL_ENABLED_DRIVERS");
659  if (env_postgis_gdal_enabled_drivers == NULL) {
660  size_t sz = sizeof(char) * (strlen(GDAL_DISABLE_ALL) + 1);
663  }
664  else {
667  );
668  }
670  4,
671  "boot_postgis_gdal_enabled_drivers = %s",
673  );
674 
675  /*
676  use POSTGIS_ENABLE_OUTDB_RASTERS to set the bootValue
677  of GUC postgis.enable_outdb_rasters
678  */
679  env_postgis_enable_outdb_rasters = getenv("POSTGIS_ENABLE_OUTDB_RASTERS");
680  if (env_postgis_enable_outdb_rasters != NULL) {
682 
683  /* out of memory */
684  if (env == NULL) {
685  elog(ERROR, "_PG_init: Cannot process environmental variable: POSTGIS_ENABLE_OUTDB_RASTERS");
686  return;
687  }
688 
689  if (strcmp(env, "1") == 0)
690  boot_postgis_enable_outdb_rasters = true;
691 
693  pfree(env);
694  }
696  4,
697  "boot_postgis_enable_outdb_rasters = %s",
698  boot_postgis_enable_outdb_rasters ? "TRUE" : "FALSE"
699  );
700 
701  /* Install liblwgeom handlers */
702  pg_install_lwgeom_handlers();
703 
704  /* Install rtcore handlers */
707  rt_pg_options);
708 
709  /* Define custom GUC variables. */
710  if ( postgis_guc_find_option("postgis.gdal_datapath") )
711  {
712  /* In this narrow case the previously installed GUC is tied to the callback in */
713  /* the previously loaded library. Probably this is happening during an */
714  /* upgrade, so the old library is where the callback ties to. */
715  elog(WARNING, "'%s' is already set and cannot be changed until you reconnect", "postgis.gdal_datapath");
716  }
717  else
718  {
719  DefineCustomStringVariable(
720  "postgis.gdal_datapath", /* name */
721  "Path to GDAL data files.", /* short_desc */
722  "Physical path to directory containing GDAL data files (sets the GDAL_DATA config option).", /* long_desc */
723  &gdal_datapath, /* valueAddr */
724  NULL, /* bootValue */
725  PGC_SUSET, /* GucContext context */
726  0, /* int flags */
727  NULL, /* GucStringCheckHook check_hook */
728  rtpg_assignHookGDALDataPath, /* GucStringAssignHook assign_hook */
729  NULL /* GucShowHook show_hook */
730  );
731  }
732 
733  if ( postgis_guc_find_option("postgis.gdal_enabled_drivers") )
734  {
735  /* In this narrow case the previously installed GUC is tied to the callback in */
736  /* the previously loaded library. Probably this is happening during an */
737  /* upgrade, so the old library is where the callback ties to. */
738  elog(WARNING, "'%s' is already set and cannot be changed until you reconnect", "postgis.gdal_enabled_drivers");
739  }
740  else
741  {
742  DefineCustomStringVariable(
743  "postgis.gdal_enabled_drivers", /* name */
744  "Enabled GDAL drivers.", /* short_desc */
745  "List of enabled GDAL drivers by short name. To enable/disable all drivers, use 'ENABLE_ALL' or 'DISABLE_ALL' (sets the GDAL_SKIP config option).", /* long_desc */
746  &gdal_enabled_drivers, /* valueAddr */
747  boot_postgis_gdal_enabled_drivers, /* bootValue */
748  PGC_SUSET, /* GucContext context */
749  0, /* int flags */
750  NULL, /* GucStringCheckHook check_hook */
751  rtpg_assignHookGDALEnabledDrivers, /* GucStringAssignHook assign_hook */
752  NULL /* GucShowHook show_hook */
753  );
754  }
755 
756  if ( postgis_guc_find_option("postgis.enable_outdb_rasters") )
757  {
758  /* In this narrow case the previously installed GUC is tied to the callback in */
759  /* the previously loaded library. Probably this is happening during an */
760  /* upgrade, so the old library is where the callback ties to. */
761  elog(WARNING, "'%s' is already set and cannot be changed until you reconnect", "postgis.enable_outdb_rasters");
762  }
763  else
764  {
765  DefineCustomBoolVariable(
766  "postgis.enable_outdb_rasters", /* name */
767  "Enable Out-DB raster bands", /* short_desc */
768  "If true, rasters can access data located outside the database", /* long_desc */
769  &enable_outdb_rasters, /* valueAddr */
770  boot_postgis_enable_outdb_rasters, /* bootValue */
771  PGC_SUSET, /* GucContext context */
772  0, /* int flags */
773  NULL, /* GucBoolCheckHook check_hook */
774  rtpg_assignHookEnableOutDBRasters, /* GucBoolAssignHook assign_hook */
775  NULL /* GucShowHook show_hook */
776  );
777  }
778 
779  if ( postgis_guc_find_option("postgis.gdal_vsi_options") )
780  {
781  elog(WARNING, "'%s' is already set and cannot be changed until you reconnect", "postgis.gdal_vsi_options");
782  }
783  else
784  {
785  DefineCustomStringVariable(
786  "postgis.gdal_vsi_options", /* name */
787  "VSI config options", /* short_desc */
788  "Set the config options to be used when opening /vsi/ network files", /* long_desc */
789  &gdal_vsi_options, /* valueAddr */
790  "", /* bootValue */
791  PGC_USERSET, /* GucContext context */
792  0, /* int flags */
793  rt_pg_vsi_check_options, /* GucStringCheckHook check_hook */
794  NULL, /* GucStringAssignHook assign_hook */
795  NULL /* GucShowHook show_hook */
796  );
797  }
798 
799  /* Revert back to old context */
800  MemoryContextSwitchTo(old_context);
801 }
802 
803 /* Module unload callback */
804 void
805 _PG_fini(void) {
806 
807  MemoryContext old_context;
808 
809  old_context = MemoryContextSwitchTo(TopMemoryContext);
810 
811  /* Clean up */
815 
819 
820  /* Revert back to old context */
821  MemoryContextSwitchTo(old_context);
822 }
823 
824 
825 
char * s
Definition: cu_in_wkt.c:23
char result[OUT_DOUBLE_BUFFER_SIZE]
Definition: cu_print.c:267
#define TRUE
Definition: dbfopen.c:73
#define FALSE
Definition: dbfopen.c:72
int rt_util_gdal_register_all(int force_register_all)
Definition: rt_util.c:339
rt_gdaldriver rt_raster_gdal_drivers(uint32_t *drv_count, uint8_t cancc)
Returns a set of available GDAL drivers.
Definition: rt_raster.c:1838
#define GDAL_ENABLE_ALL
Definition: librtcore.h:2192
#define GDAL_DISABLE_ALL
Definition: librtcore.h:2193
#define GDAL_VSICURL
Definition: librtcore.h:2194
void rt_set_handlers_options(rt_allocator allocator, rt_reallocator reallocator, rt_deallocator deallocator, rt_message_handler error_handler, rt_message_handler info_handler, rt_message_handler warning_handler, rt_options options_handler)
Definition: rt_context.c:169
def fmt
Definition: pixval.py:93
size_t option_list_length(char **olist)
Returns the total number of keys and values in the list.
Definition: optionlist.c:73
void option_list_parse(char *input, char **olist)
option_list is a null-terminated list of strings, where every odd string is a key and every even stri...
Definition: optionlist.c:86
#define OPTION_LIST_SIZE
Definition: optionlist.h:31
char * rtpg_trim(const char *input)
char ** rtpg_strsplit(const char *str, const char *delimiter, uint32_t *n)
static char * rt_pg_options(const char *varname)
Definition: rtpostgis.c:245
static char * env_postgis_enable_outdb_rasters
Definition: rtpostgis.c:464
static void * rt_pg_alloc(size_t size)
Definition: rtpostgis.c:168
void _PG_init(void)
Definition: rtpostgis.c:643
static char * gdal_vsi_options
Definition: rtpostgis.c:454
PG_MODULE_MAGIC
Definition: rtpostgis.c:152
static void rt_pg_vsi_load_options(const char *vsiname, stringlist_t *s)
Definition: rtpostgis.c:345
static void rtpg_assignHookGDALDataPath(const char *newpath, void *extra)
Definition: rtpostgis.c:468
static void rt_pg_error(const char *fmt, va_list ap) __attribute__((format(printf
Definition: rtpostgis.c:206
void _PG_fini(void)
Definition: rtpostgis.c:805
bool enable_outdb_rasters
Definition: rt_band.c:417
static void rt_pg_notice(const char *fmt, va_list ap) __attribute__((format(printf
Definition: rtpostgis.c:220
static bool rt_pg_vsi_check_options(char **newval, void **extra, GucSource source)
Definition: rtpostgis.c:413
stringlist_t * vsi_option_stringlist
Definition: rtpostgis.c:262
static void rt_pg_vsi_load_all_options(void)
Definition: rtpostgis.c:384
static char * env_postgis_gdal_enabled_drivers
Definition: rtpostgis.c:462
static char * boot_postgis_gdal_enabled_drivers
Definition: rtpostgis.c:463
#define RT_MSG_MAXLEN
Definition: rtpostgis.c:160
static void rtpg_assignHookGDALEnabledDrivers(const char *enabled_drivers, void *extra)
Definition: rtpostgis.c:485
static void * rt_pg_realloc(void *mem, size_t size)
Definition: rtpostgis.c:180
static void rt_pg_free(void *ptr)
Definition: rtpostgis.c:196
char * gdal_enabled_drivers
Definition: rt_util.c:378
static void rtpg_assignHookEnableOutDBRasters(bool enable, void *extra)
Definition: rtpostgis.c:636
#define __attribute__
Definition: rtpostgis.c:146
static void rt_pg_debug(const char *fmt, va_list ap) __attribute__((format(printf
Definition: rtpostgis.c:234
static char * gdal_datapath
Definition: rtpostgis.c:453
#define POSTGIS_RT_DEBUG(level, msg)
Definition: rtpostgis.h:61
#define POSTGIS_RT_DEBUGF(level, msg,...)
Definition: rtpostgis.h:65
const char * stringlist_find(stringlist_t *s, const char *key)
Definition: stringlist.c:129
void stringlist_add_string_nosort(stringlist_t *s, const char *string)
Definition: stringlist.c:117
void stringlist_sort(stringlist_t *s)
Definition: stringlist.c:123
stringlist_t * stringlist_create(void)
Definition: stringlist.c:71