bcgov / watershedBC

Summarize watershed attributes in BC
https://bcgov-env.shinyapps.io/watershedBC/
Apache License 2.0
7 stars 0 forks source link

Add options for processing speed... #5

Open bevingtona opened 10 months ago

bevingtona commented 10 months ago

Simplify polygons? Display as points?

bevingtona commented 8 months ago

Use ST_Subdivide in query ? Looks like it can speed things up substantially... like 300%

library(microbenchmark)
library(sf)

qq1 <- paste0("WITH subdivided_my_wkt AS (SELECT ST_Subdivide(ST_GeomFromText('", new_ws2_wkt, "', 3005), 100) AS geom),
             clipped_polys AS (
                 SELECT w.*, 
                        ST_Intersection(w.geom, s.geom) AS clipped_geom
                 FROM fwa_wetlands w 
                 CROSS JOIN subdivided_my_wkt s
                 WHERE ST_Intersects(w.geom, s.geom))
             SELECT *, 
             ST_Area(clipped_geom) AS clipped_area_m2
             FROM clipped_polys")

qq2 <- paste0("SELECT w.*, ST_Intersection(w.geom, ST_GeomFromText('", new_ws2_wkt, "', 3005)) AS clipped_geom
               FROM fwa_wetlands w 
               WHERE ST_Intersects(w.geom, ST_GeomFromText('", new_ws2_wkt, "', 3005))")

microbenchmark(st_read(conn, query = qq1),st_read(conn, query = qq2), times = 1)