4

My goal is to use elevation data I loaded into PostgreSQL using pg_raster and intersecting it with a polygon. Within this polygon I would like to find the largest value in the pg_raster.

My first step is to get the intersection to work.

SELECT ST_Intersection(ST_Polygon('LINESTRING(75 29, 75.1 29, 75.1 28.1, 75 28.1,75 29)'::geometry, 4326),rast) 
FROM srtm 
WHERE ST_Intersects(rast,ST_Polygon('LINESTRING(75 29, 75.1 29, 75.1 28.1, 75 28.1,75 29)'::geometry, 4326));

However it is currently giving me the following error

ERROR: array size exceeds the maximum allowed (1073741823)

Each raster record is 3601x3601

I'm using PostgreSQL 11

Vince
20.5k16 gold badges49 silver badges65 bronze badges
asked Aug 28, 2021 at 18:54
2
  • 1
    Have you tried a smaller polygon to see if it yields any results before using this rather big one? Commented Aug 28, 2021 at 20:33
  • This was actually a shrunkened polygon. I've gone as small as a square with the two opposite corners being 75,29-> 75.001,29.001 . Commented Aug 29, 2021 at 20:03

1 Answer 1

2

The problem was the size of the raster and not the size of the polygon (which was pretty obvious in hindsight) To resolve this I sacrificed precision and rescaled the data which got me past this issue.

insert into srtmup select rid, st_rescale(rast,0.003) from srtm;

However though this helped with the st_intersection function I realized that this was turning the rast into a geometry and using the st_interesection on the geometry which is not what I wanted.

Instead what I used was st_clip which would create a new raster clipped to the polygon of interest and this worked with the rescaled and original data.

select rid,(st_summarystats(st_clip(rast,ST_Polygon('LINESTRING(75 29, 75.1 29, 75.1 28.1, 75 28.1,75 29)'::geometry, 4326)))).*
from srtmup 
where st_intersects(rast,ST_Polygon('LINESTRING(75 29, 75.001 29, 75.001 28.001, 75 28.001,75 29)'::geometry, 4326))

;

answered Aug 30, 2021 at 0:20

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.