PostGIS  2.2.8dev-r@@SVN_REVISION@@

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 2273 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.

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