PG_FUNCTION_INFO_V1(RASTER_out);
Datum RASTER_out(PG_FUNCTION_ARGS)
{
+ elog(WARNING, "RASTER_out: Starting...");
rt_pgraster *pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
rt_raster raster = NULL;
uint32_t hexwkbsize = 0;
TupleDesc tupdesc;
SPITupleTable * tuptable = NULL;
HeapTuple tuple;
+ char * strFromText = NULL;
POSTGIS_RT_DEBUG(2, "RASTER_mapAlgebra: Starting...");
/* Deserialize raster */
pgraster = (rt_pgraster *)PG_DETOAST_DATUM_COPY(PG_GETARG_DATUM(0));
ctx = get_rt_context(fcinfo);
+ if (!ctx)
+ {
+ ereport(ERROR,
+ (errcode(ERRCODE_OUT_OF_MEMORY),
+ errmsg("RASTER_mapAlgebra: Could not deserialize raster")));
+ PG_RETURN_NULL();
+ }
raster = rt_raster_deserialize(ctx, pgraster);
- if ( ! raster )
+ if (!raster)
{
ereport(ERROR,
(errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("RASTER_mapAlgebra: Could not deserialize raster")));
+ rt_context_destroy(ctx);
PG_RETURN_NULL();
}
width = rt_raster_get_width(ctx, raster);
height = rt_raster_get_height(ctx, raster);
- newrast = rt_raster_new(ctx,
- rt_raster_get_width(ctx, raster),
- rt_raster_get_height(ctx, raster));
+ newrast = rt_raster_new(ctx, width, height);
if ( ! newrast ) {
elog(ERROR, "RASTER_mapAlgebra: Could not create a new raster");
if (!pgraster) PG_RETURN_NULL();
SET_VARSIZE(pgraster, pgraster->size);
+
+ rt_raster_destroy(ctx, raster);
+ rt_context_destroy(ctx);
PG_RETURN_POINTER(pgraster);
}
if (!pgraster) PG_RETURN_NULL();
SET_VARSIZE(pgraster, pgraster->size);
+
+ rt_raster_destroy(ctx, raster);
+ rt_context_destroy(ctx);
PG_RETURN_POINTER(pgraster);
}
band = rt_raster_get_band(ctx, raster, nband - 1);
if ( ! band ) {
elog(ERROR, "RASTER_mapAlgebra: Could not get band %d for raster", nband);
+ rt_raster_destroy(ctx, raster);
+ rt_context_destroy(ctx);
PG_RETURN_NULL();
}
}
else {
- newpixeltype = rt_pixtype_index_from_name(ctx,
- text_to_cstring(PG_GETARG_TEXT_P(4)));
+ strFromText = text_to_cstring(PG_GETARG_TEXT_P(4));
+ newpixeltype = rt_pixtype_index_from_name(ctx,strFromText);
+ lwfree(strFromText);
if (newpixeltype == PT_END)
newpixeltype = rt_band_get_pixtype(ctx, band);
}
if (newpixeltype == PT_END) {
elog(ERROR, "RASTER_mapAlgebra: Invalid pixeltype. Aborting");
+
+ rt_raster_destroy(ctx, raster);
+ rt_context_destroy(ctx);
+
PG_RETURN_NULL();
}
strncpy(initexpr, "SELECT ", strlen("SELECT "));
strncpy(initexpr + strlen("SELECT "), strtoupper(expression),
strlen(expression));
- initexpr[len] = '\0';
+ initexpr[len] = '\0';
POSTGIS_RT_DEBUGF(3, "RASTER_mapAlgebra: Expression is %s", initexpr);
if (ret != SPI_OK_SELECT || SPI_tuptable == NULL || SPI_processed != 1) {
elog(ERROR, "RASTER_mapAlgebra: Invalid construction for nodata "
"expression. Aborting");
- SPI_finish();
+
+ /* Free memory allocated out of the current context */
+ if (expression)
+ lwfree(expression);
+ if (nodatavalueexpr)
+ lwfree(nodatavalueexpr);
+ rt_raster_destroy(ctx, raster);
+ rt_context_destroy(ctx);
+
+ if (SPI_tuptable)
+ SPI_freetuptable(tuptable);
+
+ /* TODO: make postgres to crash when dealing with BIG rasters */
+ //SPI_finish();
PG_RETURN_NULL();
}
tuple = tuptable->vals[0];
newinitialvalue = atof(SPI_getvalue(tuple, tupdesc, 1));
+ SPI_freetuptable(tuptable);
+
POSTGIS_RT_DEBUGF(3, "RASTER_mapAlgebra: new initial value = %f",
newinitialvalue);
SET_VARSIZE(pgraster, pgraster->size);
+ /* Free memory allocated out of the current context */
+ if (expression)
+ lwfree(expression);
+ if (nodatavalueexpr)
+ lwfree(nodatavalueexpr);
+ rt_raster_destroy(ctx, raster);
+ rt_context_destroy(ctx);
+
/* Disconnect function from SPI manager */
- SPI_finish();
+ /* TODO: make postgres to crash when dealing with BIG rasters */
+ //SPI_finish();
PG_RETURN_POINTER(pgraster);
}
/* Serialize created raster */
pgraster = rt_raster_serialize(ctx, newrast);
- if (!pgraster) PG_RETURN_NULL();
+ if (!pgraster)
+ {
+ /* Free memory allocated out of the current context */
+ if (expression)
+ lwfree(expression);
+ if (nodatavalueexpr)
+ lwfree(nodatavalueexpr);
+ rt_raster_destroy(ctx, raster);
+ rt_context_destroy(ctx);
+
+ PG_RETURN_NULL();
+ }
SET_VARSIZE(pgraster, pgraster->size);
+ /* Free memory allocated out of the current context */
+ if (expression)
+ lwfree(expression);
+ if (nodatavalueexpr)
+ lwfree(nodatavalueexpr);
+ rt_raster_destroy(ctx, raster);
+ rt_context_destroy(ctx);
+
/* Disconnect function from SPI manager */
- SPI_finish();
+ /* TODO: make postgres to crash when dealing with BIG rasters */
+ //SPI_finish();
PG_RETURN_POINTER(pgraster);
}
if (ret != SPI_OK_SELECT || SPI_tuptable == NULL || SPI_processed != 1) {
elog(ERROR, "RASTER_mapAlgebra: Invalid construction for expression. Aborting");
- SPI_finish();
+
+ /* Free memory allocated out of the current context */
+ if (expression)
+ lwfree(expression);
+ if (nodatavalueexpr)
+ lwfree(nodatavalueexpr);
+ rt_raster_destroy(ctx, raster);
+ rt_context_destroy(ctx);
+
+ if (SPI_tuptable)
+ SPI_freetuptable(tuptable);
+
+ /* TODO: make postgres to crash when dealing with BIG rasters */
+ //SPI_finish();
PG_RETURN_NULL();
}
tuple = tuptable->vals[0];
newval = atof(SPI_getvalue(tuple, tupdesc, 1));
+ SPI_freetuptable(tuptable);
+
POSTGIS_RT_DEBUGF(3, "RASTER_mapAlgebra: New raster value = %f",
newval);
if (expression == NULL || skipcomputation == 2) {
/* Serialize created raster */
pgraster = rt_raster_serialize(ctx, newrast);
- if (!pgraster) PG_RETURN_NULL();
+ if (!pgraster)
+ {
+ /* Free memory allocated out of the current context */
+ if (expression)
+ lwfree(expression);
+ if (nodatavalueexpr)
+ lwfree(nodatavalueexpr);
+ rt_raster_destroy(ctx, raster);
+ rt_context_destroy(ctx);
+
+ PG_RETURN_NULL();
+ }
SET_VARSIZE(pgraster, pgraster->size);
+ /* Free memory allocated out of the current context */
+ if (expression)
+ lwfree(expression);
+ if (nodatavalueexpr)
+ lwfree(nodatavalueexpr);
+ rt_raster_destroy(ctx, raster);
+ rt_context_destroy(ctx);
+
/* Disconnect function from SPI manager */
- SPI_finish();
+ /* TODO: make postgres to crash when dealing with BIG rasters */
+ //SPI_finish();
PG_RETURN_POINTER(pgraster);
}
/* Serialize created raster */
pgraster = rt_raster_serialize(ctx, newrast);
- if (!pgraster) PG_RETURN_NULL();
+ if (!pgraster)
+ {
+ /* Free memory allocated out of the current context */
+ if (expression)
+ lwfree(expression);
+ if (nodatavalueexpr)
+ lwfree(nodatavalueexpr);
+ rt_raster_destroy(ctx, raster);
+ rt_context_destroy(ctx);
+
+ PG_RETURN_NULL();
+ }
SET_VARSIZE(pgraster, pgraster->size);
+ /* Free memory allocated out of the current context */
+ if (expression)
+ lwfree(expression);
+ if (nodatavalueexpr)
+ lwfree(nodatavalueexpr);
+ rt_raster_destroy(ctx, raster);
+ rt_context_destroy(ctx);
+
/* Disconnect function from SPI manager */
- SPI_finish();
+ /* TODO: make postgres to crash when dealing with BIG rasters */
+ //SPI_finish();
PG_RETURN_POINTER(pgraster);
}
if (ret != SPI_OK_SELECT || SPI_tuptable == NULL || SPI_processed != 1) {
elog(ERROR, "RASTER_mapAlgebra: Invalid construction for expression. Aborting");
- SPI_finish();
+
+ /* Free memory allocated out of the current context */
+ if (expression)
+ lwfree(expression);
+ if (nodatavalueexpr)
+ lwfree(nodatavalueexpr);
+ rt_raster_destroy(ctx, raster);
+ rt_context_destroy(ctx);
+
+ if (SPI_tuptable)
+ SPI_freetuptable(tuptable);
+
+ /* TODO: make postgres to crash when dealing with BIG rasters */
+ //SPI_finish();
PG_RETURN_NULL();
}
tuple = tuptable->vals[0];
newval = atof(SPI_getvalue(tuple, tupdesc, 1));
+
+ SPI_freetuptable(tuptable);
}
else
/* The newrast band has been modified */
+ POSTGIS_RT_DEBUG(3, "RASTER_mapAlgebra: raster modified, serializing it.");
/* Serialize created raster */
pgraster = rt_raster_serialize(ctx, newrast);
- if (!pgraster) PG_RETURN_NULL();
+ if (!pgraster)
+ {
+ /* Free memory allocated out of the current context */
+ if (expression)
+ lwfree(expression);
+ if (nodatavalueexpr)
+ lwfree(nodatavalueexpr);
+ rt_raster_destroy(ctx, raster);
+ rt_context_destroy(ctx);
+
+ PG_RETURN_NULL();
+ }
SET_VARSIZE(pgraster, pgraster->size);
+ POSTGIS_RT_DEBUG(3, "RASTER_mapAlgebra: raster serialized");
+
+ /* Free memory allocated out of the current context */
+ if (expression)
+ lwfree(expression);
+ if (nodatavalueexpr)
+ lwfree(nodatavalueexpr);
+ rt_raster_destroy(ctx, raster);
+ rt_context_destroy(ctx);
+
/* Disconnect function from SPI manager */
- SPI_finish();
+ /* TODO: make postgres to crash when dealing with BIG rasters */
+ //SPI_finish();
+
+
+ POSTGIS_RT_DEBUG(4, "RASTER_mapAlgebra: returning raster")
PG_RETURN_POINTER(pgraster);
}