3026 unsigned char *wkb = NULL;
3055 if (PG_ARGISNULL(0) || PG_ARGISNULL(2))
3061 elog(ERROR,
"RASTER_clip: Could not initialize argument structure");
3066 pgraster = (
rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
3070 if (arg->
raster == NULL) {
3072 PG_FREE_IF_COPY(pgraster, 0);
3073 elog(ERROR,
"RASTER_clip: Could not deserialize raster");
3079 elog(NOTICE,
"Input raster is empty or has no bands. Returning empty raster");
3082 PG_FREE_IF_COPY(pgraster, 0);
3086 elog(ERROR,
"RASTER_clip: Could not create empty raster");
3095 SET_VARSIZE(pgrtn, pgrtn->
size);
3096 PG_RETURN_POINTER(pgrtn);
3104 gser = PG_GETARG_GSERIALIZED_P(2);
3116 elog(NOTICE,
"Geometry provided does not have the same SRID as the raster. Returning NULL");
3119 PG_FREE_IF_COPY(pgraster, 0);
3121 PG_FREE_IF_COPY(gser, 2);
3127 if (!PG_ARGISNULL(4) && !PG_GETARG_BOOL(4))
3134 PG_FREE_IF_COPY(pgraster, 0);
3136 PG_FREE_IF_COPY(gser, 2);
3138 elog(ERROR,
"RASTER_clip: Could not get convex hull of raster");
3145 PG_FREE_IF_COPY(gser, 2);
3150 elog(NOTICE,
"The input raster and input geometry do not intersect. Returning empty raster");
3153 PG_FREE_IF_COPY(pgraster, 0);
3158 elog(ERROR,
"RASTER_clip: Could not create empty raster");
3167 SET_VARSIZE(pgrtn, pgrtn->
size);
3168 PG_RETURN_POINTER(pgrtn);
3172 if (!PG_ARGISNULL(1)) {
3173 array = PG_GETARG_ARRAYTYPE_P(1);
3174 etype = ARR_ELEMTYPE(array);
3175 get_typlenbyvalalign(etype, &typlen, &typbyval, &typalign);
3183 PG_FREE_IF_COPY(pgraster, 0);
3185 elog(ERROR,
"RASTER_clip: Invalid data type for band indexes");
3192 typlen, typbyval, typalign,
3197 if (arg->
band == NULL) {
3199 PG_FREE_IF_COPY(pgraster, 0);
3201 elog(ERROR,
"RASTER_clip: Could not allocate memory for band arguments");
3205 for (i = 0, j = 0; i < arg->
numbands; i++) {
3206 if (nulls[i])
continue;
3210 arg->
band[j].
nband = DatumGetInt16(e[i]) - 1;
3213 arg->
band[j].
nband = DatumGetInt32(e[i]) - 1;
3220 if (j < arg->numbands) {
3222 if (arg->
band == NULL) {
3224 PG_FREE_IF_COPY(pgraster, 0);
3226 elog(ERROR,
"RASTER_clip: Could not reallocate memory for band arguments");
3234 for (i = 0; i < arg->
numbands; i++) {
3236 elog(NOTICE,
"Band at index %d not found in raster", arg->
band[i].
nband + 1);
3238 PG_FREE_IF_COPY(pgraster, 0);
3253 if (arg->
band == NULL) {
3256 PG_FREE_IF_COPY(pgraster, 0);
3259 elog(ERROR,
"RASTER_clip: Could not allocate memory for band arguments");
3263 for (i = 0; i < arg->
numbands; i++) {
3272 if (!PG_ARGISNULL(3)) {
3273 array = PG_GETARG_ARRAYTYPE_P(3);
3274 etype = ARR_ELEMTYPE(array);
3275 get_typlenbyvalalign(etype, &typlen, &typbyval, &typalign);
3283 PG_FREE_IF_COPY(pgraster, 0);
3285 elog(ERROR,
"RASTER_clip: Invalid data type for NODATA values");
3292 typlen, typbyval, typalign,
3297 for (i = 0, j = 0; i < arg->
numbands; i++, j++) {
3338 if (arg->
mask == NULL) {
3340 PG_FREE_IF_COPY(pgraster, 0);
3341 elog(ERROR,
"RASTER_clip: Could not rasterize intersection geometry");
3352 if (itrset == NULL) {
3354 PG_FREE_IF_COPY(pgraster, 0);
3355 elog(ERROR,
"RASTER_clip: Could not allocate memory for iterator arguments");
3360 for (i = 0; i < arg->
numbands; i++) {
3361 POSTGIS_RT_DEBUGF(4,
"band arg %d (nband, hasnodata, nodataval) = (%d, %d, %f)",
3389 PG_FREE_IF_COPY(pgraster, 0);
3390 elog(ERROR,
"RASTER_clip: Could not create output raster");
3399 PG_FREE_IF_COPY(pgraster, 0);
3400 elog(ERROR,
"RASTER_clip: Could not add NODATA band to output raster");
3414 itrset[1].
nband = 0;
3422 hasnodata, nodataval,
3434 PG_FREE_IF_COPY(pgraster, 0);
3435 elog(ERROR,
"RASTER_clip: Could not run raster iterator function");
3450 PG_FREE_IF_COPY(pgraster, 0);
3451 elog(NOTICE,
"RASTER_clip: Could not get band from working raster");
3460 PG_FREE_IF_COPY(pgraster, 0);
3461 elog(ERROR,
"RASTER_clip: Could not add new band to output raster");
3471 PG_FREE_IF_COPY(pgraster, 0);
3481 SET_VARSIZE(pgrtn, pgrtn->
size);
3482 PG_RETURN_POINTER(pgrtn);
int clamp_srid(int srid)
Return a valid SRID from an arbitrary integer Raises a notice if what comes out is different from wha...
void * rt_raster_serialize(rt_raster raster)
Return this raster in serialized form.
int rt_raster_get_num_bands(rt_raster raster)
static void rtpg_clip_arg_destroy(rtpg_clip_arg arg)
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
void rt_raster_get_geotransform_matrix(rt_raster raster, double *gt)
Get 6-element array of raster geotransform matrix.
void lwgeom_free(LWGEOM *geom)
rt_errorstate rt_raster_from_two_rasters(rt_raster rast1, rt_raster rast2, rt_extenttype extenttype, rt_raster *rtnraster, double *offset)
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.
static int rtpg_clip_callback(rt_iterator_arg arg, void *userarg, double *value, int *nodata)
int lwgeom_ndims(const LWGEOM *geom)
Return the number of dimensions (2, 3, 4) in a geometry.
int rt_raster_is_empty(rt_raster raster)
Return TRUE if the raster is empty.
#define POSTGIS_RT_DEBUGF(level, msg,...)
rt_errorstate rt_band_get_nodata(rt_band band, double *nodata)
Get NODATA value.
rt_errorstate rt_raster_get_convex_hull(rt_raster raster, LWGEOM **hull)
Get raster's convex hull.
uint8_t * lwgeom_to_wkb(const LWGEOM *geom, uint8_t variant, size_t *size_out)
Convert LWGEOM to a char* in WKB format.
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.
#define SRID_UNKNOWN
Unknown SRID value.
LWGEOM * lwgeom_intersection(const LWGEOM *geom1, const LWGEOM *geom2)
LWGEOM * lwgeom_force_2d(const LWGEOM *geom)
Strip out the Z/M components of an LWGEOM.
rt_band rt_raster_get_band(rt_raster raster, int bandNum)
Return Nth band, or NULL if unavailable.
int rt_band_get_hasnodata_flag(rt_band band)
Get hasnodata flag value.
int rt_raster_has_band(rt_raster raster, int nband)
Return TRUE if the raster has a band of this number.
void rt_raster_set_srid(rt_raster raster, int32_t srid)
Set raster's SRID.
int32_t rt_raster_get_srid(rt_raster raster)
Get raster's SRID.
void rt_raster_destroy(rt_raster raster)
Release memory associated to a raster.
rt_raster rt_raster_new(uint32_t width, uint32_t height)
Construct a raster with given dimensions.
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_raster_iterator(rt_iterator itrset, uint16_t itrcount, rt_extenttype extenttype, rt_raster customextent, rt_pixtype pixtype, uint8_t hasnodata, double nodataval, uint16_t distancex, uint16_t distancey, rt_mask mask, void *userarg, int(*callback)(rt_iterator_arg arg, void *userarg, double *value, int *nodata), rt_raster *rtnraster)
n-raster iterator.
static rtpg_clip_arg rtpg_clip_arg_init()
rt_pixtype rt_band_get_pixtype(rt_band band)
Return pixeltype of this band.
int rt_raster_add_band(rt_raster raster, rt_band band, int index)
Add band data to a raster.
int rt_band_get_isnodata_flag(rt_band band)
Get isnodata flag value.
#define POSTGIS_RT_DEBUG(level, msg)
int lwgeom_is_empty(const LWGEOM *geom)
Return true or false depending on whether a geometry is an "empty" geometry (no vertices members) ...
rt_raster rt_raster_deserialize(void *serialized, int header_only)
Return a raster from a serialized form.
int32_t gserialized_get_srid(const GSERIALIZED *s)
Extract the SRID from the serialized form (it is packed into three bytes so this is a handy function)...