I have a raster dataset imported into POSTGIS with SRS 3857. This table is named as prcp0000_normal3857.
The dataset represents statewide precipitation in Montana and can be downloaded below. ftp://mco.cfc.umt.edu/prcp/Daymet/yearly_normals/geotiff/PRCP0000_normal.zip
After downloading the data was reprojected to 3857, tiled and renamed prcp0000_normal3857 using the GDAL command below.
gdalwarp -co "TILED=yes" -of GTiff -t_srs epsg:3857 -r bilinear prcp0000_normal.tif prcp0000_normal3857.tif
The data was loaded into the POSTGIS database test1 using the command below
raster2pgsql -I -C -s 3857 /dataFolder/prcp0000_normal3857.tif -F -t 100x100 | psql -d test1
I also have a geojson polygon in SRS 4326 that I would like to use to get some zonal statistics on the raster
The geojson looks like this
'{"type":"Polygon","coordinates":
[[[-108.797607421875,47.73193447949174],
[-109.31396484375,47.234489635299184],
[-107.9296875,46.800059446787316],
[-106.89697265625,47.12247581664114],
[-107.02880859375,47.76148371616669],
[-108.797607421875,47.73193447949174]]]
}'
Based on suggestions from John's answer I am trying the POSTGIS command below
WITH clip(geom) AS
(SELECT
ST_Transform(
ST_SetSRID(
ST_GeomFromGeoJSON(
'{"type":"Polygon","coordinates":
[[[-108.797607421875,47.73193447949174],
[-109.31396484375,47.234489635299184],
[-107.9296875,46.800059446787316],
[-106.89697265625,47.12247581664114],
[-107.02880859375,47.76148371616669],
[-108.797607421875,47.73193447949174]]]
}'),
4326),
3857)
)
SELECT ST_Clip(rast, geom, true)
FROM prcp0000_normal3857, clip;
POSTGIS just returns the errors below with no other info
I feel like I am close but not indicating the band and perhaps messing up the ST_Transform command
Update: I can now successfully run the ST_Summary stats on the table and make the clip. I just need to know how to bring two together.
The ST_SummaryStats quickly returns the mean on the entire table by using the comment below. I just need to know how to pass the clipped raster piece instead of the entire "prcp0000_normal3857" table
SELECT (ST_SummaryStats(rast)).mean FROM "prcp0000_normal3857";
Some more updates. I've fixed projection issues etc and can successfully run ST_SummaryStats using the intersect between the raster and the geojson. That looks like this
SELECT (ST_SummaryStats(rast)).*
FROM "prcp0000_normal3857"
WHERE st_intersects(rast, (ST_Transform(
ST_SetSRID(
ST_GeomFromGeoJSON(
'{"type":"Polygon","coordinates":
[[[-116.8402862548828,37.59464778787345],[-116.75582885742188,37.59464778787345],[-116.75582885742188,37.67050562029702],[-116.8402862548828,37.67050562029702],[-116.8402862548828,37.59464778787345]]]
}'),
4326),
3857)));
This returns summary stats below but not exactly clipped since I used st_intersects. Results are below. I am thinking there are multiple rows because my raster is tiled.
Bringing it all back together. When I try and clip the raster and run st_summarystats, I still get an error that the raster and polygon do not intersect. At this point, the data are correctly projected and I feel I am missing something in the syntax of this command.
WITH clip(geom) AS
(SELECT
(ST_Transform(
ST_SetSRID(
ST_GeomFromGeoJSON(
'{"type":"Polygon","coordinates":
[[[-116.8402862548828,37.59464778787345],[-116.75582885742188,37.59464778787345],[-116.75582885742188,37.67050562029702],[-116.8402862548828,37.67050562029702],[-116.8402862548828,37.59464778787345]]]
}'),
4326),
3857))
)
SELECT (ST_SummaryStats(ST_Clip(rast, geom, true))).* FROM
"prcp0000_normal3857", clip;