How many options we have currently. Right now we'll have at most 1, but that could change in future
3006 double offset[4] = {0.};
3016 int mask_height = 0;
3023 char **options = NULL;
3025 int options_len = 0;
3030 if (PG_ARGISNULL(0) || PG_ARGISNULL(2))
3036 elog(ERROR,
"RASTER_clip: Could not initialize argument structure");
3041 pgraster = (
rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
3045 if (arg->
raster == NULL) {
3047 PG_FREE_IF_COPY(pgraster, 0);
3048 elog(ERROR,
"RASTER_clip: Could not deserialize raster");
3054 elog(NOTICE,
"Input raster is empty or has no bands. Returning empty raster");
3057 PG_FREE_IF_COPY(pgraster, 0);
3061 elog(ERROR,
"RASTER_clip: Could not create empty raster");
3070 SET_VARSIZE(pgrtn, pgrtn->
size);
3071 PG_RETURN_POINTER(pgrtn);
3079 gser = PG_GETARG_GSERIALIZED_P(2);
3091 elog(NOTICE,
"Geometry provided does not have the same SRID as the raster. Returning NULL");
3094 PG_FREE_IF_COPY(pgraster, 0);
3096 PG_FREE_IF_COPY(gser, 2);
3102 if (!PG_ARGISNULL(4) && !PG_GETARG_BOOL(4))
3109 PG_FREE_IF_COPY(pgraster, 0);
3111 PG_FREE_IF_COPY(gser, 2);
3113 elog(ERROR,
"RASTER_clip: Could not get convex hull of raster");
3120 PG_FREE_IF_COPY(gser, 2);
3125 elog(NOTICE,
"The input raster and input geometry do not intersect. Returning empty raster");
3128 PG_FREE_IF_COPY(pgraster, 0);
3133 elog(ERROR,
"RASTER_clip: Could not create empty raster");
3142 SET_VARSIZE(pgrtn, pgrtn->
size);
3143 PG_RETURN_POINTER(pgrtn);
3147 if (!PG_ARGISNULL(1)) {
3148 array = PG_GETARG_ARRAYTYPE_P(1);
3149 etype = ARR_ELEMTYPE(array);
3150 get_typlenbyvalalign(etype, &typlen, &typbyval, &typalign);
3158 PG_FREE_IF_COPY(pgraster, 0);
3160 elog(ERROR,
"RASTER_clip: Invalid data type for band indexes");
3167 typlen, typbyval, typalign,
3172 if (arg->
band == NULL) {
3174 PG_FREE_IF_COPY(pgraster, 0);
3176 elog(ERROR,
"RASTER_clip: Could not allocate memory for band arguments");
3180 for (i = 0, j = 0; i < arg->
numbands; i++) {
3181 if (nulls[i])
continue;
3185 arg->
band[j].
nband = DatumGetInt16(e[i]) - 1;
3188 arg->
band[j].
nband = DatumGetInt32(e[i]) - 1;
3195 if (j < arg->numbands) {
3197 if (arg->
band == NULL) {
3199 PG_FREE_IF_COPY(pgraster, 0);
3201 elog(ERROR,
"RASTER_clip: Could not reallocate memory for band arguments");
3209 for (i = 0; i < arg->
numbands; i++) {
3211 elog(NOTICE,
"Band at index %d not found in raster", arg->
band[i].
nband + 1);
3213 PG_FREE_IF_COPY(pgraster, 0);
3228 if (arg->
band == NULL) {
3231 PG_FREE_IF_COPY(pgraster, 0);
3234 elog(ERROR,
"RASTER_clip: Could not allocate memory for band arguments");
3238 for (i = 0; i < arg->
numbands; i++) {
3247 if (!PG_ARGISNULL(3)) {
3248 array = PG_GETARG_ARRAYTYPE_P(3);
3249 etype = ARR_ELEMTYPE(array);
3250 get_typlenbyvalalign(etype, &typlen, &typbyval, &typalign);
3258 PG_FREE_IF_COPY(pgraster, 0);
3260 elog(ERROR,
"RASTER_clip: Invalid data type for NODATA values");
3267 typlen, typbyval, typalign,
3272 for (i = 0, j = 0; i < arg->
numbands; i++, j++) {
3298 if (!PG_ARGISNULL(5) && PG_GETARG_BOOL(5) ==
TRUE){
3299 options_len = options_len + 1;
3300 options = (
char **) palloc(
sizeof(
char *) * options_len);
3301 options[options_len - 1] = palloc(
sizeof(
char*) * (strlen(
"ALL_TOUCHED=TRUE") + 1));
3302 strcpy(options[options_len - 1],
"ALL_TOUCHED=TRUE");
3307 options = (
char **) repalloc(options,
sizeof(
char *) * options_len);
3308 options[options_len - 1] = NULL;
3335 if (options_len) pfree(options);
3337 if (arg->
mask == NULL) {
3339 PG_FREE_IF_COPY(pgraster, 0);
3340 elog(ERROR,
"RASTER_clip: Could not rasterize intersection geometry");
3352 PG_FREE_IF_COPY(pgraster, 0);
3353 elog(ERROR,
"RASTER_clip: Could not compute extent of rasters");
3362 for (i = 0; i < arg->
numbands; i++) {
3383 PG_FREE_IF_COPY(pgraster, 0);
3384 elog(ERROR,
"RASTER_clip: Could not add new band in output raster");
3398 for (
y = 0;
y < height;
y++) {
3399 for (
x = 0;
x < width;
x++) {
3400 mask_x =
x - (int)offset[2];
3401 mask_y =
y - (int)offset[3];
3405 mask_x < mask_width &&
3407 mask_y < mask_height
3414 PG_FREE_IF_COPY(pgraster, 0);
3415 elog(ERROR,
"RASTER_clip: Could not get pixel value");
3423 input_x =
x - (int)offset[0];
3424 input_y =
y - (int)offset[1];
3428 PG_FREE_IF_COPY(pgraster, 0);
3429 elog(ERROR,
"RASTER_clip: Could not get pixel value");
3439 PG_FREE_IF_COPY(pgraster, 0);
3440 elog(ERROR,
"RASTER_clip: Could not set pixel value");
3448 PG_FREE_IF_COPY(pgraster, 0);
3458 SET_VARSIZE(pgrtn, pgrtn->
size);
3459 PG_RETURN_POINTER(pgrtn);
int32_t gserialized_get_srid(const GSERIALIZED *g)
Extract the SRID from the serialized form (it is packed into three bytes so this is a handy function)...
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
int lwgeom_ndims(const LWGEOM *geom)
Return the number of dimensions (2, 3, 4) in a geometry.
void lwgeom_free(LWGEOM *geom)
LWGEOM * lwgeom_intersection(const LWGEOM *geom1, const LWGEOM *geom2)
#define LWSIZE_GET(varsize)
Macro for reading the size from the GSERIALIZED size attribute.
#define SRID_UNKNOWN
Unknown SRID value.
lwvarlena_t * lwgeom_to_wkb_varlena(const LWGEOM *geom, uint8_t variant)
int32_t clamp_srid(int32_t srid)
Return a valid SRID from an arbitrary integer Raises a notice if what comes out is different from wha...
LWGEOM * lwgeom_force_2d(const LWGEOM *geom)
Strip out the Z/M components of an LWGEOM.
int32_t rt_raster_get_srid(rt_raster raster)
Get raster's SRID.
int rt_raster_generate_new_band(rt_raster raster, rt_pixtype pixtype, double initialvalue, uint32_t hasnodata, double nodatavalue, int index)
Generate a new inline band and add it to a raster.
int rt_band_get_hasnodata_flag(rt_band band)
Get hasnodata flag value.
rt_errorstate rt_band_get_pixel(rt_band band, int x, int y, double *value, int *nodata)
Get pixel value.
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
int rt_band_get_isnodata_flag(rt_band band)
Get isnodata flag value.
rt_raster rt_raster_new(uint32_t width, uint32_t height)
Construct a raster with given dimensions.
int rt_raster_has_band(rt_raster raster, int nband)
Return TRUE if the raster has a band of this number.
void * rt_raster_serialize(rt_raster raster)
Return this raster in serialized form.
double rt_band_get_min_value(rt_band band)
Returns the minimal possible value for the band according to the pixel type.
rt_errorstate rt_band_set_pixel(rt_band band, int x, int y, double val, int *converted)
Set single pixel's value.
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.
uint16_t rt_raster_get_num_bands(rt_raster raster)
rt_errorstate rt_raster_get_convex_hull(rt_raster raster, LWGEOM **hull)
Get raster's convex hull.
uint16_t rt_raster_get_height(rt_raster raster)
void rt_raster_set_srid(rt_raster raster, int32_t srid)
Set raster's SRID.
rt_errorstate rt_band_get_nodata(rt_band band, double *nodata)
Get NODATA value.
rt_pixtype rt_band_get_pixtype(rt_band band)
Return pixeltype of this band.
uint16_t rt_raster_get_width(rt_raster raster)
rt_errorstate rt_raster_from_two_rasters(rt_raster rast1, rt_raster rast2, rt_extenttype extenttype, rt_raster *rtnraster, double *offset)
void rt_raster_get_geotransform_matrix(rt_raster raster, double *gt)
Get 6-element array of raster geotransform matrix.
rt_raster rt_raster_deserialize(void *serialized, int header_only)
Return a raster from a serialized form.
int rt_raster_is_empty(rt_raster raster)
Return TRUE if the raster is empty.
rt_band rt_raster_get_band(rt_raster raster, int bandNum)
Return Nth band, or NULL if unavailable.
static int lwgeom_is_empty(const LWGEOM *geom)
Return true or false depending on whether a geometry is an "empty" geometry (no vertices members)
static rtpg_clip_arg rtpg_clip_arg_init(void)
static void rtpg_clip_arg_destroy(rtpg_clip_arg arg)
#define POSTGIS_RT_DEBUG(level, msg)