11 Commits

Author SHA1 Message Date
Carla
e4c8d11ef7 Merge pull request #192 from CartoDB/182-removing_tests
Removing namedplace tests and adding granularity on them
2015-11-27 15:58:06 +01:00
Carla Iriberri
2d05db5597 More granularity for tests 2015-11-25 18:54:14 +01:00
Carla Iriberri
d0e0e5e94f First cleaning of functions 2015-11-25 16:58:30 +01:00
Carla Iriberri
3dd0827765 Reviews tests and adds granularity 2015-11-25 12:45:03 +01:00
Carla
cfef57be74 Merge pull request #187 from CartoDB/186-missing_guessing_in_extension
Add guessing function and tests to extension
2015-11-20 17:19:50 +01:00
Carla
182b34244e Delete namedplace_country_guessing.sql 2015-11-20 16:53:14 +01:00
Carla Iriberri
df1d6c0d8d Add guessing function and tests to extension 2015-11-20 15:50:46 +01:00
Carla
905d1c9348 Merge pull request #184 from CartoDB/oriol_adds
Oriol adds
2015-11-19 15:31:04 +01:00
oriolbx
2805a1c05c Update README.md 2015-11-18 12:30:09 +01:00
oriolbx
3e6593d480 Update README.md 2015-11-18 11:59:51 +01:00
oriolbx
913c4ade63 Update README.md 2015-11-18 11:57:47 +01:00
7 changed files with 4727 additions and 2149 deletions

View File

@@ -1247,6 +1247,84 @@ Data library
**Source URL**: http://www.mass.gov/anf/research-and-tech/it-serv-and-support/application-serv/office-of-geographic-information-massgis/datalayers/structures.html
#### Municipalities of Spain
**Description**: Geometries for the municipalities of Spain. Official dataset from Instituto Geográfico Nacional de España.
**Source**: [Instituto Geográfico Nacional](http://www.ign.es/)
**License**: Public data - Attribution required
**Table Name**: ign_spanish_adm3_municipalities
**Sync Table**: `false`
**Source URL**: http://centrodedescargas.cnig.es/CentroDescargas/equipamiento/lineas_limite.zip
#### Municipalities of Spain with displaced Canary Islands
**Description**: Geometries for the municipalities of Spain with the Canary Islands displaced. Official dataset from Instituto Geográfico Nacional de España.
**Source**: [Instituto Geográfico Nacional](http://www.ign.es/)
**License**: Public data - Attribution required
**Table Name**: ign_spanish_adm3_municipalities_displaced_canary
**Sync Table**: `false`
**Source URL**: http://centrodedescargas.cnig.es/CentroDescargas/equipamiento/lineas_limite.zip
#### Provinces of Spain
**Description**: Geometries for the provinces of Spain. Official dataset from Instituto Geográfico Nacional de España.
**Source**: [Instituto Geográfico Nacional](http://www.ign.es/)
**License**: Public data - Attribution required
**Table Name**: ign_spanish_adm2_provinces
**Sync Table**: `false`
**Source URL**: http://centrodedescargas.cnig.es/CentroDescargas/equipamiento/lineas_limite.zip
#### Provinces of Spain with displaced Canary Islands
**Description**: Geometries for the provinces of Spain with the Canary Islands displaced. Official dataset from Instituto Geográfico Nacional de España.
**Source**: [Instituto Geográfico Nacional](http://www.ign.es/)
**License**: Public data - Attribution required
**Table Name**: ign_spanish_adm2_provinces_displaced_canary
**Sync Table**: `false`
**Source URL**: http://centrodedescargas.cnig.es/CentroDescargas/equipamiento/lineas_limite.zip
#### Autonomous Communities of Spain.
**Description**: Geometries for the autonomous communities of Spain. Official dataset from Instituto Geográfico Nacional de España.
**Source**: [Instituto Geográfico Nacional](http://www.ign.es/)
**License**: Public data - Attribution required
**Table Name**: ign_spanish_adm1_ccaa
**Sync Table**: `false`
**Source URL**: http://centrodedescargas.cnig.es/CentroDescargas/equipamiento/lineas_limite.zip
#### Autonomous communities of Spain with displaced Canary Islands
**Description**: Geometries for the autonomous communities of Spain with the Canary Islands displaced. Official dataset from Instituto Geográfico Nacional de España.
**Source**: [Instituto Geográfico Nacional](http://www.ign.es/)
**License**: Public data - Attribution required
**Table Name**: ign_spanish_adm1_ccaa_displaced_canary
**Sync Table**: `false`
**Source URL**: http://centrodedescargas.cnig.es/CentroDescargas/equipamiento/lineas_limite.zip
***
### Historic

View File

@@ -46,13 +46,25 @@ SELECT (geocode_namedplace(Array['Portland', 'Portland', 'New York City'], Array
Portland | Maine | USA | | f
(3 rows)
SELECT namedplace_guess_country(Array['granada', 'jaen', 'cordoba', 'madrid', 'valladolid']);
namedplace_guess_country
--------------------------
(1 row)
-- Add a named place source
COPY global_cities_alternates_limited (geoname_id, name, the_geom, created_at, updated_at, the_geom_webmercator, preferred, lowername, cartodb_id, admin1_geonameid, iso2, admin1) FROM stdin;
COPY global_cities_points_limited (geoname_id, name, asciiname, altnames, featclass, featcode, iso2, admin1, admin2, population, the_geom, created_at, updated_at, the_geom_webmercator, cartodb_id, lowername) FROM stdin;
-- Check that the geocoding function is callable, should return success = true
SELECT (geocode_namedplace(Array['Barcelona'])).*
SELECT (geocode_namedplace(Array['Barcelona'])).*;
q | geom | success
-----------+----------------------------------------------------+---------
Barcelona | 0101000020E6100000CA15DEE522E653C0A4C2D842902B4540 | t
(1 row)
SELECT namedplace_guess_country(Array['Barcelona']);
namedplace_guess_country
--------------------------
ES
(1 row)

View File

@@ -288,6 +288,40 @@ CREATE OR REPLACE FUNCTION geocode_namedplace(places text[]) RETURNS SETOF geoco
END
$$;
CREATE OR REPLACE FUNCTION namedplace_guess_country(places text[])
RETURNS text AS $$
DECLARE
country_code text;
threshold CONSTANT float := 0.8;
input_length integer := array_length(places, 1);
BEGIN
BEGIN
WITH hist AS (
SELECT count(DISTINCT(lower(p.s), gp.iso2)) AS c, iso2
FROM global_cities_points_limited gp
inner join (SELECT unnest(places) AS s) p
ON (gp.lowername = lower(s))
GROUP BY iso2
),
best_two AS (
SELECT iso2, c
FROM hist
WHERE c > input_length * threshold
ORDER BY c DESC
LIMIT 2
)
SELECT iso2 INTO STRICT country_code
FROM (SELECT iso2, c, max(c) over() AS maxcount FROM best_two) bt
WHERE bt.c = bt.maxcount;
EXCEPTION
WHEN NO_DATA_FOUND OR too_many_rows THEN
RETURN NULL;
END;
RETURN country_code;
END;
$$ LANGUAGE 'plpgsql' SECURITY DEFINER IMMUTABLE;
--------------------------------------------------------------------------------
-- Support tables

View File

@@ -5,6 +5,7 @@ SELECT (geocode_namedplace(Array['sunapee', 'sunapeeee', 'New York City', 'Madri
SELECT (geocode_namedplace(Array['Portland', 'Portland', 'New York City'], Array['Maine', 'Oregon', NULL], 'USA')).*;
SELECT (geocode_namedplace(Array['Portland'], 'Oregon', 'USA')).*;
SELECT (geocode_namedplace(Array['Portland', 'Portland', 'New York City'], Array['Maine', 'Oregon', NULL], Array['USA'])).*;
SELECT namedplace_guess_country(Array['granada', 'jaen', 'cordoba', 'madrid', 'valladolid']);
-- Add a named place source
COPY global_cities_alternates_limited (geoname_id, name, the_geom, created_at, updated_at, the_geom_webmercator, preferred, lowername, cartodb_id, admin1_geonameid, iso2, admin1) FROM stdin;
@@ -16,5 +17,6 @@ COPY global_cities_points_limited (geoname_id, name, asciiname, altnames, featcl
\.
-- Check that the geocoding function is callable, should return success = true
SELECT (geocode_namedplace(Array['Barcelona'])).*
SELECT (geocode_namedplace(Array['Barcelona'])).*;
SELECT namedplace_guess_country(Array['Barcelona']);

View File

@@ -1,35 +0,0 @@
-- Return a guess about the country where the places are located, if possible.
-- E.g: SELECT namedplace_guess_country(Array['granada', 'jaen', 'cordoba', 'madrid', 'valladolid']); => NULL
-- E.g: SELECT namedplace_guess_country(Array['granada', 'jaén', 'córdoba', 'madrid', 'valladolid', 'peligros'])); => 'ES'
CREATE OR REPLACE FUNCTION namedplace_guess_country(places text[])
RETURNS text AS $$
DECLARE
country_code text;
threshold CONSTANT float := 0.8;
input_length integer := array_length(places, 1);
BEGIN
BEGIN
WITH hist AS (
SELECT count(DISTINCT(lower(p.s), gp.iso2)) AS c, iso2
FROM global_cities_points_limited gp
inner join (SELECT unnest(places) AS s) p
ON (gp.lowername = lower(s))
GROUP BY iso2
),
best_two AS (
SELECT iso2, c
FROM hist
WHERE c > input_length * threshold
ORDER BY c DESC
LIMIT 2
)
SELECT iso2 INTO STRICT country_code
FROM (SELECT iso2, c, max(c) over() AS maxcount FROM best_two) bt
WHERE bt.c = bt.maxcount;
EXCEPTION
WHEN NO_DATA_FOUND OR too_many_rows THEN
RETURN NULL;
END;
RETURN country_code;
END;
$$ LANGUAGE 'plpgsql' SECURITY DEFINER IMMUTABLE;

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff