major refactor of internals
This commit is contained in:
parent
4886222776
commit
7c093741dc
@ -38,24 +38,15 @@ def get_tablename_query(column_id, boundary_id, timespan):
|
||||
timespan, give back the current table hash from the data observatory
|
||||
"""
|
||||
return """
|
||||
SELECT t.tablename, geoid_ct.colname colname, t.id table_id
|
||||
FROM observatory.obs_table t,
|
||||
observatory.obs_column_table geoid_ct,
|
||||
observatory.obs_column_table data_ct
|
||||
WHERE
|
||||
t.id = geoid_ct.table_id AND
|
||||
t.id = data_ct.table_id AND
|
||||
geoid_ct.column_id =
|
||||
(SELECT source_id
|
||||
FROM observatory.obs_column_to_column
|
||||
WHERE target_id = '{boundary_id}'
|
||||
AND reltype = 'geom_ref'
|
||||
) AND
|
||||
data_ct.column_id = '{column_id}' AND
|
||||
timespan = '{timespan}'
|
||||
""".format(column_id=column_id,
|
||||
boundary_id=boundary_id,
|
||||
timespan=timespan)
|
||||
SELECT numer_tablename, numer_geomref_colname, numer_tid,
|
||||
geom_tablename, geom_geomref_colname, geom_tid
|
||||
FROM observatory.obs_meta
|
||||
WHERE numer_id = '{numer_id}' AND
|
||||
geom_id = '{geom_id}' AND
|
||||
numer_timespan = '{numer_timespan}'
|
||||
""".format(numer_id=column_id,
|
||||
geom_id=boundary_id,
|
||||
numer_timespan=timespan)
|
||||
|
||||
|
||||
METADATA_TABLES = ['obs_table', 'obs_column_table', 'obs_column', 'obs_column_tag',
|
||||
@ -76,7 +67,7 @@ FIXTURES = [
|
||||
('us.census.acs.B05001006_quantile', 'us.census.tiger.census_tract', '2010 - 2014'),
|
||||
('us.census.acs.B08006001_quantile', 'us.census.tiger.census_tract', '2010 - 2014'),
|
||||
('us.census.acs.B08006002_quantile', 'us.census.tiger.census_tract', '2010 - 2014'),
|
||||
('us.census.acs.B08006008_quantile', 'us.census.tiger.census_tract', '2010 - 2014'),
|
||||
('us.census.acs.B08301010_quantile', 'us.census.tiger.census_tract', '2010 - 2014'),
|
||||
('us.census.acs.B08006009_quantile', 'us.census.tiger.census_tract', '2010 - 2014'),
|
||||
('us.census.acs.B08006011_quantile', 'us.census.tiger.census_tract', '2010 - 2014'),
|
||||
('us.census.acs.B08006015_quantile', 'us.census.tiger.census_tract', '2010 - 2014'),
|
||||
@ -130,7 +121,6 @@ FIXTURES = [
|
||||
('us.census.acs.B15003022', 'us.census.tiger.block_group', '2010 - 2014'),
|
||||
('us.census.acs.B15003023', 'us.census.tiger.block_group', '2010 - 2014'),
|
||||
('us.census.acs.B19013001', 'us.census.tiger.block_group', '2010 - 2014'),
|
||||
('us.census.acs.B19083001', 'us.census.tiger.block_group', '2010 - 2014'),
|
||||
('us.census.acs.B19301001', 'us.census.tiger.block_group', '2010 - 2014'),
|
||||
('us.census.acs.B25001001', 'us.census.tiger.block_group', '2010 - 2014'),
|
||||
('us.census.acs.B25002003', 'us.census.tiger.block_group', '2010 - 2014'),
|
||||
@ -143,15 +133,6 @@ FIXTURES = [
|
||||
('us.census.acs.B25081002', 'us.census.tiger.block_group', '2010 - 2014'),
|
||||
('us.census.acs.B08134001', 'us.census.tiger.block_group', '2010 - 2014'),
|
||||
('us.census.acs.B08134002', 'us.census.tiger.block_group', '2010 - 2014'),
|
||||
('us.census.acs.B08134003', 'us.census.tiger.block_group', '2010 - 2014'),
|
||||
('us.census.acs.B08134004', 'us.census.tiger.block_group', '2010 - 2014'),
|
||||
('us.census.acs.B08134005', 'us.census.tiger.block_group', '2010 - 2014'),
|
||||
('us.census.acs.B08134006', 'us.census.tiger.block_group', '2010 - 2014'),
|
||||
('us.census.acs.B08134007', 'us.census.tiger.block_group', '2010 - 2014'),
|
||||
('us.census.acs.B08134008', 'us.census.tiger.block_group', '2010 - 2014'),
|
||||
('us.census.acs.B08134009', 'us.census.tiger.block_group', '2010 - 2014'),
|
||||
('us.census.acs.B08134010', 'us.census.tiger.block_group', '2010 - 2014'),
|
||||
('us.census.acs.B08135001', 'us.census.tiger.block_group', '2010 - 2014'),
|
||||
('us.census.acs.B19001002', 'us.census.tiger.block_group', '2010 - 2014'),
|
||||
('us.census.acs.B19001003', 'us.census.tiger.block_group', '2010 - 2014'),
|
||||
('us.census.acs.B19001004', 'us.census.tiger.block_group', '2010 - 2014'),
|
||||
@ -168,27 +149,71 @@ FIXTURES = [
|
||||
('us.census.acs.B19001015', 'us.census.tiger.block_group', '2010 - 2014'),
|
||||
('us.census.acs.B19001016', 'us.census.tiger.block_group', '2010 - 2014'),
|
||||
('us.census.acs.B19001017', 'us.census.tiger.block_group', '2010 - 2014'),
|
||||
('us.census.tiger.census_tract', 'us.census.tiger.census_tract', '2015'),
|
||||
('us.census.tiger.census_tract', 'us.census.tiger.census_tract', '2014'),
|
||||
('us.census.tiger.block_group', 'us.census.tiger.block_group', '2015'),
|
||||
('us.census.tiger.zcta5', 'us.census.tiger.zcta5', '2015'),
|
||||
('us.census.tiger.county', 'us.census.tiger.county', '2015'),
|
||||
('us.census.acs.B01001002', 'us.census.tiger.block_group', '2010 - 2014'),
|
||||
('us.census.acs.B01003001', 'us.census.tiger.census_tract', '2010 - 2014'),
|
||||
('us.census.acs.B01003001_quantile', 'us.census.tiger.census_tract', '2010 - 2014'),
|
||||
('us.census.acs.B01003001', 'us.census.tiger.block_group', '2010 - 2014'),
|
||||
('us.census.spielman_singleton_segments.X2', 'us.census.tiger.census_tract', '2010 - 2014'),
|
||||
('us.census.acs.B01001002', 'us.census.tiger.census_tract', '2010 - 2014'),
|
||||
('us.census.acs.B01001026', 'us.census.tiger.census_tract', '2010 - 2014'),
|
||||
('us.census.acs.B01002001', 'us.census.tiger.census_tract', '2010 - 2014'),
|
||||
('us.census.acs.B03002003', 'us.census.tiger.census_tract', '2010 - 2014'),
|
||||
('us.census.acs.B03002004', 'us.census.tiger.census_tract', '2010 - 2014'),
|
||||
('us.census.acs.B03002006', 'us.census.tiger.census_tract', '2010 - 2014'),
|
||||
('us.census.acs.B03002012', 'us.census.tiger.census_tract', '2010 - 2014'),
|
||||
('us.census.acs.B03002005', 'us.census.tiger.census_tract', '2010 - 2014'),
|
||||
('us.census.acs.B03002008', 'us.census.tiger.census_tract', '2010 - 2014'),
|
||||
('us.census.acs.B03002009', 'us.census.tiger.census_tract', '2010 - 2014'),
|
||||
('us.census.acs.B03002002', 'us.census.tiger.census_tract', '2010 - 2014'),
|
||||
('us.census.acs.B11001001', 'us.census.tiger.census_tract', '2010 - 2014'),
|
||||
('us.census.acs.B15003001', 'us.census.tiger.census_tract', '2010 - 2014'),
|
||||
('us.census.acs.B15003017', 'us.census.tiger.census_tract', '2010 - 2014'),
|
||||
('us.census.acs.B15003019', 'us.census.tiger.census_tract', '2010 - 2014'),
|
||||
('us.census.acs.B15003020', 'us.census.tiger.census_tract', '2010 - 2014'),
|
||||
('us.census.acs.B15003021', 'us.census.tiger.census_tract', '2010 - 2014'),
|
||||
('us.census.acs.B15003022', 'us.census.tiger.census_tract', '2010 - 2014'),
|
||||
('us.census.acs.B15003023', 'us.census.tiger.census_tract', '2010 - 2014'),
|
||||
('us.census.acs.B19013001', 'us.census.tiger.census_tract', '2010 - 2014'),
|
||||
('us.census.acs.B19083001', 'us.census.tiger.census_tract', '2010 - 2014'),
|
||||
('us.census.acs.B19301001', 'us.census.tiger.census_tract', '2010 - 2014'),
|
||||
('us.census.acs.B25001001', 'us.census.tiger.census_tract', '2010 - 2014'),
|
||||
('us.census.acs.B25002003', 'us.census.tiger.census_tract', '2010 - 2014'),
|
||||
('us.census.acs.B25004002', 'us.census.tiger.census_tract', '2010 - 2014'),
|
||||
('us.census.acs.B25004004', 'us.census.tiger.census_tract', '2010 - 2014'),
|
||||
('us.census.acs.B25058001', 'us.census.tiger.census_tract', '2010 - 2014'),
|
||||
('us.census.acs.B25071001', 'us.census.tiger.census_tract', '2010 - 2014'),
|
||||
('us.census.acs.B25075001', 'us.census.tiger.census_tract', '2010 - 2014'),
|
||||
('us.census.acs.B25075025', 'us.census.tiger.census_tract', '2010 - 2014'),
|
||||
('us.census.acs.B25081002', 'us.census.tiger.census_tract', '2010 - 2014'),
|
||||
('us.census.acs.B08134001', 'us.census.tiger.census_tract', '2010 - 2014'),
|
||||
('us.census.acs.B08134002', 'us.census.tiger.census_tract', '2010 - 2014'),
|
||||
('us.census.acs.B08134008', 'us.census.tiger.census_tract', '2010 - 2014'),
|
||||
('us.census.acs.B08134008', 'us.census.tiger.census_tract', '2010 - 2014'),
|
||||
('us.census.acs.B08134010', 'us.census.tiger.census_tract', '2010 - 2014'),
|
||||
('us.census.acs.B19001002', 'us.census.tiger.census_tract', '2010 - 2014'),
|
||||
('us.census.acs.B19001003', 'us.census.tiger.census_tract', '2010 - 2014'),
|
||||
('us.census.acs.B19001004', 'us.census.tiger.census_tract', '2010 - 2014'),
|
||||
('us.census.acs.B19001005', 'us.census.tiger.census_tract', '2010 - 2014'),
|
||||
('us.census.acs.B19001006', 'us.census.tiger.census_tract', '2010 - 2014'),
|
||||
('us.census.acs.B19001007', 'us.census.tiger.census_tract', '2010 - 2014'),
|
||||
('us.census.acs.B19001008', 'us.census.tiger.census_tract', '2010 - 2014'),
|
||||
('us.census.acs.B19001009', 'us.census.tiger.census_tract', '2010 - 2014'),
|
||||
('us.census.acs.B19001010', 'us.census.tiger.census_tract', '2010 - 2014'),
|
||||
('us.census.acs.B19001011', 'us.census.tiger.census_tract', '2010 - 2014'),
|
||||
('us.census.acs.B19001012', 'us.census.tiger.census_tract', '2010 - 2014'),
|
||||
('us.census.acs.B19001013', 'us.census.tiger.census_tract', '2010 - 2014'),
|
||||
('us.census.acs.B19001014', 'us.census.tiger.census_tract', '2010 - 2014'),
|
||||
('us.census.acs.B19001015', 'us.census.tiger.census_tract', '2010 - 2014'),
|
||||
('us.census.acs.B19001016', 'us.census.tiger.census_tract', '2010 - 2014'),
|
||||
('us.census.acs.B19001017', 'us.census.tiger.census_tract', '2010 - 2014'),
|
||||
('us.census.spielman_singleton_segments.X10', 'us.census.tiger.census_tract', '2010 - 2014'),
|
||||
('us.census.spielman_singleton_segments.X31', 'us.census.tiger.census_tract', '2010 - 2014'),
|
||||
('us.census.spielman_singleton_segments.X55', 'us.census.tiger.census_tract', '2010 - 2014'),
|
||||
('us.zillow.AllHomes_Zhvi', 'us.census.tiger.zcta5', '2014-01'),
|
||||
('us.zillow.AllHomes_Zhvi', 'us.census.tiger.zcta5', '2016-06'),
|
||||
('whosonfirst.wof_country_geom', 'whosonfirst.wof_country_geom', '2016'),
|
||||
('us.census.tiger.zcta5_clipped', 'us.census.tiger.zcta5_clipped', '2014'),
|
||||
('us.census.tiger.block_group_clipped', 'us.census.tiger.block_group_clipped', '2014'),
|
||||
('us.census.tiger.census_tract_clipped', 'us.census.tiger.census_tract_clipped', '2014'),
|
||||
('us.census.tiger.pointlm', 'us.census.tiger.pointlm', '2016'),
|
||||
('us.census.tiger.prisecroads', 'us.census.tiger.prisecroads', '2016'),
|
||||
('whosonfirst.wof_country_name', 'whosonfirst.wof_country_geom', '2016'),
|
||||
('us.census.acs.B01003001', 'us.census.tiger.zcta5_clipped', '2010 - 2014'),
|
||||
('us.census.acs.B01003001', 'us.census.tiger.block_group_clipped', '2010 - 2014'),
|
||||
('us.census.acs.B01003001', 'us.census.tiger.census_tract_clipped', '2010 - 2014'),
|
||||
('us.census.tiger.fullname', 'us.census.tiger.pointlm_geom', '2016'),
|
||||
('us.census.tiger.fullname', 'us.census.tiger.prisecroads_geom', '2016'),
|
||||
('us.census.tiger.name', 'us.census.tiger.county', '2015'),
|
||||
]
|
||||
|
||||
OUTFILE_PATH = os.path.join(os.path.dirname(__file__), '..',
|
||||
@ -238,15 +263,19 @@ def main():
|
||||
tablename_query = get_tablename_query(column_id, boundary_id, timespan)
|
||||
resp = query(tablename_query).fetchone()
|
||||
if resp:
|
||||
tablename, colname, table_id = resp
|
||||
numer_tablename, numer_colname, numer_table_id = resp[0:3]
|
||||
geom_tablename, geom_colname, geom_table_id = resp[3:6]
|
||||
else:
|
||||
print("Could not find table for {}, {}, {}".format(
|
||||
raise Exception("Could not find table for {}, {}, {}".format(
|
||||
column_id, boundary_id, timespan))
|
||||
continue
|
||||
table_colname = (tablename, colname, boundary_id, table_id, )
|
||||
if table_colname not in unique_tables:
|
||||
print(table_colname)
|
||||
unique_tables.add(table_colname)
|
||||
numer = (numer_tablename, numer_colname, numer_table_id, )
|
||||
geom = (geom_tablename, geom_colname, geom_table_id, )
|
||||
if numer not in unique_tables:
|
||||
print(numer)
|
||||
unique_tables.add(numer)
|
||||
if geom not in unique_tables:
|
||||
print(geom)
|
||||
unique_tables.add(geom)
|
||||
|
||||
print unique_tables
|
||||
|
||||
@ -297,12 +326,12 @@ def main():
|
||||
])
|
||||
elif tablename in ('obs_column_table', 'obs_column_table_tile',
|
||||
'obs_column_table_tile_simple'):
|
||||
where = 'WHERE column_id IN ({numer_ids}) ' \
|
||||
'OR column_id IN ({geom_ids}) ' \
|
||||
'OR table_id IN ({table_ids}) '.format(
|
||||
where = '''WHERE table_id IN ({table_ids}) AND
|
||||
(column_id IN ({numer_ids}) OR column_id IN ({geom_ids}))
|
||||
'''.format(
|
||||
numer_ids=','.join(["'{}'".format(x) for x, _, _ in FIXTURES]),
|
||||
geom_ids=','.join(["'{}'".format(x) for _, x, _ in FIXTURES]),
|
||||
table_ids=','.join(["'{}'".format(x) for _, _, _, x in unique_tables])
|
||||
table_ids=','.join(["'{}'".format(x) for _, _, x in unique_tables])
|
||||
)
|
||||
elif tablename == 'obs_column_to_column':
|
||||
where = "WHERE " + " OR ".join([
|
||||
@ -314,21 +343,20 @@ def main():
|
||||
where = 'WHERE timespan IN ({timespans}) ' \
|
||||
'OR id IN ({table_ids}) '.format(
|
||||
timespans=','.join(["'{}'".format(x) for _, _, x in FIXTURES]),
|
||||
table_ids=','.join(["'{}'".format(x) for _, _, _, x in unique_tables])
|
||||
table_ids=','.join(["'{}'".format(x) for _, _, x in unique_tables])
|
||||
)
|
||||
else:
|
||||
where = ''
|
||||
dump('*', tablename, where)
|
||||
|
||||
for tablename, colname, boundary_id, table_id in unique_tables:
|
||||
if 'zcta5' in boundary_id:
|
||||
for tablename, colname, table_id in unique_tables:
|
||||
if 'zcta5' in table_id or 'zillow_zip' in table_id:
|
||||
where = '\'11%\''
|
||||
compare = 'LIKE'
|
||||
elif boundary_id in ('us.census.tiger.prisecroads',
|
||||
'us.census.tiger.pointlm'):
|
||||
elif 'pri_sec_roads' in table_id or 'point_landmark' in table_id:
|
||||
dump('*', tablename, 'WHERE geom && ST_MakeEnvelope(-74,40.69,-73.9,40.72, 4326)')
|
||||
continue
|
||||
elif 'whosonfirst' in boundary_id:
|
||||
elif 'whosonfirst' in table_id:
|
||||
where = '(\'85632785\',\'85633051\',\'85633111\',\'85633147\',\'85633253\',\'85633267\')'
|
||||
compare = 'IN'
|
||||
else:
|
||||
|
@ -219,4 +219,3 @@ CREATE AGGREGATE cdb_observatory.FIRST (
|
||||
basetype = anyelement,
|
||||
stype = anyelement
|
||||
);
|
||||
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -485,7 +485,7 @@ BEGIN
|
||||
, null::numeric meanmediansize
|
||||
FROM clipped_geom_reagg
|
||||
) SELECT
|
||||
((100.0 / (1+abs(log(1 + $3) - log(1 + numgeoms::Numeric)))) * percentfill)::Numeric
|
||||
((100.0 / (1+abs(log(0.0001 + $3) - log(0.0001 + numgeoms::Numeric)))) * percentfill)::Numeric
|
||||
AS score, *
|
||||
FROM final
|
||||
$string$ USING bounds, filter_geom_ids, desired_num_geoms;
|
||||
|
@ -40,44 +40,11 @@ BEGIN
|
||||
RAISE EXCEPTION 'Invalid geometry type (%), expecting ''ST_Point''', ST_GeometryType(geom);
|
||||
END IF;
|
||||
|
||||
-- choose appropriate table based on time_span
|
||||
IF time_span IS NULL
|
||||
THEN
|
||||
SELECT x.target_tables INTO target_table
|
||||
FROM cdb_observatory._OBS_SearchTables(boundary_id,
|
||||
time_span) As x(target_tables,
|
||||
timespans)
|
||||
ORDER BY x.timespans DESC
|
||||
LIMIT 1;
|
||||
ELSE
|
||||
-- TODO: modify for only one table returned instead of arbitrarily choosing
|
||||
-- one with LIMIT 1 (could be conflict between clipped vs non-clipped
|
||||
-- boundaries in the metadata tables)
|
||||
SELECT x.target_tables INTO target_table
|
||||
FROM cdb_observatory._OBS_SearchTables(boundary_id,
|
||||
time_span) As x(target_tables,
|
||||
timespans)
|
||||
WHERE x.timespans = time_span
|
||||
LIMIT 1;
|
||||
END IF;
|
||||
|
||||
-- if no tables are found, raise notice and return null
|
||||
IF target_table IS NULL
|
||||
THEN
|
||||
--RAISE NOTICE 'No boundaries found for ''%'' in ''%''', ST_AsText(geom), boundary_id;
|
||||
RETURN NULL::geometry;
|
||||
END IF;
|
||||
|
||||
--RAISE NOTICE 'target_table: %', target_table;
|
||||
|
||||
-- return the first boundary in intersections
|
||||
EXECUTE format(
|
||||
'SELECT the_geom
|
||||
FROM observatory.%I
|
||||
WHERE ST_Intersects($1, the_geom)
|
||||
LIMIT 1', target_table)
|
||||
INTO boundary
|
||||
USING geom;
|
||||
EXECUTE $query$
|
||||
SELECT * FROM cdb_observatory._OBS_GetBoundariesByGeometry($1, $2, $3) LIMIT 1
|
||||
$query$ INTO boundary
|
||||
USING geom, boundary_id, time_span;
|
||||
|
||||
RETURN boundary;
|
||||
|
||||
@ -111,67 +78,17 @@ CREATE OR REPLACE FUNCTION cdb_observatory.OBS_GetBoundaryId(
|
||||
RETURNS text
|
||||
AS $$
|
||||
DECLARE
|
||||
output_id text;
|
||||
target_table text;
|
||||
geoid_colname text;
|
||||
result TEXT;
|
||||
BEGIN
|
||||
|
||||
-- If not point, raise error
|
||||
IF ST_GeometryType(geom) != 'ST_Point'
|
||||
THEN
|
||||
RAISE EXCEPTION 'Invalid geometry type (%), expecting ''ST_Point''', ST_GeometryType(geom);
|
||||
END IF;
|
||||
|
||||
-- choose appropriate table based on time_span
|
||||
IF time_span IS NULL
|
||||
THEN
|
||||
SELECT x.target_tables INTO target_table
|
||||
FROM cdb_observatory._OBS_SearchTables(boundary_id,
|
||||
time_span) As x(target_tables,
|
||||
timespans)
|
||||
ORDER BY x.timespans DESC
|
||||
LIMIT 1;
|
||||
ELSE
|
||||
SELECT x.target_tables INTO target_table
|
||||
FROM cdb_observatory._OBS_SearchTables(boundary_id,
|
||||
time_span) As x(target_tables,
|
||||
timespans)
|
||||
WHERE x.timespans = time_span
|
||||
LIMIT 1;
|
||||
END IF;
|
||||
|
||||
-- if no tables are found, raise notice and return null
|
||||
IF target_table IS NULL
|
||||
THEN
|
||||
--RAISE NOTICE 'Warning: No boundaries found for ''%''', boundary_id;
|
||||
RETURN NULL::text;
|
||||
END IF;
|
||||
|
||||
EXECUTE
|
||||
'SELECT ct.colname
|
||||
FROM observatory.obs_column_to_column c2c,
|
||||
observatory.obs_column_table ct,
|
||||
observatory.obs_table t
|
||||
WHERE c2c.reltype = ''geom_ref''
|
||||
AND ct.column_id = c2c.source_id
|
||||
AND ct.table_id = t.id
|
||||
AND t.tablename = $1'
|
||||
INTO geoid_colname
|
||||
USING target_table;
|
||||
|
||||
--RAISE NOTICE 'target_table: %, geoid_colname: %', target_table, geoid_colname;
|
||||
|
||||
-- return geometry id column value
|
||||
EXECUTE format(
|
||||
'SELECT %I::text
|
||||
FROM observatory.%I
|
||||
WHERE ST_Intersects($1, the_geom)
|
||||
LIMIT 1', geoid_colname, target_table)
|
||||
INTO output_id
|
||||
USING geom;
|
||||
|
||||
RETURN output_id;
|
||||
EXECUTE $query$
|
||||
SELECT geom_refs FROM cdb_observatory._OBS_GetBoundariesByGeometry(
|
||||
$1, $2, $3) LIMIT 1
|
||||
$query$
|
||||
INTO result
|
||||
USING geom, boundary_id, time_span;
|
||||
|
||||
RETURN result;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
|
||||
@ -203,35 +120,21 @@ CREATE OR REPLACE FUNCTION cdb_observatory.OBS_GetBoundaryById(
|
||||
RETURNS geometry(geometry, 4326)
|
||||
AS $$
|
||||
DECLARE
|
||||
boundary geometry(geometry, 4326);
|
||||
target_table text;
|
||||
geoid_colname text;
|
||||
geom_colname text;
|
||||
result GEOMETRY;
|
||||
BEGIN
|
||||
|
||||
SELECT * INTO geoid_colname, target_table, geom_colname
|
||||
FROM cdb_observatory._OBS_GetGeometryMetadata(boundary_id);
|
||||
|
||||
--RAISE NOTICE '%', target_table;
|
||||
|
||||
IF target_table IS NULL
|
||||
THEN
|
||||
--RAISE NOTICE 'No geometries found';
|
||||
RETURN NULL::geometry;
|
||||
END IF;
|
||||
|
||||
-- retrieve boundary
|
||||
EXECUTE
|
||||
format(
|
||||
'SELECT %I
|
||||
FROM observatory.%I
|
||||
WHERE %I = $1
|
||||
LIMIT 1', geom_colname, target_table, geoid_colname)
|
||||
INTO boundary
|
||||
USING geometry_id;
|
||||
|
||||
RETURN boundary;
|
||||
EXECUTE $query$
|
||||
SELECT (data->0->>'value')::Geometry
|
||||
FROM cdb_observatory.OBS_GetData(
|
||||
ARRAY[$1],
|
||||
cdb_observatory.OBS_GetMeta(
|
||||
ST_MakeEnvelope(-180, -90, 180, 90, 4326),
|
||||
('[{"geom_id": "' || $2 || '"}]')::JSON))
|
||||
$query$
|
||||
INTO result
|
||||
USING geometry_id, boundary_id;
|
||||
|
||||
RETURN result;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
|
||||
@ -245,13 +148,12 @@ CREATE OR REPLACE FUNCTION cdb_observatory._OBS_GetBoundariesByGeometry(
|
||||
boundary_id text,
|
||||
time_span text DEFAULT NULL,
|
||||
overlap_type text DEFAULT NULL)
|
||||
RETURNS TABLE(the_geom geometry, geom_refs text)
|
||||
AS $$
|
||||
RETURNS TABLE (
|
||||
the_geom geometry,
|
||||
geom_refs text
|
||||
) AS $$
|
||||
DECLARE
|
||||
boundary geometry(Geometry, 4326);
|
||||
geom_colname text;
|
||||
geoid_colname text;
|
||||
target_table text;
|
||||
meta JSON;
|
||||
BEGIN
|
||||
overlap_type := COALESCE(overlap_type, 'intersects');
|
||||
-- check inputs
|
||||
@ -259,34 +161,27 @@ BEGIN
|
||||
THEN
|
||||
-- recognized overlap type (map to ST_Contains, ST_Intersects, and ST_Within)
|
||||
RAISE EXCEPTION 'Overlap type ''%'' is not an accepted type (choose intersects, within, or contains)', overlap_type;
|
||||
ELSIF ST_GeometryType(geom) NOT IN ('ST_Polygon', 'ST_MultiPolygon')
|
||||
THEN
|
||||
RAISE EXCEPTION 'Invalid geometry type (%), expecting ''ST_MultiPolygon'' or ''ST_Polygon''', ST_GeometryType(geom);
|
||||
END IF;
|
||||
|
||||
-- TODO: add timespan in search
|
||||
-- TODO: add overlap info in search
|
||||
SELECT * INTO geoid_colname, target_table, geom_colname
|
||||
FROM cdb_observatory._OBS_GetGeometryMetadata(boundary_id);
|
||||
EXECUTE $query$
|
||||
SELECT cdb_observatory.OBS_GetMeta($1, JSON_Build_Array(JSON_Build_Object(
|
||||
'geom_id', $2, 'geom_timespan', $3)))
|
||||
$query$
|
||||
INTO meta
|
||||
USING geom, boundary_id, time_span;
|
||||
|
||||
-- if no tables are found, raise notice and return null
|
||||
IF target_table IS NULL
|
||||
THEN
|
||||
--RAISE NOTICE 'No boundaries found for bounding box ''%'' in ''%''', ST_AsText(geom), boundary_id;
|
||||
RETURN QUERY SELECT NULL::geometry, NULL::text;
|
||||
IF meta->0->>'geom_id' IS NULL THEN
|
||||
RETURN QUERY EXECUTE 'SELECT NULL::Geometry, NULL::Text LIMIT 0';
|
||||
RETURN;
|
||||
END IF;
|
||||
|
||||
--RAISE NOTICE 'target_table: %', target_table;
|
||||
|
||||
-- return first boundary in intersections
|
||||
RETURN QUERY
|
||||
EXECUTE format(
|
||||
'SELECT %I, %I::text
|
||||
FROM observatory.%I
|
||||
WHERE ST_%s($1, %I)
|
||||
', geom_colname, geoid_colname, target_table, overlap_type, geom_colname)
|
||||
USING geom;
|
||||
RETURN QUERY EXECUTE $query$
|
||||
SELECT (data->0->>'value')::Geometry the_geom, data->0->>'geomref' geom_refs
|
||||
FROM cdb_observatory.OBS_GetData(
|
||||
ARRAY[($1, 1)::geomval], $2, False
|
||||
)
|
||||
$query$ USING geom, meta;
|
||||
RETURN;
|
||||
|
||||
END;
|
||||
@ -414,27 +309,11 @@ BEGIN
|
||||
RAISE EXCEPTION 'Invalid geometry type (%), expecting ''ST_MultiPolygon'' or ''ST_Polygon''', ST_GeometryType(geom);
|
||||
END IF;
|
||||
|
||||
SELECT * INTO geoid_colname, target_table, geom_colname
|
||||
FROM cdb_observatory._OBS_GetGeometryMetadata(boundary_id);
|
||||
|
||||
-- if no tables are found, raise notice and return null
|
||||
IF target_table IS NULL
|
||||
THEN
|
||||
--RAISE NOTICE 'No boundaries found for bounding box ''%'' in ''%''', ST_AsText(geom), boundary_id;
|
||||
RETURN QUERY SELECT NULL::geometry, NULL::text;
|
||||
RETURN;
|
||||
END IF;
|
||||
|
||||
--RAISE NOTICE 'target_table: %', target_table;
|
||||
|
||||
-- return first boundary in intersections
|
||||
RETURN QUERY
|
||||
EXECUTE format(
|
||||
'SELECT ST_PointOnSurface(%I) As %s, %I::text
|
||||
FROM observatory.%I
|
||||
WHERE ST_%s($1, the_geom)
|
||||
', geom_colname, geom_colname, geoid_colname, target_table, overlap_type)
|
||||
USING geom;
|
||||
RETURN QUERY EXECUTE $query$
|
||||
SELECT ST_PointOnSurface(the_geom), geom_refs
|
||||
FROM cdb_observatory._OBS_GetBoundariesByGeometry($1, $2)
|
||||
$query$ USING geom, boundary_id;
|
||||
RETURN;
|
||||
|
||||
END;
|
||||
@ -534,46 +413,3 @@ BEGIN
|
||||
RETURN;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
|
||||
|
||||
-- _OBS_GetGeometryMetadata()
|
||||
-- TODO: add timespan in search
|
||||
-- TODO: add choice of clipped versus not clipped
|
||||
CREATE OR REPLACE FUNCTION cdb_observatory._OBS_GetGeometryMetadata(boundary_id text)
|
||||
RETURNS table(geoid_colname text, target_table text, geom_colname text)
|
||||
AS $$
|
||||
BEGIN
|
||||
|
||||
RETURN QUERY
|
||||
EXECUTE $string$
|
||||
SELECT geoid_ct.colname::text As geoid_colname,
|
||||
tablename::text,
|
||||
geom_ct.colname::text As geom_colname
|
||||
FROM observatory.obs_column_table As geoid_ct,
|
||||
observatory.obs_table As geom_t,
|
||||
observatory.obs_column_table As geom_ct,
|
||||
observatory.obs_column As geom_c
|
||||
WHERE geoid_ct.column_id
|
||||
IN (
|
||||
SELECT source_id
|
||||
FROM observatory.obs_column_to_column
|
||||
WHERE reltype = 'geom_ref'
|
||||
AND target_id = $1
|
||||
)
|
||||
AND geoid_ct.table_id = geom_t.id AND
|
||||
geom_t.id = geom_ct.table_id AND
|
||||
geom_ct.column_id = geom_c.id AND
|
||||
geom_c.type ILIKE 'geometry%' AND
|
||||
geom_c.id = $1
|
||||
ORDER BY timespan DESC
|
||||
LIMIT 1
|
||||
$string$
|
||||
USING boundary_id;
|
||||
RETURN;
|
||||
-- AND geom_t.timespan = '%s' <-- put in requested year
|
||||
-- TODO: filter by clipped vs. not so appropriate tablename are unique
|
||||
-- so the limit 1 can be removed
|
||||
RETURN;
|
||||
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
|
@ -3,24 +3,12 @@
|
||||
obs_getdemographicsnapshot_test_no_returns
|
||||
t
|
||||
(1 row)
|
||||
obs_get_median_income_at_test_point
|
||||
t
|
||||
(1 row)
|
||||
obs_get_median_income_at_null_island
|
||||
t
|
||||
(1 row)
|
||||
test_point_segmentation
|
||||
t
|
||||
(1 row)
|
||||
null_island_segmentation
|
||||
t
|
||||
(1 row)
|
||||
getcategories_at_test_point_1
|
||||
t
|
||||
(1 row)
|
||||
getcategories_at_null_island
|
||||
t
|
||||
(1 row)
|
||||
obs_getmeasure_zhvi_point_test
|
||||
t
|
||||
(1 row)
|
||||
|
@ -93,9 +93,3 @@ t
|
||||
obs_getpointsbypointandradius_around_null_island
|
||||
t
|
||||
(1 row)
|
||||
geoid_name_matches|table_name_matches|geom_name_matches
|
||||
t|t|t
|
||||
(1 row)
|
||||
geoid_name_matches|table_name_matches|geom_name_matches
|
||||
t|t|t
|
||||
(1 row)
|
||||
|
28
src/pg/test/fixtures/drop_fixtures.sql
vendored
28
src/pg/test/fixtures/drop_fixtures.sql
vendored
@ -14,20 +14,20 @@ DROP TABLE IF EXISTS observatory.obs_meta_geom;
|
||||
DROP TABLE IF EXISTS observatory.obs_meta_timespan;
|
||||
DROP TABLE IF EXISTS observatory.obs_column_table_tile;
|
||||
DROP TABLE IF EXISTS observatory.obs_column_table_tile_simple;
|
||||
DROP TABLE IF EXISTS observatory.obs_fcd4e4f5610f6764973ef8c0c215b2e80bec8963;
|
||||
DROP TABLE IF EXISTS observatory.obs_e32f8e59c7c8861ee5ee4029b3ace2af9a5c9caf;
|
||||
DROP TABLE IF EXISTS observatory.obs_6c1309a64d8f3e6986061f4d1ca7b57743e75e74;
|
||||
DROP TABLE IF EXISTS observatory.obs_0310c639744a2014bb1af82709228f05b59e7d3d;
|
||||
DROP TABLE IF EXISTS observatory.obs_7615e8622a68bfc5fe37c69c9880edfb40250103;
|
||||
DROP TABLE IF EXISTS observatory.obs_d39f7fe5959891c8296490d83c22ded31c54af13;
|
||||
DROP TABLE IF EXISTS observatory.obs_08025e1287e3af2b5de571d06562ba8d3bdb48e9;
|
||||
DROP TABLE IF EXISTS observatory.obs_b393b5b88c6adda634b2071a8005b03c551b609a;
|
||||
DROP TABLE IF EXISTS observatory.obs_78fb6c1d6ff6505225175922c2c389ce48d7632c;
|
||||
DROP TABLE IF EXISTS observatory.obs_65f29658e096ca1485bf683f65fdbc9f05ec3c5d;
|
||||
DROP TABLE IF EXISTS observatory.obs_a01cd5d8ccaa6531cef715071e9307e6b1987ec3;
|
||||
DROP TABLE IF EXISTS observatory.obs_fc050f0b8673cfe3c6aa1040f749eb40975691b7;
|
||||
DROP TABLE IF EXISTS observatory.obs_1a098da56badf5f32e336002b0a81708c40d29cd;
|
||||
DROP TABLE IF EXISTS observatory.obs_1ea93bbc109c87c676b3270789dacf7a1430db6c;
|
||||
DROP TABLE IF EXISTS observatory.obs_87a814e485deabe3b12545a537f693d16ca702c2;
|
||||
DROP TABLE IF EXISTS observatory.obs_c4411eba732408d47d73281772dbf03d60645dec;
|
||||
DROP TABLE IF EXISTS observatory.obs_1746e37b7cd28cb131971ea4187d42d71f09c5f3;
|
||||
DROP TABLE IF EXISTS observatory.obs_fcd4e4f5610f6764973ef8c0c215b2e80bec8963;
|
||||
DROP TABLE IF EXISTS observatory.obs_c4411eba732408d47d73281772dbf03d60645dec;
|
||||
DROP TABLE IF EXISTS observatory.obs_1a098da56badf5f32e336002b0a81708c40d29cd;
|
||||
DROP TABLE IF EXISTS observatory.obs_7615e8622a68bfc5fe37c69c9880edfb40250103;
|
||||
DROP TABLE IF EXISTS observatory.obs_a01cd5d8ccaa6531cef715071e9307e6b1987ec3;
|
||||
DROP TABLE IF EXISTS observatory.obs_6c1309a64d8f3e6986061f4d1ca7b57743e75e74;
|
||||
DROP TABLE IF EXISTS observatory.obs_0310c639744a2014bb1af82709228f05b59e7d3d;
|
||||
DROP TABLE IF EXISTS observatory.obs_87a814e485deabe3b12545a537f693d16ca702c2;
|
||||
DROP TABLE IF EXISTS observatory.obs_e32f8e59c7c8861ee5ee4029b3ace2af9a5c9caf;
|
||||
DROP TABLE IF EXISTS observatory.obs_1ea93bbc109c87c676b3270789dacf7a1430db6c;
|
||||
DROP TABLE IF EXISTS observatory.obs_b393b5b88c6adda634b2071a8005b03c551b609a;
|
||||
DROP TABLE IF EXISTS observatory.obs_8e30e6b3792430b410ba5b9e49cdc6a0d404d48f;
|
||||
DROP TABLE IF EXISTS observatory.obs_08025e1287e3af2b5de571d06562ba8d3bdb48e9;
|
||||
DROP TABLE IF EXISTS observatory.obs_d39f7fe5959891c8296490d83c22ded31c54af13;
|
||||
|
25880
src/pg/test/fixtures/load_fixtures.sql
vendored
25880
src/pg/test/fixtures/load_fixtures.sql
vendored
File diff suppressed because one or more lines are too long
@ -11,8 +11,8 @@ SELECT
|
||||
cdb_observatory._OBS_GeomTable(
|
||||
ST_SetSRID(ST_Point(-74.0059, 40.7128), 4326),
|
||||
'us.census.tiger.census_tract',
|
||||
'2014'
|
||||
) = 'obs_fc050f0b8673cfe3c6aa1040f749eb40975691b7' As _obs_geomtable_with_returned_table;
|
||||
'2015'
|
||||
) = 'obs_87a814e485deabe3b12545a537f693d16ca702c2' As _obs_geomtable_with_returned_table;
|
||||
|
||||
-- get null for unknown geometry_id
|
||||
-- should give back null
|
||||
|
@ -5,67 +5,21 @@ SET client_min_messages TO WARNING;
|
||||
--
|
||||
WITH result as(
|
||||
Select count(coalesce(OBS_GetDemographicSnapshot->>'value', 'foo')) expected_columns
|
||||
FROM cdb_observatory.OBS_GetDemographicSnapshot(cdb_observatory._TestPoint())
|
||||
FROM cdb_observatory.OBS_GetDemographicSnapshot(cdb_observatory._TestPoint(), '2010 - 2014')
|
||||
) select expected_columns = 52 as OBS_GetDemographicSnapshot_test_no_returns
|
||||
FROM result;
|
||||
|
||||
WITH result as (
|
||||
SELECT _OBS_Get::text as expected FROM
|
||||
cdb_observatory._OBS_Get(
|
||||
cdb_observatory._TestPoint(),
|
||||
Array['us.census.acs.B19013001']::text[],
|
||||
'2010 - 2014',
|
||||
'us.census.tiger.block_group'
|
||||
)
|
||||
) SELECT expected = '{"value":79292,"name":"Median Household Income in the past 12 Months","tablename":"obs_1a098da56badf5f32e336002b0a81708c40d29cd","aggregate":"median","type":"Numeric","description":"Within a geographic area, the median income received by every household on a regular basis before payments for personal income taxes, social security, union dues, medicare deductions, etc. It includes income received from wages, salary, commissions, bonuses, and tips; self-employment income from own nonfarm or farm businesses, including proprietorships and partnerships; interest, dividends, net rental income, royalty income, or income from estates and trusts; Social Security or Railroad Retirement income; Supplemental Security Income (SSI); any cash public assistance or welfare payments from the state or local welfare office; retirement, survivor, or disability benefits; and any other sources of income received regularly such as Veterans'' (VA) payments, unemployment and/or worker''s compensation, child support, and alimony."}'
|
||||
As OBS_Get_median_income_at_test_point
|
||||
FROM result;
|
||||
|
||||
-- median income at null island
|
||||
WITH result as (
|
||||
SELECT count(_OBS_Get) as expected FROM
|
||||
cdb_observatory._OBS_Get(
|
||||
ST_SetSRID(ST_Point(0, 0), 4326),
|
||||
Array['us.census.acs.B19013001']::text[],
|
||||
'2010 - 2014',
|
||||
'us.census.tiger.block_group'
|
||||
)
|
||||
) select expected = 0 as OBS_Get_median_income_at_null_island
|
||||
from result;
|
||||
|
||||
SELECT cdb_observatory.OBS_GetSegmentSnapshot(
|
||||
cdb_observatory._TestPoint(),
|
||||
'us.census.tiger.census_tract'
|
||||
)::text =
|
||||
'{"x10_segment":"Wealthy, urban without Kids","x55_segment":"Wealthy transplants displacing long-term local residents","us.census.acs.B01003001_quantile":"0.3235","us.census.acs.B01001002_quantile":"0.494716216216216","us.census.acs.B01001026_quantile":"0.183756756756757","us.census.acs.B01002001_quantile":"0.0752837837837838","us.census.acs.B03002003_quantile":"0.293162162162162","us.census.acs.B03002004_quantile":"0.455527027027027","us.census.acs.B03002006_quantile":"0.656405405405405","us.census.acs.B03002012_quantile":"0.840081081081081","us.census.acs.B05001006_quantile":"0.727135135135135","us.census.acs.B08006001_quantile":"0.688635135135135","us.census.acs.B08006002_quantile":"0.0204459459459459","us.census.acs.B08006008_quantile":"0.679324324324324","us.census.acs.B08006009_quantile":"0.996716216216216","us.census.acs.B08006011_quantile":"0.967418918918919","us.census.acs.B08006015_quantile":"0.512945945945946","us.census.acs.B08006017_quantile":"0.0504864864864865","us.census.acs.B09001001_quantile":"0.192405405405405","us.census.acs.B11001001_quantile":"0.331702702702703","us.census.acs.B14001001_quantile":"0.296283783783784","us.census.acs.B14001002_quantile":"0.045472972972973","us.census.acs.B14001005_quantile":"0.0442702702702703","us.census.acs.B14001006_quantile":"0.0829054054054054","us.census.acs.B14001007_quantile":"0.701135135135135","us.census.acs.B14001008_quantile":"0.404527027027027","us.census.acs.B15003001_quantile":"0.191824324324324","us.census.acs.B15003017_quantile":"0.864162162162162","us.census.acs.B15003022_quantile":"0.754297297297297","us.census.acs.B15003023_quantile":"0.350054054054054","us.census.acs.B16001001_quantile":"0.217635135135135","us.census.acs.B16001002_quantile":"0.85972972972973","us.census.acs.B16001003_quantile":"0.342851351351351","us.census.acs.B17001001_quantile":"0.51204054054054","us.census.acs.B17001002_quantile":"0.813540540540541","us.census.acs.B19013001_quantile":"0.0948648648648649","us.census.acs.B19083001_quantile":"0.678351351351351","us.census.acs.B19301001_quantile":"0.146108108108108","us.census.acs.B25001001_quantile":"0.149067567567568","us.census.acs.B25002003_quantile":"0","us.census.acs.B25004002_quantile":"0","us.census.acs.B25004004_quantile":"0.944554054054054","us.census.acs.B25058001_quantile":"0.398040540540541","us.census.acs.B25071001_quantile":"0.0596081081081081","us.census.acs.B25075001_quantile":"0","us.census.acs.B25075025_quantile":null}' as test_point_segmentation;
|
||||
)::JSONB =
|
||||
'{"x10_segment": "Wealthy, urban without Kids", "x55_segment": "Wealthy transplants displacing long-term local residents", "us.census.acs.B01001002_quantile": "0.494716216216216", "us.census.acs.B01001026_quantile": "0.183756756756757", "us.census.acs.B01002001_quantile": "0.0752837837837838", "us.census.acs.B01003001_quantile": "0.3235", "us.census.acs.B03002003_quantile": "0.293162162162162", "us.census.acs.B03002004_quantile": "0.455527027027027", "us.census.acs.B03002006_quantile": "0.656405405405405", "us.census.acs.B03002012_quantile": "0.840081081081081", "us.census.acs.B05001006_quantile": "0.727135135135135", "us.census.acs.B08006001_quantile": "0.688635135135135", "us.census.acs.B08006002_quantile": "0.0204459459459459", "us.census.acs.B08006009_quantile": "0.679324324324324", "us.census.acs.B08006011_quantile": "0.996716216216216", "us.census.acs.B08006015_quantile": "0.967418918918919", "us.census.acs.B08006017_quantile": "0.512945945945946", "us.census.acs.B08301010_quantile": "0.994743243243243", "us.census.acs.B09001001_quantile": "0.0504864864864865", "us.census.acs.B11001001_quantile": "0.192405405405405", "us.census.acs.B14001001_quantile": "0.331702702702703", "us.census.acs.B14001002_quantile": "0.296283783783784", "us.census.acs.B14001005_quantile": "0.045472972972973", "us.census.acs.B14001006_quantile": "0.0442702702702703", "us.census.acs.B14001007_quantile": "0.0829054054054054", "us.census.acs.B14001008_quantile": "0.701135135135135", "us.census.acs.B15003001_quantile": "0.404527027027027", "us.census.acs.B15003017_quantile": "0.191824324324324", "us.census.acs.B15003022_quantile": "0.864162162162162", "us.census.acs.B15003023_quantile": "0.754297297297297", "us.census.acs.B16001001_quantile": "0.350054054054054", "us.census.acs.B16001002_quantile": "0.217635135135135", "us.census.acs.B16001003_quantile": "0.85972972972973", "us.census.acs.B17001001_quantile": "0.342851351351351", "us.census.acs.B17001002_quantile": "0.51204054054054", "us.census.acs.B19013001_quantile": "0.813540540540541", "us.census.acs.B19083001_quantile": "0.0948648648648649", "us.census.acs.B19301001_quantile": "0.678351351351351", "us.census.acs.B25001001_quantile": "0.146108108108108", "us.census.acs.B25002003_quantile": "0.149067567567568", "us.census.acs.B25004002_quantile": "0", "us.census.acs.B25004004_quantile": "0", "us.census.acs.B25058001_quantile": "0.944554054054054", "us.census.acs.B25071001_quantile": "0.398040540540541", "us.census.acs.B25075001_quantile": "0.0596081081081081", "us.census.acs.B25075025_quantile": "0"}'::JSONB as test_point_segmentation;
|
||||
|
||||
-- segmentation around null island
|
||||
SELECT cdb_observatory.OBS_GetSegmentSnapshot(
|
||||
ST_SetSRID(ST_Point(0, 0), 4326),
|
||||
'us.census.tiger.census_tract'
|
||||
)::text = '{"x10_segment":null,"x55_segment":null,"us.census.acs.B01003001_quantile":null,"us.census.acs.B01001002_quantile":null,"us.census.acs.B01001026_quantile":null,"us.census.acs.B01002001_quantile":null,"us.census.acs.B03002003_quantile":null,"us.census.acs.B03002004_quantile":null,"us.census.acs.B03002006_quantile":null,"us.census.acs.B03002012_quantile":null,"us.census.acs.B05001006_quantile":null,"us.census.acs.B08006001_quantile":null,"us.census.acs.B08006002_quantile":null,"us.census.acs.B08006008_quantile":null,"us.census.acs.B08006009_quantile":null,"us.census.acs.B08006011_quantile":null,"us.census.acs.B08006015_quantile":null,"us.census.acs.B08006017_quantile":null,"us.census.acs.B09001001_quantile":null,"us.census.acs.B11001001_quantile":null,"us.census.acs.B14001001_quantile":null,"us.census.acs.B14001002_quantile":null,"us.census.acs.B14001005_quantile":null,"us.census.acs.B14001006_quantile":null,"us.census.acs.B14001007_quantile":null,"us.census.acs.B14001008_quantile":null,"us.census.acs.B15003001_quantile":null,"us.census.acs.B15003017_quantile":null,"us.census.acs.B15003022_quantile":null,"us.census.acs.B15003023_quantile":null,"us.census.acs.B16001001_quantile":null,"us.census.acs.B16001002_quantile":null,"us.census.acs.B16001003_quantile":null,"us.census.acs.B17001001_quantile":null,"us.census.acs.B17001002_quantile":null,"us.census.acs.B19013001_quantile":null,"us.census.acs.B19083001_quantile":null,"us.census.acs.B19301001_quantile":null,"us.census.acs.B25001001_quantile":null,"us.census.acs.B25002003_quantile":null,"us.census.acs.B25004002_quantile":null,"us.census.acs.B25004004_quantile":null,"us.census.acs.B25058001_quantile":null,"us.census.acs.B25071001_quantile":null,"us.census.acs.B25075001_quantile":null,"us.census.acs.B25075025_quantile":null}' as null_island_segmentation;
|
||||
|
||||
WITH result as (
|
||||
SELECT array_agg(_OBS_GetCategories) as expected FROM
|
||||
cdb_observatory._OBS_GetCategories(
|
||||
cdb_observatory._TestPoint(),
|
||||
Array['us.census.spielman_singleton_segments.X10'],
|
||||
'us.census.tiger.census_tract'
|
||||
)
|
||||
)
|
||||
select (expected)[1]::text = '{"category":"Wealthy, urban without Kids","name":"Spielman-Singleton Segments: 10 Clusters","tablename":"obs_65f29658e096ca1485bf683f65fdbc9f05ec3c5d","aggregate":null,"type":"Text","description":"Sociodemographic classes from Spielman and Singleton 2015, 10 clusters"}' as GetCategories_at_test_point_1
|
||||
from result;
|
||||
|
||||
WITH result as (
|
||||
SELECT array_agg(_OBS_GetCategories) as expected FROM
|
||||
cdb_observatory._OBS_GetCategories(
|
||||
ST_SetSRID(ST_Point(0,0), 4326),
|
||||
Array['us.census.spielman_singleton_segments.X10'],
|
||||
'us.census.tiger.census_tract'
|
||||
)
|
||||
)
|
||||
select expected[0] is NULL as GetCategories_at_null_island
|
||||
from result;
|
||||
)::text is null as null_island_segmentation;
|
||||
|
||||
-- Point-based OBS_GetMeasure with zillow
|
||||
SELECT abs(OBS_GetMeasure_zhvi_point - 597900) / 597900 < 5.0 AS OBS_GetMeasure_zhvi_point_test FROM cdb_observatory.OBS_GetMeasure(
|
||||
@ -158,7 +112,7 @@ SELECT cdb_observatory.OBS_GetCategory(
|
||||
|
||||
-- Poly-based OBS_GetCategory
|
||||
SELECT cdb_observatory.OBS_GetCategory(
|
||||
cdb_observatory._TestArea(), 'us.census.spielman_singleton_segments.X10') = 'Wealthy, urban without Kids' As obs_getcategory_polygon;
|
||||
cdb_observatory._TestArea(), 'us.census.spielman_singleton_segments.X10') = 'Hispanic and Young' As obs_getcategory_polygon;
|
||||
|
||||
-- NULL Input OBS_GetCategory
|
||||
SELECT cdb_observatory.OBS_GetCategory(
|
||||
@ -215,7 +169,7 @@ SELECT cdb_observatory.OBS_GetUSCensusCategory(
|
||||
|
||||
-- Area-based OBS_GetUSCensusCategory
|
||||
SELECT cdb_observatory.OBS_GetUSCensusCategory(
|
||||
cdb_observatory._testarea(), 'Spielman-Singleton Segments: 10 Clusters') = 'Wealthy, urban without Kids' As OBS_GetUSCensusCategory_polygon;
|
||||
cdb_observatory._testarea(), 'Spielman-Singleton Segments: 10 Clusters') = 'Hispanic and Young' As OBS_GetUSCensusCategory_polygon;
|
||||
|
||||
-- Null-input OBS_GetUSCensusCategory
|
||||
SELECT cdb_observatory.OBS_GetUSCensusCategory(
|
||||
|
@ -10,11 +10,11 @@ SET client_min_messages TO WARNING;
|
||||
|
||||
-- _OBS_SearchTables tests
|
||||
SELECT
|
||||
t.table_name = 'obs_1babf5a26a1ecda5fb74963e88408f71d0364b81' As _OBS_SearchTables_tables_match,
|
||||
t.timespan = '2014' As _OBS_SearchTables_timespan_matches
|
||||
t.table_name = 'obs_0310c639744a2014bb1af82709228f05b59e7d3d' As _OBS_SearchTables_tables_match,
|
||||
t.timespan = '2015' As _OBS_SearchTables_timespan_matches
|
||||
FROM cdb_observatory._OBS_SearchTables(
|
||||
'us.census.tiger.county',
|
||||
'2014'
|
||||
'2015'
|
||||
) As t(table_name, timespan);
|
||||
|
||||
-- _OBS_SearchTables tests
|
||||
@ -362,7 +362,7 @@ SELECT ARRAY_AGG(column_id ORDER BY score DESC) =
|
||||
|
||||
SELECT ARRAY_AGG(column_id ORDER BY score DESC) =
|
||||
ARRAY['us.census.tiger.block_group', 'us.census.tiger.census_tract',
|
||||
'us.census.tiger.county', 'us.census.tiger.zcta5']
|
||||
'us.census.tiger.zcta5', 'us.census.tiger.county']
|
||||
AS _obs_geometryscores_5km_buffer
|
||||
FROM cdb_observatory._OBS_GetGeometryScores(
|
||||
ST_Buffer(ST_SetSRID(ST_MakePoint(-73.9, 40.7), 4326)::Geography, 5000)::Geometry(Geometry, 4326),
|
||||
@ -382,7 +382,7 @@ SELECT ARRAY_AGG(column_id ORDER BY score DESC) =
|
||||
|
||||
SELECT ARRAY_AGG(column_id ORDER BY score DESC) =
|
||||
ARRAY[ 'us.census.tiger.zcta5', 'us.census.tiger.census_tract',
|
||||
'us.census.tiger.county', 'us.census.tiger.block_group']
|
||||
'us.census.tiger.county', 'us.census.tiger.block_group' ]
|
||||
AS _obs_geometryscores_500km_buffer
|
||||
FROM cdb_observatory._OBS_GetGeometryScores(
|
||||
ST_Buffer(ST_SetSRID(ST_MakePoint(-73.9, 40.7), 4326)::Geography, 500000)::Geometry(Geometry, 4326),
|
||||
@ -410,7 +410,7 @@ SELECT JSON_Object_Agg(column_id, numgeoms::int ORDER BY numgeoms DESC)::Text
|
||||
WHERE table_id LIKE '%2015%';
|
||||
|
||||
SELECT JSON_Object_Agg(column_id, numgeoms::int ORDER BY numgeoms DESC)::Text =
|
||||
'{ "us.census.tiger.block_group" : 899, "us.census.tiger.census_tract" : 328, "us.census.tiger.zcta5" : 45, "us.census.tiger.county" : 1 }'
|
||||
'{ "us.census.tiger.block_group" : 880, "us.census.tiger.census_tract" : 310, "us.census.tiger.zcta5" : 45, "us.census.tiger.county" : 1 }'
|
||||
AS _obs_geometryscores_numgeoms_5km_buffer
|
||||
FROM cdb_observatory._OBS_GetGeometryScores(
|
||||
ST_Buffer(ST_SetSRID(ST_MakePoint(-73.9, 40.7), 4326)::Geography, 5000)::Geometry(Geometry, 4326),
|
||||
@ -419,7 +419,7 @@ SELECT JSON_Object_Agg(column_id, numgeoms::int ORDER BY numgeoms DESC)::Text =
|
||||
WHERE table_id LIKE '%2015%';
|
||||
|
||||
SELECT JSON_Object_Agg(column_id, numgeoms::int ORDER BY numgeoms DESC)::Text =
|
||||
'{ "us.census.tiger.block_group" : 12112, "us.census.tiger.census_tract" : 3792, "us.census.tiger.zcta5" : 550, "us.census.tiger.county" : 14 }'
|
||||
'{ "us.census.tiger.block_group" : 11531, "us.census.tiger.census_tract" : 3601, "us.census.tiger.zcta5" : 550, "us.census.tiger.county" : 14 }'
|
||||
AS _obs_geometryscores_numgeoms_50km_buffer
|
||||
FROM cdb_observatory._OBS_GetGeometryScores(
|
||||
ST_Buffer(ST_SetSRID(ST_MakePoint(-73.9, 40.7), 4326)::Geography, 50000)::Geometry(Geometry, 4326),
|
||||
@ -428,7 +428,7 @@ SELECT JSON_Object_Agg(column_id, numgeoms::int ORDER BY numgeoms DESC)::Text =
|
||||
WHERE table_id LIKE '%2015%';
|
||||
|
||||
SELECT JSON_Object_Agg(column_id, numgeoms::int ORDER BY numgeoms DESC)::Text =
|
||||
'{ "us.census.tiger.block_group" : 48420, "us.census.tiger.census_tract" : 15775, "us.census.tiger.zcta5" : 6533, "us.census.tiger.county" : 304 }'
|
||||
'{ "us.census.tiger.block_group" : 48917, "us.census.tiger.census_tract" : 15969, "us.census.tiger.zcta5" : 6534, "us.census.tiger.county" : 314 }'
|
||||
AS _obs_geometryscores_numgeoms_500km_buffer
|
||||
FROM cdb_observatory._OBS_GetGeometryScores(
|
||||
ST_Buffer(ST_SetSRID(ST_MakePoint(-73.9, 40.7), 4326)::Geography, 500000)::Geometry(Geometry, 4326),
|
||||
@ -437,7 +437,7 @@ SELECT JSON_Object_Agg(column_id, numgeoms::int ORDER BY numgeoms DESC)::Text =
|
||||
WHERE table_id LIKE '%2015%';
|
||||
|
||||
SELECT JSON_Object_Agg(column_id, numgeoms::int ORDER BY numgeoms DESC)::Text =
|
||||
'{ "us.census.tiger.block_group" : 169119, "us.census.tiger.census_tract" : 56380, "us.census.tiger.zcta5" : 26498, "us.census.tiger.county" : 2704 }'
|
||||
'{ "us.census.tiger.block_group" : 169191, "us.census.tiger.census_tract" : 56469, "us.census.tiger.zcta5" : 26525, "us.census.tiger.county" : 2753 }'
|
||||
AS _obs_geometryscores_numgeoms_2500km_buffer
|
||||
FROM cdb_observatory._OBS_GetGeometryScores(
|
||||
ST_Buffer(ST_SetSRID(ST_MakePoint(-73.9, 40.7), 4326)::Geography, 2500000)::Geometry(Geometry, 4326),
|
||||
@ -446,8 +446,8 @@ SELECT JSON_Object_Agg(column_id, numgeoms::int ORDER BY numgeoms DESC)::Text =
|
||||
WHERE table_id LIKE '%2015%';
|
||||
|
||||
SELECT ARRAY_AGG(column_id ORDER BY score DESC) =
|
||||
ARRAY['us.census.tiger.county', 'us.census.tiger.census_tract',
|
||||
'us.census.tiger.zcta5', 'us.census.tiger.block_group']
|
||||
ARRAY['us.census.tiger.county', 'us.census.tiger.zcta5',
|
||||
'us.census.tiger.census_tract', 'us.census.tiger.block_group']
|
||||
AS _obs_geometryscores_500km_buffer_50_geoms
|
||||
FROM cdb_observatory._OBS_GetGeometryScores(
|
||||
ST_Buffer(ST_SetSRID(ST_MakePoint(-73.9, 40.7), 4326)::Geography, 50000)::Geometry(Geometry, 4326),
|
||||
@ -457,7 +457,7 @@ SELECT ARRAY_AGG(column_id ORDER BY score DESC) =
|
||||
|
||||
SELECT ARRAY_AGG(column_id ORDER BY score DESC)
|
||||
= ARRAY['us.census.tiger.zcta5', 'us.census.tiger.census_tract',
|
||||
'us.census.tiger.block_group', 'us.census.tiger.county']
|
||||
'us.census.tiger.county', 'us.census.tiger.block_group']
|
||||
AS _obs_geometryscores_500km_buffer_500_geoms
|
||||
FROM cdb_observatory._OBS_GetGeometryScores(
|
||||
ST_Buffer(ST_SetSRID(ST_MakePoint(-73.9, 40.7), 4326)::Geography, 50000)::Geometry(Geometry, 4326),
|
||||
|
@ -42,7 +42,7 @@ SELECT cdb_observatory.OBS_GetBoundary(
|
||||
SELECT cdb_observatory.OBS_GetBoundary(
|
||||
cdb_observatory._TestPoint(),
|
||||
'us.census.tiger.census_tract',
|
||||
'2014'
|
||||
'2015'
|
||||
) = :'cartodb_census_tract_geometry' As OBS_GetBoundary_year_census_tract;
|
||||
|
||||
-- should return null
|
||||
@ -65,7 +65,7 @@ SELECT cdb_observatory.OBS_GetBoundaryId(
|
||||
SELECT cdb_observatory.OBS_GetBoundaryId(
|
||||
cdb_observatory._TestPoint(),
|
||||
'us.census.tiger.census_tract',
|
||||
'2014'
|
||||
'2015'
|
||||
) = '36047048500'::text As OBS_GetBoundaryId_cartodb_census_tract_with_year;
|
||||
|
||||
-- should give back '36047', the geoid of cartodb's county (King's/
|
||||
@ -104,7 +104,7 @@ SELECT cdb_observatory.OBS_GetBoundaryById(
|
||||
SELECT cdb_observatory.OBS_GetBoundaryById(
|
||||
'36047',
|
||||
'us.census.tiger.county',
|
||||
'2014'
|
||||
'2015'
|
||||
) = :'cartodb_county_geometry' OBS_GetBoundaryById_boundary_id_mismatch_geom_id;
|
||||
|
||||
-- should give null since boundary_id does not match geometry reference id
|
||||
@ -115,8 +115,7 @@ SELECT cdb_observatory.OBS_GetBoundaryById(
|
||||
|
||||
-- _OBS_GetBoundariesByGeometry
|
||||
|
||||
SELECT
|
||||
array_agg(geom_refs) = Array[ '1104486618765', '1104486642837',
|
||||
SELECT array_agg(geom_refs) = Array[ '1104486618765', '1104486642837',
|
||||
'1104991798384', '1105044325367',
|
||||
'1105089330200', '1105089331758']
|
||||
As _OBS_GetBoundariesByGeometry_roads_around_cartodb
|
||||
@ -126,7 +125,7 @@ FROM (
|
||||
-- near CartoDB's office
|
||||
ST_MakeEnvelope(-74,40.69,-73.99,40.7,
|
||||
4326),
|
||||
'us.census.tiger.prisecroads')
|
||||
'us.census.tiger.prisecroads_geom')
|
||||
ORDER BY geom_refs ASC
|
||||
) As m(the_geom, geom_refs);
|
||||
|
||||
@ -140,7 +139,7 @@ FROM (
|
||||
-- near CartoDB's office
|
||||
ST_MakeEnvelope(-73.9452409744,40.6988851644,-73.9280319214,40.7101254524,
|
||||
4326),
|
||||
'us.census.tiger.pointlm')
|
||||
'us.census.tiger.pointlm_geom')
|
||||
ORDER BY geom_refs ASC
|
||||
) As m(the_geom, geom_refs);
|
||||
|
||||
@ -299,7 +298,7 @@ FROM (
|
||||
-73.9280319214,40.7101254524,
|
||||
4326),
|
||||
'us.census.tiger.census_tract',
|
||||
'2014')
|
||||
'2015')
|
||||
ORDER BY geom_refs ASC
|
||||
) As m(the_geom, geom_refs);
|
||||
|
||||
@ -342,7 +341,7 @@ FROM (
|
||||
cdb_observatory._testpoint(),
|
||||
500,
|
||||
'us.census.tiger.census_tract',
|
||||
'2014')
|
||||
'2015')
|
||||
ORDER BY geom_refs ASC
|
||||
) As m(the_geom, geom_refs);
|
||||
|
||||
@ -359,21 +358,4 @@ FROM (
|
||||
ORDER BY geom_refs ASC
|
||||
) As m(the_geom, geom_refs);
|
||||
|
||||
-- _OBS_GetGeometryMetadata
|
||||
-- get metadata for census tracts
|
||||
|
||||
SELECT
|
||||
geoid_colname = 'geoid' As geoid_name_matches,
|
||||
target_table = 'obs_87a814e485deabe3b12545a537f693d16ca702c2' As table_name_matches,
|
||||
geom_colname = 'the_geom' As geom_name_matches
|
||||
FROM cdb_observatory._OBS_GetGeometryMetadata('us.census.tiger.census_tract')
|
||||
As m(geoid_colname, target_table, geom_colname);
|
||||
|
||||
-- get metadata for boundaries with clipped geometries
|
||||
SELECT
|
||||
geoid_colname = 'geoid' As geoid_name_matches,
|
||||
target_table = 'obs_fcd4e4f5610f6764973ef8c0c215b2e80bec8963' As table_name_matches,
|
||||
geom_colname = 'the_geom' As geom_name_matches
|
||||
FROM cdb_observatory._OBS_GetGeometryMetadata('us.census.tiger.census_tract_clipped') As m(geoid_colname, target_table, geom_colname);
|
||||
|
||||
\i test/fixtures/drop_fixtures.sql
|
||||
|
Loading…
Reference in New Issue
Block a user