From 352916d2475266bbc9c18122e56a5a5be6f8fee4 Mon Sep 17 00:00:00 2001 From: Carla Iriberri Date: Mon, 30 Nov 2015 15:39:25 +0100 Subject: [PATCH] Cleans country inputs and fixes some trailing spaces --- geocoder/admin1/sql/geocoder.sql | 38 ++++++++++------------ geocoder/extension/sql/0.0.1/20_admin1.sql | 22 ++++++------- geocoder/setup/admin1/response-types.sql | 16 +++++++-- 3 files changed, 43 insertions(+), 33 deletions(-) diff --git a/geocoder/admin1/sql/geocoder.sql b/geocoder/admin1/sql/geocoder.sql index c6ff759..0f1e5dd 100644 --- a/geocoder/admin1/sql/geocoder.sql +++ b/geocoder/admin1/sql/geocoder.sql @@ -1,11 +1,9 @@ --Text array, country array -- - - --- Function --- CREATE OR REPLACE FUNCTION geocode_admin_country_v1(names text[], country text[]) RETURNS SETOF geocode_admin_country_v1 AS $$ -DECLARE +DECLARE ret geocode_admin_country_v1%rowtype; nans TEXT[]; BEGIN @@ -21,19 +19,19 @@ DECLARE END IF; - FOR ret IN WITH - p AS (SELECT r.p, r.q, c, (SELECT iso3 FROM country_decoder WHERE lower(r.c) = ANY (synonyms)) i FROM (SELECT trim(replace(lower(unnest(names)),'.',' ')) p, unnest(names) q, unnest(country) c) r) + FOR ret IN WITH + p AS (SELECT r.p, r.q, c, (SELECT iso3 FROM country_decoder WHERE lower(regexp_replace(r.c, '[^a-zA-Z\u00C0-\u00ff]+', '', 'g'))::text = ANY (synonyms)) i FROM (SELECT trim(replace(lower(unnest(names)),'.',' ')) p, unnest(names) q, unnest(country) c) r) SELECT q, c, geom, CASE WHEN geom IS NULL THEN FALSE ELSE TRUE END AS success FROM ( SELECT q, c, ( - SELECT the_geom + SELECT the_geom FROM global_province_polygons - WHERE p.p = ANY (synonyms) + WHERE p.p = ANY (synonyms) AND iso3 = p.i -- To calculate frequency, I simply counted the number of users - -- we had signed up in each country. Countries with more users, + -- we had signed up in each country. Countries with more users, -- we favor higher in the geocoder :) ORDER BY frequency DESC LIMIT 1 ) geom @@ -51,23 +49,23 @@ $$ LANGUAGE 'plpgsql'; --- Function --- CREATE OR REPLACE FUNCTION geocode_admin_country_v1(name text[]) RETURNS SETOF geocode_admin_v1 AS $$ -DECLARE +DECLARE ret geocode_admin_v1%rowtype; BEGIN FOR ret IN SELECT q, geom, CASE WHEN geom IS NULL THEN FALSE ELSE TRUE END AS success FROM ( - SELECT + SELECT q, ( - SELECT the_geom + SELECT the_geom FROM global_province_polygons - WHERE d.c = ANY (synonyms) + WHERE d.c = ANY (synonyms) ORDER BY frequency DESC LIMIT 1 ) geom FROM (SELECT trim(replace(lower(unnest(name)),'.',' ')) c, unnest(name) q) d ) v - LOOP + LOOP RETURN NEXT ret; END LOOP; RETURN; @@ -80,23 +78,23 @@ $$ LANGUAGE 'plpgsql'; --- Function --- CREATE OR REPLACE FUNCTION geocode_admin_country_v1(name text[], inputcountry text) RETURNS SETOF geocode_admin_v1 AS $$ - DECLARE + DECLARE ret geocode_admin_v1%rowtype; BEGIN - FOR ret IN WITH - p AS (SELECT r.c, r.q, (SELECT iso3 FROM country_decoder WHERE lower(inputcountry) = ANY (synonyms)) i FROM (SELECT trim(replace(lower(unnest(name)),'.',' ')) c, unnest(name) q) r) + FOR ret IN WITH + p AS (SELECT r.c, r.q, (SELECT iso3 FROM country_decoder WHERE lower(regexp_replace(inputcountry, '[^a-zA-Z\u00C0-\u00ff]+', '', 'g'))::text = ANY (synonyms)) i FROM (SELECT trim(replace(lower(unnest(name)),'.',' ')) c, unnest(name) q) r) SELECT q, geom, CASE WHEN geom IS NULL THEN FALSE ELSE TRUE END AS success FROM ( - SELECT + SELECT q, ( - SELECT the_geom + SELECT the_geom FROM global_province_polygons - WHERE p.c = ANY (synonyms) + WHERE p.c = ANY (synonyms) AND iso3 = p.i -- To calculate frequency, I simply counted the number of users - -- we had signed up in each country. Countries with more users, + -- we had signed up in each country. Countries with more users, -- we favor higher in the geocoder :) ORDER BY frequency DESC LIMIT 1 ) geom diff --git a/geocoder/extension/sql/0.0.1/20_admin1.sql b/geocoder/extension/sql/0.0.1/20_admin1.sql index 8d4310a..756ae0e 100644 --- a/geocoder/extension/sql/0.0.1/20_admin1.sql +++ b/geocoder/extension/sql/0.0.1/20_admin1.sql @@ -13,9 +13,9 @@ CREATE OR REPLACE FUNCTION geocode_admin1_polygons(name text[]) RETURNS SETOF ge FROM ( SELECT q, ( - SELECT the_geom + SELECT the_geom FROM global_province_polygons - WHERE d.c = ANY (synonyms) + WHERE d.c = ANY (synonyms) ORDER BY frequency DESC LIMIT 1 ) geom FROM (SELECT trim(replace(lower(unnest(name)),'.',' ')) c, unnest(name) q) d @@ -31,23 +31,23 @@ $$; CREATE OR REPLACE FUNCTION geocode_admin1_polygons(name text[], inputcountry text) RETURNS SETOF geocode_admin_v1 LANGUAGE plpgsql SECURITY DEFINER AS $$ - DECLARE + DECLARE ret geocode_admin_v1%rowtype; BEGIN - FOR ret IN WITH + FOR ret IN WITH p AS (SELECT r.c, r.q, (SELECT iso3 FROM country_decoder WHERE lower(inputcountry) = ANY (synonyms)) i FROM (SELECT trim(replace(lower(unnest(name)),'.',' ')) c, unnest(name) q) r) SELECT q, geom, CASE WHEN geom IS NULL THEN FALSE ELSE TRUE END AS success FROM ( SELECT q, ( - SELECT the_geom + SELECT the_geom FROM global_province_polygons - WHERE p.c = ANY (synonyms) + WHERE p.c = ANY (synonyms) AND iso3 = p.i -- To calculate frequency, I simply counted the number of users - -- we had signed up in each country. Countries with more users, + -- we had signed up in each country. Countries with more users, -- we favor higher in the geocoder :) ORDER BY frequency DESC LIMIT 1 ) geom @@ -63,7 +63,7 @@ $$; CREATE OR REPLACE FUNCTION geocode_admin1_polygons(names text[], country text[]) RETURNS SETOF geocode_admin_country_v1 LANGUAGE plpgsql SECURITY DEFINER AS $$ - DECLARE + DECLARE ret geocode_admin_country_v1%rowtype; nans TEXT[]; BEGIN @@ -86,12 +86,12 @@ CREATE OR REPLACE FUNCTION geocode_admin1_polygons(names text[], country text[]) FROM ( SELECT q, c, ( - SELECT the_geom + SELECT the_geom FROM global_province_polygons - WHERE p.p = ANY (synonyms) + WHERE p.p = ANY (synonyms) AND iso3 = p.i -- To calculate frequency, I simply counted the number of users - -- we had signed up in each country. Countries with more users, + -- we had signed up in each country. Countries with more users, -- we favor higher in the geocoder :) ORDER BY frequency DESC LIMIT 1 ) geom diff --git a/geocoder/setup/admin1/response-types.sql b/geocoder/setup/admin1/response-types.sql index acc7abc..a3484ff 100644 --- a/geocoder/setup/admin1/response-types.sql +++ b/geocoder/setup/admin1/response-types.sql @@ -1,3 +1,15 @@ -- Response types for admin1 geocoder -CREATE TYPE geocode_admin_v1 AS (q TEXT, geom GEOMETRY, success BOOLEAN); -CREATE TYPE geocode_admin_country_v1 AS (q TEXT, c TEXT, geom GEOMETRY, success BOOLEAN); + +CREATE TYPE geocode_admin_v1 AS ( + q text, + geom geometry, + success boolean +); + + +CREATE TYPE geocode_admin_country_v1 AS ( + q text, + c text, + geom geometry, + success boolean +);