/* entire band is nodata */
if (rt_band_get_isnodata_flag(band) != FALSE) {
+ stats = (rt_bandstats) rtalloc(sizeof(struct rt_bandstats_t));
+ if (NULL == stats) {
+ rterror("rt_band_get_summary_stats: Unable to allocate memory for stats");
+ return NULL;
+ }
+
+ stats->sample = 1;
+ stats->sorted = 0;
+ stats->values = NULL;
+
if (exclude_nodata_value) {
rtwarn("All pixels of band have the NODATA value");
- return NULL;
+
+ stats->count = 0;
+ stats->min = stats->max = 0;
+ stats->sum = 0;
+ stats->mean = 0;
+ stats->stddev = -1;
}
else {
- stats = (rt_bandstats) rtalloc(sizeof(struct rt_bandstats_t));
- if (NULL == stats) {
- rterror("rt_band_get_summary_stats: Unable to allocate memory for stats");
- return NULL;
- }
-
- stats->sample = 1;
stats->count = band->width * band->height;
stats->min = stats->max = nodata;
stats->sum = stats->count * nodata;
stats->mean = nodata;
stats->stddev = 0;
- stats->values = NULL;
- stats->sorted = 0;
-
- return stats;
}
+
+ return stats;
}
/* clamp percentage */
}
}
+ /* initialize stats */
+ stats = (rt_bandstats) rtalloc(sizeof(struct rt_bandstats_t));
+ if (NULL == stats) {
+ rterror("rt_band_get_summary_stats: Unable to allocate memory for stats");
+ return NULL;
+ }
+ stats->sample = sample;
+ stats->count = 0;
+ stats->sum = 0;
+ stats->mean = 0;
+ stats->stddev = -1;
+ stats->min = stats->max = 0;
+ stats->values = NULL;
+ stats->sorted = 0;
+
for (x = 0, j = 0, k = 0; x < band->width; x++) {
y = -1;
diff = 0;
if (y >= band->height || z > sample_per) break;
rtn = rt_band_get_pixel(band, x, y, &value);
+#if POSTGIS_DEBUG_LEVEL > 0
+ if (rtn != -1)
+ RASTER_DEBUGF(4, "(x, y, value) = (%d,%d, %f)", x, y, value);
+#endif
j++;
if (
}
/* min/max */
- if (NULL == stats) {
- stats = (rt_bandstats) rtalloc(sizeof(struct rt_bandstats_t));
- if (NULL == stats) {
- rterror("rt_band_get_summary_stats: Unable to allocate memory for stats");
- return NULL;
- }
-
- stats->sample = sample;
- stats->count = 0;
-
- stats->sum = 0;
- stats->mean = 0;
- stats->stddev = -1;
+ if (stats->count < 1) {
+ stats->count = 1;
stats->min = stats->max = value;
-
- stats->values = NULL;
- stats->sorted = 0;
-
}
else {
if (value < stats->min)
RASTER_DEBUG(3, "sampling complete");
+ stats->count = k;
if (k > 0) {
if (inc_vals) {
/* free unused memory */
stats->values = values;
}
- stats->count = k;
stats->sum = sum;
stats->mean = sum / k;
}
}
/* inc_vals thus values allocated but not used */
- else if (inc_vals) {
+ else if (inc_vals)
rtdealloc(values);
- }
+
+ /* if count is zero and do_sample is one */
+ if (k < 0 && do_sample)
+ rtwarn("All sampled pixels of band have the NODATA value");
#if POSTGIS_DEBUG_LEVEL > 0
- if (NULL != stats) {
- stop = clock();
- elapsed = ((double) (stop - start)) / CLOCKS_PER_SEC;
- RASTER_DEBUGF(3, "(time, count, mean, stddev, min, max) = (%0.4f, %d, %f, %f, %f, %f)",
- elapsed, stats->count, stats->mean, stats->stddev, stats->min, stats->max);
- }
+ stop = clock();
+ elapsed = ((double) (stop - start)) / CLOCKS_PER_SEC;
+ RASTER_DEBUGF(3, "(time, count, mean, stddev, min, max) = (%0.4f, %d, %f, %f, %f, %f)",
+ elapsed, stats->count, stats->mean, stats->stddev, stats->min, stats->max);
#endif
RASTER_DEBUG(3, "done");
/* get band */
band = rt_raster_get_band(raster, bandindex - 1);
if (!band) {
- elog(NOTICE, "Could not find raster band of index %d. Returning NULL", bandindex);
+ elog(NOTICE, "Could not find band at index %d. Returning NULL", bandindex);
rt_raster_destroy(raster);
PG_RETURN_NULL();
}
rt_raster_destroy(raster);
PG_FREE_IF_COPY(pgraster, 0);
if (NULL == stats) {
- elog(NOTICE, "Could not retrieve summary statistics of band of index %d. Returning NULL", bandindex);
+ elog(NOTICE, "Unable to compute summary statistics for band at index %d. Returning NULL", bandindex);
PG_RETURN_NULL();
}
/* get band */
band = rt_raster_get_band(raster, bandindex - 1);
if (!band) {
- elog(NOTICE, "Could not find raster band of index %d. Returning NULL", bandindex);
+ elog(NOTICE, "Could not find band at index %d. Returning NULL", bandindex);
if (SPI_tuptable) SPI_freetuptable(tuptable);
SPI_cursor_close(portal);
rt_raster_destroy(raster);
if (NULL == stats) {
- elog(NOTICE, "Could not retrieve summary statistics of band of index %d. Returning NULL", bandindex);
+ elog(NOTICE, "Unable to compute summary statistics for band at index %d. Returning NULL", bandindex);
if (SPI_tuptable) SPI_freetuptable(tuptable);
SPI_cursor_close(portal);
}
/* initialize rtn */
- if (NULL == rtn) {
- rtn = (rt_bandstats) palloc(sizeof(struct rt_bandstats_t));
+ if (stats->count > 0) {
if (NULL == rtn) {
- elog(ERROR, "RASTER_summaryStatsCoverage: Unable to allocate memory for summary stats\n");
-
- if (SPI_tuptable) SPI_freetuptable(tuptable);
- SPI_cursor_close(portal);
- SPI_finish();
-
- PG_RETURN_NULL();
- }
+ rtn = (rt_bandstats) palloc(sizeof(struct rt_bandstats_t));
+ if (NULL == rtn) {
+ elog(ERROR, "RASTER_summaryStatsCoverage: Unable to allocate memory for summary stats of coverage\n");
- rtn->sample = stats->sample;
- rtn->count = stats->count;
- rtn->min = stats->min;
- rtn->max = stats->max;
- rtn->sum = stats->sum;
- rtn->mean = stats->mean;
- rtn->stddev = -1;
+ if (SPI_tuptable) SPI_freetuptable(tuptable);
+ SPI_cursor_close(portal);
+ SPI_finish();
- rtn->values = NULL;
- rtn->sorted = 0;
- }
- else {
- rtn->count += stats->count;
- rtn->sum += stats->sum;
+ PG_RETURN_NULL();
+ }
- if (stats->min < rtn->min)
+ rtn->sample = stats->sample;
+ rtn->count = stats->count;
rtn->min = stats->min;
- if (stats->max > rtn->max)
rtn->max = stats->max;
+ rtn->sum = stats->sum;
+ rtn->mean = stats->mean;
+ rtn->stddev = -1;
+
+ rtn->values = NULL;
+ rtn->sorted = 0;
+ }
+ else {
+ rtn->count += stats->count;
+ rtn->sum += stats->sum;
+
+ if (stats->min < rtn->min)
+ rtn->min = stats->min;
+ if (stats->max > rtn->max)
+ rtn->max = stats->max;
+ }
}
pfree(stats);
SPI_finish();
if (NULL == rtn) {
- elog(ERROR, "RASTER_summaryStatsCoverage: Unable to get coverage summary stats\n");
+ elog(ERROR, "RASTER_summaryStatsCoverage: Unable to compute coverage summary stats\n");
PG_RETURN_NULL();
}
/* get band */
band = rt_raster_get_band(raster, bandindex - 1);
if (!band) {
- elog(NOTICE, "Could not find raster band of index %d. Returning NULL", bandindex);
+ elog(NOTICE, "Could not find band at index %d. Returning NULL", bandindex);
rt_raster_destroy(raster);
SRF_RETURN_DONE(funcctx);
}
rt_raster_destroy(raster);
PG_FREE_IF_COPY(pgraster, 0);
if (NULL == stats || NULL == stats->values) {
- elog(NOTICE, "Could not retrieve summary statistics of raster band of index %d", bandindex);
+ elog(NOTICE, "Unable to compute summary statistics for band at index %d", bandindex);
+ SRF_RETURN_DONE(funcctx);
+ }
+ else if (stats->count < 1) {
+ elog(NOTICE, "Unable to compute histogram for band at index %d as the band has no values", bandindex);
SRF_RETURN_DONE(funcctx);
}
if (bin_width_count) pfree(bin_width);
pfree(stats);
if (NULL == hist || !count) {
- elog(NOTICE, "Could not retrieve histogram of raster band of index %d", bandindex);
+ elog(NOTICE, "Unable to compute histogram for band at index %d", bandindex);
SRF_RETURN_DONE(funcctx);
}
spi_result = SPI_execute(sql, TRUE, 0);
pfree(sql);
if (spi_result != SPI_OK_SELECT || SPI_tuptable == NULL || SPI_processed != 1) {
- elog(ERROR, "RASTER_histogramCoverage: Could not get coverage summary stats");
+ elog(ERROR, "RASTER_histogramCoverage: Could not get summary stats of coverage");
if (SPI_tuptable) SPI_freetuptable(tuptable);
SPI_finish();
tmp = SPI_getvalue(tuple, tupdesc, 1);
if (NULL == tmp || !strlen(tmp)) {
- elog(ERROR, "RASTER_histogramCoverage: Could not get coverage summary stats");
+ elog(ERROR, "RASTER_histogramCoverage: Could not get summary stats of coverage");
if (SPI_tuptable) SPI_freetuptable(tuptable);
SPI_finish();
tmp = SPI_getvalue(tuple, tupdesc, 2);
if (NULL == tmp || !strlen(tmp)) {
- elog(ERROR, "RASTER_histogramCoverage: Could not get coverage summary stats");
+ elog(ERROR, "RASTER_histogramCoverage: Could not get summary stats of coverage");
if (SPI_tuptable) SPI_freetuptable(tuptable);
SPI_finish();
/* get band */
band = rt_raster_get_band(raster, bandindex - 1);
if (!band) {
- elog(NOTICE, "Could not find raster band of index %d. Returning NULL", bandindex);
+ elog(NOTICE, "Could not find band at index %d. Returning NULL", bandindex);
if (SPI_tuptable) SPI_freetuptable(tuptable);
SPI_cursor_close(portal);
rt_raster_destroy(raster);
if (NULL == stats) {
- elog(NOTICE, "Could not retrieve summary statistics of band of index %d. Returning NULL", bandindex);
+ elog(NOTICE, "Unable to compute summary statistics for band at index %d. Returning NULL", bandindex);
if (SPI_tuptable) SPI_freetuptable(tuptable);
SPI_cursor_close(portal);
}
/* get histogram */
- hist = rt_band_get_histogram(stats, bin_count, bin_width, bin_width_count, right, min, max, &count);
- pfree(stats);
- if (NULL == hist || !count) {
- elog(NOTICE, "Could not retrieve histogram of raster band of index %d", bandindex);
-
- if (SPI_tuptable) SPI_freetuptable(tuptable);
- SPI_cursor_close(portal);
- SPI_finish();
-
- if (NULL != covhist) pfree(covhist);
- if (bin_width_count) pfree(bin_width);
-
- SRF_RETURN_DONE(funcctx);
- }
-
- POSTGIS_RT_DEBUGF(3, "%d bins returned", count);
-
- /* coverage histogram */
- if (NULL == covhist) {
- covhist = (rt_histogram) palloc(sizeof(struct rt_histogram_t) * count);
- if (NULL == covhist) {
- elog(ERROR, "RASTER_histogramCoverage: Unable to allocate memory for coverage histogram");
+ if (stats->count > 0) {
+ hist = rt_band_get_histogram(stats, bin_count, bin_width, bin_width_count, right, min, max, &count);
+ pfree(stats);
+ if (NULL == hist || !count) {
+ elog(NOTICE, "Unable to compute histogram for band at index %d", bandindex);
if (SPI_tuptable) SPI_freetuptable(tuptable);
SPI_cursor_close(portal);
SPI_finish();
+ if (NULL != covhist) pfree(covhist);
if (bin_width_count) pfree(bin_width);
- pfree(hist);
SRF_RETURN_DONE(funcctx);
}
- for (i = 0; i < count; i++) {
- sum += hist[i].count;
- covhist[i].count = hist[i].count;
- covhist[i].percent = 0;
- covhist[i].min = hist[i].min;
- covhist[i].max = hist[i].max;
+ POSTGIS_RT_DEBUGF(3, "%d bins returned", count);
+
+ /* coverage histogram */
+ if (NULL == covhist) {
+ covhist = (rt_histogram) palloc(sizeof(struct rt_histogram_t) * count);
+ if (NULL == covhist) {
+ elog(ERROR, "RASTER_histogramCoverage: Unable to allocate memory for histogram of coverage");
+
+ if (SPI_tuptable) SPI_freetuptable(tuptable);
+ SPI_cursor_close(portal);
+ SPI_finish();
+
+ if (bin_width_count) pfree(bin_width);
+ pfree(hist);
+
+ SRF_RETURN_DONE(funcctx);
+ }
+
+ for (i = 0; i < count; i++) {
+ sum += hist[i].count;
+ covhist[i].count = hist[i].count;
+ covhist[i].percent = 0;
+ covhist[i].min = hist[i].min;
+ covhist[i].max = hist[i].max;
+ }
}
- }
- else {
- for (i = 0; i < count; i++) {
- sum += hist[i].count;
- covhist[i].count += hist[i].count;
+ else {
+ for (i = 0; i < count; i++) {
+ sum += hist[i].count;
+ covhist[i].count += hist[i].count;
+ }
}
- }
- pfree(hist);
+ pfree(hist);
- /* assuming bin_count wasn't set, force consistency */
- if (bin_count <= 0) bin_count = count;
+ /* assuming bin_count wasn't set, force consistency */
+ if (bin_count <= 0) bin_count = count;
+ }
/* next record */
SPI_cursor_fetch(portal, TRUE, 1);
/* get band */
band = rt_raster_get_band(raster, bandindex - 1);
if (!band) {
- elog(NOTICE, "Could not find raster band of index %d. Returning NULL", bandindex);
+ elog(NOTICE, "Could not find band at index %d. Returning NULL", bandindex);
rt_raster_destroy(raster);
SRF_RETURN_DONE(funcctx);
}
rt_raster_destroy(raster);
PG_FREE_IF_COPY(pgraster, 0);
if (NULL == stats || NULL == stats->values) {
- elog(NOTICE, "Could not retrieve summary statistics of raster band of index %d", bandindex);
+ elog(NOTICE, "Could not retrieve summary statistics for band at index %d", bandindex);
+ SRF_RETURN_DONE(funcctx);
+ }
+ else if (stats->count < 1) {
+ elog(NOTICE, "Unable to compute quantiles for band at index %d as the band has no values", bandindex);
SRF_RETURN_DONE(funcctx);
}
if (quantiles_count) pfree(quantiles);
pfree(stats);
if (NULL == quant || !count) {
- elog(NOTICE, "Could not retrieve quantiles of raster band of index %d", bandindex);
+ elog(NOTICE, "Unable to compute quantiles for band at index %d", bandindex);
SRF_RETURN_DONE(funcctx);
}
spi_result = SPI_execute(sql, TRUE, 0);
pfree(sql);
if (spi_result != SPI_OK_SELECT || SPI_tuptable == NULL || SPI_processed != 1) {
- elog(ERROR, "RASTER_quantileCoverage: Could not get coverage summary stats");
+ elog(ERROR, "RASTER_quantileCoverage: Could not get summary stats of coverage");
if (SPI_tuptable) SPI_freetuptable(tuptable);
SPI_finish();
tmp = SPI_getvalue(tuple, tupdesc, 1);
if (NULL == tmp || !strlen(tmp)) {
- elog(ERROR, "RASTER_quantileCoverage: Could not get coverage summary stats");
+ elog(ERROR, "RASTER_quantileCoverage: Could not get summary stats of coverage");
if (SPI_tuptable) SPI_freetuptable(tuptable);
SPI_finish();
rt_raster_destroy(raster);
if (NULL == covquant || !count) {
- elog(NOTICE, "Could not retrieve quantiles of roaster band of index %d", bandindex);
+ elog(NOTICE, "Unable to compute quantiles for band at index %d", bandindex);
if (SPI_tuptable) SPI_freetuptable(tuptable);
SPI_cursor_close(portal);
/* get band */
band = rt_raster_get_band(raster, bandindex - 1);
if (!band) {
- elog(NOTICE, "Could not find raster band of index %d. Returning NULL", bandindex);
+ elog(NOTICE, "Could not find band at index %d. Returning NULL", bandindex);
rt_raster_destroy(raster);
SRF_RETURN_DONE(funcctx);
}
rt_raster_destroy(raster);
PG_FREE_IF_COPY(pgraster, 0);
if (NULL == vcnts || !count) {
- elog(NOTICE, "Could not count the values of raster band of index %d", bandindex);
+ elog(NOTICE, "Unable to count the values for band at index %d", bandindex);
SRF_RETURN_DONE(funcctx);
}
/* get band */
band = rt_raster_get_band(raster, bandindex - 1);
if (!band) {
- elog(NOTICE, "Could not find raster band of index %d. Returning NULL", bandindex);
+ elog(NOTICE, "Could not find band at index %d. Returning NULL", bandindex);
if (SPI_tuptable) SPI_freetuptable(tuptable);
SPI_cursor_close(portal);
rt_band_destroy(band);
rt_raster_destroy(raster);
if (NULL == vcnts || !count) {
- elog(NOTICE, "Could not count the values of raster band of index %d", bandindex);
+ elog(NOTICE, "Unable to count the values for band at index %d", bandindex);
SRF_RETURN_DONE(funcctx);
}
*/
covvcnts = (rt_valuecount) malloc(sizeof(struct rt_valuecount_t) * count);
if (NULL == covvcnts) {
- elog(ERROR, "RASTER_valueCountCoverage: Unable to allocate memory for coverage value counts");
+ elog(ERROR, "RASTER_valueCountCoverage: Unable to allocate memory for value counts of coverage");
if (SPI_tuptable) SPI_freetuptable(tuptable);
SPI_cursor_close(portal);
covcount++;
covvcnts = realloc(covvcnts, sizeof(struct rt_valuecount_t) * covcount);
if (NULL == covvcnts) {
- elog(ERROR, "RASTER_valueCountCoverage: Unable to change allocated memory for coverage value counts");
+ elog(ERROR, "RASTER_valueCountCoverage: Unable to change allocated memory for value counts of coverage");
if (SPI_tuptable) SPI_freetuptable(tuptable);
SPI_cursor_close(portal);
BEGIN
- RAISE DEBUG 'Parameters: catalog=%, schema=%, table=%, column=%, srid=%, pixel_types=%, out_db=%, regular_blocking=%, nodata_values=%, scale_x=%, scale_y=%, blocksize_x=%, blocksize_y=%',
+ --RAISE DEBUG 'Parameters: catalog=%, schema=%, table=%, column=%, srid=%, pixel_types=%, out_db=%, regular_blocking=%, nodata_values=%, scale_x=%, scale_y=%, blocksize_x=%, blocksize_y=%',
p_catalog_name, p_schema_name, p_table_name, p_column_name, p_srid, p_pixel_types, p_out_db, p_regular_blocking, p_nodata_values, p_scale_x, p_scale_y, p_blocksize_x, p_blocksize_y;
-- Validate required parametersa and combinations
RAISE EXCEPTION 'Invalid SRID';
RETURN 'fail';
END IF;
- RAISE DEBUG 'Verified SRID = %', p_srid;
+ --RAISE DEBUG 'Verified SRID = %', p_srid;
END IF;
FOR pti IN array_lower(pixel_types, 1) .. array_upper(pixel_types, 1) LOOP
IF p_pixel_types[npti] = pixel_types[pti] THEN
pixel_types_found := 1;
- RAISE DEBUG 'Identified pixel type %', p_pixel_types[npti];
+ --RAISE DEBUG 'Identified pixel type %', p_pixel_types[npti];
END IF;
END LOOP;
sql := 'SELECT nspname FROM pg_namespace '
|| 'WHERE text(nspname) = ' || quote_literal(p_schema_name)
|| 'LIMIT 1';
- RAISE DEBUG '%', sql;
+ --RAISE DEBUG '%', sql;
EXECUTE sql INTO real_schema;
IF ( real_schema IS NULL ) THEN
END IF;
IF ( real_schema IS NULL ) THEN
- RAISE DEBUG 'Detecting schema';
+ --RAISE DEBUG 'Detecting schema';
sql := 'SELECT n.nspname AS schemaname '
|| 'FROM pg_catalog.pg_class c '
|| 'JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace '
|| quote_literal('pg_catalog') || ', ' || quote_literal('pg_toast') || ')'
|| ' AND pg_catalog.pg_table_is_visible(c.oid)'
|| ' AND c.relname = ' || quote_literal(p_table_name);
- RAISE DEBUG '%', sql;
+ --RAISE DEBUG '%', sql;
EXECUTE sql INTO real_schema;
IF ( real_schema IS NULL ) THEN
sql := 'ALTER TABLE '
|| quote_ident(real_schema) || '.' || quote_ident(p_table_name)
|| ' ADD COLUMN ' || quote_ident(p_column_name) || ' raster ';
- RAISE DEBUG '%', sql;
+ --RAISE DEBUG '%', sql;
EXECUTE sql;
|| ' AND r_table_schema = ' || quote_literal(real_schema)
|| ' AND r_table_name = ' || quote_literal(p_table_name)
|| ' AND r_column = ' || quote_literal(p_column_name);
- RAISE DEBUG '%', sql;
+ --RAISE DEBUG '%', sql;
EXECUTE sql;
|| COALESCE(quote_literal(p_blocksize_x), 'NULL') || ','
|| COALESCE(quote_literal(p_blocksize_y), 'NULL') || ','
|| COALESCE(quote_literal(p_extent::text), 'NULL') || ')';
- RAISE DEBUG '%', sql;
+ --RAISE DEBUG '%', sql;
EXECUTE sql;
|| quote_ident('enforce_srid_' || p_column_name)
|| ' CHECK (ST_SRID(' || quote_ident(p_column_name)
|| ') = ' || p_srid::text || ')';
- RAISE DEBUG '%', sql;
+ --RAISE DEBUG '%', sql;
EXECUTE sql;