[postgis-users] Distance from Rast and LineString overlay

Nicolas Ribot nicky666 at gmail.com
Wed Oct 25 02:28:09 PDT 2023


Hello,

I would polygonize the raster with gdal_polygonize in the database, as it
is very fast. Then extract the boundaries  (linestring), then subdivide
them for better performance.
>From there, I would intersect the 1km lines for each point with forest
boundaries and sort intersection points by distance. First point would give
distanceA, second point distance B, and so on.
By using polygon boundaries, I don't think you need st_touches or other
predicates: intersection between boundaries and direction line gives you
the result.

Nicolas

On Tue, 24 Oct 2023 at 18:27, Eloi Ribeiro via postgis-users <
postgis-users at lists.osgeo.org> wrote:

> 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:
>
> [image: 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 val
> HAVING 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 geom
> FROM 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
>
> _______________________________________________
> postgis-users mailing list
> postgis-users at lists.osgeo.org
> https://lists.osgeo.org/mailman/listinfo/postgis-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/postgis-users/attachments/20231025/80ff86f0/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/20231025/80ff86f0/attachment.png>


More information about the postgis-users mailing list