2274 {
2277 uint32_t i = 0;
2278 uint32_t j = 0;
2279 char **elements = NULL;
2280 uint32_t n = 0;
2281 GDALDriverH drv = NULL;
2282 char *tmp = NULL;
2283 int argit = 0;
2284
2286
2287#ifdef USE_NLS
2288 setlocale (LC_ALL, "");
2289 bindtextdomain (PACKAGE, LOCALEDIR);
2290 textdomain (PACKAGE);
2291#endif
2292
2293
2294 if (argc == 1) {
2296 exit(0);
2297 }
2298
2299
2301 if (config == NULL) {
2302 rterror(
_(
"Could not allocate memory for loader configuration"));
2303 exit(1);
2304 }
2306
2307
2308
2309
2310
2311 for (argit = 1; argit < argc; argit++) {
2312 char *optarg, *ptr;
2313
2314
2315 if (
CSEQUAL(argv[argit],
"-s") && argit < argc - 1) {
2316 optarg = argv[++argit];
2317 ptr = strchr(optarg, ':');
2318 if (ptr) {
2319 *ptr++ = '\0';
2320 sscanf(optarg,
"%d", &config->
srid);
2321 sscanf(ptr,
"%d", &config->
out_srid);
2322 } else {
2323 config->
srid = atoi(optarg);
2324 }
2325 }
2326
2327 else if (
CSEQUAL(argv[argit],
"-b") && argit < argc - 1) {
2328 elements =
strsplit(argv[++argit],
",", &n);
2329 if (n < 1) {
2330 rterror(
_(
"Could not process -b"));
2332 exit(1);
2333 }
2334
2336 for (j = 0; j < n; j++) {
2337 char *t =
trim(elements[j]);
2338 char **minmax = NULL;
2339 int *range = NULL;
2340 uint32_t p = 0;
2341 uint32_t l = 0;
2342 int m = 0;
2343 uint32_t o = 0;
2344
2345
2347 if (o == 2) {
2348 if (!
array_range(atoi(minmax[0]), atoi(minmax[1]), 1, &range, &p)) {
2349 rterror(
_(
"Could not allocate memory for storing band indices"));
2350 for (l = 0; l < o; l++)
2353 for (j = 0; j < n; j++)
2358 exit(1);
2359 }
2360 }
2361 else {
2362 p = 1;
2364 if (range == NULL) {
2365 rterror(
_(
"Could not allocate memory for storing band indices"));
2366 for (l = 0; l < o; l++)
2369 for (j = 0; j < n; j++)
2374 exit(1);
2375 }
2376 *range = atoi(t);
2377 }
2378
2382 if (config->
nband == NULL) {
2383 rterror(
_(
"Could not allocate memory for storing band indices"));
2385 for (l = 0; l < o; l++)
2388 for (j = 0; j < n; j++)
2393 exit(1);
2394 }
2395
2396 for (l = 0; l < p; l++, m++)
2397 config->
nband[m] = range[l];
2398
2400
2401 for (l = 0; l < o; l++)
2404
2407 }
2409 elements = NULL;
2410 n = 0;
2411
2413 if (config->
nband[j] < 1) {
2414 rterror(
_(
"Band index %d must be greater than 0"), config->
nband[j]);
2416 exit(1);
2417 }
2418 }
2419 }
2420
2421 else if (
CSEQUAL(argv[argit],
"-t") && argit < argc - 1) {
2422 if (
CSEQUAL(argv[++argit],
"auto")) {
2425 }
2426 else {
2427 elements =
strsplit(argv[argit],
"x", &n);
2428 if (n != 2) {
2429 rterror(
_(
"Could not process -t"));
2431 exit(1);
2432 }
2433
2434 for (j = 0; j < n; j++) {
2435 char *t =
trim(elements[j]);
2439 }
2441 elements = NULL;
2442 n = 0;
2443
2444 for (j = 0; j < 2; j++) {
2446 rterror(
_(
"Tile size must be greater than 0x0"));
2448 exit(1);
2449 }
2450 }
2451 }
2452 }
2453
2454 else if (
CSEQUAL(argv[argit],
"-P")) {
2456 }
2457
2458 else if (
CSEQUAL(argv[argit],
"-R")) {
2460 }
2461
2462 else if (
CSEQUAL(argv[argit],
"-d")) {
2464 }
2465
2466 else if (
CSEQUAL(argv[argit],
"-a")) {
2468 }
2469
2470 else if (
CSEQUAL(argv[argit],
"-c")) {
2472 }
2473
2474 else if (
CSEQUAL(argv[argit],
"-p")) {
2476 }
2477
2478 else if (
CSEQUAL(argv[argit],
"-f") && argit < argc - 1) {
2479 const size_t len = (strlen(argv[++argit]) + 1);
2482 rterror(
_(
"Could not allocate memory for storing raster column name"));
2484 exit(1);
2485 }
2487 }
2488
2489 else if (
CSEQUAL(argv[argit],
"-F")) {
2491 }
2492
2493 else if (
CSEQUAL(argv[argit],
"-n") && argit < argc - 1) {
2494 const size_t len = (strlen(argv[++argit]) + 1);
2497 rterror(
_(
"Could not allocate memory for storing filename column name"));
2499 exit(1);
2500 }
2503 }
2504
2505 else if (
CSEQUAL(argv[argit],
"-l") && argit < argc - 1) {
2506 elements =
strsplit(argv[++argit],
",", &n);
2507 if (n < 1) {
2508 rterror(
_(
"Could not process -l"));
2510 exit(1);
2511 }
2512
2516 rterror(
_(
"Could not allocate memory for storing overview factors"));
2518 exit(1);
2519 }
2520 for (j = 0; j < n; j++) {
2521 char *t =
trim(elements[j]);
2525 }
2527 elements = NULL;
2528 n = 0;
2529
2534 exit(1);
2535 }
2536 }
2537 }
2538
2539 else if (
CSEQUAL(argv[argit],
"-q")) {
2541 }
2542
2543 else if (
CSEQUAL(argv[argit],
"-I")) {
2545 }
2546
2547 else if (
CSEQUAL(argv[argit],
"-M")) {
2549 }
2550
2551 else if (
CSEQUAL(argv[argit],
"-C")) {
2553 }
2554
2555 else if (
CSEQUAL(argv[argit],
"-x")) {
2557 }
2558
2559 else if (
CSEQUAL(argv[argit],
"-r")) {
2561 }
2562
2563 else if (
CSEQUAL(argv[argit],
"-T") && argit < argc - 1) {
2564 const size_t len = (strlen(argv[++argit]) + 1);
2567 rterror(
_(
"Could not allocate memory for storing tablespace of new table"));
2569 exit(1);
2570 }
2571 strncpy(config->
tablespace, argv[argit], len);
2572 }
2573
2574 else if (
CSEQUAL(argv[argit],
"-X") && argit < argc - 1) {
2575 const size_t len = (strlen(argv[++argit]) + 1);
2578 rterror(
_(
"Could not allocate memory for storing tablespace of new indices"));
2580 exit(1);
2581 }
2583 }
2584
2585 else if (
CSEQUAL(argv[argit],
"-N") && argit < argc - 1) {
2587 config->
nodataval = atof(argv[++argit]);
2588 }
2589
2590 else if (
CSEQUAL(argv[argit],
"-k")) {
2592 }
2593
2594 else if (
CSEQUAL(argv[argit],
"-E") && argit < argc - 1) {
2595 config->
endian = atoi(argv[++argit]);
2597 }
2598
2599 else if (
CSEQUAL(argv[argit],
"-V") && argit < argc - 1) {
2600 config->
version = atoi(argv[++argit]);
2602 }
2603
2604 else if (
CSEQUAL(argv[argit],
"-e")) {
2606 }
2607
2608 else if (
CSEQUAL(argv[argit],
"-Y")) {
2610
2611 if ( argit < argc - 1) {
2612 optarg = argv[argit + 1];
2613 if (atoi(optarg) > 0 ) {
2615 ++argit;
2616 }
2617 }
2618 }
2619
2620
2621
2622 else if (
CSEQUAL(argv[argit],
"-G")) {
2623 uint32_t drv_count = 0;
2625 if (drv_set == NULL || !drv_count) {
2626 rterror(
_(
"Could not get list of available GDAL raster formats"));
2627 }
2628 else {
2629 printf(
_(
"Supported GDAL raster formats:\n"));
2630 for (j = 0; j < drv_count; j++) {
2631 printf(
_(
" %s\n"), drv_set[j].long_name);
2632
2636 }
2638 }
2639
2641 exit(0);
2642 }
2643
2644 else if (
CSEQUAL(argv[argit],
"-?")) {
2647 exit(0);
2648 }
2649 else {
2650 size_t len;
2653 if (config->
rt_file == NULL) {
2654 rterror(
_(
"Could not allocate memory for storing raster files"));
2656 exit(1);
2657 }
2658
2659 len = strlen(argv[argit]) + 1;
2662 rterror(
_(
"Could not allocate memory for storing raster filename"));
2664 exit(1);
2665 }
2667 }
2668 }
2669
2672 rterror(
_(
"Invalid argument combination - cannot use -Y with -s FROM_SRID:TO_SRID"));
2673 exit(1);
2674 }
2675 }
2676
2677
2678 GDALAllRegister();
2679
2680
2684 exit(1);
2685 }
2686
2687
2688
2689
2692
2693 if (drv == NULL) {
2694 char *ptr;
2696
2697
2698 if (ptr) {
2700 if (config->
schema == NULL) {
2701 rterror(
_(
"Could not allocate memory for storing schema name"));
2703 exit(1);
2704 }
2707
2709 if (config->
table == NULL) {
2710 rterror(
_(
"Could not allocate memory for storing table name"));
2712 exit(1);
2713 }
2716 }
2717
2718 else {
2721 if (config->
table == NULL) {
2722 rterror(
_(
"Could not allocate memory for storing table name"));
2724 exit(1);
2725 }
2727 }
2728
2731 if (config->
rt_file == NULL) {
2732 rterror(
_(
"Could not reallocate the memory holding raster names"));
2734 exit(1);
2735 }
2736 }
2737 }
2738
2739
2740
2741
2742
2743
2745 drv = GDALIdentifyDriver(config->
rt_file[i], NULL);
2746
2747 if (drv == NULL) {
2750 exit(1);
2751 }
2752 }
2753
2754
2757 rterror(
_(
"Could not allocate memory for cleaned raster filenames"));
2759 exit(1);
2760 }
2762 char *file;
2763 char *ptr;
2764
2766 if (file == NULL) {
2767 rterror(
_(
"Could not allocate memory for cleaned raster filename"));
2769 exit(1);
2770 }
2771 strcpy(file, config->
rt_file[i]);
2772
2773 for (ptr = file + strlen(file); ptr > file; ptr--) {
2774 if (*ptr == '/' || *ptr == '\\') {
2775 ptr++;
2776 break;
2777 }
2778 }
2779
2782 rterror(
_(
"Could not allocate memory for cleaned raster filename"));
2784 exit(1);
2785 }
2788 }
2789
2790
2791
2792
2793
2794
2795 if (config->
table == NULL) {
2796 char *file;
2797 char *ptr;
2798
2800 if (file == NULL) {
2801 rterror(
_(
"Could not allocate memory for proxy table name"));
2803 exit(1);
2804 }
2806
2807 for (ptr = file + strlen(file); ptr > file; ptr--) {
2808 if (*ptr == '.') {
2809 *ptr = '\0';
2810 break;
2811 }
2812 }
2813
2814 config->
table =
rtalloc(
sizeof(
char) * (strlen(file) + 1));
2815 if (config->
table == NULL) {
2816 rterror(
_(
"Could not allocate memory for proxy table name"));
2818 exit(1);
2819 }
2820 strcpy(config->
table, file);
2822 }
2823
2824
2828 rterror(
_(
"Could not allocate memory for default raster column name"));
2830 exit(1);
2831 }
2833 }
2834
2835
2839 rterror(
_(
"Could not allocate memory for default filename column name"));
2841 exit(1);
2842 }
2844 }
2845
2846
2847
2848
2849
2850
2852 if (config->
schema != NULL)
2854 if (config->
table != NULL)
2864 }
2865
2866
2867
2868
2869
2871 char factor[4];
2874 rterror(
_(
"Could not allocate memory for overview table names"));
2876 exit(1);
2877 }
2878
2880 sprintf(factor,
"%d", config->
overview[i]);
2881
2884 rterror(
_(
"Could not allocate memory for overview table name"));
2886 exit(1);
2887 }
2889 }
2890 }
2891
2892
2893
2894
2895
2897 rtwarn(
_(
"The schema name \"%s\" may exceed the maximum string length permitted for PostgreSQL identifiers (%d)"),
2900 );
2901 }
2903 rtwarn(
_(
"The table name \"%s\" may exceed the maximum string length permitted for PostgreSQL identifiers (%d)"),
2906 );
2907 }
2909 rtwarn(
_(
"The column name \"%s\" may exceed the maximum string length permitted for PostgreSQL identifiers (%d)"),
2912 );
2913 }
2915 rtwarn(
_(
"The column name \"%s\" may exceed the maximum string length permitted for PostgreSQL identifiers (%d)"),
2918 );
2919 }
2921 rtwarn(
_(
"The tablespace name \"%s\" may exceed the maximum string length permitted for PostgreSQL identifiers (%d)"),
2924 );
2925 }
2927 rtwarn(
_(
"The index tablespace name \"%s\" may exceed the maximum string length permitted for PostgreSQL identifiers (%d)"),
2930 );
2931 }
2935 rtwarn(
_(
"The overview table name \"%s\" may exceed the maximum string length permitted for PostgreSQL identifiers (%d)"),
2938 );
2939 }
2940 }
2941 }
2942
2943
2944
2945
2946
2947 if (config->
schema != NULL) {
2949 if (tmp == NULL) {
2950 rterror(
_(
"Could not allocate memory for quoting schema name"));
2952 exit(1);
2953 }
2954
2955 sprintf(tmp,
"\"%s\".", config->
schema);
2958 }
2959 if (config->
table != NULL) {
2960 tmp =
rtalloc(
sizeof(
char) * (strlen(config->
table) + 3));
2961 if (tmp == NULL) {
2962 rterror(
_(
"Could not allocate memory for quoting table name"));
2964 exit(1);
2965 }
2966
2967 sprintf(tmp,
"\"%s\"", config->
table);
2969 config->
table = tmp;
2970 }
2973 if (tmp == NULL) {
2974 rterror(
_(
"Could not allocate memory for quoting raster column name"));
2976 exit(1);
2977 }
2978
2982 }
2985 if (tmp == NULL) {
2986 rterror(
_(
"Could not allocate memory for quoting raster column name"));
2988 exit(1);
2989 }
2990
2994 }
2997 if (tmp == NULL) {
2998 rterror(
_(
"Could not allocate memory for quoting tablespace name"));
3000 exit(1);
3001 }
3002
3006 }
3009 if (tmp == NULL) {
3010 rterror(
_(
"Could not allocate memory for quoting index tablespace name"));
3012 exit(1);
3013 }
3014
3018 }
3022 if (tmp == NULL) {
3023 rterror(
_(
"Could not allocate memory for quoting overview table name"));
3025 exit(1);
3026 }
3027
3031 }
3032 }
3033
3034
3035
3036
3037
3038
3041 rterror(
_(
"Could not allocate memory for output string buffer"));
3043 exit(1);
3044 }
3046
3047
3049 rterror(
_(
"Unable to process rasters"));
3052 exit(1);
3053 }
3054
3056
3059
3060 return 0;
3061}
#define SRID_UNKNOWN
Unknown SRID value.
void rterror(const char *fmt,...) __attribute__((format(printf
Wrappers used for reporting errors and info.
void * rtalloc(size_t size)
Wrappers used for managing memory.
void void void rtwarn(const char *fmt,...) __attribute__((format(printf
rt_gdaldriver rt_raster_gdal_drivers(uint32_t *drv_count, uint8_t cancc)
Returns a set of available GDAL drivers.
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)
uint32_t max_tiles_per_copy
max tiles per copy