PostGIS  2.5.1dev-r@@SVN_REVISION@@

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 2276 of file raster2pgsql.c.

References _, array_range(), buffer(), raster_loader_config::constraints, raster_loader_config::copy_statements, CSEQUAL, raster_loader_config::endian, raster_loader_config::file_column, raster_loader_config::file_column_name, flush_stringbuffer(), raster_loader_config::hasnodata, raster_loader_config::idx, raster_loader_config::idx_tablespace, init_config(), init_stringbuffer(), raster_loader_config::maintenance, raster_loader_config::max_extent, MAXNAMELEN, MAXOVFACTOR, MINOVFACTOR, raster_loader_config::nband, raster_loader_config::nband_count, raster_loader_config::nodataval, raster_loader_config::opt, raster_loader_config::out_srid, raster_loader_config::outdb, raster_loader_config::overview, raster_loader_config::overview_count, raster_loader_config::overview_table, raster_loader_config::pad_tile, process_rasters(), raster_loader_config::quoteident, raster_loader_config::raster_column, raster_loader_config::regular_blocking, raster_loader_config::rt_file, raster_loader_config::rt_file_count, raster_loader_config::rt_filename, rt_init_allocators(), rt_raster_gdal_drivers(), rtalloc(), rtdealloc(), rtdealloc_config(), rtdealloc_stringbuffer(), rterror(), rtrealloc(), rtwarn(), raster_loader_config::schema, raster_loader_config::skip_nodataval_check, raster_loader_config::srid, SRID_UNKNOWN, strsplit(), strtolower(), raster_loader_config::table, raster_loader_config::tablespace, raster_loader_config::tile_size, raster_loader_config::transaction, trim(), usage(), and raster_loader_config::version.

2276  {
2277  RTLOADERCFG *config = NULL;
2278  STRINGBUFFER *buffer = NULL;
2279  uint32_t i = 0;
2280  uint32_t j = 0;
2281  char **elements = NULL;
2282  uint32_t n = 0;
2283  GDALDriverH drv = NULL;
2284  char *tmp = NULL;
2285  int argit = 0;
2286 
2288 
2289 #ifdef USE_NLS
2290  setlocale (LC_ALL, "");
2291  bindtextdomain (PACKAGE, LOCALEDIR);
2292  textdomain (PACKAGE);
2293 #endif
2294 
2295  /* no args, show usage */
2296  if (argc == 1) {
2297  usage();
2298  exit(0);
2299  }
2300 
2301  /* initialize config */
2302  config = rtalloc(sizeof(RTLOADERCFG));
2303  if (config == NULL) {
2304  rterror(_("Could not allocate memory for loader configuration"));
2305  exit(1);
2306  }
2307  init_config(config);
2308 
2309  /****************************************************************************
2310  * parse arguments
2311  ****************************************************************************/
2312 
2313  for (argit = 1; argit < argc; argit++) {
2314  char *optarg, *ptr;
2315  /* srid */
2316 
2317  if (CSEQUAL(argv[argit], "-s") && argit < argc - 1) {
2318  optarg = argv[++argit];
2319  ptr = strchr(optarg, ':');
2320  if (ptr) {
2321  *ptr++ = '\0';
2322  sscanf(optarg, "%d", &config->srid);
2323  sscanf(ptr, "%d", &config->out_srid);
2324  } else {
2325  config->srid = atoi(optarg);
2326  }
2327  }
2328  /* band index */
2329  else if (CSEQUAL(argv[argit], "-b") && argit < argc - 1) {
2330  elements = strsplit(argv[++argit], ",", &n);
2331  if (n < 1) {
2332  rterror(_("Could not process -b"));
2333  rtdealloc_config(config);
2334  exit(1);
2335  }
2336 
2337  config->nband_count = 0;
2338  for (j = 0; j < n; j++) {
2339  char *t = trim(elements[j]);
2340  char **minmax = NULL;
2341  int *range = NULL;
2342  uint32_t p = 0;
2343  uint32_t l = 0;
2344  int m = 0;
2345  uint32_t o = 0;
2346 
2347  /* is t a range? */
2348  minmax = strsplit(t, "-", &o);
2349  if (o == 2) {
2350  if (!array_range(atoi(minmax[0]), atoi(minmax[1]), 1, &range, &p)) {
2351  rterror(_("Could not allocate memory for storing band indices"));
2352  for (l = 0; l < o; l++)
2353  rtdealloc(minmax[l]);
2354  rtdealloc(minmax);
2355  for (j = 0; j < n; j++)
2356  rtdealloc(elements[j]);
2357  rtdealloc(elements);
2358  rtdealloc(t);
2359  rtdealloc_config(config);
2360  exit(1);
2361  }
2362  }
2363  else {
2364  p = 1;
2365  range = rtalloc(sizeof(int));
2366  if (range == NULL) {
2367  rterror(_("Could not allocate memory for storing band indices"));
2368  for (l = 0; l < o; l++)
2369  rtdealloc(minmax[l]);
2370  rtdealloc(minmax);
2371  for (j = 0; j < n; j++)
2372  rtdealloc(elements[j]);
2373  rtdealloc(elements);
2374  rtdealloc(t);
2375  rtdealloc_config(config);
2376  exit(1);
2377  }
2378  *range = atoi(t);
2379  }
2380 
2381  m = config->nband_count;
2382  config->nband_count += p;
2383  config->nband = rtrealloc(config->nband, sizeof(int) * config->nband_count);
2384  if (config->nband == NULL) {
2385  rterror(_("Could not allocate memory for storing band indices"));
2386  rtdealloc(range);
2387  for (l = 0; l < o; l++)
2388  rtdealloc(minmax[l]);
2389  rtdealloc(minmax);
2390  for (j = 0; j < n; j++)
2391  rtdealloc(elements[j]);
2392  rtdealloc(elements);
2393  rtdealloc(t);
2394  rtdealloc_config(config);
2395  exit(1);
2396  }
2397 
2398  for (l = 0; l < p; l++, m++)
2399  config->nband[m] = range[l];
2400 
2401  rtdealloc(range);
2402 
2403  for (l = 0; l < o; l++)
2404  rtdealloc(minmax[l]);
2405  rtdealloc(minmax);
2406 
2407  rtdealloc(t);
2408  rtdealloc(elements[j]);
2409  }
2410  rtdealloc(elements);
2411  elements = NULL;
2412  n = 0;
2413 
2414  for (j = 0; j < config->nband_count; j++) {
2415  if (config->nband[j] < 1) {
2416  rterror(_("Band index %d must be greater than 0"), config->nband[j]);
2417  rtdealloc_config(config);
2418  exit(1);
2419  }
2420  }
2421  }
2422  /* tile size */
2423  else if (CSEQUAL(argv[argit], "-t") && argit < argc - 1) {
2424  if (CSEQUAL(argv[++argit], "auto")) {
2425  config->tile_size[0] = -1;
2426  config->tile_size[1] = -1;
2427  }
2428  else {
2429  elements = strsplit(argv[argit], "x", &n);
2430  if (n != 2) {
2431  rterror(_("Could not process -t"));
2432  rtdealloc_config(config);
2433  exit(1);
2434  }
2435 
2436  for (j = 0; j < n; j++) {
2437  char *t = trim(elements[j]);
2438  config->tile_size[j] = atoi(t);
2439  rtdealloc(t);
2440  rtdealloc(elements[j]);
2441  }
2442  rtdealloc(elements);
2443  elements = NULL;
2444  n = 0;
2445 
2446  for (j = 0; j < 2; j++) {
2447  if (config->tile_size[j] < 1) {
2448  rterror(_("Tile size must be greater than 0x0"));
2449  rtdealloc_config(config);
2450  exit(1);
2451  }
2452  }
2453  }
2454  }
2455  /* pad tiles */
2456  else if (CSEQUAL(argv[argit], "-P")) {
2457  config->pad_tile = 1;
2458  }
2459  /* out-of-db raster */
2460  else if (CSEQUAL(argv[argit], "-R")) {
2461  config->outdb = 1;
2462  }
2463  /* drop table and recreate */
2464  else if (CSEQUAL(argv[argit], "-d")) {
2465  config->opt = 'd';
2466  }
2467  /* append to table */
2468  else if (CSEQUAL(argv[argit], "-a")) {
2469  config->opt = 'a';
2470  }
2471  /* create new table */
2472  else if (CSEQUAL(argv[argit], "-c")) {
2473  config->opt = 'c';
2474  }
2475  /* prepare only */
2476  else if (CSEQUAL(argv[argit], "-p")) {
2477  config->opt = 'p';
2478  }
2479  /* raster column name */
2480  else if (CSEQUAL(argv[argit], "-f") && argit < argc - 1) {
2481  const size_t len = (strlen(argv[++argit]) + 1);
2482  config->raster_column = rtalloc(sizeof(char) * len);
2483  if (config->raster_column == NULL) {
2484  rterror(_("Could not allocate memory for storing raster column name"));
2485  rtdealloc_config(config);
2486  exit(1);
2487  }
2488  strncpy(config->raster_column, argv[argit], len);
2489  }
2490  /* filename column */
2491  else if (CSEQUAL(argv[argit], "-F")) {
2492  config->file_column = 1;
2493  }
2494  /* filename column name */
2495  else if (CSEQUAL(argv[argit], "-n") && argit < argc - 1) {
2496  const size_t len = (strlen(argv[++argit]) + 1);
2497  config->file_column_name = rtalloc(sizeof(char) * len);
2498  if (config->file_column_name == NULL) {
2499  rterror(_("Could not allocate memory for storing filename column name"));
2500  rtdealloc_config(config);
2501  exit(1);
2502  }
2503  strncpy(config->file_column_name, argv[argit], len);
2504  config->file_column = 1;
2505  }
2506  /* overview factors */
2507  else if (CSEQUAL(argv[argit], "-l") && argit < argc - 1) {
2508  elements = strsplit(argv[++argit], ",", &n);
2509  if (n < 1) {
2510  rterror(_("Could not process -l"));
2511  rtdealloc_config(config);
2512  exit(1);
2513  }
2514 
2515  config->overview_count = n;
2516  config->overview = rtalloc(sizeof(int) * n);
2517  if (config->overview == NULL) {
2518  rterror(_("Could not allocate memory for storing overview factors"));
2519  rtdealloc_config(config);
2520  exit(1);
2521  }
2522  for (j = 0; j < n; j++) {
2523  char *t = trim(elements[j]);
2524  config->overview[j] = atoi(t);
2525  rtdealloc(t);
2526  rtdealloc(elements[j]);
2527  }
2528  rtdealloc(elements);
2529  elements = NULL;
2530  n = 0;
2531 
2532  for (j = 0; j < (uint32_t)config->overview_count; j++) {
2533  if (config->overview[j] < MINOVFACTOR || config->overview[j] > MAXOVFACTOR) {
2534  rterror(_("Overview factor %d is not between %d and %d"), config->overview[j], MINOVFACTOR, MAXOVFACTOR);
2535  rtdealloc_config(config);
2536  exit(1);
2537  }
2538  }
2539  }
2540  /* quote identifiers */
2541  else if (CSEQUAL(argv[argit], "-q")) {
2542  config->quoteident = 1;
2543  }
2544  /* create index */
2545  else if (CSEQUAL(argv[argit], "-I")) {
2546  config->idx = 1;
2547  }
2548  /* maintenance */
2549  else if (CSEQUAL(argv[argit], "-M")) {
2550  config->maintenance = 1;
2551  }
2552  /* set constraints */
2553  else if (CSEQUAL(argv[argit], "-C")) {
2554  config->constraints = 1;
2555  }
2556  /* disable extent constraint */
2557  else if (CSEQUAL(argv[argit], "-x")) {
2558  config->max_extent = 0;
2559  }
2560  /* enable regular_blocking */
2561  else if (CSEQUAL(argv[argit], "-r")) {
2562  config->regular_blocking = 1;
2563  }
2564  /* tablespace of new table */
2565  else if (CSEQUAL(argv[argit], "-T") && argit < argc - 1) {
2566  const size_t len = (strlen(argv[++argit]) + 1);
2567  config->tablespace = rtalloc(len);
2568  if (config->tablespace == NULL) {
2569  rterror(_("Could not allocate memory for storing tablespace of new table"));
2570  rtdealloc_config(config);
2571  exit(1);
2572  }
2573  strncpy(config->tablespace, argv[argit], len);
2574  }
2575  /* tablespace of new index */
2576  else if (CSEQUAL(argv[argit], "-X") && argit < argc - 1) {
2577  const size_t len = (strlen(argv[++argit]) + 1);
2578  config->idx_tablespace = rtalloc(len);
2579  if (config->idx_tablespace == NULL) {
2580  rterror(_("Could not allocate memory for storing tablespace of new indices"));
2581  rtdealloc_config(config);
2582  exit(1);
2583  }
2584  strncpy(config->idx_tablespace, argv[argit], len);
2585  }
2586  /* nodata value */
2587  else if (CSEQUAL(argv[argit], "-N") && argit < argc - 1) {
2588  config->hasnodata = 1;
2589  config->nodataval = atof(argv[++argit]);
2590  }
2591  /* skip NODATA value check for bands */
2592  else if (CSEQUAL(argv[argit], "-k")) {
2593  config->skip_nodataval_check = 1;
2594  }
2595  /* endianness */
2596  else if (CSEQUAL(argv[argit], "-E") && argit < argc - 1) {
2597  config->endian = atoi(argv[++argit]);
2598  config->endian = 1;
2599  }
2600  /* version */
2601  else if (CSEQUAL(argv[argit], "-V") && argit < argc - 1) {
2602  config->version = atoi(argv[++argit]);
2603  config->version = 0;
2604  }
2605  /* transaction */
2606  else if (CSEQUAL(argv[argit], "-e")) {
2607  config->transaction = 0;
2608  }
2609  /* COPY statements */
2610  else if (CSEQUAL(argv[argit], "-Y")) {
2611  config->copy_statements = 1;
2612  }
2613  /* GDAL formats */
2614  else if (CSEQUAL(argv[argit], "-G")) {
2615  uint32_t drv_count = 0;
2616  rt_gdaldriver drv_set = rt_raster_gdal_drivers(&drv_count, 0);
2617  if (drv_set == NULL || !drv_count) {
2618  rterror(_("Could not get list of available GDAL raster formats"));
2619  }
2620  else {
2621  printf(_("Supported GDAL raster formats:\n"));
2622  for (j = 0; j < drv_count; j++) {
2623  printf(_(" %s\n"), drv_set[j].long_name);
2624 
2625  rtdealloc(drv_set[j].short_name);
2626  rtdealloc(drv_set[j].long_name);
2627  rtdealloc(drv_set[j].create_options);
2628  }
2629  rtdealloc(drv_set);
2630  }
2631 
2632  rtdealloc_config(config);
2633  exit(0);
2634  }
2635  /* help */
2636  else if (CSEQUAL(argv[argit], "-?")) {
2637  usage();
2638  rtdealloc_config(config);
2639  exit(0);
2640  }
2641  else {
2642  size_t len;
2643  config->rt_file_count++;
2644  config->rt_file = (char **) rtrealloc(config->rt_file, sizeof(char *) * config->rt_file_count);
2645  if (config->rt_file == NULL) {
2646  rterror(_("Could not allocate memory for storing raster files"));
2647  rtdealloc_config(config);
2648  exit(1);
2649  }
2650 
2651  len = strlen(argv[argit]) + 1;
2652  config->rt_file[config->rt_file_count - 1] = rtalloc(sizeof(char) * len);
2653  if (config->rt_file[config->rt_file_count - 1] == NULL) {
2654  rterror(_("Could not allocate memory for storing raster filename"));
2655  rtdealloc_config(config);
2656  exit(1);
2657  }
2658  strncpy(config->rt_file[config->rt_file_count - 1], argv[argit], len);
2659  }
2660  }
2661 
2662  if (config->srid != config->out_srid && config->out_srid != SRID_UNKNOWN) {
2663  if (config->copy_statements) {
2664  rterror(_("Invalid argument combination - cannot use -Y with -s FROM_SRID:TO_SRID"));
2665  exit(1);
2666  }
2667  }
2668 
2669  /* register GDAL drivers */
2670  GDALAllRegister();
2671 
2672  /* no files provided */
2673  if (!config->rt_file_count) {
2674  rterror(_("No raster provided"));
2675  rtdealloc_config(config);
2676  exit(1);
2677  }
2678  /*
2679  at least two files, see if last is table
2680  last isn't recognized by GDAL
2681  */
2682  else if (config->rt_file_count > 1) {
2683  drv = GDALIdentifyDriver(config->rt_file[config->rt_file_count - 1], NULL);
2684 
2685  if (drv == NULL) {
2686  char *ptr;
2687  ptr = strchr(config->rt_file[config->rt_file_count - 1], '.');
2688 
2689  /* schema.table */
2690  if (ptr) {
2691  config->schema = rtalloc(sizeof(char) * (ptr - config->rt_file[config->rt_file_count - 1] + 1));
2692  if (config->schema == NULL) {
2693  rterror(_("Could not allocate memory for storing schema name"));
2694  rtdealloc_config(config);
2695  exit(1);
2696  }
2697  snprintf(config->schema, ptr - config->rt_file[config->rt_file_count - 1] + 1, "%s", config->rt_file[config->rt_file_count - 1]);
2698  config->schema[ptr - config->rt_file[config->rt_file_count - 1]] = '\0';
2699 
2700  config->table = rtalloc(sizeof(char) * (strlen(config->rt_file[config->rt_file_count - 1]) - strlen(config->schema) + 1));
2701  if (config->table == NULL) {
2702  rterror(_("Could not allocate memory for storing table name"));
2703  rtdealloc_config(config);
2704  exit(1);
2705  }
2706  snprintf(config->table, strlen(config->rt_file[config->rt_file_count - 1]) - strlen(config->schema), "%s", ptr + 1);
2707  config->table[strlen(config->rt_file[config->rt_file_count - 1]) - strlen(config->schema)] = '\0';
2708  }
2709  /* table */
2710  else {
2711  const size_t len = strlen(config->rt_file[config->rt_file_count - 1]) + 1;
2712  config->table = rtalloc(sizeof(char) * len);
2713  if (config->table == NULL) {
2714  rterror(_("Could not allocate memory for storing table name"));
2715  rtdealloc_config(config);
2716  exit(1);
2717  }
2718  strncpy(config->table, config->rt_file[config->rt_file_count - 1], len);
2719  }
2720 
2721  rtdealloc(config->rt_file[--(config->rt_file_count)]);
2722  config->rt_file = (char **) rtrealloc(config->rt_file, sizeof(char *) * config->rt_file_count);
2723  if (config->rt_file == NULL) {
2724  rterror(_("Could not reallocate the memory holding raster names"));
2725  rtdealloc_config(config);
2726  exit(1);
2727  }
2728  }
2729  }
2730 
2731  /****************************************************************************
2732  * validate raster files
2733  ****************************************************************************/
2734 
2735  /* check that GDAL recognizes all files */
2736  for (i = 0; i < config->rt_file_count; i++) {
2737  drv = GDALIdentifyDriver(config->rt_file[i], NULL);
2738 
2739  if (drv == NULL) {
2740  rterror(_("Unable to read raster file: %s"), config->rt_file[i]);
2741  rtdealloc_config(config);
2742  exit(1);
2743  }
2744  }
2745 
2746  /* process each file for just the filename */
2747  config->rt_filename = (char **) rtalloc(sizeof(char *) * config->rt_file_count);
2748  if (config->rt_filename == NULL) {
2749  rterror(_("Could not allocate memory for cleaned raster filenames"));
2750  rtdealloc_config(config);
2751  exit(1);
2752  }
2753  for (i = 0; i < config->rt_file_count; i++) {
2754  char *file;
2755  char *ptr;
2756 
2757  file = rtalloc(sizeof(char) * (strlen(config->rt_file[i]) + 1));
2758  if (file == NULL) {
2759  rterror(_("Could not allocate memory for cleaned raster filename"));
2760  rtdealloc_config(config);
2761  exit(1);
2762  }
2763  strcpy(file, config->rt_file[i]);
2764 
2765  for (ptr = file + strlen(file); ptr > file; ptr--) {
2766  if (*ptr == '/' || *ptr == '\\') {
2767  ptr++;
2768  break;
2769  }
2770  }
2771 
2772  config->rt_filename[i] = rtalloc(sizeof(char) * (strlen(ptr) + 1));
2773  if (config->rt_filename[i] == NULL) {
2774  rterror(_("Could not allocate memory for cleaned raster filename"));
2775  rtdealloc_config(config);
2776  exit(1);
2777  }
2778  strcpy(config->rt_filename[i], ptr);
2779  rtdealloc(file);
2780  }
2781 
2782  /****************************************************************************
2783  * defaults for table and column names
2784  ****************************************************************************/
2785 
2786  /* first file as proxy table name */
2787  if (config->table == NULL) {
2788  char *file;
2789  char *ptr;
2790 
2791  file = rtalloc(sizeof(char) * (strlen(config->rt_filename[0]) + 1));
2792  if (file == NULL) {
2793  rterror(_("Could not allocate memory for proxy table name"));
2794  rtdealloc_config(config);
2795  exit(1);
2796  }
2797  strcpy(file, config->rt_filename[0]);
2798 
2799  for (ptr = file + strlen(file); ptr > file; ptr--) {
2800  if (*ptr == '.') {
2801  *ptr = '\0';
2802  break;
2803  }
2804  }
2805 
2806  config->table = rtalloc(sizeof(char) * (strlen(file) + 1));
2807  if (config->table == NULL) {
2808  rterror(_("Could not allocate memory for proxy table name"));
2809  rtdealloc_config(config);
2810  exit(1);
2811  }
2812  strcpy(config->table, file);
2813  rtdealloc(file);
2814  }
2815 
2816  /* raster_column not specified, default to "rast" */
2817  if (config->raster_column == NULL) {
2818  config->raster_column = rtalloc(sizeof(char) * (strlen("rast") + 1));
2819  if (config->raster_column == NULL) {
2820  rterror(_("Could not allocate memory for default raster column name"));
2821  rtdealloc_config(config);
2822  exit(1);
2823  }
2824  strcpy(config->raster_column, "rast");
2825  }
2826 
2827  /* file_column_name not specified, default to "filename" */
2828  if (config->file_column_name == NULL) {
2829  config->file_column_name = rtalloc(sizeof(char) * (strlen("filename") + 1));
2830  if (config->file_column_name == NULL) {
2831  rterror(_("Could not allocate memory for default filename column name"));
2832  rtdealloc_config(config);
2833  exit(1);
2834  }
2835  strcpy(config->file_column_name, "filename");
2836  }
2837 
2838  /****************************************************************************
2839  * literal PostgreSQL identifiers disabled
2840  ****************************************************************************/
2841 
2842  /* no quotes, lower case everything */
2843  if (!config->quoteident) {
2844  if (config->schema != NULL)
2845  config->schema = strtolower(config->schema);
2846  if (config->table != NULL)
2847  config->table = strtolower(config->table);
2848  if (config->raster_column != NULL)
2849  config->raster_column = strtolower(config->raster_column);
2850  if (config->file_column_name != NULL)
2851  config->file_column_name = strtolower(config->file_column_name);
2852  if (config->tablespace != NULL)
2853  config->tablespace = strtolower(config->tablespace);
2854  if (config->idx_tablespace != NULL)
2855  config->idx_tablespace = strtolower(config->idx_tablespace);
2856  }
2857 
2858  /****************************************************************************
2859  * overview table names
2860  ****************************************************************************/
2861 
2862  if (config->overview_count) {
2863  char factor[4];
2864  config->overview_table = rtalloc(sizeof(char *) * config->overview_count);
2865  if (config->overview_table == NULL) {
2866  rterror(_("Could not allocate memory for overview table names"));
2867  rtdealloc_config(config);
2868  exit(1);
2869  }
2870 
2871  for (i = 0; i < config->overview_count; i++) {
2872  sprintf(factor, "%d", config->overview[i]);
2873 
2874  config->overview_table[i] = rtalloc(sizeof(char) * (strlen("o__") + strlen(factor) + strlen(config->table) + 1));
2875  if (config->overview_table[i] == NULL) {
2876  rterror(_("Could not allocate memory for overview table name"));
2877  rtdealloc_config(config);
2878  exit(1);
2879  }
2880  sprintf(config->overview_table[i], "o_%d_%s", config->overview[i], config->table);
2881  }
2882  }
2883 
2884  /****************************************************************************
2885  * check that identifiers won't get truncated
2886  ****************************************************************************/
2887 
2888  if (config->schema != NULL && strlen(config->schema) > MAXNAMELEN) {
2889  rtwarn(_("The schema name \"%s\" may exceed the maximum string length permitted for PostgreSQL identifiers (%d)"),
2890  config->schema,
2891  MAXNAMELEN
2892  );
2893  }
2894  if (config->table != NULL && strlen(config->table) > MAXNAMELEN) {
2895  rtwarn(_("The table name \"%s\" may exceed the maximum string length permitted for PostgreSQL identifiers (%d)"),
2896  config->table,
2897  MAXNAMELEN
2898  );
2899  }
2900  if (config->raster_column != NULL && strlen(config->raster_column) > MAXNAMELEN) {
2901  rtwarn(_("The column name \"%s\" may exceed the maximum string length permitted for PostgreSQL identifiers (%d)"),
2902  config->raster_column,
2903  MAXNAMELEN
2904  );
2905  }
2906  if (config->file_column_name != NULL && strlen(config->file_column_name) > MAXNAMELEN) {
2907  rtwarn(_("The column name \"%s\" may exceed the maximum string length permitted for PostgreSQL identifiers (%d)"),
2908  config->file_column_name,
2909  MAXNAMELEN
2910  );
2911  }
2912  if (config->tablespace != NULL && strlen(config->tablespace) > MAXNAMELEN) {
2913  rtwarn(_("The tablespace name \"%s\" may exceed the maximum string length permitted for PostgreSQL identifiers (%d)"),
2914  config->tablespace,
2915  MAXNAMELEN
2916  );
2917  }
2918  if (config->idx_tablespace != NULL && strlen(config->idx_tablespace) > MAXNAMELEN) {
2919  rtwarn(_("The index tablespace name \"%s\" may exceed the maximum string length permitted for PostgreSQL identifiers (%d)"),
2920  config->idx_tablespace,
2921  MAXNAMELEN
2922  );
2923  }
2924  if (config->overview_count) {
2925  for (i = 0; i < config->overview_count; i++) {
2926  if (strlen(config->overview_table[i]) > MAXNAMELEN) {
2927  rtwarn(_("The overview table name \"%s\" may exceed the maximum string length permitted for PostgreSQL identifiers (%d)"),
2928  config->overview_table[i],
2929  MAXNAMELEN
2930  );
2931  }
2932  }
2933  }
2934 
2935  /****************************************************************************
2936  * double quote identifiers
2937  ****************************************************************************/
2938 
2939  if (config->schema != NULL) {
2940  tmp = rtalloc(sizeof(char) * (strlen(config->schema) + 4));
2941  if (tmp == NULL) {
2942  rterror(_("Could not allocate memory for quoting schema name"));
2943  rtdealloc_config(config);
2944  exit(1);
2945  }
2946 
2947  sprintf(tmp, "\"%s\".", config->schema);
2948  rtdealloc(config->schema);
2949  config->schema = tmp;
2950  }
2951  if (config->table != NULL) {
2952  tmp = rtalloc(sizeof(char) * (strlen(config->table) + 3));
2953  if (tmp == NULL) {
2954  rterror(_("Could not allocate memory for quoting table name"));
2955  rtdealloc_config(config);
2956  exit(1);
2957  }
2958 
2959  sprintf(tmp, "\"%s\"", config->table);
2960  rtdealloc(config->table);
2961  config->table = tmp;
2962  }
2963  if (config->raster_column != NULL) {
2964  tmp = rtalloc(sizeof(char) * (strlen(config->raster_column) + 3));
2965  if (tmp == NULL) {
2966  rterror(_("Could not allocate memory for quoting raster column name"));
2967  rtdealloc_config(config);
2968  exit(1);
2969  }
2970 
2971  sprintf(tmp, "\"%s\"", config->raster_column);
2972  rtdealloc(config->raster_column);
2973  config->raster_column = tmp;
2974  }
2975  if (config->file_column_name != NULL) {
2976  tmp = rtalloc(sizeof(char) * (strlen(config->file_column_name) + 3));
2977  if (tmp == NULL) {
2978  rterror(_("Could not allocate memory for quoting raster column name"));
2979  rtdealloc_config(config);
2980  exit(1);
2981  }
2982 
2983  sprintf(tmp, "\"%s\"", config->file_column_name);
2984  rtdealloc(config->file_column_name);
2985  config->file_column_name = tmp;
2986  }
2987  if (config->tablespace != NULL) {
2988  tmp = rtalloc(sizeof(char) * (strlen(config->tablespace) + 3));
2989  if (tmp == NULL) {
2990  rterror(_("Could not allocate memory for quoting tablespace name"));
2991  rtdealloc_config(config);
2992  exit(1);
2993  }
2994 
2995  sprintf(tmp, "\"%s\"", config->tablespace);
2996  rtdealloc(config->tablespace);
2997  config->tablespace = tmp;
2998  }
2999  if (config->idx_tablespace != NULL) {
3000  tmp = rtalloc(sizeof(char) * (strlen(config->idx_tablespace) + 3));
3001  if (tmp == NULL) {
3002  rterror(_("Could not allocate memory for quoting index tablespace name"));
3003  rtdealloc_config(config);
3004  exit(1);
3005  }
3006 
3007  sprintf(tmp, "\"%s\"", config->idx_tablespace);
3008  rtdealloc(config->idx_tablespace);
3009  config->idx_tablespace = tmp;
3010  }
3011  if (config->overview_count) {
3012  for (i = 0; i < config->overview_count; i++) {
3013  tmp = rtalloc(sizeof(char) * (strlen(config->overview_table[i]) + 3));
3014  if (tmp == NULL) {
3015  rterror(_("Could not allocate memory for quoting overview table name"));
3016  rtdealloc_config(config);
3017  exit(1);
3018  }
3019 
3020  sprintf(tmp, "\"%s\"", config->overview_table[i]);
3021  rtdealloc(config->overview_table[i]);
3022  config->overview_table[i] = tmp;
3023  }
3024  }
3025 
3026  /****************************************************************************
3027  * processing of rasters
3028  ****************************************************************************/
3029 
3030  /* initialize string buffer */
3031  buffer = rtalloc(sizeof(STRINGBUFFER));
3032  if (buffer == NULL) {
3033  rterror(_("Could not allocate memory for output string buffer"));
3034  rtdealloc_config(config);
3035  exit(1);
3036  }
3037  init_stringbuffer(buffer);
3038 
3039  /* pass off to processing function */
3040  if (!process_rasters(config, buffer)) {
3041  rterror(_("Unable to process rasters"));
3042  rtdealloc_stringbuffer(buffer, 1);
3043  rtdealloc_config(config);
3044  exit(1);
3045  }
3046 
3047  flush_stringbuffer(buffer);
3048 
3049  rtdealloc_stringbuffer(buffer, 1);
3050  rtdealloc_config(config);
3051 
3052  return 0;
3053 }
static void flush_stringbuffer(STRINGBUFFER *buffer)
Definition: raster2pgsql.c:803
static int array_range(int min, int max, int step, int **range, uint32_t *len)
Definition: raster2pgsql.c:93
#define CSEQUAL(a, b)
Definition: raster2pgsql.h:48
static void rtdealloc_stringbuffer(STRINGBUFFER *buffer, int freebuffer)
Definition: raster2pgsql.c:777
#define _(String)
Definition: shpcommon.h:24
void rterror(const char *fmt,...)
Wrappers used for reporting errors and info.
Definition: rt_context.c:199
void * rtalloc(size_t size)
Wrappers used for managing memory.
Definition: rt_context.c:171
void * rtrealloc(void *mem, size_t size)
Definition: rt_context.c:179
unsigned int uint32_t
Definition: uthash.h:78
Datum buffer(PG_FUNCTION_ARGS)
#define MINOVFACTOR
Definition: raster2pgsql.h:58
static char * strtolower(char *str)
Definition: raster2pgsql.c:186
static int process_rasters(RTLOADERCFG *config, STRINGBUFFER *buffer)
static void rt_init_allocators(void)
Definition: raster2pgsql.c:64
void rtwarn(const char *fmt,...)
Definition: rt_context.c:224
#define SRID_UNKNOWN
Unknown SRID value.
Definition: liblwgeom.h:187
#define MAXNAMELEN
Definition: raster2pgsql.h:55
static char ** strsplit(const char *str, const char *delimiter, uint32_t *n)
Definition: raster2pgsql.c:197
static void rtdealloc_config(RTLOADERCFG *config)
Definition: raster2pgsql.c:731
static void init_stringbuffer(STRINGBUFFER *buffer)
Definition: raster2pgsql.c:771
void rtdealloc(void *mem)
Definition: rt_context.c:186
rt_gdaldriver rt_raster_gdal_drivers(uint32_t *drv_count, uint8_t cancc)
Returns a set of available GDAL drivers.
Definition: rt_raster.c:1716
static char * trim(const char *input)
Definition: raster2pgsql.c:262
static void init_config(RTLOADERCFG *config)
Definition: raster2pgsql.c:694
#define MAXOVFACTOR
Definition: raster2pgsql.h:59
static void usage()
Definition: raster2pgsql.c:327
Here is the call graph for this function: