PostGIS  3.3.9dev-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 
456 static char *gdal_enabled_drivers = NULL;
457 static bool enable_outdb_rasters = false;
458 
459 /* ---------------------------------------------------------------- */
460 /* Useful variables */
461 /* ---------------------------------------------------------------- */
462 
466 
467 /* postgis.gdal_datapath */
468 static void
469 rtpg_assignHookGDALDataPath(const char *newpath, void *extra) {
470  POSTGIS_RT_DEBUGF(4, "newpath = %s", newpath);
471  POSTGIS_RT_DEBUGF(4, "gdaldatapath = %s", gdal_datapath);
472 
473  /* clear finder cache */
474  CPLFinderClean();
475 
476  /* clear cached OSR */
477  OSRCleanup();
478 
479  /* set GDAL_DATA */
480  CPLSetConfigOption("GDAL_DATA", newpath);
481  POSTGIS_RT_DEBUGF(4, "GDAL_DATA = %s", CPLGetConfigOption("GDAL_DATA", ""));
482 }
483 
484 /* postgis.gdal_enabled_drivers */
485 static void
486 rtpg_assignHookGDALEnabledDrivers(const char *enabled_drivers, void *extra) {
487  int enable_all = 0;
488  int disable_all = 0;
489  int vsicurl = 0;
490 
491  char **enabled_drivers_array = NULL;
492  uint32_t enabled_drivers_count = 0;
493  bool *enabled_drivers_found = NULL;
494  char *gdal_skip = NULL;
495 
496  uint32_t i;
497  uint32_t j;
498 
499  POSTGIS_RT_DEBUGF(4, "GDAL_SKIP = %s", CPLGetConfigOption("GDAL_SKIP", ""));
500  POSTGIS_RT_DEBUGF(4, "enabled_drivers = %s", enabled_drivers);
501 
502  /* if NULL, nothing to do */
503  if (enabled_drivers == NULL)
504  return;
505 
506  elog(DEBUG4, "Enabling GDAL drivers: %s", enabled_drivers);
507 
508  /* destroy the driver manager */
509  /* this is the only way to ensure GDAL_SKIP is recognized */
510  GDALDestroyDriverManager();
511  CPLSetConfigOption("GDAL_SKIP", NULL);
512 
513  /* force wrapper function to call GDALAllRegister() */
515 
516  enabled_drivers_array = rtpg_strsplit(enabled_drivers, " ", &enabled_drivers_count);
517  enabled_drivers_found = palloc(sizeof(bool) * enabled_drivers_count);
518  memset(enabled_drivers_found, FALSE, sizeof(bool) * enabled_drivers_count);
519 
520  /* scan for keywords DISABLE_ALL and ENABLE_ALL */
521  disable_all = 0;
522  enable_all = 0;
523  if (strstr(enabled_drivers, GDAL_DISABLE_ALL) != NULL) {
524  for (i = 0; i < enabled_drivers_count; i++) {
525  if (strstr(enabled_drivers_array[i], GDAL_DISABLE_ALL) != NULL) {
526  enabled_drivers_found[i] = TRUE;
527  disable_all = 1;
528  }
529  }
530  }
531  else if (strstr(enabled_drivers, GDAL_ENABLE_ALL) != NULL) {
532  for (i = 0; i < enabled_drivers_count; i++) {
533  if (strstr(enabled_drivers_array[i], GDAL_ENABLE_ALL) != NULL) {
534  enabled_drivers_found[i] = TRUE;
535  enable_all = 1;
536  }
537  }
538  }
539  else if (strstr(enabled_drivers, GDAL_VSICURL) != NULL) {
540  for (i = 0; i < enabled_drivers_count; i++) {
541  if (strstr(enabled_drivers_array[i], GDAL_VSICURL) != NULL) {
542  enabled_drivers_found[i] = TRUE;
543  vsicurl = 1;
544  }
545  }
546  }
547 
548  if (!enable_all) {
549  int found = 0;
550  uint32_t drv_count = 0;
551  rt_gdaldriver drv_set = rt_raster_gdal_drivers(&drv_count, 0);
552 
553  POSTGIS_RT_DEBUGF(4, "driver count = %d", drv_count);
554 
555  /* all other drivers than those in new drivers are added to GDAL_SKIP */
556  for (i = 0; i < drv_count; i++) {
557  found = 0;
558 
559  if (!disable_all) {
560  /* gdal driver found in enabled_drivers, continue to thorough search */
561  if (strstr(enabled_drivers, drv_set[i].short_name) != NULL) {
562  /* thorough search of enabled_drivers */
563  for (j = 0; j < enabled_drivers_count; j++) {
564  /* driver found */
565  if (strcmp(enabled_drivers_array[j], drv_set[i].short_name) == 0) {
566  enabled_drivers_found[j] = TRUE;
567  found = 1;
568  }
569  }
570  }
571  }
572 
573  /* driver found, continue */
574  if (found)
575  continue;
576 
577  /* driver not found, add to gdal_skip */
578  if (gdal_skip == NULL) {
579  gdal_skip = palloc(sizeof(char) * (strlen(drv_set[i].short_name) + 1));
580  gdal_skip[0] = '\0';
581  }
582  else {
583  gdal_skip = repalloc(
584  gdal_skip,
585  sizeof(char) * (
586  strlen(gdal_skip) + 1 + strlen(drv_set[i].short_name) + 1
587  )
588  );
589  strcat(gdal_skip, " ");
590  }
591  strcat(gdal_skip, drv_set[i].short_name);
592  }
593 
594  for (i = 0; i < drv_count; i++) {
595  pfree(drv_set[i].short_name);
596  pfree(drv_set[i].long_name);
597  pfree(drv_set[i].create_options);
598  }
599  if (drv_count) pfree(drv_set);
600 
601  }
602 
603  for (i = 0; i < enabled_drivers_count; i++) {
604  if (enabled_drivers_found[i])
605  continue;
606 
607  if (disable_all)
608  elog(WARNING, "%s set. Ignoring GDAL driver: %s", GDAL_DISABLE_ALL, enabled_drivers_array[i]);
609  else if (enable_all)
610  elog(WARNING, "%s set. Ignoring GDAL driver: %s", GDAL_ENABLE_ALL, enabled_drivers_array[i]);
611  else
612  elog(WARNING, "Unknown GDAL driver: %s", enabled_drivers_array[i]);
613  }
614 
615  if (vsicurl)
616  elog(WARNING, "%s set.", GDAL_VSICURL);
617 
618  /* destroy the driver manager */
619  /* this is the only way to ensure GDAL_SKIP is recognized */
620  GDALDestroyDriverManager();
621 
622  /* set GDAL_SKIP */
623  POSTGIS_RT_DEBUGF(4, "gdal_skip = %s", gdal_skip);
624  CPLSetConfigOption("GDAL_SKIP", gdal_skip);
625  if (gdal_skip != NULL) pfree(gdal_skip);
626 
627  /* force wrapper function to call GDALAllRegister() */
629 
630  pfree(enabled_drivers_array);
631  pfree(enabled_drivers_found);
632  POSTGIS_RT_DEBUGF(4, "GDAL_SKIP = %s", CPLGetConfigOption("GDAL_SKIP", ""));
633 }
634 
635 /* postgis.enable_outdb_rasters */
636 static void
637 rtpg_assignHookEnableOutDBRasters(bool enable, void *extra) {
638  /* do nothing for now */
639 }
640 
641 
642 /* Module load callback */
643 void
644 _PG_init(void) {
645 
646  bool boot_postgis_enable_outdb_rasters = false;
647  MemoryContext old_context;
648 
649  /*
650  * Change to context for memory allocation calls like palloc() in the
651  * extension initialization routine
652  */
653  old_context = MemoryContextSwitchTo(TopMemoryContext);
654 
655  /*
656  use POSTGIS_GDAL_ENABLED_DRIVERS to set the bootValue
657  of GUC postgis.gdal_enabled_drivers
658  */
659  env_postgis_gdal_enabled_drivers = getenv("POSTGIS_GDAL_ENABLED_DRIVERS");
660  if (env_postgis_gdal_enabled_drivers == NULL) {
661  size_t sz = sizeof(char) * (strlen(GDAL_DISABLE_ALL) + 1);
664  }
665  else {
668  );
669  }
671  4,
672  "boot_postgis_gdal_enabled_drivers = %s",
674  );
675 
676  /*
677  use POSTGIS_ENABLE_OUTDB_RASTERS to set the bootValue
678  of GUC postgis.enable_outdb_rasters
679  */
680  env_postgis_enable_outdb_rasters = getenv("POSTGIS_ENABLE_OUTDB_RASTERS");
681  if (env_postgis_enable_outdb_rasters != NULL) {
683 
684  /* out of memory */
685  if (env == NULL) {
686  elog(ERROR, "_PG_init: Cannot process environmental variable: POSTGIS_ENABLE_OUTDB_RASTERS");
687  return;
688  }
689 
690  if (strcmp(env, "1") == 0)
691  boot_postgis_enable_outdb_rasters = true;
692 
694  pfree(env);
695  }
697  4,
698  "boot_postgis_enable_outdb_rasters = %s",
699  boot_postgis_enable_outdb_rasters ? "TRUE" : "FALSE"
700  );
701 
702  /* Install liblwgeom handlers */
703  pg_install_lwgeom_handlers();
704 
705  /* Install rtcore handlers */
708  rt_pg_options);
709 
710  /* Define custom GUC variables. */
711  if ( postgis_guc_find_option("postgis.gdal_datapath") )
712  {
713  /* In this narrow case the previously installed GUC is tied to the callback in */
714  /* the previously loaded library. Probably this is happening during an */
715  /* upgrade, so the old library is where the callback ties to. */
716  elog(WARNING, "'%s' is already set and cannot be changed until you reconnect", "postgis.gdal_datapath");
717  }
718  else
719  {
720  DefineCustomStringVariable(
721  "postgis.gdal_datapath", /* name */
722  "Path to GDAL data files.", /* short_desc */
723  "Physical path to directory containing GDAL data files (sets the GDAL_DATA config option).", /* long_desc */
724  &gdal_datapath, /* valueAddr */
725  NULL, /* bootValue */
726  PGC_SUSET, /* GucContext context */
727  0, /* int flags */
728  NULL, /* GucStringCheckHook check_hook */
729  rtpg_assignHookGDALDataPath, /* GucStringAssignHook assign_hook */
730  NULL /* GucShowHook show_hook */
731  );
732  }
733 
734  if ( postgis_guc_find_option("postgis.gdal_enabled_drivers") )
735  {
736  /* In this narrow case the previously installed GUC is tied to the callback in */
737  /* the previously loaded library. Probably this is happening during an */
738  /* upgrade, so the old library is where the callback ties to. */
739  elog(WARNING, "'%s' is already set and cannot be changed until you reconnect", "postgis.gdal_enabled_drivers");
740  }
741  else
742  {
743  DefineCustomStringVariable(
744  "postgis.gdal_enabled_drivers", /* name */
745  "Enabled GDAL drivers.", /* short_desc */
746  "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 */
747  &gdal_enabled_drivers, /* valueAddr */
748  boot_postgis_gdal_enabled_drivers, /* bootValue */
749  PGC_SUSET, /* GucContext context */
750  0, /* int flags */
751  NULL, /* GucStringCheckHook check_hook */
752  rtpg_assignHookGDALEnabledDrivers, /* GucStringAssignHook assign_hook */
753  NULL /* GucShowHook show_hook */
754  );
755  }
756 
757  if ( postgis_guc_find_option("postgis.enable_outdb_rasters") )
758  {
759  /* In this narrow case the previously installed GUC is tied to the callback in */
760  /* the previously loaded library. Probably this is happening during an */
761  /* upgrade, so the old library is where the callback ties to. */
762  elog(WARNING, "'%s' is already set and cannot be changed until you reconnect", "postgis.enable_outdb_rasters");
763  }
764  else
765  {
766  DefineCustomBoolVariable(
767  "postgis.enable_outdb_rasters", /* name */
768  "Enable Out-DB raster bands", /* short_desc */
769  "If true, rasters can access data located outside the database", /* long_desc */
770  &enable_outdb_rasters, /* valueAddr */
771  boot_postgis_enable_outdb_rasters, /* bootValue */
772  PGC_SUSET, /* GucContext context */
773  0, /* int flags */
774  NULL, /* GucBoolCheckHook check_hook */
775  rtpg_assignHookEnableOutDBRasters, /* GucBoolAssignHook assign_hook */
776  NULL /* GucShowHook show_hook */
777  );
778  }
779 
780  if ( postgis_guc_find_option("postgis.gdal_vsi_options") )
781  {
782  elog(WARNING, "'%s' is already set and cannot be changed until you reconnect", "postgis.gdal_vsi_options");
783  }
784  else
785  {
786  DefineCustomStringVariable(
787  "postgis.gdal_vsi_options", /* name */
788  "VSI config options", /* short_desc */
789  "Set the config options to be used when opening /vsi/ network files", /* long_desc */
790  &gdal_vsi_options, /* valueAddr */
791  "", /* bootValue */
792  PGC_USERSET, /* GucContext context */
793  0, /* int flags */
794  rt_pg_vsi_check_options, /* GucStringCheckHook check_hook */
795  NULL, /* GucStringAssignHook assign_hook */
796  NULL /* GucShowHook show_hook */
797  );
798  }
799 
800  /* Revert back to old context */
801  MemoryContextSwitchTo(old_context);
802 }
803 
804 /* Module unload callback */
805 void
806 _PG_fini(void) {
807 
808  MemoryContext old_context;
809 
810  old_context = MemoryContextSwitchTo(TopMemoryContext);
811 
812  /* Clean up */
816 
820 
821  /* Revert back to old context */
822  MemoryContextSwitchTo(old_context);
823 }
824 
825 
826 
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:465
static void * rt_pg_alloc(size_t size)
Definition: rtpostgis.c:168
void _PG_init(void)
Definition: rtpostgis.c:644
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:469
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:806
static bool enable_outdb_rasters
Definition: rtpostgis.c:457
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:463
static char * boot_postgis_gdal_enabled_drivers
Definition: rtpostgis.c:464
#define RT_MSG_MAXLEN
Definition: rtpostgis.c:160
static void rtpg_assignHookGDALEnabledDrivers(const char *enabled_drivers, void *extra)
Definition: rtpostgis.c:486
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
static char * gdal_enabled_drivers
Definition: rtpostgis.c:456
static void rtpg_assignHookEnableOutDBRasters(bool enable, void *extra)
Definition: rtpostgis.c:637
#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:69
#define POSTGIS_RT_DEBUGF(level, msg,...)
Definition: rtpostgis.h:73
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