/* * * WKTRaster - Raster Types for PostGIS * * Copyright (C) 2011-2013 Regents of the University of California * * Copyright (C) 2010-2011 Jorge Arevalo * Copyright (C) 2010-2011 David Zwarg * Copyright (C) 2009-2011 Pierre Racine * Copyright (C) 2009-2011 Mateusz Loskot * Copyright (C) 2008-2009 Sandro Santilli * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * */ /*************************************************************** * Some rules for returning NOTICE or ERROR... * * Send an ERROR like: * * elog(ERROR, "RASTER_out: Could not deserialize raster"); * * only when: * * -something wrong happen with memory, * -a function got an invalid argument ('3BUI' as pixel type) so that no row can * be processed * * *** IMPORTANT: elog(ERROR, ...) does NOT return to calling function *** * * Send a NOTICE like: * * elog(NOTICE, "Invalid band index (must use 1-based). Returning NULL"); * * when arguments (e.g. x, y, band) are NULL or out of range so that some or * most rows can be processed anyway * * in this case, * for SET functions or function normally returning a modified raster, return * the original raster * for GET functions, return NULL * try to deduce a valid parameter value if it makes sense (e.g. out of range * index for addBand) * * Do not put the name of the faulty function for NOTICEs, only with ERRORs. * ****************************************************************/ /****************************************************************************** * Some notes on memory management... * * Every time a SQL function is called, PostgreSQL creates a new memory context. * So, all the memory allocated with palloc/repalloc in that context is * automatically free'd at the end of the function. If you want some data to * live between function calls, you have 2 options: * * - Use fcinfo->flinfo->fn_mcxt context to store the data (by pointing the * data you want to keep with fcinfo->flinfo->fn_extra) * - Use SRF funcapi, and storing the data at multi_call_memory_ctx (by pointing * the data you want to keep with funcctx->user_fctx. funcctx is created by * funcctx = SPI_FIRSTCALL_INIT()). Recommended way in functions returning rows, * like RASTER_dumpAsPolygons (see section 34.9.9 at * http://www.postgresql.org/docs/8.4/static/xfunc-c.html). * * But raster code follows the same philosophy than the rest of PostGIS: keep * memory as clean as possible. So, we free all allocated memory. * * TODO: In case of functions returning NULL, we should free the memory too. *****************************************************************************/ /****************************************************************************** * Notes for use of PG_DETOAST_DATUM(), PG_DETOAST_DATUM_SLICE() * and PG_DETOAST_DATUM_COPY() * * When ONLY getting raster (not band) metadata, use PG_DETOAST_DATUM_SLICE() * as it is generally quicker to get only the chunk of memory that contains * the raster metadata. * * Example: PG_DETOAST_DATUM_SLICE(PG_GETARG_DATUM(0), 0, * sizeof(struct rt_raster_serialized_t)) * * When ONLY setting raster or band(s) metadata OR reading band data, use * PG_DETOAST_DATUM() as rt_raster_deserialize() allocates local memory * for the raster and band(s) metadata. * * Example: PG_DETOAST_DATUM(PG_GETARG_DATUM(0)) * * When SETTING band pixel values, use PG_DETOAST_DATUM_COPY(). This is * because band data (not metadata) is just a pointer to the correct * memory location in the detoasted datum. What is returned from * PG_DETOAST_DATUM() may or may not be a copy of the input datum. * * From the comments in postgresql/src/include/fmgr.h... * * pg_detoast_datum() gives you either the input datum (if not toasted) * or a detoasted copy allocated with palloc(). * * From the mouth of Tom Lane... * http://archives.postgresql.org/pgsql-hackers/2002-01/msg01289.php * * PG_DETOAST_DATUM_COPY guarantees to give you a copy, even if the * original wasn't toasted. This allows you to scribble on the input, * in case that happens to be a useful way of forming your result. * Without a forced copy, a routine for a pass-by-ref datatype must * NEVER, EVER scribble on its input ... because very possibly it'd * be scribbling on a valid tuple in a disk buffer, or a valid entry * in the syscache. * * The key detail above is that the raster datatype is a varlena, a * passed by reference datatype. * * Example: PG_DETOAST_DATUM_COPY(PG_GETARG_DATUM(0)) * * If in doubt, use PG_DETOAST_DATUM_COPY() as that guarantees that the input * datum is copied for use. *****************************************************************************/ /* PostgreSQL */ #include "postgres.h" /* for palloc */ #include "fmgr.h" /* for PG_MODULE_MAGIC */ #include "libpq/pqsignal.h" #include "utils/guc.h" #include "utils/builtins.h" #include "utils/memutils.h" #include "utils/elog.h" #include "miscadmin.h" /* PostGIS */ #include "postgis_config.h" #include "liblwgeom.h" #include "lwgeom_pg.h" #include "stringlist.h" #include "optionlist.h" /* PostGIS Raster */ #include "rtpostgis.h" #include "rtpg_internal.h" static void rtpg_interrupt_liblwgeom_callback(void) { #ifdef WIN32 if (UNBLOCKED_SIGNAL_QUEUE()) pgwin32_dispatch_queued_signals(); #endif if (QueryCancelPending || ProcDiePending) lwgeom_request_interrupt(); else lwgeom_cancel_interrupt(); } static lwinterrupt_callback *prev_liblwgeom_interrupt_callback = NULL; #ifndef __GNUC__ # define __attribute__ (x) #endif /* * This is required for builds against pgsql */ #ifdef PG_MODULE_MAGIC_EXT PG_MODULE_MAGIC_EXT( .name = "postgis_raster", .version = POSTGIS_LIB_VERSION ); #else PG_MODULE_MAGIC; #endif /* Module load callback */ void _PG_init(void); /* Module unload callback */ void _PG_fini(void); #define RT_MSG_MAXLEN 256 /* ---------------------------------------------------------------- */ /* Memory allocation / error reporting hooks */ /* ---------------------------------------------------------------- */ static void * rt_pg_alloc(size_t size) { void * result; POSTGIS_RT_DEBUGF(5, "rt_pgalloc(%ld) called", (long int) size); result = palloc(size); return result; } static void * rt_pg_realloc(void *mem, size_t size) { void * result; POSTGIS_RT_DEBUGF(5, "rt_pg_realloc(%ld) called", (long int) size); if (mem) result = repalloc(mem, size); else result = palloc(size); return result; } static void rt_pg_free(void *ptr) { POSTGIS_RT_DEBUG(5, "rt_pfree called"); pfree(ptr); } static void rt_pg_error(const char *fmt, va_list ap) __attribute__(( format(printf,1,0) )); static void rt_pg_error(const char *fmt, va_list ap) { char errmsg[RT_MSG_MAXLEN+1]; vsnprintf (errmsg, RT_MSG_MAXLEN, fmt, ap); errmsg[RT_MSG_MAXLEN]='\0'; ereport(ERROR, (errmsg_internal("%s", errmsg))); } static void rt_pg_notice(const char *fmt, va_list ap) __attribute__(( format(printf,1,0) )); static void rt_pg_notice(const char *fmt, va_list ap) { char msg[RT_MSG_MAXLEN+1]; vsnprintf (msg, RT_MSG_MAXLEN, fmt, ap); msg[RT_MSG_MAXLEN]='\0'; ereport(NOTICE, (errmsg_internal("%s", msg))); } static void rt_pg_debug(const char *fmt, va_list ap) __attribute__(( format(printf,1,0) )); static void rt_pg_debug(const char *fmt, va_list ap) { char msg[RT_MSG_MAXLEN+1]; vsnprintf (msg, RT_MSG_MAXLEN, fmt, ap); msg[RT_MSG_MAXLEN]='\0'; ereport(DEBUG1, (errmsg_internal("%s", msg))); } static char * rt_pg_options(const char* varname) { char optname[128]; char *optvalue; snprintf(optname, 128, "postgis.%s", varname); /* GetConfigOptionByName(name, found_name, missing_ok) */ optvalue = GetConfigOptionByName(optname, NULL, true); if (optvalue && strlen(optvalue) == 0) return NULL; else return optvalue; } /* ---------------------------------------------------------------- */ /* GDAL allowed config options for VSI filesystems */ /* ---------------------------------------------------------------- */ static stringlist_t *vsi_option_stringlist = NULL; #if POSTGIS_GDAL_VERSION < 20300 /* * For older versions of GDAL we have extracted the list of options * that were available at the 2.2 release and use that * as our set of allowed VSI network file options. */ static void rt_pg_vsi_load_all_options(void) { const char * gdaloption; const char * const gdaloptions[] = { "aws_access_key_id", "aws_https", "aws_max_keys", "aws_s3_endpoint", "aws_region", "aws_request_payer", "aws_secret_access_key", "aws_session_token", "aws_timestamp", "aws_virtual_hosting", "cpl_gs_timestamp", "cpl_gs_endpoint", "gs_secret_access_key", "gs_access_key_id", "goa2_client_id", "goa2_client_secret", "cpl_curl_enable_vsimem", "cpl_curl_gzip", "cpl_curl_verbose", "gdal_http_auth", "gdal_http_connecttimeout", "gdal_http_cookie", "gdal_http_header_file", "gdal_http_low_speed_time", "gdal_http_low_speed_limit", "gdal_http_max_retry", "gdal_http_netrc", "gdal_http_proxy", "gdal_http_proxyuserpwd", "gdal_http_retry_delay", "gdal_http_userpwd", "gdal_http_timeout", "gdal_http_unsafessl", "gdal_http_useragent", "gdal_disable_readdir_on_open", "gdal_proxy_auth", "curl_ca_bundle", "ssl_cert_file", "vsi_cache_size", "cpl_vsil_curl_use_head", "cpl_vsil_curl_use_s3_redirect", "cpl_vsil_curl_max_ranges", "cpl_vsil_curl_use_cache", "cpl_vsil_curl_allowed_filename", "cpl_vsil_curl_allowed_extensions", "cpl_vsil_curl_slow_get_size", "vsi_cache", "vsis3_chunk_size", NULL }; const char * const * gdaloptionsptr = gdaloptions; vsi_option_stringlist = stringlist_create(); while((gdaloption = *gdaloptionsptr++)) { stringlist_add_string_nosort(vsi_option_stringlist, gdaloption); } stringlist_sort(vsi_option_stringlist); } #else /* POSTGIS_GDAL_VERSION < 20300 */ /* * For newer versions of GDAL the VSIGetFileSystemOptions() call returns * all the allowed options for each VSI network file type, and we just have * to keep the list of VSI types statically in rt_pg_vsi_load_all_options(). */ static void rt_pg_vsi_load_options(const char* vsiname, stringlist_t *s) { CPLXMLNode *root, *optNode; const char *xml = VSIGetFileSystemOptions(vsiname); if (!xml) return; root = CPLParseXMLString(xml); if (!root) { elog(ERROR, "%s: Unable to read options for VSI %s", __func__, vsiname); return; } optNode = CPLSearchXMLNode(root, "Option"); if (!optNode) { CPLDestroyXMLNode(root); elog(ERROR, "%s: Unable to find