finishing getgeometrybyid

This commit is contained in:
Andy Eschbacher
2016-04-21 09:48:20 -04:00
parent 8f161c1e68
commit 16d65d01f3

View File

@@ -1,14 +1,14 @@
-- Returns the polygon(s) that overlap with the input geometry.
-- Input:
-- :param geom geometry: input geometry
-- :param geometry_level text: table to get polygon from (can be approximate name)
-- :param boundary_id text: table to get polygon from (can be approximate name)
-- :param use_literal boolean: use the literal table name (defaults to true)
-- From an input point geometry, find the boundary which intersects with the centroid of the input geometry
CREATE OR REPLACE FUNCTION Andy_OBS_GetGeometry(
geom geometry(Geometry, 4326),
geometry_level text DEFAULT '"us.census.tiger".census_tract', -- TODO: from a specified column id list (e.g., list of available catalog, see OBS_List)
boundary_id text DEFAULT '"us.census.tiger".census_tract', -- TODO: from a specified column id list (e.g., list of available catalog, see OBS_List)
time_span text DEFAULT '2009 - 2013')
RETURNS geometry(Geometry, 4326)
AS $$
@@ -26,12 +26,12 @@ BEGIN
RAISE EXCEPTION 'Invalid geometry type (%), expecting ''ST_Point''', ST_GeometryType(geom);
END IF;
target_table_list := OBS_SearchTables(geometry_level, time_span);
target_table_list := OBS_SearchTables(boundary_id, time_span);
-- if no tables are found, raise notice and return null
IF array_length(target_table_list, 1) IS NULL
THEN
RAISE NOTICE 'No boundaries found for ''%'' in ''%''', ST_AsText(geom), geometry_level;
RAISE NOTICE 'No boundaries found for ''%'' in ''%''', ST_AsText(geom), boundary_id;
RETURN NULL::geometry;
ELSE
-- else, choose first result
@@ -56,7 +56,7 @@ $$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION ANDY_OBS_GetGeometryId(
geom geometry(Geometry, 4326),
geometry_level text DEFAULT '"us.census.tiger".census_tract',
boundary_id text DEFAULT '"us.census.tiger".census_tract',
time_span text DEFAULT '2009 - 2013'
)
RETURNS text
@@ -73,12 +73,12 @@ BEGIN
RAISE EXCEPTION 'Error: Invalid geometry type (%), expecting ''ST_Point''', ST_GeometryType(geom);
END IF;
target_table_list := OBS_SearchTables(geometry_level, time_span);
target_table_list := OBS_SearchTables(boundary_id, time_span);
-- if no tables are found, raise error
IF array_length(target_table_list, 1) IS NULL
THEN
RAISE NOTICE 'Error: No boundaries found for ''%''', geometry_level;
RAISE NOTICE 'Error: No boundaries found for ''%''', boundary_id;
RETURN NULL::text;
ELSE
target_table = target_table_list[1];
@@ -100,11 +100,14 @@ BEGIN
END;
$$ LANGUAGE plpgsql;
--
-- Given a geometry reference (e.g., geoid for US Census), and it's geometry level (see OBS_ListGeomColumns() for all available boundary ids), give back the boundary that corresponds to that reference and level.
-- @param geom_ref text: identifier for boundary geometry corresponding to a boundary id `boundary_id`. E.g., '36047' is a geoid for US Census Tiger boundaries corresponding to a county (047) in New York State (36)
-- @param boundary_id:
CREATE OR REPLACE FUNCTION OBS_GetGeometryById(
geom_ref text, -- ex: '36047'
geometry_level text -- ex: '"us.census.tiger".county'
boundary_id text -- ex: '"us.census.tiger".county'
)
RETURNS geometry(geometry, 4326)
AS $$
@@ -136,7 +139,7 @@ BEGIN
geom_t.id = geom_ct.table_id and
geom_ct.column_id = geom_c.id and
geom_c.type ilike 'geometry'
$string$, geometry_level
$string$, boundary_id
) INTO geoid_colname, target_table, geom_colname;
IF target_table IS NULL
@@ -146,12 +149,13 @@ BEGIN
END IF;
-- retrieve boundary
EXECUTE format(
'SELECT t.%s
FROM observatory.%s As t
WHERE t.%s = ''%s''
LIMIT 1', geom_colname, target_table, geoid_colname, geom_ref)
INTO boundary;
EXECUTE
'SELECT t.$1
FROM observatory.$2 As t
WHERE t.$1 = ''$3''
LIMIT 1'
INTO boundary
USING geom_colname, target_table, geom_ref;
RETURN boundary;