1

I have a test raster that covers the world ((-180 90), (180 -90)). The SRID is WGS 84 (EPSG:4326). Values for cells in the north-western quadrant ((-180 90), (0 0)) are random integers in the range 1 to 10 (inclusive). Values for other cells are -99; this is the explicit NODATA value.

The test raster is stored in a PostGIS database (v.2.1). I'd like to compute summary statistics for a buffered point where the buffer is specified in either decimal degrees or in meters.

The Problem: My query using decimal degrees and the geometry type works as expected. However, the same query, using meters and the geography type, which I then cast back to the geometry type, does not. Can anyone suggest why this is the case?

Here's my query, where the buffer is specified in decimal degrees:

SELECT
    filename,
    (stats).*
FROM (
    SELECT
        filename,
        ST_SummaryStats(
            ST_Clip(
                rast,
                1,
                ST_Buffer(
                    ST_SetSRID(
                        ST_Point(-90, 45),
                        4326
                    ),
                    1
                ),
                TRUE
            ),
            1,
            TRUE
        ) AS stats
    FROM
        rasters
    WHERE
        filename IN ('raster_grid_1_1_nodata.tif')
) AS stats_tbl;

As you can see, there are filename and rast columns that contain the name of the input file and the input raster, respectively. The result is as expected.

Here's my query, where the buffer is specified in meters:

SELECT
    (summarystats).*
FROM (
    SELECT
        ST_SummaryStats(
            ST_Clip(
                rast,
                1,
                CAST(
                    ST_Buffer(
                        CAST(
                            ST_SetSRID(
                                ST_Point(-90, 45),
                                4326
                            ) AS geography
                        ),
                        1000
                    ) AS geometry
                ),
                TRUE
            ),
            1,
            TRUE
        ) AS summarystats
    FROM
        rasters
    WHERE
        filename IN ('raster_grid_1_1_nodata.tif')
) AS summarystats_tbl;

As you can see, I pass a geography to ST_Buffer so that the radius is specified in meters. The result, however, is not as expected: PostGIS reports that all pixels have the NODATA value and, consequently, does not compute summary statistics. Can anyone suggest why this is the case?

1

I haven't tested this on your example, but I just ran into a nasty PostGIS bug that hasn't been fixed since it was opened 4 years ago:

When using ST_Clip(rast,geom) where geom is smaller than a single pixel of the raster, the returned raster from ST_Clip has no value." See ticket #2586.

Could it be that one of your geometries was smaller than your pixel size?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.