From ea7c16fbaf60fd400a943006b3a55fce174a9c0e Mon Sep 17 00:00:00 2001 From: Paul Norman Date: Mon, 21 Dec 2015 23:59:26 -0800 Subject: [PATCH] Convert some simple functions from plpgsql to sql SQL is a faster language to call, and these are very simple functions. --- scripts-available/CDB_LatLng.sql | 12 ++++-------- scripts-available/CDB_XYZ.sql | 21 +++------------------ 2 files changed, 7 insertions(+), 26 deletions(-) diff --git a/scripts-available/CDB_LatLng.sql b/scripts-available/CDB_LatLng.sql index d1304c3..7f08f61 100644 --- a/scripts-available/CDB_LatLng.sql +++ b/scripts-available/CDB_LatLng.sql @@ -8,16 +8,12 @@ -- CREATE OR REPLACE FUNCTION CDB_LatLng (lat NUMERIC, lng NUMERIC) RETURNS geometry as $$ -BEGIN -- this function is silly - RETURN ST_SetSRID(ST_MakePoint(lng,lat),4326); -END; -$$ language plpgsql IMMUTABLE; + SELECT ST_SetSRID(ST_MakePoint(lng,lat),4326); +$$ language SQL IMMUTABLE; CREATE OR REPLACE FUNCTION CDB_LatLng (lat FLOAT8, lng FLOAT8) RETURNS geometry as $$ -BEGIN -- this function is silly - RETURN ST_SetSRID(ST_MakePoint(lng,lat),4326); -END; -$$ language plpgsql IMMUTABLE; + SELECT ST_SetSRID(ST_MakePoint(lng,lat),4326); +$$ language SQL IMMUTABLE; diff --git a/scripts-available/CDB_XYZ.sql b/scripts-available/CDB_XYZ.sql index 92e7dae..fa38171 100644 --- a/scripts-available/CDB_XYZ.sql +++ b/scripts-available/CDB_XYZ.sql @@ -5,24 +5,9 @@ CREATE OR REPLACE FUNCTION CDB_XYZ_Resolution(z INTEGER) RETURNS FLOAT8 AS $$ -DECLARE - earth_circumference FLOAT8; - tile_size INTEGER; - full_resolution FLOAT8; -BEGIN - - -- Earth equatorial circumference in meters (according to wikipedia) - earth_circumference := 40075017; - - -- Size of each tile in pixels (1:1 aspect ratio) - tile_size := 256; - - full_resolution := earth_circumference/tile_size; - - RETURN full_resolution / (power(2,z)); - -END -$$ LANGUAGE 'plpgsql' IMMUTABLE STRICT; + -- circumference divided by 256 is z0 resolution, then divide by 2^z + SELECT 40075017 / 256 / power(2, z); +$$ LANGUAGE SQL IMMUTABLE STRICT; -- } -- {