2262 {
2265 uint32_t i = 0;
2266 uint32_t j = 0;
2267 char **elements = NULL;
2268 uint32_t n = 0;
2269 GDALDriverH drv = NULL;
2270 char *tmp = NULL;
2271 int argit = 0;
2272
2274
2275#ifdef USE_NLS
2276 setlocale (LC_ALL, "");
2277 bindtextdomain (PACKAGE, LOCALEDIR);
2278 textdomain (PACKAGE);
2279#endif
2280
2281
2282 if (argc == 1) {
2284 exit(0);
2285 }
2286
2287
2289 if (config == NULL) {
2290 rterror(
_(
"Could not allocate memory for loader configuration"));
2291 exit(1);
2292 }
2294
2295
2296
2297
2298
2299 for (argit = 1; argit < argc; argit++) {
2300 char *optarg, *ptr;
2301
2302
2303 if (
CSEQUAL(argv[argit],
"-s") && argit < argc - 1) {
2304 optarg = argv[++argit];
2305 ptr = strchr(optarg, ':');
2306 if (ptr) {
2307 *ptr++ = '\0';
2308 sscanf(optarg,
"%d", &config->
srid);
2309 sscanf(ptr,
"%d", &config->
out_srid);
2310 } else {
2311 config->
srid = atoi(optarg);
2312 }
2313 }
2314
2315 else if (
CSEQUAL(argv[argit],
"-b") && argit < argc - 1) {
2316 elements =
strsplit(argv[++argit],
",", &n);
2317 if (n < 1) {
2318 rterror(
_(
"Could not process -b"));
2320 exit(1);
2321 }
2322
2324 for (j = 0; j < n; j++) {
2325 char *t =
trim(elements[j]);
2326 char **minmax = NULL;
2327 int *range = NULL;
2328 uint32_t p = 0;
2329 uint32_t l = 0;
2330 int m = 0;
2331 uint32_t o = 0;
2332
2333
2335 if (o == 2) {
2336 if (!
array_range(atoi(minmax[0]), atoi(minmax[1]), 1, &range, &p)) {
2337 rterror(
_(
"Could not allocate memory for storing band indices"));
2338 for (l = 0; l < o; l++)
2341 for (j = 0; j < n; j++)
2346 exit(1);
2347 }
2348 }
2349 else {
2350 p = 1;
2352 if (range == NULL) {
2353 rterror(
_(
"Could not allocate memory for storing band indices"));
2354 for (l = 0; l < o; l++)
2357 for (j = 0; j < n; j++)
2362 exit(1);
2363 }
2364 *range = atoi(t);
2365 }
2366
2370 if (config->
nband == NULL) {
2371 rterror(
_(
"Could not allocate memory for storing band indices"));
2373 for (l = 0; l < o; l++)
2376 for (j = 0; j < n; j++)
2381 exit(1);
2382 }
2383
2384 for (l = 0; l < p; l++, m++)
2385 config->
nband[m] = range[l];
2386
2388
2389 for (l = 0; l < o; l++)
2392
2395 }
2397 elements = NULL;
2398 n = 0;
2399
2401 if (config->
nband[j] < 1) {
2402 rterror(
_(
"Band index %d must be greater than 0"), config->
nband[j]);
2404 exit(1);
2405 }
2406 }
2407 }
2408
2409 else if (
CSEQUAL(argv[argit],
"-t") && argit < argc - 1) {
2410 if (
CSEQUAL(argv[++argit],
"auto")) {
2413 }
2414 else {
2415 elements =
strsplit(argv[argit],
"x", &n);
2416 if (n != 2) {
2417 rterror(
_(
"Could not process -t"));
2419 exit(1);
2420 }
2421
2422 for (j = 0; j < n; j++) {
2423 char *t =
trim(elements[j]);
2427 }
2429 elements = NULL;
2430 n = 0;
2431
2432 for (j = 0; j < 2; j++) {
2434 rterror(
_(
"Tile size must be greater than 0x0"));
2436 exit(1);
2437 }
2438 }
2439 }
2440 }
2441
2442 else if (
CSEQUAL(argv[argit],
"-P")) {
2444 }
2445
2446 else if (
CSEQUAL(argv[argit],
"-R")) {
2448 }
2449
2450 else if (
CSEQUAL(argv[argit],
"-d")) {
2452 }
2453
2454 else if (
CSEQUAL(argv[argit],
"-a")) {
2456 }
2457
2458 else if (
CSEQUAL(argv[argit],
"-c")) {
2460 }
2461
2462 else if (
CSEQUAL(argv[argit],
"-p")) {
2464 }
2465
2466 else if (
CSEQUAL(argv[argit],
"-f") && argit < argc - 1) {
2467 const size_t len = (strlen(argv[++argit]) + 1);
2470 rterror(
_(
"Could not allocate memory for storing raster column name"));
2472 exit(1);
2473 }
2475 }
2476
2477 else if (
CSEQUAL(argv[argit],
"-F")) {
2479 }
2480
2481 else if (
CSEQUAL(argv[argit],
"-n") && argit < argc - 1) {
2482 const size_t len = (strlen(argv[++argit]) + 1);
2485 rterror(
_(
"Could not allocate memory for storing filename column name"));
2487 exit(1);
2488 }
2491 }
2492
2493 else if (
CSEQUAL(argv[argit],
"-l") && argit < argc - 1) {
2494 elements =
strsplit(argv[++argit],
",", &n);
2495 if (n < 1) {
2496 rterror(
_(
"Could not process -l"));
2498 exit(1);
2499 }
2500
2504 rterror(
_(
"Could not allocate memory for storing overview factors"));
2506 exit(1);
2507 }
2508 for (j = 0; j < n; j++) {
2509 char *t =
trim(elements[j]);
2513 }
2515 elements = NULL;
2516 n = 0;
2517
2522 exit(1);
2523 }
2524 }
2525 }
2526
2527 else if (
CSEQUAL(argv[argit],
"-q")) {
2529 }
2530
2531 else if (
CSEQUAL(argv[argit],
"-I")) {
2533 }
2534
2535 else if (
CSEQUAL(argv[argit],
"-M")) {
2537 }
2538
2539 else if (
CSEQUAL(argv[argit],
"-C")) {
2541 }
2542
2543 else if (
CSEQUAL(argv[argit],
"-x")) {
2545 }
2546
2547 else if (
CSEQUAL(argv[argit],
"-r")) {
2549 }
2550
2551 else if (
CSEQUAL(argv[argit],
"-T") && argit < argc - 1) {
2552 const size_t len = (strlen(argv[++argit]) + 1);
2555 rterror(
_(
"Could not allocate memory for storing tablespace of new table"));
2557 exit(1);
2558 }
2559 strncpy(config->
tablespace, argv[argit], len);
2560 }
2561
2562 else if (
CSEQUAL(argv[argit],
"-X") && argit < argc - 1) {
2563 const size_t len = (strlen(argv[++argit]) + 1);
2566 rterror(
_(
"Could not allocate memory for storing tablespace of new indices"));
2568 exit(1);
2569 }
2571 }
2572
2573 else if (
CSEQUAL(argv[argit],
"-N") && argit < argc - 1) {
2575 config->
nodataval = atof(argv[++argit]);
2576 }
2577
2578 else if (
CSEQUAL(argv[argit],
"-k")) {
2580 }
2581
2582 else if (
CSEQUAL(argv[argit],
"-E") && argit < argc - 1) {
2583 config->
endian = atoi(argv[++argit]);
2585 }
2586
2587 else if (
CSEQUAL(argv[argit],
"-V") && argit < argc - 1) {
2588 config->
version = atoi(argv[++argit]);
2590 }
2591
2592 else if (
CSEQUAL(argv[argit],
"-e")) {
2594 }
2595
2596 else if (
CSEQUAL(argv[argit],
"-Y")) {
2598 }
2599
2600 else if (
CSEQUAL(argv[argit],
"-G")) {
2601 uint32_t drv_count = 0;
2603 if (drv_set == NULL || !drv_count) {
2604 rterror(
_(
"Could not get list of available GDAL raster formats"));
2605 }
2606 else {
2607 printf(
_(
"Supported GDAL raster formats:\n"));
2608 for (j = 0; j < drv_count; j++) {
2609 printf(
_(
" %s\n"), drv_set[j].long_name);
2610
2614 }
2616 }
2617
2619 exit(0);
2620 }
2621
2622 else if (
CSEQUAL(argv[argit],
"-?")) {
2625 exit(0);
2626 }
2627 else {
2628 size_t len;
2631 if (config->
rt_file == NULL) {
2632 rterror(
_(
"Could not allocate memory for storing raster files"));
2634 exit(1);
2635 }
2636
2637 len = strlen(argv[argit]) + 1;
2640 rterror(
_(
"Could not allocate memory for storing raster filename"));
2642 exit(1);
2643 }
2645 }
2646 }
2647
2650 rterror(
_(
"Invalid argument combination - cannot use -Y with -s FROM_SRID:TO_SRID"));
2651 exit(1);
2652 }
2653 }
2654
2655
2656 GDALAllRegister();
2657
2658
2662 exit(1);
2663 }
2664
2665
2666
2667
2670
2671 if (drv == NULL) {
2672 char *ptr;
2674
2675
2676 if (ptr) {
2678 if (config->
schema == NULL) {
2679 rterror(
_(
"Could not allocate memory for storing schema name"));
2681 exit(1);
2682 }
2685
2687 if (config->
table == NULL) {
2688 rterror(
_(
"Could not allocate memory for storing table name"));
2690 exit(1);
2691 }
2694 }
2695
2696 else {
2699 if (config->
table == NULL) {
2700 rterror(
_(
"Could not allocate memory for storing table name"));
2702 exit(1);
2703 }
2705 }
2706
2709 if (config->
rt_file == NULL) {
2710 rterror(
_(
"Could not reallocate the memory holding raster names"));
2712 exit(1);
2713 }
2714 }
2715 }
2716
2717
2718
2719
2720
2721
2723 drv = GDALIdentifyDriver(config->
rt_file[i], NULL);
2724
2725 if (drv == NULL) {
2728 exit(1);
2729 }
2730 }
2731
2732
2735 rterror(
_(
"Could not allocate memory for cleaned raster filenames"));
2737 exit(1);
2738 }
2740 char *file;
2741 char *ptr;
2742
2744 if (file == NULL) {
2745 rterror(
_(
"Could not allocate memory for cleaned raster filename"));
2747 exit(1);
2748 }
2749 strcpy(file, config->
rt_file[i]);
2750
2751 for (ptr = file + strlen(file); ptr > file; ptr--) {
2752 if (*ptr == '/' || *ptr == '\\') {
2753 ptr++;
2754 break;
2755 }
2756 }
2757
2760 rterror(
_(
"Could not allocate memory for cleaned raster filename"));
2762 exit(1);
2763 }
2766 }
2767
2768
2769
2770
2771
2772
2773 if (config->
table == NULL) {
2774 char *file;
2775 char *ptr;
2776
2778 if (file == NULL) {
2779 rterror(
_(
"Could not allocate memory for proxy table name"));
2781 exit(1);
2782 }
2784
2785 for (ptr = file + strlen(file); ptr > file; ptr--) {
2786 if (*ptr == '.') {
2787 *ptr = '\0';
2788 break;
2789 }
2790 }
2791
2792 config->
table =
rtalloc(
sizeof(
char) * (strlen(file) + 1));
2793 if (config->
table == NULL) {
2794 rterror(
_(
"Could not allocate memory for proxy table name"));
2796 exit(1);
2797 }
2798 strcpy(config->
table, file);
2800 }
2801
2802
2806 rterror(
_(
"Could not allocate memory for default raster column name"));
2808 exit(1);
2809 }
2811 }
2812
2813
2817 rterror(
_(
"Could not allocate memory for default filename column name"));
2819 exit(1);
2820 }
2822 }
2823
2824
2825
2826
2827
2828
2830 if (config->
schema != NULL)
2832 if (config->
table != NULL)
2842 }
2843
2844
2845
2846
2847
2849 char factor[4];
2852 rterror(
_(
"Could not allocate memory for overview table names"));
2854 exit(1);
2855 }
2856
2858 sprintf(factor,
"%d", config->
overview[i]);
2859
2862 rterror(
_(
"Could not allocate memory for overview table name"));
2864 exit(1);
2865 }
2867 }
2868 }
2869
2870
2871
2872
2873
2875 rtwarn(
_(
"The schema name \"%s\" may exceed the maximum string length permitted for PostgreSQL identifiers (%d)"),
2878 );
2879 }
2881 rtwarn(
_(
"The table name \"%s\" may exceed the maximum string length permitted for PostgreSQL identifiers (%d)"),
2884 );
2885 }
2887 rtwarn(
_(
"The column name \"%s\" may exceed the maximum string length permitted for PostgreSQL identifiers (%d)"),
2890 );
2891 }
2893 rtwarn(
_(
"The column name \"%s\" may exceed the maximum string length permitted for PostgreSQL identifiers (%d)"),
2896 );
2897 }
2899 rtwarn(
_(
"The tablespace name \"%s\" may exceed the maximum string length permitted for PostgreSQL identifiers (%d)"),
2902 );
2903 }
2905 rtwarn(
_(
"The index tablespace name \"%s\" may exceed the maximum string length permitted for PostgreSQL identifiers (%d)"),
2908 );
2909 }
2913 rtwarn(
_(
"The overview table name \"%s\" may exceed the maximum string length permitted for PostgreSQL identifiers (%d)"),
2916 );
2917 }
2918 }
2919 }
2920
2921
2922
2923
2924
2925 if (config->
schema != NULL) {
2927 if (tmp == NULL) {
2928 rterror(
_(
"Could not allocate memory for quoting schema name"));
2930 exit(1);
2931 }
2932
2933 sprintf(tmp,
"\"%s\".", config->
schema);
2936 }
2937 if (config->
table != NULL) {
2938 tmp =
rtalloc(
sizeof(
char) * (strlen(config->
table) + 3));
2939 if (tmp == NULL) {
2940 rterror(
_(
"Could not allocate memory for quoting table name"));
2942 exit(1);
2943 }
2944
2945 sprintf(tmp,
"\"%s\"", config->
table);
2947 config->
table = tmp;
2948 }
2951 if (tmp == NULL) {
2952 rterror(
_(
"Could not allocate memory for quoting raster column name"));
2954 exit(1);
2955 }
2956
2960 }
2963 if (tmp == NULL) {
2964 rterror(
_(
"Could not allocate memory for quoting raster column name"));
2966 exit(1);
2967 }
2968
2972 }
2975 if (tmp == NULL) {
2976 rterror(
_(
"Could not allocate memory for quoting tablespace name"));
2978 exit(1);
2979 }
2980
2984 }
2987 if (tmp == NULL) {
2988 rterror(
_(
"Could not allocate memory for quoting index tablespace name"));
2990 exit(1);
2991 }
2992
2996 }
3000 if (tmp == NULL) {
3001 rterror(
_(
"Could not allocate memory for quoting overview table name"));
3003 exit(1);
3004 }
3005
3009 }
3010 }
3011
3012
3013
3014
3015
3016
3019 rterror(
_(
"Could not allocate memory for output string buffer"));
3021 exit(1);
3022 }
3024
3025
3027 rterror(
_(
"Unable to process rasters"));
3030 exit(1);
3031 }
3032
3034
3037
3038 return 0;
3039}
#define SRID_UNKNOWN
Unknown SRID value.
void rterror(const char *fmt,...)
Wrappers used for reporting errors and info.
void * rtalloc(size_t size)
Wrappers used for managing memory.
rt_gdaldriver rt_raster_gdal_drivers(uint32_t *drv_count, uint8_t cancc)
Returns a set of available GDAL drivers.
void rtwarn(const char *fmt,...)
void * rtrealloc(void *mem, size_t size)
void rtdealloc(void *mem)
Datum buffer(PG_FUNCTION_ARGS)
static void rtdealloc_stringbuffer(STRINGBUFFER *buffer, int freebuffer)
static int array_range(int min, int max, int step, int **range, uint32_t *len)
static void flush_stringbuffer(STRINGBUFFER *buffer)
static void init_config(RTLOADERCFG *config)
static char * strtolower(char *str)
static void rt_init_allocators(void)
static char ** strsplit(const char *str, const char *delimiter, uint32_t *n)
static void rtdealloc_config(RTLOADERCFG *config)
static void init_stringbuffer(STRINGBUFFER *buffer)
static int process_rasters(RTLOADERCFG *config, STRINGBUFFER *buffer)
static char * trim(const char *input)