prefer geographpic precision over most recent timespan, handles issues emerging from inclusion of 1-year acs

This commit is contained in:
John Krauss 2017-04-25 18:53:12 +00:00
parent 60c7f54315
commit 28694163a2
3 changed files with 21 additions and 15 deletions

View File

@ -213,8 +213,8 @@ Name | Description
---- | ----------- ---- | -----------
extent | A geometry of the extent of the input geometries 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 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 num_timespan_options | 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_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. 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: 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. 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_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 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_timespan_rank | Only include timespans of this recency (for example, `1` is only the most recent timespan). No limit by default
max_score_rank | Override global `max_score_rank` for this element of metadata max_score_rank | Only include boundaries of this relevance (for example, `1` is the most relevant boundary). Is `1` by default
#### Returns #### Returns

View File

@ -102,8 +102,8 @@ $$ LANGUAGE plpgsql STABLE;
CREATE OR REPLACE FUNCTION cdb_observatory.OBS_GetMeta( CREATE OR REPLACE FUNCTION cdb_observatory.OBS_GetMeta(
geom geometry(Geometry, 4326), geom geometry(Geometry, 4326),
params JSON, params JSON,
max_timespan_rank INTEGER DEFAULT NULL, -- cutoff for timespan ranks when there's ambiguity num_timespan_options INTEGER DEFAULT NULL, -- how many timespan options to show
max_score_rank INTEGER DEFAULT NULL, -- cutoff for geom ranks when there's ambiguity num_score_options INTEGER DEFAULT NULL, -- how many score options to show
target_geoms INTEGER DEFAULT NULL target_geoms INTEGER DEFAULT NULL
) )
RETURNS JSON RETURNS JSON
@ -115,11 +115,11 @@ DECLARE
scores_clause TEXT; scores_clause TEXT;
result JSON; result JSON;
BEGIN BEGIN
IF max_timespan_rank IS NULL THEN IF num_timespan_options IS NULL THEN
max_timespan_rank := 1; num_timespan_options := 1;
END IF; END IF;
IF max_score_rank IS NULL THEN IF num_score_options IS NULL THEN
max_score_rank := 1; num_score_options := 1;
END IF; END IF;
numer_filters := (SELECT Array_Agg(val) FILTER (WHERE val IS NOT NULL) FROM (SELECT (JSON_Array_Elements(params))->>'numer_id' val) foo); 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, 'numer_id', numer_id,
'timespan_rank', dense_rank() OVER (PARTITION BY id ORDER BY numer_timespan DESC), '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), '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, 'score', scores.score,
'suggested_name', cdb_observatory.FIRST(meta.suggested_name), 'suggested_name', cdb_observatory.FIRST(meta.suggested_name),
'numer_aggregate', cdb_observatory.FIRST(meta.numer_aggregate), '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 GROUP BY id, score, numer_id, denom_id, geom_id, numer_timespan
) SELECT JSON_AGG(metadata ORDER BY id) ) SELECT JSON_AGG(metadata ORDER BY id)
FROM groups FROM groups
WHERE timespan_rank <= Coalesce((metadata->>'max_timespan_rank')::INTEGER, $4) WHERE timespan_rank <= Coalesce((metadata->>'max_timespan_rank')::INTEGER, 'infinity'::FLOAT)
AND score_rank <= Coalesce((metadata->>'max_score_rank')::INTEGER, $5) 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) $string$, meta_filter_clause, scores_clause)
INTO result INTO result
USING USING
@ -300,8 +306,8 @@ BEGIN
END, END,
target_geoms, target_geoms,
(SELECT ARRAY(SELECT json_array_elements_text(params))::json[]), (SELECT ARRAY(SELECT json_array_elements_text(params))::json[]),
max_timespan_rank, num_timespan_options,
max_score_rank, numer_filters, geom_filters num_score_options, numer_filters, geom_filters
; ;
RETURN result; RETURN result;
END; END;

View File

@ -336,7 +336,7 @@ FROM meta;
-- OBS_GetMeta for point completes several partial measures with "best" -- OBS_GetMeta for point completes several partial measures with "best"
-- metadata, includes geom alternatives if asked -- metadata, includes geom alternatives if asked
WITH meta AS (SELECT cdb_observatory.OBS_GetMeta(cdb_observatory._TestPoint(), 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 SELECT
(meta->0->>'id')::integer = 1 id, (meta->0->>'id')::integer = 1 id,
(meta->0->>'numer_id') = 'us.census.acs.B01001002' numer_id, (meta->0->>'numer_id') = 'us.census.acs.B01001002' numer_id,