Ticket #440: st_georeference.patch

File st_georeference.patch, 11.9 KB (added by dzwarg, 6 years ago)

Consolidated methods in plpgsql, removed from C

  • test/regress/rt_georeference.sql

     
    88-----------------------------------------------------------------------
    99
    1010-----------------------------------------------------------------------
    11 -- st_gdalgeotransform
     11-- st_georeference (default)
    1212-----------------------------------------------------------------------
    1313
    1414SELECT
    15     replace(st_gdalgeotransform(rast)::text, E'\n', E'EOL'),
    16     replace(st_gdalgeotransform(rast)::text, E'\n', E'EOL') =
     15    replace(st_georeference(rast)::text, E'\n', E'EOL'),
     16    replace(st_georeference(rast)::text, E'\n', E'EOL') =
    1717    '2.0000000000EOL0.0000000000EOL0.0000000000EOL3.0000000000EOL0.5000000000EOL0.5000000000EOL'
    1818FROM rt_properties_test
    1919WHERE id = 0;
    2020
    2121SELECT
    22     replace(st_gdalgeotransform(rast)::text, E'\n', E'EOL'),
    23     replace(st_gdalgeotransform(rast)::text, E'\n', E'EOL') =
     22    replace(st_georeference(rast)::text, E'\n', E'EOL'),
     23    replace(st_georeference(rast)::text, E'\n', E'EOL') =
    2424    '5.0000000000EOL0.0000000000EOL0.0000000000EOL5.0000000000EOL2.5000000000EOL2.5000000000EOL'
    2525FROM rt_properties_test
    2626WHERE id = 1;
    2727
    2828SELECT
    29     replace(st_gdalgeotransform(rast)::text, E'\n', E'EOL'),
    30     replace(st_gdalgeotransform(rast)::text, E'\n', E'EOL') =
     29    replace(st_georeference(rast)::text, E'\n', E'EOL'),
     30    replace(st_georeference(rast)::text, E'\n', E'EOL') =
    3131    '5.0000000000EOL0.0000000000EOL0.0000000000EOL5.0000000000EOL7.5000000000EOL2.5000000000EOL'
    3232FROM rt_properties_test
    3333WHERE id = 2;
    3434
    3535SELECT
    36     replace(st_gdalgeotransform(rast)::text, E'\n', E'EOL'),
    37     replace(st_gdalgeotransform(rast)::text, E'\n', E'EOL') =
     36    replace(st_georeference(rast)::text, E'\n', E'EOL'),
     37    replace(st_georeference(rast)::text, E'\n', E'EOL') =
    3838    '5.0000000000EOL0.0000000000EOL0.0000000000EOL5.0000000000EOL7.5000000000EOL2.5000000000EOL'
    3939FROM rt_properties_test
    4040WHERE id = 3;
    4141
    4242-----------------------------------------------------------------------
    43 -- st_esriworldfile
     43-- st_georeference (GDAL)
    4444-----------------------------------------------------------------------
    4545
     46SELECT
     47    replace(st_georeference(rast,'GDAL')::text, E'\n', E'EOL'),
     48    replace(st_georeference(rast,'GDAL')::text, E'\n', E'EOL') =
     49    '2.0000000000EOL0.0000000000EOL0.0000000000EOL3.0000000000EOL0.5000000000EOL0.5000000000EOL'
     50FROM rt_properties_test
     51WHERE id = 0;
     52
     53SELECT
     54    replace(st_georeference(rast,'GDAL')::text, E'\n', E'EOL'),
     55    replace(st_georeference(rast,'GDAL')::text, E'\n', E'EOL') =
     56    '5.0000000000EOL0.0000000000EOL0.0000000000EOL5.0000000000EOL2.5000000000EOL2.5000000000EOL'
     57FROM rt_properties_test
     58WHERE id = 1;
     59
     60SELECT
     61    replace(st_georeference(rast,'GDAL')::text, E'\n', E'EOL'),
     62    replace(st_georeference(rast,'GDAL')::text, E'\n', E'EOL') =
     63    '5.0000000000EOL0.0000000000EOL0.0000000000EOL5.0000000000EOL7.5000000000EOL2.5000000000EOL'
     64FROM rt_properties_test
     65WHERE id = 2;
     66
     67SELECT
     68    replace(st_georeference(rast,'GDAL')::text, E'\n', E'EOL'),
     69    replace(st_georeference(rast,'GDAL')::text, E'\n', E'EOL') =
     70    '5.0000000000EOL0.0000000000EOL0.0000000000EOL5.0000000000EOL7.5000000000EOL2.5000000000EOL'
     71FROM rt_properties_test
     72WHERE id = 3;
     73
     74-----------------------------------------------------------------------
     75-- st_georeference (ESRI)
     76-----------------------------------------------------------------------
     77
    4678SELECT
    47         replace(st_esriworldfile(rast)::text, E'\n', E'EOL'),
    48         replace(st_esriworldfile(rast)::text, E'\n', E'EOL') =
     79        replace(st_georeference(rast,'ESRI')::text, E'\n', E'EOL'),
     80        replace(st_georeference(rast,'ESRI')::text, E'\n', E'EOL') =
    4981    '2.0000000000EOL0.0000000000EOL0.0000000000EOL3.0000000000EOL1.5000000000EOL2.0000000000EOL'
    5082FROM rt_properties_test
    5183WHERE id = 0;
    5284
    5385SELECT
    54         replace(st_esriworldfile(rast)::text, E'\n', E'EOL'),
    55         replace(st_esriworldfile(rast)::text, E'\n', E'EOL') =
     86        replace(st_georeference(rast,'ESRI')::text, E'\n', E'EOL'),
     87        replace(st_georeference(rast,'ESRI')::text, E'\n', E'EOL') =
    5688    '5.0000000000EOL0.0000000000EOL0.0000000000EOL5.0000000000EOL5.0000000000EOL5.0000000000EOL'
    5789FROM rt_properties_test
    5890WHERE id = 1;
    5991
    6092SELECT
    61         replace(st_esriworldfile(rast)::text, E'\n', E'EOL'),
    62         replace(st_esriworldfile(rast)::text, E'\n', E'EOL') =
     93        replace(st_georeference(rast,'ESRI')::text, E'\n', E'EOL'),
     94        replace(st_georeference(rast,'ESRI')::text, E'\n', E'EOL') =
    6395    '5.0000000000EOL0.0000000000EOL0.0000000000EOL5.0000000000EOL10.0000000000EOL5.0000000000EOL'
    6496FROM rt_properties_test
    6597WHERE id = 2;
    6698
    6799SELECT
    68         replace(st_esriworldfile(rast)::text, E'\n', E'EOL'),
    69         replace(st_esriworldfile(rast)::text, E'\n', E'EOL') =
     100        replace(st_georeference(rast,'ESRI')::text, E'\n', E'EOL'),
     101        replace(st_georeference(rast,'ESRI')::text, E'\n', E'EOL') =
    70102    '5.0000000000EOL0.0000000000EOL0.0000000000EOL5.0000000000EOL10.0000000000EOL5.0000000000EOL'
    71103FROM rt_properties_test
    72104WHERE id = 3;
  • test/regress/rt_georeference_expected

     
    225.0000000000EOL0.0000000000EOL0.0000000000EOL5.0000000000EOL2.5000000000EOL2.5000000000EOL|t
    335.0000000000EOL0.0000000000EOL0.0000000000EOL5.0000000000EOL7.5000000000EOL2.5000000000EOL|t
    445.0000000000EOL0.0000000000EOL0.0000000000EOL5.0000000000EOL7.5000000000EOL2.5000000000EOL|t
     52.0000000000EOL0.0000000000EOL0.0000000000EOL3.0000000000EOL0.5000000000EOL0.5000000000EOL|t
     65.0000000000EOL0.0000000000EOL0.0000000000EOL5.0000000000EOL2.5000000000EOL2.5000000000EOL|t
     75.0000000000EOL0.0000000000EOL0.0000000000EOL5.0000000000EOL7.5000000000EOL2.5000000000EOL|t
     85.0000000000EOL0.0000000000EOL0.0000000000EOL5.0000000000EOL7.5000000000EOL2.5000000000EOL|t
    592.0000000000EOL0.0000000000EOL0.0000000000EOL3.0000000000EOL1.5000000000EOL2.0000000000EOL|t
    6105.0000000000EOL0.0000000000EOL0.0000000000EOL5.0000000000EOL5.0000000000EOL5.0000000000EOL|t
    7115.0000000000EOL0.0000000000EOL0.0000000000EOL5.0000000000EOL10.0000000000EOL5.0000000000EOL|t
  • rt_pg/rtpostgis.sql.in.c

     
    183183    AS 'MODULE_PATHNAME','RASTER_setUpperLeftRaster'
    184184    LANGUAGE 'C' _IMMUTABLE_STRICT;
    185185
    186 CREATEFUNCTION st_gdalgeotransform(raster)
    187     RETURNS TEXT
    188     AS 'MODULE_PATHNAME','RASTER_getGDALGeoTransform'
    189     LANGUAGE 'C' _IMMUTABLE_STRICT;
     186CREATEFUNCTION st_georeference(raster)
     187    RETURNS text AS
     188$$
     189DECLARE
     190    rast alias for $1;
     191    result text;
     192BEGIN
     193    SELECT st_georeference(rast, 'GDAL') INTO result;
     194    RETURN result;
     195END;
     196$$
     197LANGUAGE 'plpgsql' _IMMUTABLE_STRICT; -- WITH (isstrict);
    190198
    191 CREATEFUNCTION st_esriworldfile(raster) RETURNS text AS
     199CREATEFUNCTION st_georeference(raster, text) RETURNS text AS
    192200$$
    193201DECLARE
    194202        rast alias for $1;
     203    format alias for $2;
    195204        x numeric;
    196205        result text;
    197206BEGIN
    198         SELECT st_pixelsizex(rast)::numeric INTO x;
    199         result = trunc(x, 10) || E'\n';
     207    IF format = 'ESRI' THEN
     208            SELECT st_pixelsizex(rast)::numeric INTO x;
     209            result = trunc(x, 10) || E'\n';
    200210
    201         SELECT st_rotationy(rast)::numeric INTO x;
    202         result = result || trunc(x, 10) || E'\n';
     211            SELECT st_rotationy(rast)::numeric INTO x;
     212            result = result || trunc(x, 10) || E'\n';
    203213
    204         SELECT st_rotationx(rast)::numeric INTO x;
    205         result = result || trunc(x, 10) || E'\n';
     214            SELECT st_rotationx(rast)::numeric INTO x;
     215            result = result || trunc(x, 10) || E'\n';
    206216
    207         SELECT st_pixelsizey(rast)::numeric INTO x;
    208         result = result || trunc(x, 10) || E'\n';
     217            SELECT st_pixelsizey(rast)::numeric INTO x;
     218            result = result || trunc(x, 10) || E'\n';
    209219
    210         SELECT (st_upperleftx(rast) + st_pixelsizex(rast)*0.5 + st_rotationx(rast)*0.5)::numeric INTO x;
    211         result = result || trunc(x, 10) || E'\n';
     220            SELECT (st_upperleftx(rast) + st_pixelsizex(rast)*0.5 + st_rotationx(rast)*0.5)::numeric INTO x;
     221            result = result || trunc(x, 10) || E'\n';
    212222
    213         SELECT (st_upperlefty(rast) + st_pixelsizey(rast)*0.5 + st_rotationy(rast)*0.5)::numeric INTO x;
    214         result = result || trunc(x, 10) || E'\n';
     223            SELECT (st_upperlefty(rast) + st_pixelsizey(rast)*0.5 + st_rotationy(rast)*0.5)::numeric INTO x;
     224            result = result || trunc(x, 10) || E'\n';
     225    ELSE -- IF format = 'GDAL' THEN
     226            SELECT st_pixelsizex(rast)::numeric INTO x;
     227            result = trunc(x, 10) || E'\n';
    215228
     229            SELECT st_rotationy(rast)::numeric INTO x;
     230            result = result || trunc(x, 10) || E'\n';
     231
     232            SELECT st_rotationx(rast)::numeric INTO x;
     233            result = result || trunc(x, 10) || E'\n';
     234
     235            SELECT st_pixelsizey(rast)::numeric INTO x;
     236            result = result || trunc(x, 10) || E'\n';
     237
     238            SELECT st_upperleftx(rast)::numeric INTO x;
     239            result = result || trunc(x, 10) || E'\n';
     240
     241            SELECT st_upperlefty(rast)::numeric INTO x;
     242            result = result || trunc(x, 10) || E'\n';
     243    END IF;
     244
    216245        RETURN result;
    217246END;
    218247$$
    219248LANGUAGE 'plpgsql' _IMMUTABLE_STRICT; -- WITH (isstrict);
    220249
    221 
    222250CREATEFUNCTION st_numbands(raster)
    223251    RETURNS integer
    224252    AS 'MODULE_PATHNAME','RASTER_getNumBands'
  • rt_pg/rt_pg.c

     
    107107Datum RASTER_getYUpperLeft(PG_FUNCTION_ARGS);
    108108Datum RASTER_setUpperLeftXY(PG_FUNCTION_ARGS);
    109109Datum RASTER_setUpperLeftRaster(PG_FUNCTION_ARGS);
    110 Datum RASTER_getGDALGeoTransform(PG_FUNCTION_ARGS);
    111110Datum RASTER_getBandPixelType(PG_FUNCTION_ARGS);
    112111Datum RASTER_getBandPixelTypeName(PG_FUNCTION_ARGS);
    113112Datum RASTER_getBandNoDataValue(PG_FUNCTION_ARGS);
     
    10411040}
    10421041
    10431042/**
    1044  * Return the georeference of the raster as a string representing
    1045  * the 6 doubles of an equivalent world file (including the carriage return).
    1046  */
    1047 PG_FUNCTION_INFO_V1(RASTER_getGDALGeoTransform);
    1048 Datum RASTER_getGDALGeoTransform(PG_FUNCTION_ARGS)
    1049 {
    1050     rt_pgraster *pgraster = (rt_pgraster *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
    1051     rt_raster raster;
    1052     rt_context ctx = get_rt_context(fcinfo);
    1053     text *result = NULL;
    1054     size_t text_size = 0;
    1055     const size_t buf_size = 6 * 32 + 128; /* modest buffer size for 6 factors */
    1056     char buf[buf_size];
    1057     double xdim, ydim;  /* line 1 and 4 */
    1058     double xrot, yrot;  /* line 2 and 3 */
    1059     double xul, yul;    /* line 5 and 6 */
    1060 
    1061     memset(buf, 0, buf_size);
    1062 
    1063     /* TODO: can be optimized to only detoast the header! */
    1064     raster = rt_raster_deserialize(ctx, pgraster);
    1065     if ( ! raster ) {
    1066         elog(ERROR, "Could not deserialize raster");
    1067         PG_RETURN_NULL();
    1068     }
    1069 
    1070     xdim = rt_raster_get_pixel_width(ctx, raster);
    1071     xrot = rt_raster_get_x_rotation(ctx, raster);
    1072     yrot = rt_raster_get_y_rotation(ctx, raster);
    1073     ydim = rt_raster_get_pixel_height(ctx, raster);
    1074     xul = rt_raster_get_x_offset(ctx, raster);
    1075     yul = rt_raster_get_y_offset(ctx, raster);
    1076 
    1077     /* TODO:
    1078      * 1) Do we want to stick to WorldFile spec and output y-dim as negative?
    1079      *    Now, we output what we store.
    1080      * 2) Do we need to 'rotate' x-/y-coorindate of upper left pixel?
    1081      */
    1082     assert('\0' == buf[0] && '\0' == buf[buf_size - 1]);
    1083     snprintf(buf, buf_size, "%.10f\n%.10f\n%.10f\n%.10f\n%.10f\n%.10f\n",
    1084              xdim, xrot, yrot, ydim, xul, yul);
    1085     buf[buf_size - 1] = '\0';
    1086 
    1087     text_size = strlen(buf);
    1088     result = palloc(VARHDRSZ + text_size);
    1089     if ( ! result ) {
    1090         elog(ERROR, "Could not allocate memory for output text object");
    1091         PG_RETURN_NULL();
    1092     }
    1093 
    1094     SET_VARSIZE(result, VARHDRSZ + text_size);
    1095     memcpy(VARDATA(result), buf, text_size);
    1096     PG_RETURN_TEXT_P(result);
    1097 }
    1098 
    1099 /**
    11001043 * Return pixel type of the specified band of raster.
    11011044 * Band index is 1-based.
    11021045 */