diff --git a/doc/measures_functions.md b/doc/measures_functions.md index 255e7bf..f42fb09 100644 --- a/doc/measures_functions.md +++ b/doc/measures_functions.md @@ -213,8 +213,8 @@ Name | Description ---- | ----------- extent | A geometry of the extent of the input geometries metadata | A JSON array composed of metadata input objects. Each indicates one desired measure for an output column, and optionally additional parameters about that column -max_timespan_rank | How many historical time periods to include. Defaults to 1 -max_boundary_score_rank | How many alternative boundary levels to include. Defaults to 1 +num_timespan_options | How many historical time periods to include. Defaults to 1 +num_score_options | How many alternative boundary levels to include. Defaults to 1 target_geoms | Target number of geometries. Boundaries with close to this many objects within `extent` will be ranked highest. The schema of the metadata input objects are as follows: @@ -229,8 +229,8 @@ numer_timespan | The desired timespan for the measurement. Defaults to most rec geom_timespan | The desired timespan for the geometry. Defaults to timespan matching numer_timespan if left unspecified. target_area | Instead of aiming to have `target_geoms` in the area of the geometry passed as `extent`, fill this area. Unit is square degrees WGS84. Set this to `0` if you want to use the smallest source geometry for this element of metadata, for example if you're passing in points. target_geoms | Override global `target_geoms` for this element of metadata -max_timespan_rank | Override global `max_timespan_rank` for this element of metadata -max_score_rank | Override global `max_score_rank` for this element of metadata +max_timespan_rank | Only include timespans of this recency (for example, `1` is only the most recent timespan). No limit by default +max_score_rank | Only include boundaries of this relevance (for example, `1` is the most relevant boundary). Is `1` by default #### Returns diff --git a/src/pg/sql/41_observatory_augmentation.sql b/src/pg/sql/41_observatory_augmentation.sql index 710da76..8521a76 100644 --- a/src/pg/sql/41_observatory_augmentation.sql +++ b/src/pg/sql/41_observatory_augmentation.sql @@ -102,8 +102,8 @@ $$ LANGUAGE plpgsql STABLE; CREATE OR REPLACE FUNCTION cdb_observatory.OBS_GetMeta( geom geometry(Geometry, 4326), params JSON, - max_timespan_rank INTEGER DEFAULT NULL, -- cutoff for timespan ranks when there's ambiguity - max_score_rank INTEGER DEFAULT NULL, -- cutoff for geom ranks when there's ambiguity + num_timespan_options INTEGER DEFAULT NULL, -- how many timespan options to show + num_score_options INTEGER DEFAULT NULL, -- how many score options to show target_geoms INTEGER DEFAULT NULL ) RETURNS JSON @@ -115,11 +115,11 @@ DECLARE scores_clause TEXT; result JSON; BEGIN - IF max_timespan_rank IS NULL THEN - max_timespan_rank := 1; + IF num_timespan_options IS NULL THEN + num_timespan_options := 1; END IF; - IF max_score_rank IS NULL THEN - max_score_rank := 1; + IF num_score_options IS NULL THEN + num_score_options := 1; END IF; numer_filters := (SELECT Array_Agg(val) FILTER (WHERE val IS NOT NULL) FROM (SELECT (JSON_Array_Elements(params))->>'numer_id' val) foo); @@ -244,6 +244,10 @@ BEGIN 'numer_id', numer_id, 'timespan_rank', dense_rank() OVER (PARTITION BY id ORDER BY numer_timespan DESC), 'score_rank', dense_rank() OVER (PARTITION BY id ORDER BY score DESC), + 'timespan_rownum', row_number() over + (PARTITION BY id, score ORDER BY numer_timespan DESC, Coalesce(denom_id, '')), + 'score_rownum', row_number() over + (PARTITION BY id, numer_timespan ORDER BY score DESC, Coalesce(denom_id, '')), 'score', scores.score, 'suggested_name', cdb_observatory.FIRST(meta.suggested_name), 'numer_aggregate', cdb_observatory.FIRST(meta.numer_aggregate), @@ -289,8 +293,10 @@ BEGIN GROUP BY id, score, numer_id, denom_id, geom_id, numer_timespan ) SELECT JSON_AGG(metadata ORDER BY id) FROM groups - WHERE timespan_rank <= Coalesce((metadata->>'max_timespan_rank')::INTEGER, $4) - AND score_rank <= Coalesce((metadata->>'max_score_rank')::INTEGER, $5) + WHERE timespan_rank <= Coalesce((metadata->>'max_timespan_rank')::INTEGER, 'infinity'::FLOAT) + AND score_rank <= Coalesce((metadata->>'max_score_rank')::INTEGER, 1) + AND (metadata->>'timespan_rownum')::INTEGER <= $4 + AND (metadata->>'score_rownum')::INTEGER <= $5 $string$, meta_filter_clause, scores_clause) INTO result USING @@ -300,8 +306,8 @@ BEGIN END, target_geoms, (SELECT ARRAY(SELECT json_array_elements_text(params))::json[]), - max_timespan_rank, - max_score_rank, numer_filters, geom_filters + num_timespan_options, + num_score_options, numer_filters, geom_filters ; RETURN result; END; diff --git a/src/pg/test/sql/41_observatory_augmentation_test.sql b/src/pg/test/sql/41_observatory_augmentation_test.sql index 881fc5b..80b1c79 100644 --- a/src/pg/test/sql/41_observatory_augmentation_test.sql +++ b/src/pg/test/sql/41_observatory_augmentation_test.sql @@ -336,7 +336,7 @@ FROM meta; -- OBS_GetMeta for point completes several partial measures with "best" -- metadata, includes geom alternatives if asked WITH meta AS (SELECT cdb_observatory.OBS_GetMeta(cdb_observatory._TestPoint(), - '[{"numer_id": "us.census.acs.B01001002"}]', null, 2) meta) + '[{"numer_id": "us.census.acs.B01001002", "max_score_rank": 2}]', null, 2) meta) SELECT (meta->0->>'id')::integer = 1 id, (meta->0->>'numer_id') = 'us.census.acs.B01001002' numer_id,