Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Work on functions for eoapi #56

Merged
merged 2 commits into from
Mar 15, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions src/eoapi/vector/eoapi/vector/sql/functions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ SELECT
content
FROM pgstac.collections;


CREATE OR REPLACE FUNCTION pg_temp.pgstac_hash(
IN queryhash text,
IN bounds geometry DEFAULT ST_MakeEnvelope(-180,-90,180,90,4326),
Expand Down Expand Up @@ -132,3 +133,55 @@ BEGIN
RETURN;
END;
$$ LANGUAGE PLPGSQL;

CREATE OR REPLACE FUNCTION pg_temp.pgstac_hash_count(
IN queryhash text,
IN bounds geometry DEFAULT ST_MakeEnvelope(-180,-90,180,90,4326),
IN depth int DEFAULT 1,
OUT geom geometry,
OUT cnt bigint
) RETURNS SETOF RECORD AS $$
DECLARE
search record;
xmin float := ST_XMin(bounds);
xmax float := ST_XMax(bounds);
ymin float := ST_YMin(bounds);
ymax float := ST_YMax(bounds);
w float := (xmax - xmin) / depth;
h float := (ymax - ymin) / depth;
q text;
BEGIN
SELECT * INTO search FROM pgstac.searches WHERE hash=queryhash;
DROP VIEW IF EXISTS searchitems;
EXECUTE format($q$
CREATE TEMP VIEW searchitems AS
SELECT geometry
FROM pgstac.items WHERE %s
AND ST_Intersects(geometry, %L)
;
$q$,
search._where,
bounds
);
RETURN QUERY
WITH grid AS (
SELECT
ST_MakeEnvelope(
xmin + w * (a-1),
ymin + h * (b-1),
xmin + w * a,
ymin + h * b,
4326
vincentsarago marked this conversation as resolved.
Show resolved Hide resolved
) as geom
FROM generate_series(1, depth) a, generate_series(1, depth) b
)
SELECT
grid.geom,
count(*) as cnt
FROM
grid
JOIN searchitems ON (ST_Intersects(searchitems.geometry, grid.geom))
GROUP BY 1
;
END;
$$ LANGUAGE PLPGSQL;