From e0ee93bc3e5b66750110c55952d45b0392af5ea4 Mon Sep 17 00:00:00 2001 From: andrewxhill Date: Fri, 22 Aug 2014 13:21:14 -0400 Subject: [PATCH 1/8] removed single array method of the admin1 polygon geocoder --- geocoder/admin1/sql/geocoder.sql | 59 -------------------------------- geocoder/setup/indexes.sql | 2 ++ 2 files changed, 2 insertions(+), 59 deletions(-) diff --git a/geocoder/admin1/sql/geocoder.sql b/geocoder/admin1/sql/geocoder.sql index efc3b9d..8d0d012 100644 --- a/geocoder/admin1/sql/geocoder.sql +++ b/geocoder/admin1/sql/geocoder.sql @@ -1,64 +1,5 @@ --- Usage ---SELECT (geocode_admin1_polygons(Array['TX','Cuidad Real', 'sevilla'])).* - ---- Function - -CREATE OR REPLACE FUNCTION test_geocode_admin1_polygons(name text[]) - RETURNS SETOF geocode_admin_v1 AS $$ - 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 - q, ( - SELECT the_geom - FROM global_province_polygons - WHERE d.c = ANY (synonyms) - -- To calculate frequency, I simply counted the number of 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 - FROM (SELECT trim(replace(lower(unnest(name)),'.',' ')) c, unnest(name) q) d - ) v - LOOP - RETURN NEXT ret; - END LOOP; - RETURN; -END -$$ LANGUAGE 'plpgsql' SECURITY DEFINER; -Text array, country name - - --- CREATE OR REPLACE FUNCTION test_geocode_admin1_polygons(name text[]) --- RETURNS SETOF geocode_admin_v1 AS $$ --- DECLARE --- ret geocode_admin_v1%rowtype; --- BEGIN --- -- FOR ret IN --- RETURN QUERY --- SELECT --- d.q, n.the_geom as geom, --- CASE WHEN s.adm1_code IS NULL then FALSE ELSE TRUE END AS success --- FROM ( --- SELECT --- q, lower(regexp_replace(q, '[^a-zA-Z\u00C0-\u00ff]+', '', 'g'))::text x --- FROM (SELECT unnest(name) q) g --- ) d --- LEFT OUTER JOIN --- admin1_synonyms s ON name_ = d.x --- LEFT OUTER JOIN --- ne_admin1_v3 n ON s.adm1_code = n.adm1_code; --- END --- $$ LANGUAGE 'plpgsql' SECURITY DEFINER; - - ---- Usage - --- SELECT (geocode_admin1_polygons(Array['az', 'Texas'], 'Ecuador')).* --- Function diff --git a/geocoder/setup/indexes.sql b/geocoder/setup/indexes.sql index bfd1da4..4def421 100644 --- a/geocoder/setup/indexes.sql +++ b/geocoder/setup/indexes.sql @@ -7,3 +7,5 @@ CREATE INDEX idx_admin0_synonyms_rank ON admin0_synonyms (rank); -- create indexes on polygon table CREATE UNIQUE INDEX idx_ne_admin0_v3_adm0_a3 ON ne_admin0_v3 (adm0_a3); +-- create indexes on postal code polygon table +CREATE UNIQUE INDEX idx_postal_code_polygons_a3_code ON postal_code_polygons (adm0_a3, postal_code) \ No newline at end of file From c54af7c770219648376a6e5ddd4a2a97756163c0 Mon Sep 17 00:00:00 2001 From: Carla Date: Mon, 25 Aug 2014 12:33:47 +0200 Subject: [PATCH 2/8] add data sources --- geocoder/ip-addresses/README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/geocoder/ip-addresses/README.md b/geocoder/ip-addresses/README.md index 5702d3c..f7ee266 100644 --- a/geocoder/ip-addresses/README.md +++ b/geocoder/ip-addresses/README.md @@ -5,11 +5,14 @@ IP address geocoder ### Creation steps -1. upload a new dataset to the geocoder table, call it latest_ip_address_locations +1. Upload a new dataset to the geocoder table, call it latest_ip_address_locations 2. Run the sql/build_data_table script to update the table ### Data Sources +GeoLite2 open source database [Created by MaxMind](http://www.maxmind.com) - +http://dev.maxmind.com/geoip/geoip2/geolite2/ Download the CSV [Geolite2 City](http://geolite.maxmind.com/download/geoip/database/GeoLite2-City-CSV.zip) + ### Preparation details From 8cd501a4be1b16e2184cb466985a7c0e3b22da0b Mon Sep 17 00:00:00 2001 From: Carla Date: Mon, 25 Aug 2014 12:38:52 +0200 Subject: [PATCH 3/8] modifies comment in sql for title --- geocoder/ip-addresses/sql/build_data_table.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/geocoder/ip-addresses/sql/build_data_table.sql b/geocoder/ip-addresses/sql/build_data_table.sql index dbf5dc3..93fc454 100644 --- a/geocoder/ip-addresses/sql/build_data_table.sql +++ b/geocoder/ip-addresses/sql/build_data_table.sql @@ -1,5 +1,5 @@ ----- Postal Code Polygon table --- +---- IP addresses table --- --- --- -- Clear table From ec78d147bf447b1cf9872da92656b3c0f9967320 Mon Sep 17 00:00:00 2001 From: Carla Date: Tue, 26 Aug 2014 11:00:13 +0200 Subject: [PATCH 4/8] solves #50 --- geocoder/postal-codes/README.md | 36 ++++++++++++++++++- .../sql/build_zipcode_points_table.sql | 26 ++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 geocoder/postal-codes/sql/build_zipcode_points_table.sql diff --git a/geocoder/postal-codes/README.md b/geocoder/postal-codes/README.md index dd49dc9..7c4b55f 100644 --- a/geocoder/postal-codes/README.md +++ b/geocoder/postal-codes/README.md @@ -17,9 +17,43 @@ USA polygons - http://www2.census.gov/geo/tiger/TIGER2013/ZCTA5/tl_2013_us_zcta5 French polygons - http://www.data.gouv.fr/dataset/fond-de-carte-des-codes-postaux +All countries points [GeoNames](www.geonames.org) - http://download.geonames.org/export/zip/allCountries.zip + ### Preparation details # Postal code geocoder (points) -todo +1. Download the allCountries.zip file from [GeoNames](www.geonames.org). Import and rename the table as tmp_zipcode_points. You can follow the manual process explained below instead. + +This dataset includes data for the following countries: + +```` +CH, ES, GU, ZA, MX, SJ, NL, RU, AX, TH, AR, MY, RE, LK, GB, IS, GL, JE, DK, IN, +SI, GP, MQ, BR, SM, BG, NZ, MP, CZ, DO, MD, PK, TR, VI, BD, GG, LT, PM, MC, US, +IT, LU, SK, LI, PR, IM, NO, PT, PL, FI, JP, CA, DE, HU, PH, SE, VA, YT, MK, FR, +MH, RO, FO, GF, AD, HR, DZ, GT, AU, AS, BE, AT +```` + +The columns that are loaded are the following ones: +field_1: corresponding to ISO2 +field_10: corresponds to latitude +field_11: corresponds to longitude +field_2: corresponds to ZIP code + +2. Georeference the table using field11 as longitude and field10 as latitude in order to construct the_geom. + +3. Add column iso3 (text) and run sql/build_zipcode_points_table.sql. + + +**Alternative manual process** + +Open the allCountries.txt file with Excel an add a new row on top. Delete columns C-I and L. + +In the first row, add the following columns: iso2, zipcode, lat, long. + +Import the file ignoring step 2. + +_The big size of the dataset may cause interruptions in the processing of the coordinates after uploading the file, manipulating the file before importing is a faster workaround._ + + diff --git a/geocoder/postal-codes/sql/build_zipcode_points_table.sql b/geocoder/postal-codes/sql/build_zipcode_points_table.sql new file mode 100644 index 0000000..30ea6db --- /dev/null +++ b/geocoder/postal-codes/sql/build_zipcode_points_table.sql @@ -0,0 +1,26 @@ + +---- Postal Code Points table --- + +-- Clear table + +DELETE FROM zipcode_points; + +-- Insert points + +DELETE FROM zipcode_points; + +INSERT INTO zip_code_points (the_geom, zipcode, iso3) +SELECT the_geom, zipcode, + ( + SELECT country_decoder.iso3 FROM country_decoder + WHERE tmp_zipcode_points.iso2 = country_decoder.iso2 + ) +FROM tmp_zipcode_points +); + + +-- Drops temporary table + +DROP TABLE tmp_zipcode_points; + + From 5e5fc1777cbac63e76bdbf72889123d48b4f6706 Mon Sep 17 00:00:00 2001 From: Carla Date: Tue, 26 Aug 2014 11:09:14 +0200 Subject: [PATCH 5/8] fixes markdown --- geocoder/postal-codes/README.md | 39 ++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/geocoder/postal-codes/README.md b/geocoder/postal-codes/README.md index 7c4b55f..dc686d9 100644 --- a/geocoder/postal-codes/README.md +++ b/geocoder/postal-codes/README.md @@ -24,35 +24,38 @@ All countries points [GeoNames](www.geonames.org) - http://download.geonames.org # Postal code geocoder (points) -1. Download the allCountries.zip file from [GeoNames](www.geonames.org). Import and rename the table as tmp_zipcode_points. You can follow the manual process explained below instead. +Download the allCountries.zip file from [GeoNames](www.geonames.org). Import and rename the table as tmp_zipcode_points. You can follow the manual process explained below instead. -This dataset includes data for the following countries: + This dataset includes data for the following countries: + + ```` + CH, ES, GU, ZA, MX, SJ, NL, RU, AX, TH, AR, MY, RE, LK, GB, IS, GL, JE, DK, IN, + SI, GP, MQ, BR, SM, BG, NZ, MP, CZ, DO, MD, PK, TR, VI, BD, GG, LT, PM, MC, US, + IT, LU, SK, LI, PR, IM, NO, PT, PL, FI, JP, CA, DE, HU, PH, SE, VA, YT, MK, FR, + MH, RO, FO, GF, AD, HR, DZ, GT, AU, AS, BE, AT + ```` -```` -CH, ES, GU, ZA, MX, SJ, NL, RU, AX, TH, AR, MY, RE, LK, GB, IS, GL, JE, DK, IN, -SI, GP, MQ, BR, SM, BG, NZ, MP, CZ, DO, MD, PK, TR, VI, BD, GG, LT, PM, MC, US, -IT, LU, SK, LI, PR, IM, NO, PT, PL, FI, JP, CA, DE, HU, PH, SE, VA, YT, MK, FR, -MH, RO, FO, GF, AD, HR, DZ, GT, AU, AS, BE, AT -```` + The columns that are loaded are the following ones: -The columns that are loaded are the following ones: -field_1: corresponding to ISO2 -field_10: corresponds to latitude -field_11: corresponds to longitude -field_2: corresponds to ZIP code + - field_1: corresponding to ISO2 + - field_10: corresponds to latitude + - field_11: corresponds to longitude + - field_2: corresponds to ZIP code -2. Georeference the table using field11 as longitude and field10 as latitude in order to construct the_geom. +1. Georeference the table using field11 as longitude and field10 as latitude in order to construct the_geom. -3. Add column iso3 (text) and run sql/build_zipcode_points_table.sql. +2. Add column iso3 (text) and run sql/build_zipcode_points_table.sql. **Alternative manual process** -Open the allCountries.txt file with Excel an add a new row on top. Delete columns C-I and L. +1. Open the allCountries.txt file with Excel an add a new row on top. -In the first row, add the following columns: iso2, zipcode, lat, long. +2. Delete columns C-I and L. -Import the file ignoring step 2. +3. In the first row, add the following columns: iso2, zipcode, lat, long. + +4. Import the file ignoring step 1. _The big size of the dataset may cause interruptions in the processing of the coordinates after uploading the file, manipulating the file before importing is a faster workaround._ From a4d7962682e4e1d5a817b05dcad816142eca4354 Mon Sep 17 00:00:00 2001 From: Carla Date: Tue, 26 Aug 2014 11:19:53 +0200 Subject: [PATCH 6/8] adds info for polygons and reorganizes document --- geocoder/postal-codes/README.md | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/geocoder/postal-codes/README.md b/geocoder/postal-codes/README.md index 7c4b55f..9b3c89e 100644 --- a/geocoder/postal-codes/README.md +++ b/geocoder/postal-codes/README.md @@ -3,8 +3,14 @@ Postal code geocoder (polygons) ### Function +By following the next steps a table is populated with zipcodes from Australia, Canada, USA and France (identified by iso3) related with their spatial location in terms of polygons. + ### Creation steps +1. Import the four files attached in the section "Datasources". + +2. Run sql/build_data_table.sql. Notice that table "postal_code_polygons" should exist in advance with columns: _the_geom_, _adm0_a3_ and _postal_code_. + ### Data Sources Australian polygons - http://www.abs.gov.au/AUSSTATS/abs@.nsf/DetailsPage/2033.0.55.0012011?OpenDocument @@ -17,14 +23,21 @@ USA polygons - http://www2.census.gov/geo/tiger/TIGER2013/ZCTA5/tl_2013_us_zcta5 French polygons - http://www.data.gouv.fr/dataset/fond-de-carte-des-codes-postaux -All countries points [GeoNames](www.geonames.org) - http://download.geonames.org/export/zip/allCountries.zip - ### Preparation details +The names of the imported files are: + +- doc for Australia table +- gfsa000a11a_e for Canada table +- tl_2013_us_zcta510 for USA table +- codes_postaux for France table + # Postal code geocoder (points) -1. Download the allCountries.zip file from [GeoNames](www.geonames.org). Import and rename the table as tmp_zipcode_points. You can follow the manual process explained below instead. +### Function + +By following the next steps a table is populated with zipcodes of different countries (identified by iso3) related with their spatial location in terms of points. This dataset includes data for the following countries: @@ -35,6 +48,11 @@ IT, LU, SK, LI, PR, IM, NO, PT, PL, FI, JP, CA, DE, HU, PH, SE, VA, YT, MK, FR, MH, RO, FO, GF, AD, HR, DZ, GT, AU, AS, BE, AT ```` +### Creation steps + +1. Download the allCountries.zip file from [GeoNames](www.geonames.org). Import and rename the table as tmp_zipcode_points. You can follow the manual process explained below instead. + + The columns that are loaded are the following ones: field_1: corresponding to ISO2 field_10: corresponds to latitude @@ -54,6 +72,12 @@ In the first row, add the following columns: iso2, zipcode, lat, long. Import the file ignoring step 2. +### Data Sources + +All countries points [GeoNames](www.geonames.org) - http://download.geonames.org/export/zip/allCountries.zip + +### Preparation details + _The big size of the dataset may cause interruptions in the processing of the coordinates after uploading the file, manipulating the file before importing is a faster workaround._ From 8482759057651d0eca654721b10c333bc028c070 Mon Sep 17 00:00:00 2001 From: Carla Date: Tue, 26 Aug 2014 11:24:43 +0200 Subject: [PATCH 7/8] postal-codes --- geocoder/postal-codes/README.md | 65 ++++++++++++++++++++++----------- 1 file changed, 43 insertions(+), 22 deletions(-) diff --git a/geocoder/postal-codes/README.md b/geocoder/postal-codes/README.md index dc686d9..9b3c89e 100644 --- a/geocoder/postal-codes/README.md +++ b/geocoder/postal-codes/README.md @@ -3,8 +3,14 @@ Postal code geocoder (polygons) ### Function +By following the next steps a table is populated with zipcodes from Australia, Canada, USA and France (identified by iso3) related with their spatial location in terms of polygons. + ### Creation steps +1. Import the four files attached in the section "Datasources". + +2. Run sql/build_data_table.sql. Notice that table "postal_code_polygons" should exist in advance with columns: _the_geom_, _adm0_a3_ and _postal_code_. + ### Data Sources Australian polygons - http://www.abs.gov.au/AUSSTATS/abs@.nsf/DetailsPage/2033.0.55.0012011?OpenDocument @@ -17,45 +23,60 @@ USA polygons - http://www2.census.gov/geo/tiger/TIGER2013/ZCTA5/tl_2013_us_zcta5 French polygons - http://www.data.gouv.fr/dataset/fond-de-carte-des-codes-postaux -All countries points [GeoNames](www.geonames.org) - http://download.geonames.org/export/zip/allCountries.zip - ### Preparation details +The names of the imported files are: + +- doc for Australia table +- gfsa000a11a_e for Canada table +- tl_2013_us_zcta510 for USA table +- codes_postaux for France table + # Postal code geocoder (points) -Download the allCountries.zip file from [GeoNames](www.geonames.org). Import and rename the table as tmp_zipcode_points. You can follow the manual process explained below instead. +### Function - This dataset includes data for the following countries: - - ```` - CH, ES, GU, ZA, MX, SJ, NL, RU, AX, TH, AR, MY, RE, LK, GB, IS, GL, JE, DK, IN, - SI, GP, MQ, BR, SM, BG, NZ, MP, CZ, DO, MD, PK, TR, VI, BD, GG, LT, PM, MC, US, - IT, LU, SK, LI, PR, IM, NO, PT, PL, FI, JP, CA, DE, HU, PH, SE, VA, YT, MK, FR, - MH, RO, FO, GF, AD, HR, DZ, GT, AU, AS, BE, AT - ```` +By following the next steps a table is populated with zipcodes of different countries (identified by iso3) related with their spatial location in terms of points. - The columns that are loaded are the following ones: +This dataset includes data for the following countries: - - field_1: corresponding to ISO2 - - field_10: corresponds to latitude - - field_11: corresponds to longitude - - field_2: corresponds to ZIP code +```` +CH, ES, GU, ZA, MX, SJ, NL, RU, AX, TH, AR, MY, RE, LK, GB, IS, GL, JE, DK, IN, +SI, GP, MQ, BR, SM, BG, NZ, MP, CZ, DO, MD, PK, TR, VI, BD, GG, LT, PM, MC, US, +IT, LU, SK, LI, PR, IM, NO, PT, PL, FI, JP, CA, DE, HU, PH, SE, VA, YT, MK, FR, +MH, RO, FO, GF, AD, HR, DZ, GT, AU, AS, BE, AT +```` -1. Georeference the table using field11 as longitude and field10 as latitude in order to construct the_geom. +### Creation steps -2. Add column iso3 (text) and run sql/build_zipcode_points_table.sql. +1. Download the allCountries.zip file from [GeoNames](www.geonames.org). Import and rename the table as tmp_zipcode_points. You can follow the manual process explained below instead. + + +The columns that are loaded are the following ones: +field_1: corresponding to ISO2 +field_10: corresponds to latitude +field_11: corresponds to longitude +field_2: corresponds to ZIP code + +2. Georeference the table using field11 as longitude and field10 as latitude in order to construct the_geom. + +3. Add column iso3 (text) and run sql/build_zipcode_points_table.sql. **Alternative manual process** -1. Open the allCountries.txt file with Excel an add a new row on top. +Open the allCountries.txt file with Excel an add a new row on top. Delete columns C-I and L. -2. Delete columns C-I and L. +In the first row, add the following columns: iso2, zipcode, lat, long. -3. In the first row, add the following columns: iso2, zipcode, lat, long. +Import the file ignoring step 2. -4. Import the file ignoring step 1. +### Data Sources + +All countries points [GeoNames](www.geonames.org) - http://download.geonames.org/export/zip/allCountries.zip + +### Preparation details _The big size of the dataset may cause interruptions in the processing of the coordinates after uploading the file, manipulating the file before importing is a faster workaround._ From 24d53e5f6fa54d06c275cfe0943f6c0821d1cb55 Mon Sep 17 00:00:00 2001 From: andrewxhill Date: Wed, 27 Aug 2014 12:46:30 -0400 Subject: [PATCH 8/8] created test_ version of new admin1 geocoder function --- geocoder/admin1/sql/geocoder.sql | 118 +++++++++++++++++-------------- geocoder/setup/indexes.sql | 6 +- 2 files changed, 68 insertions(+), 56 deletions(-) diff --git a/geocoder/admin1/sql/geocoder.sql b/geocoder/admin1/sql/geocoder.sql index 8d0d012..cc19133 100644 --- a/geocoder/admin1/sql/geocoder.sql +++ b/geocoder/admin1/sql/geocoder.sql @@ -2,37 +2,73 @@ --- SELECT (geocode_admin1_polygons(Array['az', 'Texas'], 'Ecuador')).* + --- Function CREATE OR REPLACE FUNCTION test_geocode_admin1_polygons(name text[], inputcountry text) - RETURNS SETOF geocode_admin_v1 AS $$ + RETURNS SETOF geocode_admin_country_v1 AS $$ DECLARE - ret geocode_admin_v1%rowtype; + ret geocode_admin_country_v1%rowtype; + adm0 TEXT; + adm0_check BOOLEAN := TRUE; 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) - SELECT - q, geom, CASE WHEN geom IS NULL THEN FALSE ELSE TRUE END AS success - FROM ( - SELECT - q, ( - SELECT the_geom - FROM global_province_polygons - 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 favor higher in the geocoder :) - ORDER BY frequency DESC LIMIT 1 - ) geom - FROM p) n - LOOP - RETURN NEXT ret; - END LOOP; + IF inputcountry IS NULL THEN + adm0_check = FALSE; + END IF; + IF trim(inputcountry)='' THEN + adm0_check = FALSE; + END IF; + + IF adm0_check IS TRUE THEN + SELECT INTO adm0 adm0_a3 FROM admin0_synonyms WHERE name_ = lower(regexp_replace(inputcountry, '[^a-zA-Z\u00C0-\u00ff]+', '', 'g'))::text LIMIT 1; + + FOR ret IN + SELECT + q, inputcountry, geom, CASE WHEN geom IS NULL THEN FALSE ELSE TRUE END AS success + FROM ( + SELECT + q, ( + SELECT the_geom FROM qs_adm1 WHERE global_id = ( + SELECT global_id + FROM admin1_synonyms + WHERE name_ = lower(regexp_replace(d.q, '[^a-zA-Z\u00C0-\u00ff]+', '', 'g'))::text + AND adm0_a3 = adm0 + LIMIT 1 + ) + ) geom + FROM (SELECT unnest(name) q) d + ) v + LOOP + RETURN NEXT ret; + END LOOP; + + --Handle cases where country couldn't be found + ELSE + FOR ret IN + SELECT + q, inputcountry, geom, CASE WHEN geom IS NULL THEN FALSE ELSE TRUE END AS success + FROM ( + SELECT + q, ( + SELECT the_geom FROM qs_adm1 WHERE global_id = ( + SELECT global_id + FROM admin1_synonyms + WHERE name_ = lower(regexp_replace(d.q, '[^a-zA-Z\u00C0-\u00ff]+', '', 'g'))::text + LIMIT 1 + ) + ) geom + FROM (SELECT unnest(name) q) d + ) v + LOOP + RETURN NEXT ret; + END LOOP; + END IF; RETURN; END -$$ LANGUAGE 'plpgsql' SECURITY DEFINER; -Text array, country array +$$ LANGUAGE 'plpgsql'; + + +--Text array, country array --- Usage @@ -44,40 +80,12 @@ CREATE OR REPLACE FUNCTION test_geocode_admin1_polygons(names text[], country te RETURNS SETOF geocode_admin_country_v1 AS $$ DECLARE ret geocode_admin_country_v1%rowtype; - nans TEXT[]; BEGIN - - SELECT array_agg(p) INTO nans FROM (SELECT unnest(names) p, unnest(country) c) g WHERE c IS NULL; - - IF 0 < array_length(nans, 1) THEN - SELECT array_agg(p), array_agg(c) INTO names, country FROM (SELECT unnest(names) p, unnest(country) c) g WHERE c IS NOT NULL; - FOR ret IN SELECT g.q, NULL as c, g.geom, g.success FROM (SELECT (geocode_admin1_polygons(nans)).*) g LOOP - RETURN NEXT ret; - END LOOP; - 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) - SELECT - q, c, geom, CASE WHEN geom IS NULL THEN FALSE ELSE TRUE END AS success - FROM ( - SELECT - q, c, ( - SELECT the_geom - FROM global_province_polygons - 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 favor higher in the geocoder :) - ORDER BY frequency DESC LIMIT 1 - ) geom - FROM p) n - LOOP + FOR ret IN SELECT (test_geocode_admin1_polygons(array_agg(n), c)).* FROM (SELECT unnest(names) n, unnest(country) c) a GROUP BY c LOOP RETURN NEXT ret; END LOOP; RETURN; END -$$ LANGUAGE 'plpgsql' SECURITY DEFINER; \ No newline at end of file +$$ LANGUAGE 'plpgsql'; + diff --git a/geocoder/setup/indexes.sql b/geocoder/setup/indexes.sql index 4def421..9b2bbb0 100644 --- a/geocoder/setup/indexes.sql +++ b/geocoder/setup/indexes.sql @@ -4,8 +4,12 @@ CREATE INDEX idx_admin0_synonyms_name_ ON admin0_synonyms (name_); CREATE INDEX idx_admin0_synonyms_rank ON admin0_synonyms (rank); -- CREATE INDEX idx_admin0_synonyms_name_rank ON admin0_synonyms (name_, rank); +-- Index on admin1 id +CREATE UNIQUE INDEX idx_qs_adm1_global_id ON qs_adm1 (global_id) +CREATE INDEX idx_admin1_synonyms_name_adm0 ON admin1_synonyms (name_, adm0_a3) + -- create indexes on polygon table CREATE UNIQUE INDEX idx_ne_admin0_v3_adm0_a3 ON ne_admin0_v3 (adm0_a3); -- create indexes on postal code polygon table -CREATE UNIQUE INDEX idx_postal_code_polygons_a3_code ON postal_code_polygons (adm0_a3, postal_code) \ No newline at end of file +CREATE UNIQUE INDEX idx_postal_code_polygons_a3_code ON postal_code_polygons (adm0_a3, postal_code)