Skip to content

Commit

Permalink
Fix rank of archipelago countries
Browse files Browse the repository at this point in the history
Fix #342
  • Loading branch information
jirik committed Nov 20, 2017
1 parent 6258130 commit 30539e7
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion layers/place/update_country_point.sql
Expand Up @@ -9,6 +9,9 @@ ALTER TABLE osm_country_point DROP CONSTRAINT IF EXISTS osm_country_point_rank_c
CREATE OR REPLACE FUNCTION update_osm_country_point() RETURNS VOID AS $$
BEGIN

UPDATE osm_country_point AS osm
SET "rank" = 7;

WITH important_country_point AS (
SELECT osm.geometry, osm.osm_id, osm.name, COALESCE(NULLIF(osm.name_en, ''), ne.name) AS name_en, ne.scalerank, ne.labelrank
FROM ne_10m_admin_0_countries AS ne, osm_country_point AS osm
Expand All @@ -27,9 +30,36 @@ BEGIN
FROM important_country_point AS ne
WHERE osm.osm_id = ne.osm_id;

-- Repeat the step for archipelago countries like Philippines or Indonesia
-- whose label point is not within country's polygon
WITH important_country_point AS (
SELECT
osm.osm_id,
-- osm.name,
ne.scalerank,
ne.labelrank,
-- ST_Distance(osm.geometry, ne.geometry) AS distance,
ROW_NUMBER()
OVER (
PARTITION BY osm.osm_id
ORDER BY
ST_Distance(osm.geometry, ne.geometry)
) AS rk
FROM osm_country_point osm,
ne_10m_admin_0_countries AS ne
WHERE
NOT (osm."rank" BETWEEN 1 AND 6)
)
UPDATE osm_country_point AS osm
-- Normalize both scalerank and labelrank into a ranking system from 1 to 6
-- where the ranks are still distributed uniform enough across all countries
SET "rank" = LEAST(6, CEILING((ne.scalerank + ne.labelrank)/2.0))
FROM important_country_point AS ne
WHERE osm.osm_id = ne.osm_id AND ne.rk = 1;

UPDATE osm_country_point AS osm
SET "rank" = 6
WHERE "rank" IS NULL;
WHERE "rank" = 7;

-- TODO: This shouldn't be necessary? The rank function makes something wrong...
UPDATE osm_country_point AS osm
Expand Down

0 comments on commit 30539e7

Please sign in to comment.