raster = rt_raster_deserialize(pgraster, FALSE);
if (!raster) {
elog(ERROR, "RASTER_out: Could not deserialize raster");
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_NULL();
}
if (!hexwkb) {
elog(ERROR, "RASTER_out: Could not HEX-WKBize raster");
rt_raster_destroy(raster);
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_NULL();
}
/* Free the raster objects used */
rt_raster_destroy(raster);
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_CSTRING(hexwkb);
}
raster = rt_raster_deserialize(pgraster, FALSE);
if (!raster) {
elog(ERROR, "RASTER_to_bytea: Could not deserialize raster");
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_NULL();
}
if (!wkb) {
elog(ERROR, "RASTER_to_bytea: Could not allocate and generate WKB data");
rt_raster_destroy(raster);
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_NULL();
}
/* Free raster objects used */
rt_raster_destroy(raster);
pfree(wkb);
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_POINTER(result);
}
raster = rt_raster_deserialize(pgraster, FALSE);
if (!raster) {
elog(ERROR, "RASTER_to_binary: Could not deserialize raster");
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_NULL();
}
if (!wkb) {
elog(ERROR, "RASTER_to_binary: Could not allocate and generate WKB data");
rt_raster_destroy(raster);
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_NULL();
}
/* Free raster objects used */
rt_raster_destroy(raster);
pfree(wkb);
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_POINTER(result);
}
raster = rt_raster_deserialize(pgraster, TRUE);
if ( ! raster ) {
elog(ERROR, "RASTER_convex_hull: Could not deserialize raster");
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_NULL();
}
if ( ! convexhull ) {
elog(ERROR, "RASTER_convex_hull: Could not get raster's convex hull");
rt_raster_destroy(raster);
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_NULL();
}
}
/* Free raster and lwgeom memory */
rt_raster_destroy(raster);
- lwfree(convexhull);
+ pfree(convexhull);
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_POINTER(gser);
}
pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
raster = rt_raster_deserialize(pgraster, FALSE);
if (!raster) {
+ PG_FREE_IF_COPY(pgraster, 0);
ereport(ERROR, (
errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("Could not deserialize raster")
if (nband < 1 || nband > numbands) {
elog(NOTICE, "Invalid band index (must use 1-based). Returning NULL");
rt_raster_destroy(raster);
+ PG_FREE_IF_COPY(pgraster, 0);
MemoryContextSwitchTo(oldcontext);
SRF_RETURN_DONE(funcctx);
}
*/
geomval = rt_raster_gdal_polygonize(raster, nband - 1, &nElements);
rt_raster_destroy(raster);
+ PG_FREE_IF_COPY(pgraster, 0);
if (NULL == geomval) {
ereport(ERROR, (
errcode(ERRCODE_NO_DATA_FOUND),
raster = rt_raster_deserialize(pgraster, TRUE);
if ( ! raster ) {
elog(ERROR, "RASTER_getSRID: Could not deserialize raster");
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_NULL();
}
srid = rt_raster_get_srid(raster);
rt_raster_destroy(raster);
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_INT32(srid);
}
Datum RASTER_setSRID(PG_FUNCTION_ARGS)
{
rt_pgraster *pgraster = NULL;
+ rt_pgraster *pgrtn = NULL;
rt_raster raster;
int32_t newSRID = PG_GETARG_INT32(1);
raster = rt_raster_deserialize(pgraster, FALSE);
if (!raster) {
elog(ERROR, "RASTER_setSRID: Could not deserialize raster");
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_NULL();
}
rt_raster_set_srid(raster, newSRID);
- pgraster = rt_raster_serialize(raster);
+ pgrtn = rt_raster_serialize(raster);
rt_raster_destroy(raster);
- if (!pgraster) PG_RETURN_NULL();
+ PG_FREE_IF_COPY(pgraster, 0);
+ if (!pgrtn) PG_RETURN_NULL();
- SET_VARSIZE(pgraster, pgraster->size);
+ SET_VARSIZE(pgrtn, pgrtn->size);
- PG_RETURN_POINTER(pgraster);
+ PG_RETURN_POINTER(pgrtn);
}
/**
raster = rt_raster_deserialize(pgraster, TRUE);
if ( ! raster ) {
elog(ERROR, "RASTER_getWidth: Could not deserialize raster");
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_NULL();
}
width = rt_raster_get_width(raster);
rt_raster_destroy(raster);
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_INT32(width);
}
raster = rt_raster_deserialize(pgraster, TRUE);
if ( ! raster ) {
elog(ERROR, "RASTER_getHeight: Could not deserialize raster");
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_NULL();
}
height = rt_raster_get_height(raster);
rt_raster_destroy(raster);
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_INT32(height);
}
raster = rt_raster_deserialize(pgraster, TRUE);
if ( ! raster ) {
elog(ERROR, "RASTER_getNumBands: Could not deserialize raster");
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_NULL();
}
num_bands = rt_raster_get_num_bands(raster);
rt_raster_destroy(raster);
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_INT32(num_bands);
}
raster = rt_raster_deserialize(pgraster, TRUE);
if ( ! raster ) {
elog(ERROR, "RASTER_getXScale: Could not deserialize raster");
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_NULL();
}
xsize = rt_raster_get_x_scale(raster);
rt_raster_destroy(raster);
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_FLOAT8(xsize);
}
raster = rt_raster_deserialize(pgraster, TRUE);
if ( ! raster ) {
elog(ERROR, "RASTER_getYScale: Could not deserialize raster");
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_NULL();
}
ysize = rt_raster_get_y_scale(raster);
rt_raster_destroy(raster);
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_FLOAT8(ysize);
}
PG_FUNCTION_INFO_V1(RASTER_setScale);
Datum RASTER_setScale(PG_FUNCTION_ARGS)
{
- rt_pgraster *pgraster = NULL;
- rt_raster raster;
- double size = PG_GETARG_FLOAT8(1);
-
- if (PG_ARGISNULL(0)) PG_RETURN_NULL();
- pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
- raster = rt_raster_deserialize(pgraster, FALSE);
- if (! raster ) {
- elog(ERROR, "RASTER_setScale: Could not deserialize raster");
- PG_RETURN_NULL();
- }
+ rt_pgraster *pgraster = NULL;
+ rt_pgraster *pgrtn = NULL;
+ rt_raster raster;
+ double size = PG_GETARG_FLOAT8(1);
- rt_raster_set_scale(raster, size, size);
+ if (PG_ARGISNULL(0))
+ PG_RETURN_NULL();
+ pgraster = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
+ raster = rt_raster_deserialize(pgraster, FALSE);
+ if (!raster) {
+ elog(ERROR, "RASTER_setScale: Could not deserialize raster");
+ PG_FREE_IF_COPY(pgraster, 0);
+ PG_RETURN_NULL();
+ }
- pgraster = rt_raster_serialize(raster);
- rt_raster_destroy(raster);
- if ( ! pgraster ) PG_RETURN_NULL();
+ rt_raster_set_scale(raster, size, size);
- SET_VARSIZE(pgraster, pgraster->size);
+ pgrtn = rt_raster_serialize(raster);
+ rt_raster_destroy(raster);
+ PG_FREE_IF_COPY(pgraster, 0);
+ if (!pgrtn)
+ PG_RETURN_NULL();
- PG_RETURN_POINTER(pgraster);
+ SET_VARSIZE(pgrtn, pgrtn->size);
+ PG_RETURN_POINTER(pgrtn);
}
/**
PG_FUNCTION_INFO_V1(RASTER_setScaleXY);
Datum RASTER_setScaleXY(PG_FUNCTION_ARGS)
{
- rt_pgraster *pgraster = NULL;
- rt_raster raster;
- double xscale = PG_GETARG_FLOAT8(1);
- double yscale = PG_GETARG_FLOAT8(2);
-
- if (PG_ARGISNULL(0)) PG_RETURN_NULL();
- pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
- raster = rt_raster_deserialize(pgraster, FALSE);
- if (! raster ) {
- elog(ERROR, "RASTER_setScaleXY: Could not deserialize raster");
- PG_RETURN_NULL();
- }
-
- rt_raster_set_scale(raster, xscale, yscale);
+ rt_pgraster *pgraster = NULL;
+ rt_pgraster *pgrtn = NULL;
+ rt_raster raster;
+ double xscale = PG_GETARG_FLOAT8(1);
+ double yscale = PG_GETARG_FLOAT8(2);
- pgraster = rt_raster_serialize(raster);
- rt_raster_destroy(raster);
- if ( ! pgraster ) PG_RETURN_NULL();
+ if (PG_ARGISNULL(0))
+ PG_RETURN_NULL();
+ pgraster = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
+ raster = rt_raster_deserialize(pgraster, FALSE);
+ if (!raster) {
+ elog(ERROR, "RASTER_setScaleXY: Could not deserialize raster");
+ PG_FREE_IF_COPY(pgraster, 0);
+ PG_RETURN_NULL();
+ }
- SET_VARSIZE(pgraster, pgraster->size);
+ rt_raster_set_scale(raster, xscale, yscale);
+ pgrtn = rt_raster_serialize(raster);
+ rt_raster_destroy(raster);
+ PG_FREE_IF_COPY(pgraster, 0);
+ if (!pgrtn)
+ PG_RETURN_NULL();
- PG_RETURN_POINTER(pgraster);
+ SET_VARSIZE(pgrtn, pgrtn->size);
+ PG_RETURN_POINTER(pgrtn);
}
/**
raster = rt_raster_deserialize(pgraster, TRUE);
if ( ! raster ) {
elog(ERROR, "RASTER_getXSkew: Could not deserialize raster");
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_NULL();
}
xskew = rt_raster_get_x_skew(raster);
rt_raster_destroy(raster);
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_FLOAT8(xskew);
}
raster = rt_raster_deserialize(pgraster, TRUE);
if ( ! raster ) {
elog(ERROR, "RASTER_getYSkew: Could not deserialize raster");
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_NULL();
}
yskew = rt_raster_get_y_skew(raster);
rt_raster_destroy(raster);
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_FLOAT8(yskew);
}
PG_FUNCTION_INFO_V1(RASTER_setSkew);
Datum RASTER_setSkew(PG_FUNCTION_ARGS)
{
- rt_pgraster *pgraster = NULL;
- rt_raster raster;
- double skew = PG_GETARG_FLOAT8(1);
-
- if (PG_ARGISNULL(0)) PG_RETURN_NULL();
- pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
- raster = rt_raster_deserialize(pgraster, FALSE);
- if (! raster ) {
- elog(ERROR, "RASTER_setSkew: Could not deserialize raster");
- PG_RETURN_NULL();
- }
+ rt_pgraster *pgraster = NULL;
+ rt_pgraster *pgrtn = NULL;
+ rt_raster raster;
+ double skew = PG_GETARG_FLOAT8(1);
- rt_raster_set_skews(raster, skew, skew);
+ if (PG_ARGISNULL(0))
+ PG_RETURN_NULL();
+ pgraster = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
+ raster = rt_raster_deserialize(pgraster, FALSE);
+ if (!raster) {
+ elog(ERROR, "RASTER_setSkew: Could not deserialize raster");
+ PG_FREE_IF_COPY(pgraster, 0);
+ PG_RETURN_NULL();
+ }
- pgraster = rt_raster_serialize(raster);
- rt_raster_destroy(raster);
- if ( ! pgraster ) PG_RETURN_NULL();
+ rt_raster_set_skews(raster, skew, skew);
- SET_VARSIZE(pgraster, pgraster->size);
+ pgrtn = rt_raster_serialize(raster);
+ rt_raster_destroy(raster);
+ PG_FREE_IF_COPY(pgraster, 0);
+ if (!pgrtn)
+ PG_RETURN_NULL();
- PG_RETURN_POINTER(pgraster);
+ SET_VARSIZE(pgrtn, pgrtn->size);
+ PG_RETURN_POINTER(pgrtn);
}
/**
PG_FUNCTION_INFO_V1(RASTER_setSkewXY);
Datum RASTER_setSkewXY(PG_FUNCTION_ARGS)
{
- rt_pgraster *pgraster = NULL;
- rt_raster raster;
- double xskew = PG_GETARG_FLOAT8(1);
- double yskew = PG_GETARG_FLOAT8(2);
-
- if (PG_ARGISNULL(0)) PG_RETURN_NULL();
- pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
- raster = rt_raster_deserialize(pgraster, FALSE);
- if (! raster ) {
- elog(ERROR, "RASTER_setSkewXY: Could not deserialize raster");
- PG_RETURN_NULL();
- }
+ rt_pgraster *pgraster = NULL;
+ rt_pgraster *pgrtn = NULL;
+ rt_raster raster;
+ double xskew = PG_GETARG_FLOAT8(1);
+ double yskew = PG_GETARG_FLOAT8(2);
- rt_raster_set_skews(raster, xskew, yskew);
+ if (PG_ARGISNULL(0))
+ PG_RETURN_NULL();
+ pgraster = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
+ raster = rt_raster_deserialize(pgraster, FALSE);
+ if (!raster) {
+ elog(ERROR, "RASTER_setSkewXY: Could not deserialize raster");
+ PG_FREE_IF_COPY(pgraster, 0);
+ PG_RETURN_NULL();
+ }
- pgraster = rt_raster_serialize(raster);
- rt_raster_destroy(raster);
- if ( ! pgraster ) PG_RETURN_NULL();
+ rt_raster_set_skews(raster, xskew, yskew);
- SET_VARSIZE(pgraster, pgraster->size);
+ pgrtn = rt_raster_serialize(raster);
+ rt_raster_destroy(raster);
+ PG_FREE_IF_COPY(pgraster, 0);
+ if (!pgrtn)
+ PG_RETURN_NULL();
- PG_RETURN_POINTER(pgraster);
+ SET_VARSIZE(pgrtn, pgrtn->size);
+ PG_RETURN_POINTER(pgrtn);
}
/**
raster = rt_raster_deserialize(pgraster, TRUE);
if ( ! raster ) {
elog(ERROR, "RASTER_getXUpperLeft: Could not deserialize raster");
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_NULL();
}
xul = rt_raster_get_x_offset(raster);
rt_raster_destroy(raster);
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_FLOAT8(xul);
}
raster = rt_raster_deserialize(pgraster, TRUE);
if ( ! raster ) {
elog(ERROR, "RASTER_getYUpperLeft: Could not deserialize raster");
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_NULL();
}
yul = rt_raster_get_y_offset(raster);
rt_raster_destroy(raster);
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_FLOAT8(yul);
}
PG_FUNCTION_INFO_V1(RASTER_setUpperLeftXY);
Datum RASTER_setUpperLeftXY(PG_FUNCTION_ARGS)
{
- rt_pgraster *pgraster = NULL;
- rt_raster raster;
- double xoffset = PG_GETARG_FLOAT8(1);
- double yoffset = PG_GETARG_FLOAT8(2);
-
- if (PG_ARGISNULL(0)) PG_RETURN_NULL();
- pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
- raster = rt_raster_deserialize(pgraster, FALSE);
- if (! raster ) {
- elog(ERROR, "RASTER_setUpperLeftXY: Could not deserialize raster");
- PG_RETURN_NULL();
- }
+ rt_pgraster *pgraster = NULL;
+ rt_pgraster *pgrtn = NULL;
+ rt_raster raster;
+ double xoffset = PG_GETARG_FLOAT8(1);
+ double yoffset = PG_GETARG_FLOAT8(2);
- rt_raster_set_offsets(raster, xoffset, yoffset);
+ if (PG_ARGISNULL(0))
+ PG_RETURN_NULL();
+ pgraster = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
+ raster = rt_raster_deserialize(pgraster, FALSE);
+ if (!raster) {
+ elog(ERROR, "RASTER_setUpperLeftXY: Could not deserialize raster");
+ PG_FREE_IF_COPY(pgraster, 0);
+ PG_RETURN_NULL();
+ }
- pgraster = rt_raster_serialize(raster);
- rt_raster_destroy(raster);
- if ( ! pgraster ) PG_RETURN_NULL();
+ rt_raster_set_offsets(raster, xoffset, yoffset);
- SET_VARSIZE(pgraster, pgraster->size);
+ pgrtn = rt_raster_serialize(raster);
+ rt_raster_destroy(raster);
+ PG_FREE_IF_COPY(pgraster, 0);
+ if (!pgrtn)
+ PG_RETURN_NULL();
- PG_RETURN_POINTER(pgraster);
+ SET_VARSIZE(pgrtn, pgrtn->size);
+ PG_RETURN_POINTER(pgrtn);
}
/**
raster = rt_raster_deserialize(pgraster, TRUE);
if (!raster) {
elog(ERROR, "RASTER_getPixelWidth: Could not deserialize raster");
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_NULL();
}
pwidth = sqrt(xscale*xscale + yskew*yskew);
rt_raster_destroy(raster);
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_FLOAT8(pwidth);
}
raster = rt_raster_deserialize(pgraster, TRUE);
if (!raster) {
elog(ERROR, "RASTER_getPixelHeight: Could not deserialize raster");
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_NULL();
}
pheight = sqrt(yscale*yscale + xskew*xskew);
rt_raster_destroy(raster);
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_FLOAT8(pheight);
}
PG_FUNCTION_INFO_V1(RASTER_setGeotransform);
Datum RASTER_setGeotransform(PG_FUNCTION_ARGS)
{
- rt_pgraster *pgraster ;
- rt_raster raster ;
- float8 imag, jmag, theta_i, theta_ij, xoffset, yoffset ;
-
- if (PG_ARGISNULL(0) || PG_ARGISNULL(1) || PG_ARGISNULL(2) ||
- PG_ARGISNULL(3) || PG_ARGISNULL(4) ||
- PG_ARGISNULL(5) || PG_ARGISNULL(6))
- PG_RETURN_NULL();
-
- /* get the inputs */
- pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
- imag = PG_GETARG_FLOAT8(1) ;
- jmag = PG_GETARG_FLOAT8(2) ;
- theta_i = PG_GETARG_FLOAT8(3);
- theta_ij = PG_GETARG_FLOAT8(4);
- xoffset = PG_GETARG_FLOAT8(5);
- yoffset = PG_GETARG_FLOAT8(6);
+ rt_pgraster *pgraster = NULL;
+ rt_pgraster *pgrtn = NULL;
+ rt_raster raster;
+ float8 imag, jmag, theta_i, theta_ij, xoffset, yoffset;
- raster = rt_raster_deserialize(pgraster, TRUE);
- if (!raster) {
- elog(ERROR, "RASTER_setGeotransform: Could not deserialize raster");
- PG_RETURN_NULL();
- }
+ if (
+ PG_ARGISNULL(0) || PG_ARGISNULL(1) || PG_ARGISNULL(2) ||
+ PG_ARGISNULL(3) || PG_ARGISNULL(4) ||
+ PG_ARGISNULL(5) || PG_ARGISNULL(6)
+ ) {
+ PG_RETURN_NULL();
+ }
- /* store the new geotransform */
- rt_raster_set_phys_params(raster, imag,jmag,theta_i,theta_ij) ;
- rt_raster_set_offsets(raster, xoffset, yoffset) ;
+ /* get the inputs */
+ pgraster = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
+ imag = PG_GETARG_FLOAT8(1);
+ jmag = PG_GETARG_FLOAT8(2);
+ theta_i = PG_GETARG_FLOAT8(3);
+ theta_ij = PG_GETARG_FLOAT8(4);
+ xoffset = PG_GETARG_FLOAT8(5);
+ yoffset = PG_GETARG_FLOAT8(6);
- /* prep the return value */
- pgraster = rt_raster_serialize(raster);
- if ( ! pgraster ) PG_RETURN_NULL();
+ raster = rt_raster_deserialize(pgraster, TRUE);
+ if (!raster) {
+ elog(ERROR, "RASTER_setGeotransform: Could not deserialize raster");
+ PG_FREE_IF_COPY(pgraster, 0);
+ PG_RETURN_NULL();
+ }
- SET_VARSIZE(pgraster, pgraster->size);
+ /* store the new geotransform */
+ rt_raster_set_phys_params(raster, imag,jmag,theta_i,theta_ij);
+ rt_raster_set_offsets(raster, xoffset, yoffset);
- rt_raster_destroy(raster);
+ /* prep the return value */
+ pgrtn = rt_raster_serialize(raster);
+ rt_raster_destroy(raster);
+ PG_FREE_IF_COPY(pgraster, 0);
+ if (!pgrtn)
+ PG_RETURN_NULL();
- PG_RETURN_POINTER(pgraster);
+ SET_VARSIZE(pgrtn, pgrtn->size);
+ PG_RETURN_POINTER(pgrtn);
}
-
/**
* Calculates the physically relevant parameters of the supplied raster's
* geotransform. Returns them as a set.
raster = rt_raster_deserialize(pgraster, TRUE);
if (!raster) {
elog(ERROR, "RASTER_getGeotransform: Could not deserialize raster");
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_NULL();
}
&imag, &jmag, &theta_i, &theta_ij) ;
rt_raster_destroy(raster);
+ PG_FREE_IF_COPY(pgraster, 0);
/* setup the return value infrastructure */
if (get_call_result_type(fcinfo, NULL, &result_tuple) != TYPEFUNC_COMPOSITE) {
PG_FUNCTION_INFO_V1(RASTER_setRotation);
Datum RASTER_setRotation(PG_FUNCTION_ARGS)
{
- rt_pgraster *pgraster = NULL;
- rt_raster raster;
- double rotation = PG_GETARG_FLOAT8(1);
- double imag, jmag, theta_i, theta_ij;
-
- if (PG_ARGISNULL(0)) PG_RETURN_NULL();
- pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
+ rt_pgraster *pgraster = NULL;
+ rt_pgraster *pgrtn = NULL;
+ rt_raster raster;
+ double rotation = PG_GETARG_FLOAT8(1);
+ double imag, jmag, theta_i, theta_ij;
- raster = rt_raster_deserialize(pgraster, FALSE);
- if (! raster ) {
- elog(ERROR, "RASTER_setRotation: Could not deserialize raster");
- PG_RETURN_NULL();
- }
+ if (PG_ARGISNULL(0))
+ PG_RETURN_NULL();
+ pgraster = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
- /* preserve all defining characteristics of the grid except for rotation */
- rt_raster_get_phys_params(raster, &imag, &jmag, &theta_i, &theta_ij);
- rt_raster_set_phys_params(raster, imag, jmag, rotation, theta_ij);
+ raster = rt_raster_deserialize(pgraster, FALSE);
+ if (! raster ) {
+ elog(ERROR, "RASTER_setRotation: Could not deserialize raster");
+ PG_FREE_IF_COPY(pgraster, 0);
+ PG_RETURN_NULL();
+ }
- pgraster = rt_raster_serialize(raster);
- rt_raster_destroy(raster);
- if ( ! pgraster ) PG_RETURN_NULL();
+ /* preserve all defining characteristics of the grid except for rotation */
+ rt_raster_get_phys_params(raster, &imag, &jmag, &theta_i, &theta_ij);
+ rt_raster_set_phys_params(raster, imag, jmag, rotation, theta_ij);
- SET_VARSIZE(pgraster, pgraster->size);
+ pgrtn = rt_raster_serialize(raster);
+ rt_raster_destroy(raster);
+ PG_FREE_IF_COPY(pgraster, 0);
+ if (!pgrtn)
+ PG_RETURN_NULL();
- PG_RETURN_POINTER(pgraster);
+ SET_VARSIZE(pgrtn, pgrtn->size);
+ PG_RETURN_POINTER(pgrtn);
}
/**
bandindex = PG_GETARG_INT32(1);
if ( bandindex < 1 ) {
elog(NOTICE, "Invalid band index (must use 1-based). Returning NULL");
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_NULL();
}
raster = rt_raster_deserialize(pgraster, FALSE);
if ( ! raster ) {
elog(ERROR, "RASTER_getBandPixelType: Could not deserialize raster");
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_NULL();
}
if ( ! band ) {
elog(NOTICE, "Could not find raster band of index %d when getting pixel type. Returning NULL", bandindex);
rt_raster_destroy(raster);
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_NULL();
}
pixtype = rt_band_get_pixtype(band);
rt_raster_destroy(raster);
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_INT32(pixtype);
}
bandindex = PG_GETARG_INT32(1);
if ( bandindex < 1 ) {
elog(NOTICE, "Invalid band index (must use 1-based). Returning NULL");
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_NULL();
}
raster = rt_raster_deserialize(pgraster, FALSE);
if ( ! raster ) {
elog(ERROR, "RASTER_getBandPixelTypeName: Could not deserialize raster");
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_NULL();
}
if ( ! band ) {
elog(NOTICE, "Could not find raster band of index %d when getting pixel type name. Returning NULL", bandindex);
rt_raster_destroy(raster);
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_NULL();
}
SET_VARSIZE(result, size);
rt_raster_destroy(raster);
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_TEXT_P(result);
}
bandindex = PG_GETARG_INT32(1);
if ( bandindex < 1 ) {
elog(NOTICE, "Invalid band index (must use 1-based). Returning NULL");
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_NULL();
}
raster = rt_raster_deserialize(pgraster, FALSE);
if ( ! raster ) {
elog(ERROR, "RASTER_getBandNoDataValue: Could not deserialize raster");
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_NULL();
}
if ( ! band ) {
elog(NOTICE, "Could not find raster band of index %d when getting band nodata value. Returning NULL", bandindex);
rt_raster_destroy(raster);
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_NULL();
}
if ( ! rt_band_get_hasnodata_flag(band) ) {
/* Raster does not have a nodata value set so we return NULL */
rt_raster_destroy(raster);
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_NULL();
}
nodata = rt_band_get_nodata(band);
rt_raster_destroy(raster);
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_FLOAT8(nodata);
}
PG_FUNCTION_INFO_V1(RASTER_setBandNoDataValue);
Datum RASTER_setBandNoDataValue(PG_FUNCTION_ARGS)
{
- rt_pgraster *pgraster = NULL;
- rt_raster raster = NULL;
- rt_band band = NULL;
- double nodata;
- int32_t bandindex;
- bool forcechecking = FALSE;
- bool skipset = FALSE;
-
- /* Deserialize raster */
- if (PG_ARGISNULL(0)) PG_RETURN_NULL();
- pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
-
- /* Check index is not NULL or smaller than 1 */
- if (PG_ARGISNULL(1))
- bandindex = -1;
- else
- bandindex = PG_GETARG_INT32(1);
- if (bandindex < 1) {
- elog(NOTICE, "Invalid band index (must use 1-based). Nodata value not set. Returning original raster");
- skipset = TRUE;
- }
+ rt_pgraster *pgraster = NULL;
+ rt_pgraster *pgrtn = NULL;
+ rt_raster raster = NULL;
+ rt_band band = NULL;
+ double nodata;
+ int32_t bandindex;
+ bool forcechecking = FALSE;
+ bool skipset = FALSE;
- raster = rt_raster_deserialize(pgraster, FALSE);
- if (! raster) {
- elog(ERROR, "RASTER_setBandNoDataValue: Could not deserialize raster");
- PG_RETURN_NULL();
- }
+ /* Deserialize raster */
+ if (PG_ARGISNULL(0))
+ PG_RETURN_NULL();
+ pgraster = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
- if (! skipset) {
- assert(0 <= (bandindex - 1));
- /* Fetch requested band */
- band = rt_raster_get_band(raster, bandindex - 1);
- if (! band) {
- elog(NOTICE, "Could not find raster band of index %d when setting pixel value. Nodata value not set. Returning original raster", bandindex);
- }
- else {
- if (!PG_ARGISNULL(3))
- forcechecking = PG_GETARG_BOOL(3);
+ /* Check index is not NULL or smaller than 1 */
+ if (PG_ARGISNULL(1))
+ bandindex = -1;
+ else
+ bandindex = PG_GETARG_INT32(1);
+ if (bandindex < 1) {
+ elog(NOTICE, "Invalid band index (must use 1-based). Nodata value not set. Returning original raster");
+ skipset = TRUE;
+ }
- if (PG_ARGISNULL(2)) {
- /* Set the hasnodata flag to FALSE */
- rt_band_set_hasnodata_flag(band, FALSE);
+ raster = rt_raster_deserialize(pgraster, FALSE);
+ if (!raster) {
+ elog(ERROR, "RASTER_setBandNoDataValue: Could not deserialize raster");
+ PG_FREE_IF_COPY(pgraster, 0);
+ PG_RETURN_NULL();
+ }
- POSTGIS_RT_DEBUGF(3, "Raster band %d does not have a nodata value",
- bandindex);
- }
- else {
+ if (!skipset) {
+ assert(0 <= (bandindex - 1));
- /* Get the nodata value */
- nodata = PG_GETARG_FLOAT8(2);
+ /* Fetch requested band */
+ band = rt_raster_get_band(raster, bandindex - 1);
+ if (!band) {
+ elog(NOTICE, "Could not find raster band of index %d when setting pixel value. Nodata value not set. Returning original raster", bandindex);
+ }
+ else {
+ if (!PG_ARGISNULL(3))
+ forcechecking = PG_GETARG_BOOL(3);
- /* Set the band's nodata value */
- rt_band_set_nodata(band, nodata);
+ if (PG_ARGISNULL(2)) {
+ /* Set the hasnodata flag to FALSE */
+ rt_band_set_hasnodata_flag(band, FALSE);
+ POSTGIS_RT_DEBUGF(3, "Raster band %d does not have a nodata value", bandindex);
+ }
+ else {
+ /* Get the nodata value */
+ nodata = PG_GETARG_FLOAT8(2);
- /* Set the hasnodata flag to TRUE */
- rt_band_set_hasnodata_flag(band, TRUE);
+ /* Set the band's nodata value */
+ rt_band_set_nodata(band, nodata);
- /* Recheck all pixels if requested */
- if (forcechecking)
- rt_band_check_is_nodata(band);
- }
- }
- }
+ /* Set the hasnodata flag to TRUE */
+ rt_band_set_hasnodata_flag(band, TRUE);
- pgraster = rt_raster_serialize(raster);
- rt_raster_destroy(raster);
- if (! pgraster) PG_RETURN_NULL();
+ /* Recheck all pixels if requested */
+ if (forcechecking)
+ rt_band_check_is_nodata(band);
+ }
+ }
+ }
- SET_VARSIZE(pgraster, pgraster->size);
+ pgrtn = rt_raster_serialize(raster);
+ rt_raster_destroy(raster);
+ PG_FREE_IF_COPY(pgraster, 0);
+ if (!pgrtn)
+ PG_RETURN_NULL();
- PG_RETURN_POINTER(pgraster);
+ SET_VARSIZE(pgrtn, pgrtn->size);
+ PG_RETURN_POINTER(pgrtn);
}
PG_FUNCTION_INFO_V1(RASTER_setBandIsNoData);
Datum RASTER_setBandIsNoData(PG_FUNCTION_ARGS)
{
- rt_pgraster *pgraster = NULL;
- rt_raster raster = NULL;
- rt_band band = NULL;
- int32_t bandindex;
-
- if (PG_ARGISNULL(0)) PG_RETURN_NULL();
- pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
-
- raster = rt_raster_deserialize(pgraster, FALSE);
- if ( ! raster ) {
- elog(ERROR, "RASTER_setBandIsNoData: Could not deserialize raster");
- PG_RETURN_NULL();
- }
+ rt_pgraster *pgraster = NULL;
+ rt_pgraster *pgrtn = NULL;
+ rt_raster raster = NULL;
+ rt_band band = NULL;
+ int32_t bandindex;
- /* Check index is not NULL or smaller than 1 */
- if (PG_ARGISNULL(1))
- bandindex = -1;
- else
- bandindex = PG_GETARG_INT32(1);
+ if (PG_ARGISNULL(0))
+ PG_RETURN_NULL();
+ pgraster = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
- if (bandindex < 1)
- elog(NOTICE, "Invalid band index (must use 1-based). Isnodata flag not set. Returning original raster");
- else {
+ raster = rt_raster_deserialize(pgraster, FALSE);
+ if (!raster) {
+ elog(ERROR, "RASTER_setBandIsNoData: Could not deserialize raster");
+ PG_FREE_IF_COPY(pgraster, 0);
+ PG_RETURN_NULL();
+ }
- /* Fetch requested band */
- band = rt_raster_get_band(raster, bandindex - 1);
+ /* Check index is not NULL or smaller than 1 */
+ if (PG_ARGISNULL(1))
+ bandindex = -1;
+ else
+ bandindex = PG_GETARG_INT32(1);
- if ( ! band )
- elog(NOTICE, "Could not find raster band of index %d. Isnodata flag not set. Returning original raster", bandindex);
- else
- /* Set the band's nodata value */
- rt_band_set_isnodata_flag(band, 1);
- }
+ if (bandindex < 1)
+ elog(NOTICE, "Invalid band index (must use 1-based). Isnodata flag not set. Returning original raster");
+ else {
+ /* Fetch requested band */
+ band = rt_raster_get_band(raster, bandindex - 1);
- /* Serialize raster again */
- pgraster = rt_raster_serialize(raster);
- rt_raster_destroy(raster);
- if ( ! pgraster ) PG_RETURN_NULL();
+ if (!band)
+ elog(NOTICE, "Could not find raster band of index %d. Isnodata flag not set. Returning original raster", bandindex);
+ else {
+ /* Set the band's nodata value */
+ rt_band_set_isnodata_flag(band, 1);
+ }
+ }
- SET_VARSIZE(pgraster, pgraster->size);
+ /* Serialize raster again */
+ pgrtn = rt_raster_serialize(raster);
+ rt_raster_destroy(raster);
+ PG_FREE_IF_COPY(pgraster, 0);
+ if (!pgrtn) PG_RETURN_NULL();
- PG_RETURN_POINTER(pgraster);
+ SET_VARSIZE(pgrtn, pgrtn->size);
+ PG_RETURN_POINTER(pgrtn);
}
PG_FUNCTION_INFO_V1(RASTER_bandIsNoData);
raster = rt_raster_deserialize(pgraster, FALSE);
if ( ! raster ) {
elog(ERROR, "RASTER_bandIsNoData: Could not deserialize raster");
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_NULL();
}
if ( ! band ) {
elog(NOTICE, "Could not find raster band of index %d when determining if band is nodata. Returning NULL", bandindex);
rt_raster_destroy(raster);
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_NULL();
}
rt_band_check_is_nodata(band) : rt_band_get_isnodata_flag(band);
rt_raster_destroy(raster);
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_BOOL(bandisnodata);
}
-
-
/**
* Return the path of the raster for out-db raster.
*/
raster = rt_raster_deserialize(pgraster, FALSE);
if ( ! raster ) {
elog(ERROR, "RASTER_getBandPath: Could not deserialize raster");
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_NULL();
}
if ( ! band ) {
elog(NOTICE, "Could not find raster band of index %d when getting band path. Returning NULL", bandindex);
rt_raster_destroy(raster);
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_NULL();
}
bandpath = rt_band_get_ext_path(band);
rt_band_destroy(band);
rt_raster_destroy(raster);
+ PG_FREE_IF_COPY(pgraster, 0);
if ( ! bandpath )
{
PG_RETURN_NULL();
raster = rt_raster_deserialize(pgraster, FALSE);
if (!raster) {
elog(ERROR, "RASTER_getPixelValue: Could not deserialize raster");
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_NULL();
}
elog(NOTICE, "Could not find raster band of index %d when getting pixel "
"value. Returning NULL", bandindex);
rt_raster_destroy(raster);
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_NULL();
}
/* Fetch pixel using 0-based coordinates */
if (result == -1 || (hasnodata && rt_band_get_hasnodata_flag(band) &&
pixvalue == rt_band_get_nodata(band))) {
rt_raster_destroy(raster);
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_NULL();
}
rt_raster_destroy(raster);
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_FLOAT8(pixvalue);
}
PG_FUNCTION_INFO_V1(RASTER_setPixelValue);
Datum RASTER_setPixelValue(PG_FUNCTION_ARGS)
{
- rt_pgraster *pgraster = NULL;
- rt_raster raster = NULL;
- rt_band band = NULL;
- double pixvalue = 0;
- int32_t bandindex = 0;
- int32_t x = 0;
- int32_t y = 0;
- bool skipset = FALSE;
-
- if (PG_ARGISNULL(0)) {
- PG_RETURN_NULL();
- }
+ rt_pgraster *pgraster = NULL;
+ rt_pgraster *pgrtn = NULL;
+ rt_raster raster = NULL;
+ rt_band band = NULL;
+ double pixvalue = 0;
+ int32_t bandindex = 0;
+ int32_t x = 0;
+ int32_t y = 0;
+ bool skipset = FALSE;
- /* Check index is not NULL or < 1 */
- if (PG_ARGISNULL(1))
- bandindex = -1;
- else
- bandindex = PG_GETARG_INT32(1);
- if ( bandindex < 1 ) {
- elog(NOTICE, "Invalid band index (must use 1-based). Value not set. "
- "Returning original raster");
- skipset = TRUE;
- }
+ if (PG_ARGISNULL(0))
+ PG_RETURN_NULL();
- /* Validate pixel coordinates are not null */
- if (PG_ARGISNULL(2)) {
- elog(NOTICE, "X coordinate can not be NULL when getting pixel value. "
- "Value not set. Returning original raster");
- skipset = TRUE;
- }
- else
- x = PG_GETARG_INT32(2);
+ /* Check index is not NULL or < 1 */
+ if (PG_ARGISNULL(1))
+ bandindex = -1;
+ else
+ bandindex = PG_GETARG_INT32(1);
+
+ if (bandindex < 1) {
+ elog(NOTICE, "Invalid band index (must use 1-based). Value not set. Returning original raster");
+ skipset = TRUE;
+ }
- if (PG_ARGISNULL(3)) {
- elog(NOTICE, "Y coordinate can not be NULL when getting pixel value. "
- "Value not set. Returning original raster");
- skipset = TRUE;
- }
- else
- y = PG_GETARG_INT32(3);
+ /* Validate pixel coordinates are not null */
+ if (PG_ARGISNULL(2)) {
+ elog(NOTICE, "X coordinate can not be NULL when getting pixel value. Value not set. Returning original raster");
+ skipset = TRUE;
+ }
+ else
+ x = PG_GETARG_INT32(2);
- POSTGIS_RT_DEBUGF(3, "Pixel coordinates (%d, %d)", x, y);
+ if (PG_ARGISNULL(3)) {
+ elog(NOTICE, "Y coordinate can not be NULL when getting pixel value. Value not set. Returning original raster");
+ skipset = TRUE;
+ }
+ else
+ y = PG_GETARG_INT32(3);
- /* Deserialize raster */
- pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
+ POSTGIS_RT_DEBUGF(3, "Pixel coordinates (%d, %d)", x, y);
- raster = rt_raster_deserialize(pgraster, FALSE);
- if ( ! raster ) {
- elog(ERROR, "RASTER_setPixelValue: Could not deserialize raster");
- PG_RETURN_NULL();
- }
+ /* Deserialize raster */
+ pgraster = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
- if (! skipset) {
- /* Fetch requested band */
- band = rt_raster_get_band(raster, bandindex - 1);
- if ( ! band ) {
- elog(NOTICE, "Could not find raster band of index %d when setting "
- "pixel value. Value not set. Returning original raster",
- bandindex);
- }
- else {
- /* Set the pixel value */
- if (PG_ARGISNULL(4)) {
- if (!rt_band_get_hasnodata_flag(band)) {
- elog(NOTICE, "Raster do not have a nodata value defined. "
- "Set band nodata value first. Nodata value not set. "
- "Returning original raster");
- }
- else {
- pixvalue = rt_band_get_nodata(band);
- rt_band_set_pixel(band, x - 1, y - 1, pixvalue);
- }
- }
- else {
- pixvalue = PG_GETARG_FLOAT8(4);
- rt_band_set_pixel(band, x - 1, y - 1, pixvalue);
- }
- }
- }
+ raster = rt_raster_deserialize(pgraster, FALSE);
+ if (!raster) {
+ elog(ERROR, "RASTER_setPixelValue: Could not deserialize raster");
+ PG_FREE_IF_COPY(pgraster, 0);
+ PG_RETURN_NULL();
+ }
- pgraster = rt_raster_serialize(raster);
- rt_raster_destroy(raster);
- if ( ! pgraster ) PG_RETURN_NULL();
+ if (!skipset) {
+ /* Fetch requested band */
+ band = rt_raster_get_band(raster, bandindex - 1);
+ if (!band) {
+ elog(NOTICE, "Could not find raster band of index %d when setting "
+ "pixel value. Value not set. Returning original raster",
+ bandindex);
+ }
+ else {
+ /* Set the pixel value */
+ if (PG_ARGISNULL(4)) {
+ if (!rt_band_get_hasnodata_flag(band)) {
+ elog(NOTICE, "Raster do not have a nodata value defined. "
+ "Set band nodata value first. Nodata value not set. "
+ "Returning original raster");
+ }
+ else {
+ pixvalue = rt_band_get_nodata(band);
+ rt_band_set_pixel(band, x - 1, y - 1, pixvalue);
+ }
+ }
+ else {
+ pixvalue = PG_GETARG_FLOAT8(4);
+ rt_band_set_pixel(band, x - 1, y - 1, pixvalue);
+ }
+ }
+ }
- SET_VARSIZE(pgraster, pgraster->size);
+ pgrtn = rt_raster_serialize(raster);
+ rt_raster_destroy(raster);
+ PG_FREE_IF_COPY(pgraster, 0);
+ if (!pgrtn)
+ PG_RETURN_NULL();
- PG_RETURN_POINTER(pgraster);
+ SET_VARSIZE(pgrtn, pgrtn->size);
+ PG_RETURN_POINTER(pgrtn);
}
/**
raster = rt_raster_deserialize(pgraster, FALSE);
if (!raster) {
elog(ERROR, "RASTER_getPixelPolygon: Could not deserialize raster");
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_NULL();
}
if (!poly) {
elog(ERROR, "RASTER_getPixelPolygon: could not get raster's pixel polygon");
rt_raster_destroy(raster);
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_NULL();
}
}
rt_raster_destroy(raster);
- lwfree(poly);
+ PG_FREE_IF_COPY(pgraster, 0);
+ pfree(poly);
PG_RETURN_POINTER(gser);
}
raster = rt_raster_deserialize(pgraster, FALSE);
if (!raster) {
elog(ERROR, "RASTER_nearestValue: Could not deserialize raster");
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_NULL();
}
if (bandindex < 1 || bandindex > num_bands) {
elog(NOTICE, "Invalid band index (must use 1-based). Returning NULL");
rt_raster_destroy(raster);
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_NULL();
}
if (gserialized_get_type(geom) != POINTTYPE) {
elog(NOTICE, "Geometry provided must be a point");
rt_raster_destroy(raster);
+ PG_FREE_IF_COPY(pgraster, 0);
+ PG_FREE_IF_COPY(geom, 2);
PG_RETURN_NULL();
}
if (gserialized_get_srid(geom) != rt_raster_get_srid(raster)) {
elog(NOTICE, "SRIDs of geometry and raster do not match");
rt_raster_destroy(raster);
+ PG_FREE_IF_COPY(pgraster, 0);
+ PG_FREE_IF_COPY(geom, 2);
PG_RETURN_NULL();
}
if (!band) {
elog(NOTICE, "Could not find band at index %d. Returning NULL", bandindex);
rt_raster_destroy(raster);
+ PG_FREE_IF_COPY(pgraster, 0);
+ PG_FREE_IF_COPY(geom, 2);
PG_RETURN_NULL();
}
if (lwgeom_is_empty(lwgeom)) {
elog(NOTICE, "Geometry provided cannot be empty");
rt_raster_destroy(raster);
+ PG_FREE_IF_COPY(pgraster, 0);
+ PG_FREE_IF_COPY(geom, 2);
PG_RETURN_NULL();
}
)) {
elog(ERROR, "RASTER_nearestValue: Unable to compute pixel coordinates from spatial coordinates");
rt_raster_destroy(raster);
+ PG_FREE_IF_COPY(pgraster, 0);
lwgeom_free(lwgeom);
+ PG_FREE_IF_COPY(geom, 2);
PG_RETURN_NULL();
}
if (rt_band_get_pixel(band, x, y, &value) < 0) {
elog(ERROR, "RASTER_nearestValue: Unable to get pixel value for band at index %d", bandindex);
rt_raster_destroy(raster);
+ PG_FREE_IF_COPY(pgraster, 0);
lwgeom_free(lwgeom);
+ PG_FREE_IF_COPY(geom, 2);
PG_RETURN_NULL();
}
)
) {
rt_raster_destroy(raster);
+ PG_FREE_IF_COPY(pgraster, 0);
lwgeom_free(lwgeom);
+ PG_FREE_IF_COPY(geom, 2);
PG_RETURN_FLOAT8(value);
}
elog(NOTICE, "No nearest value found for band at index %d", bandindex);
lwgeom_free(lwgeom);
+ PG_FREE_IF_COPY(geom, 2);
rt_raster_destroy(raster);
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_NULL();
}
if (!poly) {
elog(ERROR, "RASTER_nearestValue: Unable to get polygon of neighboring pixel");
lwgeom_free(lwgeom);
+ PG_FREE_IF_COPY(geom, 2);
rt_raster_destroy(raster);
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_NULL();
}
pfree(npixels);
lwgeom_free(lwgeom);
+ PG_FREE_IF_COPY(geom, 2);
rt_raster_destroy(raster);
+ PG_FREE_IF_COPY(pgraster, 0);
if (hasvalue)
PG_RETURN_FLOAT8(value);
raster = rt_raster_deserialize(pgraster, FALSE);
if (!raster) {
elog(ERROR, "RASTER_neighborhood: Could not deserialize raster");
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_NULL();
}
if (bandindex < 1 || bandindex > num_bands) {
elog(NOTICE, "Invalid band index (must use 1-based). Returning NULL");
rt_raster_destroy(raster);
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_NULL();
}
if (distance < 1) {
elog(NOTICE, "Invalid value for distance (must be greater than zero). Returning NULL");
rt_raster_destroy(raster);
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_NULL();
}
distance = (uint16_t) distance;
if (!band) {
elog(NOTICE, "Could not find band at index %d. Returning NULL", bandindex);
rt_raster_destroy(raster);
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_NULL();
}
rt_band_destroy(band);
rt_raster_destroy(raster);
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_NULL();
}
elog(NOTICE, "Unable to get the pixel of band at index %d. Returning NULL", bandindex);
rt_band_destroy(band);
rt_raster_destroy(raster);
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_NULL();
}
}
rt_band_destroy(band);
rt_raster_destroy(raster);
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_NULL();
}
/* free unnecessary stuff */
rt_band_destroy(band);
rt_raster_destroy(raster);
+ PG_FREE_IF_COPY(pgraster, 0);
/* convert set of rt_pixel to 2D array */
length = rt_pixel_set_to_array(
PG_FUNCTION_INFO_V1(RASTER_addband);
Datum RASTER_addband(PG_FUNCTION_ARGS)
{
- rt_pgraster *pgraster = NULL;
- rt_raster raster = NULL;
+ rt_pgraster *pgraster = NULL;
+ rt_pgraster *pgrtn = NULL;
+ rt_raster raster = NULL;
- int bandindex = 0;
- double initialvalue = 0;
- double nodatavalue = 0;
- bool hasnodata = FALSE;
- bool skipaddband = FALSE;
+ int bandindex = 0;
+ double initialvalue = 0;
+ double nodatavalue = 0;
+ bool hasnodata = FALSE;
+ bool skipaddband = FALSE;
- text *pixeltypename = NULL;
- char *new_pixeltypename = NULL;
+ text *pixeltypename = NULL;
+ char *new_pixeltypename = NULL;
- rt_pixtype pixtype = PT_END;
- int32_t oldnumbands = 0;
- int32_t numbands = 0;
+ rt_pixtype pixtype = PT_END;
+ int32_t oldnumbands = 0;
+ int32_t numbands = 0;
- /* Get the initial pixel value */
- if (PG_ARGISNULL(3))
- initialvalue = 0;
- else
- initialvalue = PG_GETARG_FLOAT8(3);
+ /* Get the initial pixel value */
+ if (PG_ARGISNULL(3))
+ initialvalue = 0;
+ else
+ initialvalue = PG_GETARG_FLOAT8(3);
- /* Get the nodata value */
- if (PG_ARGISNULL(4))
- nodatavalue = 0;
- else
- {
- nodatavalue = PG_GETARG_FLOAT8(4);
- hasnodata = TRUE;
- }
+ /* Get the nodata value */
+ if (PG_ARGISNULL(4))
+ nodatavalue = 0;
+ else {
+ nodatavalue = PG_GETARG_FLOAT8(4);
+ hasnodata = TRUE;
+ }
- /* Deserialize raster */
- if (PG_ARGISNULL(0)) PG_RETURN_NULL();
- pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
+ /* Deserialize raster */
+ if (PG_ARGISNULL(0))
+ PG_RETURN_NULL();
+ pgraster = (rt_pgraster *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
- /* Get the pixel type in text form */
- if (PG_ARGISNULL(2)) {
- elog(ERROR, "RASTER_addband: Pixel type can not be NULL");
- PG_RETURN_NULL();
- }
+ /* Get the pixel type in text form */
+ if (PG_ARGISNULL(2)) {
+ elog(ERROR, "RASTER_addband: Pixel type can not be NULL");
+ PG_FREE_IF_COPY(pgraster, 0);
+ PG_RETURN_NULL();
+ }
- pixeltypename = PG_GETARG_TEXT_P(2);
- new_pixeltypename = text_to_cstring(pixeltypename);
+ pixeltypename = PG_GETARG_TEXT_P(2);
+ new_pixeltypename = text_to_cstring(pixeltypename);
- /* Get the pixel type index */
- pixtype = rt_pixtype_index_from_name(new_pixeltypename);
- if ( pixtype == PT_END ) {
- elog(ERROR, "RASTER_addband: Invalid pixel type: %s", new_pixeltypename);
- PG_RETURN_NULL();
- }
-
- raster = rt_raster_deserialize(pgraster, FALSE);
- if ( ! raster ) {
- elog(ERROR, "RASTER_addband: Could not deserialize raster");
- PG_RETURN_NULL();
- }
+ /* Get the pixel type index */
+ pixtype = rt_pixtype_index_from_name(new_pixeltypename);
+ if (pixtype == PT_END) {
+ elog(ERROR, "RASTER_addband: Invalid pixel type: %s", new_pixeltypename);
+ PG_FREE_IF_COPY(pgraster, 0);
+ PG_RETURN_NULL();
+ }
- /* Make sure index (1 based) is in a valid range */
- oldnumbands = rt_raster_get_num_bands(raster);
- if (PG_ARGISNULL(1))
- bandindex = oldnumbands + 1;
- else
- {
- bandindex = PG_GETARG_INT32(1);
- if (bandindex < 1) {
- elog(NOTICE, "Invalid band index (must use 1-based). Band not added. "
- "Returning original raster");
- skipaddband = TRUE;
- }
- if (bandindex > oldnumbands + 1) {
- elog(NOTICE, "RASTER_addband: Band index number exceed possible "
- "values, truncated to number of band (%u) + 1", oldnumbands);
- bandindex = oldnumbands + 1;
- }
- }
+ raster = rt_raster_deserialize(pgraster, FALSE);
+ if (!raster) {
+ elog(ERROR, "RASTER_addband: Could not deserialize raster");
+ PG_FREE_IF_COPY(pgraster, 0);
+ PG_RETURN_NULL();
+ }
- if (!skipaddband) {
- bandindex = rt_raster_generate_new_band(raster, pixtype, initialvalue,
- hasnodata, nodatavalue,
- bandindex - 1);
+ /* Make sure index (1 based) is in a valid range */
+ oldnumbands = rt_raster_get_num_bands(raster);
+ if (PG_ARGISNULL(1))
+ bandindex = oldnumbands + 1;
+ else {
+ bandindex = PG_GETARG_INT32(1);
+ if (bandindex < 1) {
+ elog(NOTICE, "Invalid band index (must use 1-based). Band not added. Returning original raster");
+ skipaddband = TRUE;
+ }
+ if (bandindex > oldnumbands + 1) {
+ elog(NOTICE, "RASTER_addband: Band index number exceed possible values, truncated to number of band (%u) + 1", oldnumbands);
+ bandindex = oldnumbands + 1;
+ }
+ }
- numbands = rt_raster_get_num_bands(raster);
- if (numbands == oldnumbands || bandindex == -1) {
- elog(ERROR, "RASTER_addband: Could not add band to raster. "
- "Returning NULL");
- rt_raster_destroy(raster);
- PG_RETURN_NULL();
- }
- }
+ if (!skipaddband) {
+ bandindex = rt_raster_generate_new_band(
+ raster,
+ pixtype, initialvalue,
+ hasnodata, nodatavalue,
+ bandindex - 1
+ );
- pgraster = rt_raster_serialize(raster);
- rt_raster_destroy(raster);
- if (!pgraster) PG_RETURN_NULL();
+ numbands = rt_raster_get_num_bands(raster);
+ if (numbands == oldnumbands || bandindex == -1) {
+ elog(ERROR, "RASTER_addband: Could not add band to raster. Returning NULL");
+ rt_raster_destroy(raster);
+ PG_FREE_IF_COPY(pgraster, 0);
+ PG_RETURN_NULL();
+ }
+ }
- SET_VARSIZE(pgraster, pgraster->size);
+ pgrtn = rt_raster_serialize(raster);
+ rt_raster_destroy(raster);
+ PG_FREE_IF_COPY(pgraster, 0);
+ if (!pgrtn)
+ PG_RETURN_NULL();
- PG_RETURN_POINTER(pgraster);
+ SET_VARSIZE(pgrtn, pgrtn->size);
+ PG_RETURN_POINTER(pgrtn);
}
-
/**
* Copy a band from one raster to another one at the given position.
*/
PG_FUNCTION_INFO_V1(RASTER_copyband);
Datum RASTER_copyband(PG_FUNCTION_ARGS)
{
- rt_pgraster *pgraster = NULL;
+ rt_pgraster *pgto = NULL;
+ rt_pgraster *pgfrom = NULL;
+ rt_pgraster *pgrtn = NULL;
rt_raster torast = NULL;
rt_raster fromrast = NULL;
int toindex = 0;
/* Deserialize torast */
if (PG_ARGISNULL(0)) PG_RETURN_NULL();
- pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
+ pgto = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
- torast = rt_raster_deserialize(pgraster, FALSE);
+ torast = rt_raster_deserialize(pgto, FALSE);
if (!torast) {
elog(ERROR, "RASTER_copyband: Could not deserialize first raster");
+ PG_FREE_IF_COPY(pgto, 0);
PG_RETURN_NULL();
}
/* Deserialize fromrast */
if (!PG_ARGISNULL(1)) {
- pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(1));
+ pgfrom = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(1));
- fromrast = rt_raster_deserialize(pgraster, FALSE);
+ fromrast = rt_raster_deserialize(pgfrom, FALSE);
if (!fromrast) {
elog(ERROR, "RASTER_copyband: Could not deserialize second raster");
rt_raster_destroy(torast);
+ PG_FREE_IF_COPY(pgfrom, 1);
+ PG_FREE_IF_COPY(pgto, 0);
PG_RETURN_NULL();
}
}
rt_raster_destroy(fromrast);
+ PG_FREE_IF_COPY(pgfrom, 1);
}
/* Serialize and return torast */
- pgraster = rt_raster_serialize(torast);
+ pgrtn = rt_raster_serialize(torast);
rt_raster_destroy(torast);
- if (!pgraster) PG_RETURN_NULL();
+ PG_FREE_IF_COPY(pgto, 0);
+ if (!pgrtn) PG_RETURN_NULL();
- SET_VARSIZE(pgraster, pgraster->size);
- PG_RETURN_POINTER(pgraster);
+ SET_VARSIZE(pgrtn, pgrtn->size);
+ PG_RETURN_POINTER(pgrtn);
}
/**
ereport(ERROR,
(errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("RASTER_isEmpty: Could not deserialize raster")));
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_NULL();
}
isempty = rt_raster_is_empty(raster);
rt_raster_destroy(raster);
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_BOOL(isempty);
}
ereport(ERROR,
(errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("RASTER_hasNoBand: Could not deserialize raster")));
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_NULL();
}
hasnoband = rt_raster_has_no_band(raster, bandindex - 1);
rt_raster_destroy(raster);
+ PG_FREE_IF_COPY(pgraster, 0);
PG_RETURN_BOOL(hasnoband);
}
+/*
+ TODO: continue working on adding PG_FREE_IF_COPY from HERE
+*/
+
PG_FUNCTION_INFO_V1(RASTER_mapAlgebraExpr);
Datum RASTER_mapAlgebraExpr(PG_FUNCTION_ARGS)
{
else {
strFromText = text_to_cstring(PG_GETARG_TEXT_P(2));
newpixeltype = rt_pixtype_index_from_name(strFromText);
- lwfree(strFromText);
+ pfree(strFromText);
if (newpixeltype == PT_END)
newpixeltype = rt_band_get_pixtype(band);
}
/* We don't need this memory */
/*
- lwfree(expression);
+ pfree(expression);
expression = NULL;
*/
}
else {
strFromText = text_to_cstring(PG_GETARG_TEXT_P(2));
newpixeltype = rt_pixtype_index_from_name(strFromText);
- lwfree(strFromText);
+ pfree(strFromText);
if (newpixeltype == PT_END)
newpixeltype = rt_band_get_pixtype(band);
}