use obs_column_table_tile_raster with simpler bands for faster performance
This commit is contained in:
@@ -412,11 +412,6 @@ BEGIN
|
||||
END
|
||||
$$ LANGUAGE plpgsql;
|
||||
|
||||
--create table observatory.obs_column_table_tile_simple as select table_id, column_id, tile_id, st_band(tile, 2) as tile from observatory.obs_column_table_tile ;
|
||||
|
||||
--create table observatory.obs_column_table_tile_simpler as select table_id,
|
||||
--column_id, tile_id, st_reclass(tile, '0-65536:0-65536', '16BUI') as tile
|
||||
--from observatory.obs_column_table_tile_simple ;
|
||||
|
||||
CREATE OR REPLACE FUNCTION cdb_observatory._OBS_GetGeometryScores(
|
||||
bounds Geometry(Geometry, 4326) DEFAULT NULL,
|
||||
@@ -440,66 +435,62 @@ BEGIN
|
||||
bounds := ST_Envelope(bounds);
|
||||
END IF;
|
||||
RETURN QUERY
|
||||
EXECUTE format($string$
|
||||
SELECT
|
||||
((100.0 / (1+abs(log(1 + $3) - log(1 + numgeoms)))) * percentfill)::Numeric
|
||||
AS score, *
|
||||
FROM (
|
||||
WITH clipped_geom AS (
|
||||
SELECT column_id, table_id
|
||||
--, CASE WHEN $1 IS NOT NULL THEN ST_Clip(tile, 2, $1, True)
|
||||
, CASE WHEN $1 IS NOT NULL THEN ST_Clip(tile, $1, True)
|
||||
ELSE tile END clipped_tile
|
||||
--, tile as clipped_tile
|
||||
, tile
|
||||
FROM observatory.obs_column_table_tile
|
||||
WHERE ($1 IS NULL OR ST_Intersects($1, tile))
|
||||
AND (column_id = ANY($2) OR cardinality($2) = 0)
|
||||
), clipped_geom_countagg AS (
|
||||
SELECT column_id, table_id
|
||||
, ST_CountAgg(clipped_tile, 2, True)::Numeric notnull_pixels
|
||||
, ST_CountAgg(clipped_tile, 2, False)::Numeric pixels
|
||||
FROM clipped_geom
|
||||
GROUP BY column_id, table_id
|
||||
), clipped_geom_reagg AS (
|
||||
SELECT COUNT(*)::BIGINT cnt, a.column_id,
|
||||
cdb_observatory.FIRST(pixels) first_pixel,
|
||||
cdb_observatory.FIRST(notnull_pixels) first_notnull_pixel,
|
||||
cdb_observatory.FIRST(tile) first_tile,
|
||||
(ST_SummaryStatsAgg(clipped_tile, 2, True)).sum sum_geoms,
|
||||
(ST_SummaryStatsAgg(clipped_tile, 3, True)).mean mean_fill
|
||||
FROM clipped_geom_countagg a, clipped_geom b
|
||||
WHERE a.table_id = b.table_id
|
||||
AND a.column_id = b.column_id
|
||||
GROUP BY a.column_id, a.table_id
|
||||
) SELECT
|
||||
EXECUTE $string$
|
||||
WITH clipped_geom AS (
|
||||
SELECT column_id, table_id
|
||||
, CASE WHEN $1 IS NOT NULL THEN ST_Clip(tile, $1, True) -- -20
|
||||
ELSE tile END clipped_tile
|
||||
, tile
|
||||
FROM observatory.obs_column_table_tile_simple
|
||||
WHERE ($1 IS NULL OR ST_Intersects($1, tile))
|
||||
AND (column_id = ANY($2) OR cardinality($2) = 0)
|
||||
), clipped_geom_countagg AS (
|
||||
SELECT column_id, table_id
|
||||
, ST_CountAgg(clipped_tile, 1, True)::Numeric notnull_pixels -- -10
|
||||
, ST_CountAgg(clipped_tile, 1, False)::Numeric pixels -- -10
|
||||
FROM clipped_geom
|
||||
GROUP BY column_id, table_id
|
||||
), clipped_geom_reagg AS (
|
||||
SELECT COUNT(*)::BIGINT cnt, a.column_id,
|
||||
cdb_observatory.FIRST(pixels) first_pixel,
|
||||
cdb_observatory.FIRST(notnull_pixels) first_notnull_pixel,
|
||||
cdb_observatory.FIRST(tile) first_tile,
|
||||
(ST_SummaryStatsAgg(clipped_tile, 1, True)).sum::Numeric sum_geoms, -- ND
|
||||
(ST_SummaryStatsAgg(clipped_tile, 2, True)).mean::Numeric / 255 mean_fill --ND
|
||||
FROM clipped_geom_countagg a, clipped_geom b
|
||||
WHERE a.table_id = b.table_id
|
||||
AND a.column_id = b.column_id
|
||||
GROUP BY a.column_id, a.table_id
|
||||
), final AS (
|
||||
SELECT
|
||||
cnt, column_id
|
||||
|
||||
, (CASE WHEN first_notnull_pixel > 0
|
||||
THEN first_notnull_pixel / first_pixel
|
||||
ELSE 1
|
||||
END)::Numeric AS notnull_percent
|
||||
END)::Numeric
|
||||
AS notnull_percent
|
||||
, (CASE WHEN first_notnull_pixel > 0
|
||||
THEN sum_geoms
|
||||
ELSE COALESCE(ST_Value(first_tile, 2, ST_PointOnSurface($1)), 0)
|
||||
* (ST_Area($1) / ST_Area(ST_PixelAsPolygon(first_tile, 0, 0)) * first_pixel)
|
||||
END)::Numeric AS numgeoms
|
||||
ELSE COALESCE(ST_Value(first_tile, 1, ST_PointOnSurface($1)), 0)
|
||||
* (ST_Area($1) / ST_Area(ST_PixelAsPolygon(first_tile, 0, 0))
|
||||
* first_pixel) -- -20
|
||||
END)::Numeric
|
||||
AS numgeoms
|
||||
|
||||
, (CASE WHEN first_notnull_pixel > 0
|
||||
THEN mean_fill
|
||||
ELSE COALESCE(ST_Value(first_tile, 3, ST_PointOnSurface($1)), 0)
|
||||
END)::Numeric AS percentfill
|
||||
, null::numeric estnumgeoms, null::numeric meanmediansize
|
||||
--, ((ST_Area(ST_Transform($1, 3857)) / 1000000) / NullIf(
|
||||
-- CASE WHEN cdb_observatory.FIRST(notnull_pixels) > 0
|
||||
-- THEN (ST_SummaryStatsAgg(clipped_tile, 1, True)).mean
|
||||
-- ELSE Coalesce(ST_Value(cdb_observatory.FIRST(tile), 1, ST_PointOnSurface($1)), 0)
|
||||
-- END, 0))::Numeric AS estnumgeoms
|
||||
--, (CASE WHEN cdb_observatory.FIRST(notnull_pixels) > 0
|
||||
-- THEN (ST_SummaryStatsAgg(clipped_tile, 1, True)).mean
|
||||
-- ELSE COALESCE(ST_Value(cdb_observatory.FIRST(tile), 1, ST_PointOnSurface($1)), 0)
|
||||
-- END)::Numeric AS meanmediansize
|
||||
ELSE COALESCE(ST_Value(first_tile, 2, ST_PointOnSurface($1))::Numeric / 255, 0) -- -2
|
||||
END)::Numeric
|
||||
AS percentfill
|
||||
, null::numeric estnumgeoms
|
||||
, null::numeric meanmediansize
|
||||
FROM clipped_geom_reagg
|
||||
) foo
|
||||
$string$) USING bounds, filter_geom_ids, desired_num_geoms;
|
||||
) SELECT
|
||||
((100.0 / (1+abs(log(1 + $3) - log(1 + numgeoms::Numeric)))) * percentfill)::Numeric
|
||||
AS score, *
|
||||
FROM final
|
||||
$string$ USING bounds, filter_geom_ids, desired_num_geoms;
|
||||
RETURN;
|
||||
END
|
||||
$$ LANGUAGE plpgsql IMMUTABLE;
|
||||
|
||||
Reference in New Issue
Block a user