Merge pull request #67 from CartoDB/iss66-cast-geom-ids-to-text

cast geom ids to text
This commit is contained in:
john krauss 2016-05-20 17:59:07 -04:00
commit a1bbd5ace5
6 changed files with 70 additions and 31 deletions

View File

@ -46,6 +46,7 @@ fixtures = [
('us.census.spielman_singleton_segments.X10', 'us.census.tiger.census_tract', '2010 - 2014'), ('us.census.spielman_singleton_segments.X10', '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', '2014-01'),
('us.zillow.AllHomes_Zhvi', 'us.census.tiger.zcta5', '2016-03'), ('us.zillow.AllHomes_Zhvi', 'us.census.tiger.zcta5', '2016-03'),
('whosonfirst.wof_country_geom', 'whosonfirst.wof_country_geom', '2016'),
('us.census.tiger.zcta5_clipped', 'us.census.tiger.zcta5_clipped', '2014'), ('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.block_group_clipped', 'us.census.tiger.block_group_clipped', '2014'),
] ]
@ -76,10 +77,15 @@ with open('src/pg/test/fixtures/load_fixtures.sql', 'w') as outfile:
for tablename, colname, boundary_id in unique_tables: for tablename, colname, boundary_id in unique_tables:
if 'zcta5' in boundary_id: if 'zcta5' in boundary_id:
where = '11%' where = '\'11%\''
compare = 'LIKE'
elif 'whosonfirst' in boundary_id:
where = '(\'85632785\',\'85633051\',\'85633111\',\'85633147\',\'85633253\',\'85633267\')'
compare = 'IN'
else: else:
where = '36047%' where = '\'36047%\''
print ' '.join([select_star(tablename), "WHERE {} LIKE '{}'".format(colname, where)]) compare = 'LIKE'
cdb.dump(' '.join([select_star(tablename), "WHERE {} LIKE '{}'".format(colname, where)]), print ' '.join([select_star(tablename), "WHERE {}::text {} {}".format(colname, compare, where)])
tablename, outfile, schema='observatory') cdb.dump(' '.join([select_star(tablename), "WHERE {}::text {} {}".format(colname, compare, where)]),
tablename, outfile, schema='observatory')
dropfiles.write('DROP TABLE IF EXISTS observatory.{};\n'.format(tablename)) dropfiles.write('DROP TABLE IF EXISTS observatory.{};\n'.format(tablename))

View File

@ -161,9 +161,9 @@ BEGIN
RAISE NOTICE 'target_table: %, geoid_colname: %', target_table, geoid_colname; RAISE NOTICE 'target_table: %, geoid_colname: %', target_table, geoid_colname;
-- return name of geometry id column -- return geometry id column value
EXECUTE format( EXECUTE format(
'SELECT %I 'SELECT %I::text
FROM observatory.%I FROM observatory.%I
WHERE ST_Intersects($1, the_geom) WHERE ST_Intersects($1, the_geom)
LIMIT 1', geoid_colname, target_table) LIMIT 1', geoid_colname, target_table)
@ -282,7 +282,7 @@ BEGIN
-- return first boundary in intersections -- return first boundary in intersections
RETURN QUERY RETURN QUERY
EXECUTE format( EXECUTE format(
'SELECT %I, %I 'SELECT %I, %I::text
FROM observatory.%I FROM observatory.%I
WHERE ST_%s($1, the_geom) WHERE ST_%s($1, the_geom)
', geom_colname, geoid_colname, target_table, overlap_type) ', geom_colname, geoid_colname, target_table, overlap_type)
@ -409,7 +409,7 @@ BEGIN
RAISE EXCEPTION 'Overlap type ''%'' is not an accepted type (choose intersects, within, or contains)', overlap_type; 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') ELSIF ST_GeometryType(geom) NOT IN ('ST_Polygon', 'ST_MultiPolygon')
THEN THEN
RAISE EXCEPTION 'Invalid geometry type (%), expecting ''ST_MultiPolygon'' or ''ST_Polygon''', ST_GeometryType(geom); RAISE EXCEPTION 'Invalid geometry type (%), expecting ''ST_MultiPolygon'' or ''ST_Polygon''', ST_GeometryType(geom);
END IF; END IF;
SELECT * INTO geoid_colname, target_table, geom_colname SELECT * INTO geoid_colname, target_table, geom_colname
@ -428,7 +428,7 @@ BEGIN
-- return first boundary in intersections -- return first boundary in intersections
RETURN QUERY RETURN QUERY
EXECUTE format( EXECUTE format(
'SELECT ST_PointOnSurface(%I) As %s, %I 'SELECT ST_PointOnSurface(%I) As %s, %I::text
FROM observatory.%I FROM observatory.%I
WHERE ST_%s($1, the_geom) WHERE ST_%s($1, the_geom)
', geom_colname, geom_colname, geoid_colname, target_table, overlap_type) ', geom_colname, geom_colname, geoid_colname, target_table, overlap_type)
@ -565,6 +565,7 @@ BEGIN
geom_c.type ILIKE 'geometry' AND geom_c.type ILIKE 'geometry' AND
geom_c.id = '%s' geom_c.id = '%s'
$string$, boundary_id, boundary_id); $string$, boundary_id, boundary_id);
RETURN;
-- AND geom_t.timespan = '%s' <-- put in requested year -- AND geom_t.timespan = '%s' <-- put in requested year
-- TODO: filter by clipped vs. not so appropriate tablename are unique -- TODO: filter by clipped vs. not so appropriate tablename are unique
-- so the limit 1 can be removed -- so the limit 1 can be removed

View File

@ -57,6 +57,9 @@ t
obs_getboundariesbygeometry_tracts_around_null_island obs_getboundariesbygeometry_tracts_around_null_island
t t
(1 row) (1 row)
obs_getboundariesbygeometry_wof
t
(1 row)
obs_getboundariesbypointandradius_around_cartodb obs_getboundariesbypointandradius_around_cartodb
t t
(1 row) (1 row)

View File

@ -13,6 +13,7 @@ DROP TABLE IF EXISTS observatory.obs_7615e8622a68bfc5fe37c69c9880edfb40250103;
DROP TABLE IF EXISTS observatory.obs_1babf5a26a1ecda5fb74963e88408f71d0364b81; DROP TABLE IF EXISTS observatory.obs_1babf5a26a1ecda5fb74963e88408f71d0364b81;
DROP TABLE IF EXISTS observatory.obs_8764a6b439a4f8714f54d4b3a157bc5e36519066; DROP TABLE IF EXISTS observatory.obs_8764a6b439a4f8714f54d4b3a157bc5e36519066;
DROP TABLE IF EXISTS observatory.obs_b393b5b88c6adda634b2071a8005b03c551b609a; DROP TABLE IF EXISTS observatory.obs_b393b5b88c6adda634b2071a8005b03c551b609a;
DROP TABLE IF EXISTS observatory.obs_1ea93bbc109c87c676b3270789dacf7a1430db6c;
DROP TABLE IF EXISTS observatory.obs_fc050f0b8673cfe3c6aa1040f749eb40975691b7; DROP TABLE IF EXISTS observatory.obs_fc050f0b8673cfe3c6aa1040f749eb40975691b7;
DROP TABLE IF EXISTS observatory.obs_6c1309a64d8f3e6986061f4d1ca7b57743e75e74; DROP TABLE IF EXISTS observatory.obs_6c1309a64d8f3e6986061f4d1ca7b57743e75e74;
DROP TABLE IF EXISTS observatory.obs_d39f7fe5959891c8296490d83c22ded31c54af13; DROP TABLE IF EXISTS observatory.obs_d39f7fe5959891c8296490d83c22ded31c54af13;

File diff suppressed because one or more lines are too long

View File

@ -171,6 +171,17 @@ FROM (
ORDER BY geom_refs ASC ORDER BY geom_refs ASC
) As m(the_geom, geom_refs); ) As m(the_geom, geom_refs);
-- who's on first boundaries
SELECT
array_agg(geom_refs) = Array['85632785','85633051','85633111','85633147','85633253','85633267'] As OBS_GetBoundariesByGeometry_wof
FROM (
SELECT *
FROM cdb_observatory.OBS_GetBoundariesByGeometry(
ST_MakeEnvelope(-4.66, 40.43, 14.48, 51.99, 4326),
'whosonfirst.wof_country_geom')
ORDER BY geom_refs ASC
) As m(the_geom, geom_refs);
-- OBS_GetBoundariesByPointAndRadius -- OBS_GetBoundariesByPointAndRadius
-- check that all census tracts intersecting with the geometry are returned -- check that all census tracts intersecting with the geometry are returned