From 70ad917b1883d20b5b063500942e43030fe36f03 Mon Sep 17 00:00:00 2001 From: Bborie Park Date: Wed, 28 Dec 2011 02:19:08 +0000 Subject: [PATCH] Correct output of INSERT statements for overviews in raster2pgsql.c. Associated ticket is #1404. Aslo minor code formatting and additional code checks in rtpostgis.sql.in.c git-svn-id: http://svn.osgeo.org/postgis/trunk@8598 b70326c6-7e19-0410-871a-916f4a2858ee --- raster/loader/raster2pgsql.c | 17 +++- raster/rt_pg/rtpostgis.sql.in.c | 138 +++++++++++++++++--------------- 2 files changed, 85 insertions(+), 70 deletions(-) diff --git a/raster/loader/raster2pgsql.c b/raster/loader/raster2pgsql.c index 024688d81..79fbc8e4b 100644 --- a/raster/loader/raster2pgsql.c +++ b/raster/loader/raster2pgsql.c @@ -1128,7 +1128,7 @@ add_overview_constraints( } static int -build_overview(int idx, RTLOADERCFG *config, RASTERINFO *info, int factor, STRINGBUFFER *tileset, STRINGBUFFER *buffer) { +build_overview(int idx, RTLOADERCFG *config, RASTERINFO *info, int ovx, STRINGBUFFER *tileset, STRINGBUFFER *buffer) { GDALDatasetH hdsSrc; VRTDatasetH hdsOv; VRTSourcedRasterBandH hbandOv; @@ -1136,6 +1136,8 @@ build_overview(int idx, RTLOADERCFG *config, RASTERINFO *info, int factor, STRIN int dimOv[2] = {0}; int j = 0; + int factor; + const char *ovtable = NULL; VRTDatasetH hdsDst; VRTSourcedRasterBandH hbandDst; @@ -1158,7 +1160,14 @@ build_overview(int idx, RTLOADERCFG *config, RASTERINFO *info, int factor, STRIN /* working copy of geotransform matrix */ memcpy(gtOv, info->gt, sizeof(double) * 6); - /* loop over each overview factor */ + if (ovx >= config->overview_count) { + fprintf(stderr, _("Invalid overview index: %d\n"), ovx); + return 0; + } + factor = config->overview[ovx]; + ovtable = (const char *) config->overview_table[ovx]; + + /* factor must be within valid range */ if (factor < MINOVFACTOR || factor > MAXOVFACTOR) { fprintf(stderr, _("Overview factor %d is not between %d and %d\n"), factor, MINOVFACTOR, MAXOVFACTOR); return 0; @@ -1287,7 +1296,7 @@ build_overview(int idx, RTLOADERCFG *config, RASTERINFO *info, int factor, STRIN /* flush if tileset gets too big */ if (tileset->length > 10) { if (!insert_records( - config->schema, config->table, config->raster_column, + config->schema, ovtable, config->raster_column, (config->file_column ? config->rt_filename[idx] : NULL), config->copy_statements, tileset, buffer )) { @@ -1764,7 +1773,7 @@ process_rasters(RTLOADERCFG *config, STRINGBUFFER *buffer) { return 0; } - if (!build_overview(i, config, &rastinfo, config->overview[j], &tileset, buffer)) { + if (!build_overview(i, config, &rastinfo, j, &tileset, buffer)) { fprintf(stderr, _("Cannot create overview of factor %d for raster %s\n"), config->overview[j], config->rt_file[i]); rtdealloc_rastinfo(&rastinfo); rtdealloc_stringbuffer(&tileset, 0); diff --git a/raster/rt_pg/rtpostgis.sql.in.c b/raster/rt_pg/rtpostgis.sql.in.c index 19e6aa4c6..1fa4e482d 100644 --- a/raster/rt_pg/rtpostgis.sql.in.c +++ b/raster/rt_pg/rtpostgis.sql.in.c @@ -3481,84 +3481,82 @@ CREATE AGGREGATE ST_Union(raster, integer, text) ( -- ST_Clip ----------------------------------------------------------------------- -CREATE OR REPLACE FUNCTION ST_Clip(rast raster, band int, geom geometry, nodata float8 DEFAULT null, trimraster boolean DEFAULT false) - RETURNS raster AS - $$ - DECLARE - sourceraster raster := rast; - newrast raster; - geomrast raster; - numband int := ST_Numbands(rast); - bandstart int; - bandend int; - newextent text; - newnodata float8; - newpixtype text; - bandi int; - BEGIN - IF rast IS NULL THEN - RETURN null; - END IF; - IF geom IS NULL THEN - RETURN rast; - END IF; - IF band IS NULL THEN - bandstart := 1; - bandend := numband; - ELSEIF ST_HasNoBand(rast, band) THEN - RAISE NOTICE 'Raster do not have band %. Returning null', band; - RETURN null; - ELSE - bandstart := band; - bandend := band; - END IF; - newpixtype := ST_BandPixelType(rast, bandstart); - newnodata := coalesce(nodata, ST_BandNodataValue(rast, bandstart), ST_MinPossibleValue(newpixtype)); - newextent := CASE WHEN trimraster THEN 'INTERSECTION' ELSE 'FIRST' END; +CREATE OR REPLACE FUNCTION st_clip(rast raster, band int, geom geometry, nodata float8 DEFAULT NULL, trimraster boolean DEFAULT FALSE) + RETURNS raster + AS $$ + DECLARE + sourceraster raster := rast; + newrast raster; + geomrast raster; + numband int := ST_Numbands(rast); + bandstart int; + bandend int; + newextent text; + newnodata float8; + newpixtype text; + bandi int; + BEGIN + IF rast IS NULL THEN + RETURN NULL; + END IF; + IF geom IS NULL THEN + RETURN rast; + END IF; + IF band IS NULL THEN + bandstart := 1; + bandend := numband; + ELSEIF ST_HasNoBand(rast, band) THEN + RAISE NOTICE 'Raster do not have band %. Returning null', band; + RETURN NULL; + ELSE + bandstart := band; + bandend := band; + END IF; + + newpixtype := ST_BandPixelType(rast, bandstart); + newnodata := coalesce(nodata, ST_BandNodataValue(rast, bandstart), ST_MinPossibleValue(newpixtype)); + newextent := CASE WHEN trimraster THEN 'INTERSECTION' ELSE 'FIRST' END; --RAISE NOTICE 'newextent=%', newextent; - -- Convert the geometry to a raster - geomrast := ST_AsRaster(geom, rast, ST_BandPixelType(rast, band), 1, newnodata); + -- Convert the geometry to a raster + geomrast := ST_AsRaster(geom, rast, ST_BandPixelType(rast, band), 1, newnodata); - -- Set the newnodata - sourceraster := ST_SetBandNodataValue(sourceraster, bandstart, newnodata); + -- Set the newnodata + sourceraster := ST_SetBandNodataValue(sourceraster, bandstart, newnodata); - -- Compute the first raster band - newrast := ST_MapAlgebraExpr(sourceraster, bandstart, geomrast, 1, 'rast1', newpixtype, newextent); + -- Compute the first raster band + newrast := ST_MapAlgebraExpr(sourceraster, bandstart, geomrast, 1, 'rast1', newpixtype, newextent); - FOR bandi IN bandstart+1..bandend LOOP + FOR bandi IN bandstart+1..bandend LOOP --RAISE NOTICE 'bandi=%', bandi; - -- for each band we must determine the nodata value - newpixtype := ST_BandPixelType(rast, bandi); - newnodata := coalesce(nodata, ST_BandNodataValue(sourceraster, bandi), ST_MinPossibleValue(newpixtype)); - sourceraster := ST_SetBandNodataValue(sourceraster, bandi, newnodata); - newrast := ST_AddBand(newrast, ST_MapAlgebraExpr(sourceraster, bandi, geomrast, 1, 'rast1', newpixtype, newextent)); - END LOOP; - RETURN newrast; - END; - $$ - LANGUAGE 'plpgsql'; + -- for each band we must determine the nodata value + newpixtype := ST_BandPixelType(rast, bandi); + newnodata := coalesce(nodata, ST_BandNodataValue(sourceraster, bandi), ST_MinPossibleValue(newpixtype)); + sourceraster := ST_SetBandNodataValue(sourceraster, bandi, newnodata); + newrast := ST_AddBand(newrast, ST_MapAlgebraExpr(sourceraster, bandi, geomrast, 1, 'rast1', newpixtype, newextent)); + END LOOP; + + RETURN newrast; + END; + $$ LANGUAGE 'plpgsql' STABLE; -- Variant defaulting to band 1 -CREATE OR REPLACE FUNCTION ST_Clip(rast raster, geom geometry, nodata float8 DEFAULT null, trimraster boolean DEFAULT false) - RETURNS raster - AS $$ - SELECT ST_Clip($1, 1, $2, $3, $4); - $$ LANGUAGE 'SQL'; +CREATE OR REPLACE FUNCTION st_clip(rast raster, geom geometry, nodata float8 DEFAULT NULL, trimraster boolean DEFAULT FALSE) + RETURNS raster AS + $$ SELECT ST_Clip($1, 1, $2, $3, $4) $$ + LANGUAGE 'SQL' STABLE; -- Variant defaulting nodata to the one of the raster or the min possible value -CREATE OR REPLACE FUNCTION ST_Clip(rast raster, band int, geom geometry, trimraster boolean) - RETURNS raster - AS $$ - SELECT ST_Clip($1, $2, $3, null, $4); - $$ LANGUAGE 'SQL'; +CREATE OR REPLACE FUNCTION st_clip(rast raster, band int, geom geometry, trimraster boolean) + RETURNS raster AS + $$ SELECT ST_Clip($1, $2, $3, null, $4) $$ + LANGUAGE 'SQL' STABLE; -- Variant defaulting nodata to the one of the raster or the min possible value -CREATE OR REPLACE FUNCTION ST_Clip(rast raster, geom geometry, trimraster boolean) - RETURNS raster - AS $$ - SELECT ST_Clip($1, 1, $2, null, $3); - $$ LANGUAGE 'SQL'; +CREATE OR REPLACE FUNCTION st_clip(rast raster, geom geometry, trimraster boolean) + RETURNS raster AS + $$ SELECT ST_Clip($1, 1, $2, null, $3) $$ + LANGUAGE 'SQL' STABLE; ------------------------------------------------------------------------------ -- raster constraint functions @@ -4089,6 +4087,10 @@ CREATE OR REPLACE FUNCTION _add_raster_constraint_pixel_types(rastschema name, r RETURN FALSE; END; max := array_length(attr, 1); + IF max < 1 OR max IS NULL THEN + RAISE NOTICE 'Unable to get the pixel types of a sample raster'; + RETURN FALSE; + END IF; sql := 'ALTER TABLE ' || fqtn || ' ADD CONSTRAINT ' || quote_ident(cn) @@ -4161,6 +4163,10 @@ CREATE OR REPLACE FUNCTION _add_raster_constraint_nodata_values(rastschema name, RETURN FALSE; END; max := array_length(attr, 1); + IF max < 1 OR max IS NULL THEN + RAISE NOTICE 'Unable to get the nodata values of a sample raster'; + RETURN FALSE; + END IF; sql := 'ALTER TABLE ' || fqtn || ' ADD CONSTRAINT ' || quote_ident(cn) -- 2.40.0