PostGIS 3.7.0dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches

◆ rt_raster_gdal_rasterize()

rt_raster rt_raster_gdal_rasterize ( const unsigned char *  wkb,
uint32_t  wkb_len,
const char *  srs,
uint32_t  num_bands,
rt_pixtype pixtype,
double *  init,
double *  value,
double *  nodata,
uint8_t *  hasnodata,
int *  width,
int *  height,
double *  scale_x,
double *  scale_y,
double *  ul_xw,
double *  ul_yw,
double *  grid_xw,
double *  grid_yw,
double *  skew_x,
double *  skew_y,
char **  options 
)

Return a raster of the provided geometry.

Parameters
wkb: WKB representation of the geometry to convert
wkb_len: length of the WKB representation of the geometry
srs: the geometry's coordinate system in OGC WKT
num_bands: number of bands in the output raster
pixtype: data type of each band
init: array of values to initialize each band with
value: array of values for pixels of geometry
nodata: array of nodata values for each band
hasnodata: array flagging the presence of nodata for each band
width: the number of columns of the raster
height: the number of rows of the raster
scale_x: the pixel width of the raster
scale_y: the pixel height of the raster
ul_xw: the X value of upper-left corner of the raster
ul_yw: the Y value of upper-left corner of the raster
grid_xw: the X value of point on grid to align raster to
grid_yw: the Y value of point on grid to align raster to
skew_x: the X skew of the raster
skew_y: the Y skew of the raster
options: array of options. only option is "ALL_TOUCHED"
Returns
the raster of the provided geometry or NULL

Definition at line 2502 of file rt_raster.c.

2514 {
2515 rt_raster rast = NULL;
2516 uint32_t i = 0;
2517 int err = 0;
2518
2519 _rti_rasterize_arg arg = NULL;
2520
2521 int _dim[2] = {0};
2522 double _scale[2] = {0};
2523 double _skew[2] = {0};
2524
2525 OGRErr ogrerr;
2526 OGRGeometryH src_geom;
2527 OGREnvelope src_env;
2528 rt_envelope extent;
2529 OGRwkbGeometryType wkbtype = wkbUnknown;
2530
2531 int ul_user = 0;
2532
2533 CPLErr cplerr;
2534 double _gt[6] = {0};
2535 GDALDriverH _drv = NULL;
2536 int unload_drv = 0;
2537 GDALDatasetH _ds = NULL;
2538 GDALRasterBandH _band = NULL;
2539
2540 uint16_t _width = 0;
2541 uint16_t _height = 0;
2542
2543 RASTER_DEBUG(3, "starting");
2544
2545 assert(NULL != wkb);
2546 assert(0 != wkb_len);
2547
2548 /* internal variables */
2550 if (arg == NULL) {
2551 rterror("rt_raster_gdal_rasterize: Could not initialize internal variables");
2552 return NULL;
2553 }
2554
2555 /* no bands, raster is a mask */
2556 if (num_bands < 1) {
2557 arg->noband = 1;
2558 arg->numbands = 1;
2559
2560 arg->pixtype = (rt_pixtype *) rtalloc(sizeof(rt_pixtype));
2561 arg->pixtype[0] = PT_8BUI;
2562
2563 arg->init = (double *) rtalloc(sizeof(double));
2564 arg->init[0] = 0;
2565
2566 arg->nodata = (double *) rtalloc(sizeof(double));
2567 arg->nodata[0] = 0;
2568
2569 arg->hasnodata = (uint8_t *) rtalloc(sizeof(uint8_t));
2570 arg->hasnodata[0] = 1;
2571
2572 arg->value = (double *) rtalloc(sizeof(double));
2573 arg->value[0] = 1;
2574 }
2575 else {
2576 arg->noband = 0;
2577 arg->numbands = num_bands;
2578
2579 arg->pixtype = pixtype;
2580 arg->init = init;
2581 arg->nodata = nodata;
2582 arg->hasnodata = hasnodata;
2583 arg->value = value;
2584 }
2585
2586 /* OGR spatial reference */
2587 if (NULL != srs && strlen(srs)) {
2588 arg->src_sr = OSRNewSpatialReference(NULL);
2589 if (OSRSetFromUserInput(arg->src_sr, srs) != OGRERR_NONE) {
2590 rterror("rt_raster_gdal_rasterize: Could not create OSR spatial reference using the provided srs: %s", srs);
2592 return NULL;
2593 }
2594 }
2595
2596 /* convert WKB to OGR Geometry */
2597 ogrerr = OGR_G_CreateFromWkb((unsigned char *) wkb, arg->src_sr, &src_geom, wkb_len);
2598 if (ogrerr != OGRERR_NONE) {
2599 rterror("rt_raster_gdal_rasterize: Could not create OGR Geometry from WKB");
2600
2602 /* OGRCleanupAll(); */
2603
2604 return NULL;
2605 }
2606
2607 /* OGR Geometry is empty */
2608 if (OGR_G_IsEmpty(src_geom)) {
2609 rtinfo("Geometry provided is empty. Returning empty raster");
2610
2611 OGR_G_DestroyGeometry(src_geom);
2613 /* OGRCleanupAll(); */
2614
2615 return rt_raster_new(0, 0);
2616 }
2617
2618 /* get envelope */
2619 OGR_G_GetEnvelope(src_geom, &src_env);
2620 rt_util_from_ogr_envelope(src_env, &extent);
2621
2622 RASTER_DEBUGF(3, "Suggested raster envelope: %f, %f, %f, %f",
2623 extent.MinX, extent.MinY, extent.MaxX, extent.MaxY);
2624
2625 /* user-defined scale */
2626 if (
2627 (NULL != scale_x) &&
2628 (NULL != scale_y) &&
2629 (FLT_NEQ(*scale_x, 0.0)) &&
2630 (FLT_NEQ(*scale_y, 0.0))
2631 ) {
2632 /* for now, force scale to be in left-right, top-down orientation */
2633 _scale[0] = fabs(*scale_x);
2634 _scale[1] = fabs(*scale_y);
2635 }
2636 /* user-defined width/height */
2637 else if ((NULL != width) && (NULL != height) && (*width != 0) && (*height != 0))
2638 {
2639 _dim[0] = abs(*width);
2640 _dim[1] = abs(*height);
2641
2642 if (FLT_NEQ(extent.MaxX, extent.MinX))
2643 _scale[0] = fabs((extent.MaxX - extent.MinX) / _dim[0]);
2644 else
2645 _scale[0] = 1.;
2646
2647 if (FLT_NEQ(extent.MaxY, extent.MinY))
2648 _scale[1] = fabs((extent.MaxY - extent.MinY) / _dim[1]);
2649 else
2650 _scale[1] = 1.;
2651 }
2652 else {
2653 rterror("rt_raster_gdal_rasterize: Values must be provided for width and height or X and Y of scale");
2654
2655 OGR_G_DestroyGeometry(src_geom);
2657 /* OGRCleanupAll(); */
2658
2659 return NULL;
2660 }
2661 RASTER_DEBUGF(3, "scale (x, y) = %f, %f", _scale[0], -1 * _scale[1]);
2662 RASTER_DEBUGF(3, "dim (x, y) = %d, %d", _dim[0], _dim[1]);
2663
2664 /* user-defined skew */
2665 if (NULL != skew_x) {
2666 _skew[0] = *skew_x;
2667
2668 /*
2669 negative scale-x affects skew
2670 for now, force skew to be in left-right, top-down orientation
2671 */
2672 if (
2673 NULL != scale_x &&
2674 *scale_x < 0.
2675 ) {
2676 _skew[0] *= -1;
2677 }
2678 }
2679 if (NULL != skew_y) {
2680 _skew[1] = *skew_y;
2681
2682 /*
2683 positive scale-y affects skew
2684 for now, force skew to be in left-right, top-down orientation
2685 */
2686 if (
2687 NULL != scale_y &&
2688 *scale_y > 0.
2689 ) {
2690 _skew[1] *= -1;
2691 }
2692 }
2693
2694 /*
2695 if geometry is a point, a linestring or set of either and bounds not set,
2696 increase extent by a pixel to avoid missing points on border
2697
2698 a whole pixel is used instead of half-pixel due to backward
2699 compatibility with GDAL 1.6, 1.7 and 1.8. 1.9+ works fine with half-pixel.
2700 */
2701 wkbtype = wkbFlatten(OGR_G_GetGeometryType(src_geom));
2702 if ((
2703 (wkbtype == wkbPoint) ||
2704 (wkbtype == wkbMultiPoint) ||
2705 (wkbtype == wkbLineString) ||
2706 (wkbtype == wkbMultiLineString)
2707 ) &&
2708 _dim[0] == 0 &&
2709 _dim[1] == 0
2710 ) {
2711
2712#if POSTGIS_GDAL_VERSION > 10800
2713
2714 RASTER_DEBUG(3, "Adjusting extent for GDAL > 1.8 by half the scale on X-axis");
2715 extent.MinX -= (_scale[0] / 2.);
2716 extent.MaxX += (_scale[0] / 2.);
2717
2718 RASTER_DEBUG(3, "Adjusting extent for GDAL > 1.8 by half the scale on Y-axis");
2719 extent.MinY -= (_scale[1] / 2.);
2720 extent.MaxY += (_scale[1] / 2.);
2721
2722#else
2723
2724 RASTER_DEBUG(3, "Adjusting extent for GDAL <= 1.8 by the scale on X-axis");
2725 extent.MinX -= _scale[0];
2726 extent.MaxX += _scale[0];
2727
2728 RASTER_DEBUG(3, "Adjusting extent for GDAL <= 1.8 by the scale on Y-axis");
2729 extent.MinY -= _scale[1];
2730 extent.MaxY += _scale[1];
2731
2732#endif
2733
2734
2735 RASTER_DEBUGF(3, "Adjusted extent: %f, %f, %f, %f",
2736 extent.MinX, extent.MinY, extent.MaxX, extent.MaxY);
2737
2738 extent.UpperLeftX = extent.MinX;
2739 extent.UpperLeftY = extent.MaxY;
2740 }
2741
2742 /* reprocess extent if skewed */
2743 if (FLT_NEQ(_skew[0], 0.0) || FLT_NEQ(_skew[1], 0.0))
2744 {
2745 rt_raster skewedrast;
2746
2747 RASTER_DEBUG(3, "Computing skewed extent's envelope");
2748
2750 extent,
2751 _skew,
2752 _scale,
2753 0.01
2754 );
2755 if (skewedrast == NULL) {
2756 rterror("rt_raster_gdal_rasterize: Could not compute skewed raster");
2757
2758 OGR_G_DestroyGeometry(src_geom);
2760 /* OGRCleanupAll(); */
2761
2762 return NULL;
2763 }
2764
2765 _dim[0] = skewedrast->width;
2766 _dim[1] = skewedrast->height;
2767
2768 extent.UpperLeftX = skewedrast->ipX;
2769 extent.UpperLeftY = skewedrast->ipY;
2770
2771 rt_raster_destroy(skewedrast);
2772 }
2773
2774 /* raster dimensions */
2775 if (!_dim[0])
2776 _dim[0] = (int) fmax((fabs(extent.MaxX - extent.MinX) + (_scale[0] / 2.)) / _scale[0], 1);
2777 if (!_dim[1])
2778 _dim[1] = (int) fmax((fabs(extent.MaxY - extent.MinY) + (_scale[1] / 2.)) / _scale[1], 1);
2779
2780 /* temporary raster */
2781 rast = rt_raster_new(_dim[0], _dim[1]);
2782 if (rast == NULL) {
2783 rterror("rt_raster_gdal_rasterize: Out of memory allocating temporary raster");
2784
2785 OGR_G_DestroyGeometry(src_geom);
2787 /* OGRCleanupAll(); */
2788
2789 return NULL;
2790 }
2791
2792 /* set raster's spatial attributes */
2793 rt_raster_set_offsets(rast, extent.UpperLeftX, extent.UpperLeftY);
2794 rt_raster_set_scale(rast, _scale[0], -1 * _scale[1]);
2795 rt_raster_set_skews(rast, _skew[0], _skew[1]);
2796
2798 RASTER_DEBUGF(3, "Temp raster's geotransform: %f, %f, %f, %f, %f, %f",
2799 _gt[0], _gt[1], _gt[2], _gt[3], _gt[4], _gt[5]);
2800 RASTER_DEBUGF(3, "Temp raster's dimensions (width x height): %d x %d",
2801 _dim[0], _dim[1]);
2802
2803 /* user-specified upper-left corner */
2804 if (
2805 NULL != ul_xw &&
2806 NULL != ul_yw
2807 ) {
2808 ul_user = 1;
2809
2810 RASTER_DEBUGF(4, "Using user-specified upper-left corner: %f, %f", *ul_xw, *ul_yw);
2811
2812 /* set upper-left corner */
2813 rt_raster_set_offsets(rast, *ul_xw, *ul_yw);
2814 extent.UpperLeftX = *ul_xw;
2815 extent.UpperLeftY = *ul_yw;
2816 }
2817 else if (
2818 ((NULL != ul_xw) && (NULL == ul_yw)) ||
2819 ((NULL == ul_xw) && (NULL != ul_yw))
2820 ) {
2821 rterror("rt_raster_gdal_rasterize: Both X and Y upper-left corner values must be provided");
2822
2823 rt_raster_destroy(rast);
2824 OGR_G_DestroyGeometry(src_geom);
2826 /* OGRCleanupAll(); */
2827
2828 return NULL;
2829 }
2830
2831 /* alignment only considered if upper-left corner not provided */
2832 if (
2833 !ul_user && (
2834 (NULL != grid_xw) || (NULL != grid_yw)
2835 )
2836 ) {
2837
2838 if (
2839 ((NULL != grid_xw) && (NULL == grid_yw)) ||
2840 ((NULL == grid_xw) && (NULL != grid_yw))
2841 ) {
2842 rterror("rt_raster_gdal_rasterize: Both X and Y alignment values must be provided");
2843
2844 rt_raster_destroy(rast);
2845 OGR_G_DestroyGeometry(src_geom);
2847 /* OGRCleanupAll(); */
2848
2849 return NULL;
2850 }
2851
2852 RASTER_DEBUGF(4, "Aligning extent to user-specified grid: %f, %f", *grid_xw, *grid_yw);
2853
2854 do {
2855 double _r[2] = {0};
2856 double _w[2] = {0};
2857
2858 /* raster is already aligned */
2859 if (DBL_EQ(*grid_xw, extent.UpperLeftX) && DBL_EQ(*grid_yw, extent.UpperLeftY)) {
2860 RASTER_DEBUG(3, "Skipping raster alignment as it is already aligned to grid");
2861 break;
2862 }
2863
2864 extent.UpperLeftX = rast->ipX;
2865 extent.UpperLeftY = rast->ipY;
2866 rt_raster_set_offsets(rast, *grid_xw, *grid_yw);
2867
2868 /* process upper-left corner */
2870 rast,
2871 extent.UpperLeftX, extent.UpperLeftY,
2872 &(_r[0]), &(_r[1]),
2873 NULL
2874 ) != ES_NONE) {
2875 rterror("rt_raster_gdal_rasterize: Could not compute raster pixel for spatial coordinates");
2876
2877 rt_raster_destroy(rast);
2878 OGR_G_DestroyGeometry(src_geom);
2880 /* OGRCleanupAll(); */
2881
2882 return NULL;
2883 }
2884
2886 rast,
2887 _r[0], _r[1],
2888 &(_w[0]), &(_w[1]),
2889 NULL
2890 ) != ES_NONE) {
2891 rterror("rt_raster_gdal_rasterize: Could not compute spatial coordinates for raster pixel");
2892
2893 rt_raster_destroy(rast);
2894 OGR_G_DestroyGeometry(src_geom);
2896 /* OGRCleanupAll(); */
2897
2898 return NULL;
2899 }
2900
2901 /* shift occurred */
2902 if (FLT_NEQ(_w[0], extent.UpperLeftX)) {
2903 if (NULL == width)
2904 rast->width++;
2905 else if (NULL == scale_x) {
2906 double _c[2] = {0};
2907
2908 rt_raster_set_offsets(rast, extent.UpperLeftX, extent.UpperLeftY);
2909
2910 /* get upper-right corner */
2912 rast,
2913 rast->width, 0,
2914 &(_c[0]), &(_c[1]),
2915 NULL
2916 ) != ES_NONE) {
2917 rterror("rt_raster_gdal_rasterize: Could not compute spatial coordinates for raster pixel");
2918
2919 rt_raster_destroy(rast);
2920 OGR_G_DestroyGeometry(src_geom);
2922 /* OGRCleanupAll(); */
2923
2924 return NULL;
2925 }
2926
2927 rast->scaleX = fabs((_c[0] - _w[0]) / ((double) rast->width));
2928 }
2929 }
2930 if (FLT_NEQ(_w[1], extent.UpperLeftY)) {
2931 if (NULL == height)
2932 rast->height++;
2933 else if (NULL == scale_y) {
2934 double _c[2] = {0};
2935
2936 rt_raster_set_offsets(rast, extent.UpperLeftX, extent.UpperLeftY);
2937
2938 /* get upper-right corner */
2940 rast,
2941 0, rast->height,
2942 &(_c[0]), &(_c[1]),
2943 NULL
2944 ) != ES_NONE) {
2945 rterror("rt_raster_gdal_rasterize: Could not compute spatial coordinates for raster pixel");
2946
2947 rt_raster_destroy(rast);
2948 OGR_G_DestroyGeometry(src_geom);
2950 /* OGRCleanupAll(); */
2951
2952 return NULL;
2953 }
2954
2955 rast->scaleY = -1 * fabs((_c[1] - _w[1]) / ((double) rast->height));
2956 }
2957 }
2958
2959 rt_raster_set_offsets(rast, _w[0], _w[1]);
2960 }
2961 while (0);
2962 }
2963
2964 /*
2965 after this point, rt_envelope extent is no longer used
2966 */
2967
2968 /* get key attributes from rast */
2969 _dim[0] = rast->width;
2970 _dim[1] = rast->height;
2972
2973 /* scale-x is negative or scale-y is positive */
2974 if ((
2975 (NULL != scale_x) && (*scale_x < 0.)
2976 ) || (
2977 (NULL != scale_y) && (*scale_y > 0)
2978 )) {
2979 double _w[2] = {0};
2980
2981 /* negative scale-x */
2982 if (
2983 (NULL != scale_x) &&
2984 (*scale_x < 0.)
2985 ) {
2986 RASTER_DEBUG(3, "Processing negative scale-x");
2987
2989 rast,
2990 _dim[0], 0,
2991 &(_w[0]), &(_w[1]),
2992 NULL
2993 ) != ES_NONE) {
2994 rterror("rt_raster_gdal_rasterize: Could not compute spatial coordinates for raster pixel");
2995
2996 rt_raster_destroy(rast);
2997 OGR_G_DestroyGeometry(src_geom);
2999 /* OGRCleanupAll(); */
3000
3001 return NULL;
3002 }
3003
3004 _gt[0] = _w[0];
3005 _gt[1] = *scale_x;
3006
3007 /* check for skew */
3008 if (NULL != skew_x && FLT_NEQ(*skew_x, 0.0))
3009 _gt[2] = *skew_x;
3010 }
3011 /* positive scale-y */
3012 if (
3013 (NULL != scale_y) &&
3014 (*scale_y > 0)
3015 ) {
3016 RASTER_DEBUG(3, "Processing positive scale-y");
3017
3019 rast,
3020 0, _dim[1],
3021 &(_w[0]), &(_w[1]),
3022 NULL
3023 ) != ES_NONE) {
3024 rterror("rt_raster_gdal_rasterize: Could not compute spatial coordinates for raster pixel");
3025
3026 rt_raster_destroy(rast);
3027 OGR_G_DestroyGeometry(src_geom);
3029 /* OGRCleanupAll(); */
3030
3031 return NULL;
3032 }
3033
3034 _gt[3] = _w[1];
3035 _gt[5] = *scale_y;
3036
3037 /* check for skew */
3038 if (NULL != skew_y && FLT_NEQ(*skew_y, 0.0))
3039 _gt[4] = *skew_y;
3040 }
3041 }
3042
3043 rt_raster_destroy(rast);
3044 rast = NULL;
3045
3046 RASTER_DEBUGF(3, "Applied geotransform: %f, %f, %f, %f, %f, %f",
3047 _gt[0], _gt[1], _gt[2], _gt[3], _gt[4], _gt[5]);
3048 RASTER_DEBUGF(3, "Raster dimensions (width x height): %d x %d",
3049 _dim[0], _dim[1]);
3050
3051 /* load GDAL mem */
3052 if (!rt_util_gdal_driver_registered("MEM")) {
3053 RASTER_DEBUG(4, "Registering MEM driver");
3054 GDALRegister_MEM();
3055 unload_drv = 1;
3056 }
3057 _drv = GDALGetDriverByName("MEM");
3058 if (NULL == _drv) {
3059 rterror("rt_raster_gdal_rasterize: Could not load the MEM GDAL driver");
3060
3061 OGR_G_DestroyGeometry(src_geom);
3063 /* OGRCleanupAll(); */
3064
3065 return NULL;
3066 }
3067
3068 /* unload driver from GDAL driver manager */
3069 if (unload_drv) {
3070 RASTER_DEBUG(4, "Deregistering MEM driver");
3071 GDALDeregisterDriver(_drv);
3072 }
3073
3074 _ds = GDALCreate(_drv, "", _dim[0], _dim[1], 0, GDT_Byte, NULL);
3075 if (NULL == _ds) {
3076 rterror("rt_raster_gdal_rasterize: Could not create a GDALDataset to rasterize the geometry into");
3077
3078 OGR_G_DestroyGeometry(src_geom);
3080 /* OGRCleanupAll(); */
3081 if (unload_drv) GDALDestroyDriver(_drv);
3082
3083 return NULL;
3084 }
3085
3086 /* set geotransform */
3087 cplerr = GDALSetGeoTransform(_ds, _gt);
3088 if (cplerr != CE_None) {
3089 rterror("rt_raster_gdal_rasterize: Could not set geotransform on GDALDataset");
3090
3091 OGR_G_DestroyGeometry(src_geom);
3093 /* OGRCleanupAll(); */
3094
3095 GDALClose(_ds);
3096 if (unload_drv) GDALDestroyDriver(_drv);
3097
3098 return NULL;
3099 }
3100
3101 /* set SRS */
3102 if (NULL != arg->src_sr) {
3103 char *_srs = NULL;
3104 OSRExportToWkt(arg->src_sr, &_srs);
3105
3106 cplerr = GDALSetProjection(_ds, _srs);
3107 CPLFree(_srs);
3108 if (cplerr != CE_None) {
3109 rterror("rt_raster_gdal_rasterize: Could not set projection on GDALDataset");
3110
3111 OGR_G_DestroyGeometry(src_geom);
3113 /* OGRCleanupAll(); */
3114
3115 GDALClose(_ds);
3116 if (unload_drv) GDALDestroyDriver(_drv);
3117
3118 return NULL;
3119 }
3120 }
3121
3122 /* set bands */
3123 for (i = 0; i < arg->numbands; i++) {
3124 err = 0;
3125
3126 do {
3127 /* add band */
3128 cplerr = GDALAddBand(_ds, rt_util_pixtype_to_gdal_datatype(arg->pixtype[i]), NULL);
3129 if (cplerr != CE_None) {
3130 rterror("rt_raster_gdal_rasterize: Could not add band to GDALDataset");
3131 err = 1;
3132 break;
3133 }
3134
3135 _band = GDALGetRasterBand(_ds, i + 1);
3136 if (NULL == _band) {
3137 rterror("rt_raster_gdal_rasterize: Could not get band %d from GDALDataset", i + 1);
3138 err = 1;
3139 break;
3140 }
3141
3142 /* nodata value */
3143 if (arg->hasnodata[i]) {
3144 RASTER_DEBUGF(4, "Setting NODATA value of band %d to %f", i, arg->nodata[i]);
3145 cplerr = GDALSetRasterNoDataValue(_band, arg->nodata[i]);
3146 if (cplerr != CE_None) {
3147 rterror("rt_raster_gdal_rasterize: Could not set nodata value");
3148 err = 1;
3149 break;
3150 }
3151 RASTER_DEBUGF(4, "NODATA value set to %f", GDALGetRasterNoDataValue(_band, NULL));
3152 }
3153
3154 /* initial value */
3155 cplerr = GDALFillRaster(_band, arg->init[i], 0);
3156 if (cplerr != CE_None) {
3157 rterror("rt_raster_gdal_rasterize: Could not set initial value");
3158 err = 1;
3159 break;
3160 }
3161 }
3162 while (0);
3163
3164 if (err) {
3165
3166 OGR_G_DestroyGeometry(src_geom);
3168
3169 /* OGRCleanupAll(); */
3170
3171 GDALClose(_ds);
3172 if (unload_drv) GDALDestroyDriver(_drv);
3173
3174 return NULL;
3175 }
3176 }
3177
3178 arg->bandlist = (int *) rtalloc(sizeof(int) * arg->numbands);
3179 for (i = 0; i < arg->numbands; i++) arg->bandlist[i] = i + 1;
3180
3181 /* burn geometry */
3182 cplerr = GDALRasterizeGeometries(
3183 _ds,
3184 arg->numbands, arg->bandlist,
3185 1, &src_geom,
3186 NULL, NULL,
3187 arg->value,
3188 options,
3189 NULL, NULL
3190 );
3191 if (cplerr != CE_None) {
3192 rterror("rt_raster_gdal_rasterize: Could not rasterize geometry");
3193
3194 OGR_G_DestroyGeometry(src_geom);
3196 /* OGRCleanupAll(); */
3197
3198 GDALClose(_ds);
3199 if (unload_drv) GDALDestroyDriver(_drv);
3200
3201 return NULL;
3202 }
3203
3204 /* convert gdal dataset to raster */
3205 GDALFlushCache(_ds);
3206 RASTER_DEBUG(3, "Converting GDAL dataset to raster");
3208
3209 OGR_G_DestroyGeometry(src_geom);
3210 /* OGRCleanupAll(); */
3211
3212 GDALClose(_ds);
3213 if (unload_drv) GDALDestroyDriver(_drv);
3214
3215 if (NULL == rast) {
3216 rterror("rt_raster_gdal_rasterize: Could not rasterize geometry");
3217 return NULL;
3218 }
3219
3220 /* width, height */
3221 _width = rt_raster_get_width(rast);
3222 _height = rt_raster_get_height(rast);
3223
3224 /* check each band for pixtype */
3225 for (i = 0; i < arg->numbands; i++) {
3226 uint8_t *data = NULL;
3227 rt_band band = NULL;
3228 rt_band oldband = NULL;
3229
3230 double val = 0;
3231 int nodata = 0;
3232 int hasnodata = 0;
3233 double nodataval = 0;
3234 int x = 0;
3235 int y = 0;
3236
3237 oldband = rt_raster_get_band(rast, i);
3238 if (oldband == NULL) {
3239 rterror("rt_raster_gdal_rasterize: Could not get band %d of output raster", i);
3241 rt_raster_destroy(rast);
3242 return NULL;
3243 }
3244
3245 /* band is of user-specified type */
3246 if (rt_band_get_pixtype(oldband) == arg->pixtype[i])
3247 continue;
3248
3249 /* hasnodata, nodataval */
3250 hasnodata = rt_band_get_hasnodata_flag(oldband);
3251 if (hasnodata)
3252 rt_band_get_nodata(oldband, &nodataval);
3253
3254 /* allocate data */
3255 data = rtalloc((size_t)rt_pixtype_size(arg->pixtype[i]) * _width * _height);
3256 if (data == NULL) {
3257 rterror("rt_raster_gdal_rasterize: Could not allocate memory for band data");
3259 rt_raster_destroy(rast);
3260 return NULL;
3261 }
3262 memset(data, 0, (size_t)rt_pixtype_size(arg->pixtype[i]) * _width * _height);
3263
3264 /* create new band of correct type */
3266 _width, _height,
3267 arg->pixtype[i],
3268 hasnodata, nodataval,
3269 data
3270 );
3271 if (band == NULL) {
3272 rterror("rt_raster_gdal_rasterize: Could not create band");
3273 rtdealloc(data);
3275 rt_raster_destroy(rast);
3276 return NULL;
3277 }
3278
3279 /* give ownership of data to band */
3281
3282 /* copy pixel by pixel */
3283 for (x = 0; x < _width; x++) {
3284 for (y = 0; y < _height; y++) {
3285 err = rt_band_get_pixel(oldband, x, y, &val, &nodata);
3286 if (err != ES_NONE) {
3287 rterror("rt_raster_gdal_rasterize: Could not get pixel value");
3289 rt_raster_destroy(rast);
3290 rt_band_destroy(band);
3291 return NULL;
3292 }
3293
3294 if (nodata)
3295 val = nodataval;
3296
3297 err = rt_band_set_pixel(band, x, y, val, NULL);
3298 if (err != ES_NONE) {
3299 rterror("rt_raster_gdal_rasterize: Could not set pixel value");
3301 rt_raster_destroy(rast);
3302 rt_band_destroy(band);
3303 return NULL;
3304 }
3305 }
3306 }
3307
3308 /* replace band */
3309 oldband = rt_raster_replace_band(rast, band, i);
3310 if (oldband == NULL) {
3311 rterror("rt_raster_gdal_rasterize: Could not replace band %d of output raster", i);
3313 rt_raster_destroy(rast);
3314 rt_band_destroy(band);
3315 return NULL;
3316 }
3317
3318 /* free oldband */
3319 rt_band_destroy(oldband);
3320 }
3321
3323
3324 RASTER_DEBUG(3, "done");
3325
3326 return rast;
3327}
rt_band rt_band_new_inline(uint16_t width, uint16_t height, rt_pixtype pixtype, uint32_t hasnodata, double nodataval, uint8_t *data)
Create an in-db rt_band with no data.
Definition rt_band.c:64
void rt_band_set_ownsdata_flag(rt_band band, int flag)
Definition rt_band.c:826
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.
Definition rt_context.c:191
#define FLT_NEQ(x, y)
Definition librtcore.h:2435
#define RASTER_DEBUG(level, msg)
Definition librtcore.h:304
#define RASTER_DEBUGF(level, msg,...)
Definition librtcore.h:308
void void rtinfo(const char *fmt,...) __attribute__((format(printf
int rt_band_get_hasnodata_flag(rt_band band)
Get hasnodata flag value.
Definition rt_band.c:833
rt_errorstate rt_band_get_pixel(rt_band band, int x, int y, double *value, int *nodata)
Get pixel value.
Definition rt_band.c:1551
rt_pixtype
Definition librtcore.h:188
@ PT_8BUI
Definition librtcore.h:193
GDALDataType rt_util_pixtype_to_gdal_datatype(rt_pixtype pt)
Convert rt_pixtype to GDALDataType.
Definition rt_util.c:213
rt_errorstate rt_band_set_pixel(rt_band band, int x, int y, double val, int *converted)
Set single pixel's value.
Definition rt_band.c:1140
@ ES_NONE
Definition librtcore.h:182
void rt_band_destroy(rt_band band)
Destroy a raster band.
Definition rt_band.c:499
#define DBL_EQ(x, y)
Definition librtcore.h:2438
int rt_util_gdal_driver_registered(const char *drv)
Definition rt_util.c:463
rt_errorstate rt_band_get_nodata(rt_band band, double *nodata)
Get NODATA value.
Definition rt_band.c:2067
rt_pixtype rt_band_get_pixtype(rt_band band)
Return pixeltype of this band.
Definition rt_band.c:790
void rtdealloc(void *mem)
Definition rt_context.c:206
void rt_util_from_ogr_envelope(OGREnvelope env, rt_envelope *ext)
Definition rt_util.c:559
int rt_pixtype_size(rt_pixtype pixtype)
Return size in bytes of a value in the given pixtype.
Definition rt_pixel.c:40
int value
Definition genraster.py:62
rt_errorstate rt_raster_cell_to_geopoint(rt_raster raster, double xr, double yr, double *xw, double *yw, double *gt)
Convert an xr, yr raster point to an xw, yw point on map.
Definition rt_raster.c:637
void rt_raster_set_scale(rt_raster raster, double scaleX, double scaleY)
Set scale in projection units.
Definition rt_raster.c:141
rt_errorstate rt_raster_geopoint_to_cell(rt_raster raster, double xw, double yw, double *xr, double *yr, double *igt)
Convert an xw,yw map point to a xr,yr cell coordinate.
Definition rt_raster.c:686
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
Definition rt_raster.c:86
void rt_raster_set_skews(rt_raster raster, double skewX, double skewY)
Set skews about the X and Y axis.
Definition rt_raster.c:172
rt_raster rt_raster_new(uint32_t width, uint32_t height)
Construct a raster with given dimensions.
Definition rt_raster.c:52
rt_raster rt_raster_compute_skewed_raster(rt_envelope extent, double *skew, double *scale, double tolerance)
Definition rt_raster.c:869
rt_raster rt_raster_from_gdal_dataset(GDALDatasetH ds)
Return a raster from a GDAL dataset.
Definition rt_raster.c:2175
static void _rti_rasterize_arg_destroy(_rti_rasterize_arg arg)
Definition rt_raster.c:2452
rt_band rt_raster_replace_band(rt_raster raster, rt_band band, int index)
Replace band at provided index with new band.
Definition rt_raster.c:1404
uint16_t rt_raster_get_height(rt_raster raster)
Definition rt_raster.c:133
rt_band rt_raster_get_band(rt_raster raster, int n)
Return Nth band, or NULL if unavailable.
Definition rt_raster.c:385
uint16_t rt_raster_get_width(rt_raster raster)
Definition rt_raster.c:125
static _rti_rasterize_arg _rti_rasterize_arg_init()
Definition rt_raster.c:2426
void rt_raster_get_geotransform_matrix(rt_raster raster, double *gt)
Get 6-element array of raster geotransform matrix.
Definition rt_raster.c:588
void rt_raster_set_offsets(rt_raster raster, double x, double y)
Set insertion points in projection units.
Definition rt_raster.c:203
rt_pixtype * pixtype
Definition rt_raster.c:2417
OGRSpatialReferenceH src_sr
Definition rt_raster.c:2415
double MinX
Definition librtcore.h:167
double UpperLeftY
Definition librtcore.h:173
double UpperLeftX
Definition librtcore.h:172
double MaxX
Definition librtcore.h:168
double MinY
Definition librtcore.h:169
double MaxY
Definition librtcore.h:170
uint16_t width
Definition librtcore.h:2503
uint16_t height
Definition librtcore.h:2504

References _rti_rasterize_arg_destroy(), _rti_rasterize_arg_init(), _rti_rasterize_arg_t::bandlist, DBL_EQ, ES_NONE, FLT_NEQ, _rti_rasterize_arg_t::hasnodata, rt_raster_t::height, _rti_rasterize_arg_t::init, rt_raster_t::ipX, rt_raster_t::ipY, rt_envelope::MaxX, rt_envelope::MaxY, rt_envelope::MinX, rt_envelope::MinY, _rti_rasterize_arg_t::noband, _rti_rasterize_arg_t::nodata, _rti_rasterize_arg_t::numbands, _rti_rasterize_arg_t::pixtype, PT_8BUI, RASTER_DEBUG, RASTER_DEBUGF, rt_band_destroy(), rt_band_get_hasnodata_flag(), rt_band_get_nodata(), rt_band_get_pixel(), rt_band_get_pixtype(), rt_band_new_inline(), rt_band_set_ownsdata_flag(), rt_band_set_pixel(), rt_pixtype_size(), rt_raster_cell_to_geopoint(), rt_raster_compute_skewed_raster(), rt_raster_destroy(), rt_raster_from_gdal_dataset(), rt_raster_geopoint_to_cell(), rt_raster_get_band(), rt_raster_get_geotransform_matrix(), rt_raster_get_height(), rt_raster_get_width(), rt_raster_new(), rt_raster_replace_band(), rt_raster_set_offsets(), rt_raster_set_scale(), rt_raster_set_skews(), rt_util_from_ogr_envelope(), rt_util_gdal_driver_registered(), rt_util_pixtype_to_gdal_datatype(), rtalloc(), rtdealloc(), rterror(), rtinfo(), _rti_rasterize_arg_t::src_sr, rt_envelope::UpperLeftX, rt_envelope::UpperLeftY, _rti_rasterize_arg_t::value, and rt_raster_t::width.

Referenced by RASTER_asRaster(), RASTER_clip(), RASTER_setPixelValuesGeomval(), and test_gdal_rasterize().

Here is the call graph for this function:
Here is the caller graph for this function: