Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix rank of archipelago countries #362

Merged
merged 1 commit into from Nov 20, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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