]> granicus.if.org Git - postgis/commitdiff
Correct output of INSERT statements for overviews in raster2pgsql.c. Associated...
authorBborie Park <bkpark at ucdavis.edu>
Wed, 28 Dec 2011 02:19:08 +0000 (02:19 +0000)
committerBborie Park <bkpark at ucdavis.edu>
Wed, 28 Dec 2011 02:19:08 +0000 (02:19 +0000)
git-svn-id: http://svn.osgeo.org/postgis/trunk@8598 b70326c6-7e19-0410-871a-916f4a2858ee

raster/loader/raster2pgsql.c
raster/rt_pg/rtpostgis.sql.in.c

index 024688d811ae253f3bee95f37735445a09481f02..79fbc8e4b03d2a2f04c960f085eee4f253c7ffe9 100644 (file)
@@ -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);
index 19e6aa4c6847fec6e44f82e68c5a24db9348ace1..1fa4e482db2ee1c782eccdefd7f8bfc9759790ee 100644 (file)
@@ -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)