[postgis-users] Distance from Rast and LineString overlay

Eloi Ribeiro mail at eloiribeiro.eu
Tue Oct 24 09:26:08 PDT 2023


Dear PostGIS users,

I have a [raster](https://data.jrc.ec.europa.eu/dataset/35fb1231-849b-4017-89d8-9cadbaf9d555) (of 100m pixel) of forest (val=1) / no forest (val=NULL) and a set of points that always fall inside forest areas.

I need to calculate the distance (up to 1km), in a given direction (let's say West, 300 radians), to the border of the forest patch where it falls in (distance A in fig.). Then, the distance from the previous forest border to the next forest border (distance B in fig.), following the same direction. Like so:

[Screenshot from 2023-10-24 18-19-16.png]

I was able to do this with the following steps:
1. Vectorize forest map. Note the ST_Union, to eliminate the borders of the raster tiles and avoid these being confused as forest border.

SELECT (ST_Dump(ST_Union(geom))).geom::geometry(Polygon,3035)
FROM (
SELECT dp.*
FROM storm.forest_2020_europe,
LATERAL ST_DumpAsPolygons(rast) AS dp
) As foo
GROUP BY valHAVING val = 1;

2. Create Linestring from point, as starting point, following 300 radians and 1km length.

SELECT gid,
'1km 300 rad',
ST_MakeLine(geom, ST_Translate(geom, sin(300)*1000, cos(300)*1000)) AS geomFROM storm.point;

3. Overlay Linestring from step 2 with vectorize forest map and get the distances from Linestring segments. This part has too much SQL to put it here. I have used ST_Intersection and ST_Difference, and then ST_Touches to make sure the segment touches the first point, for the first distance and touches the EndPoint of the first segment to get the second distance.

This at national level worked just fine. But now at continent level, the resulting vectorized polygons of the forest patches are just too big and takes forever to vectorize. So, I need to do this directly with the raster forest map.

Any hints how could this be accomplished?
Thanks!

Cheers,

Eloi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/postgis-users/attachments/20231024/b17f658f/attachment.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Screenshot from 2023-10-24 18-19-16.png
Type: image/png
Size: 55040 bytes
Desc: not available
URL: <http://lists.osgeo.org/pipermail/postgis-users/attachments/20231024/b17f658f/attachment.png>


More information about the postgis-users mailing list