Integrate country and state into place

This commit is contained in:
lukasmartinelli
2016-10-24 14:44:14 +02:00
parent bdea298990
commit 24d7987c34
7 changed files with 18 additions and 28 deletions

52
layers/place/country.sql Normal file
View File

@@ -0,0 +1,52 @@
CREATE TABLE IF NOT EXISTS country_label AS (
SELECT topoint(geom) AS geometry,
name,
adm0_a3, abbrev, postal,
scalerank, labelrank,
CASE WHEN tiny < 0 THEN 0 ELSE 1 END AS is_tiny
FROM ne_10m_admin_0_countries
WHERE scalerank <= 1
);
CREATE INDEX IF NOT EXISTS country_label_geometry_idx ON country_label USING gist(geometry);
CREATE OR REPLACE VIEW country_z0 AS (
SELECT * FROM country_label WHERE scalerank = 0 AND is_tiny = 0 AND labelrank <= 2
);
CREATE OR REPLACE VIEW country_z1 AS (
SELECT * FROM country_label WHERE scalerank = 0 AND is_tiny = 0 AND labelrank <= 3
);
CREATE OR REPLACE VIEW country_z2 AS (
SELECT * FROM country_label WHERE scalerank = 0 AND is_tiny = 0 AND labelrank <= 4
);
CREATE OR REPLACE VIEW country_z3 AS (
SELECT * FROM country_label WHERE scalerank = 0 AND is_tiny = 0
);
CREATE OR REPLACE VIEW country_z5 AS (
SELECT * FROM country_label WHERE scalerank <= 1
);
CREATE OR REPLACE FUNCTION layer_country(bbox geometry, zoom_level int)
RETURNS TABLE(geometry geometry, name text, abbrev text, postal text, scalerank int, labelrank int) AS $$
SELECT geometry, name, abbrev, postal, scalerank::int, labelrank::int FROM (
SELECT * FROM country_z0
WHERE zoom_level = 0
UNION ALL
SELECT * FROM country_z1
WHERE zoom_level = 1
UNION ALL
SELECT * FROM country_z2
WHERE zoom_level BETWEEN 2 AND 4
UNION ALL
SELECT * FROM country_z3
WHERE zoom_level BETWEEN 3 AND 4
UNION ALL
SELECT * FROM country_z5
WHERE zoom_level >= 5
) AS t
WHERE geometry && bbox
ORDER BY scalerank ASC, labelrank ASC, length(name) ASC;
$$ LANGUAGE SQL IMMUTABLE;

View File

@@ -76,6 +76,10 @@ CREATE OR REPLACE VIEW place_z13 AS (
CREATE OR REPLACE FUNCTION layer_place(bbox geometry, zoom_level int, pixel_width numeric)
RETURNS TABLE(geometry geometry, name text, place text, scalerank int) AS $$
SELECT geometry, name, 'country' AS place, scalerank FROM layer_country(bbox, zoom_level)
UNION ALL
SELECT geometry, name, 'state' AS place, scalerank FROM layer_state(bbox, zoom_level)
UNION ALL
SELECT geometry, name, place, scalerank FROM (
SELECT geometry, name, place, scalerank,
row_number() OVER (
@@ -85,6 +89,7 @@ RETURNS TABLE(geometry geometry, name text, place text, scalerank int) AS $$
length(name) DESC
) AS gridrank
FROM (
--Cities
SELECT * FROM place_z2
WHERE zoom_level = 2
UNION ALL

View File

@@ -4,8 +4,11 @@ layer:
[OSM Places](http://wiki.openstreetmap.org/wiki/Key:place)
buffer_size: 128
datasource:
geometry_field: geometry
query: (SELECT * FROM layer_place(!bbox!, z(!scale_denominator!), !pixel_width!)) AS t
schema:
- ./country.sql
- ./state.sql
- ./place.sql
datasources:
- type: imposm3

43
layers/place/state.sql Normal file
View File

@@ -0,0 +1,43 @@
CREATE OR REPLACE FUNCTION fix_win1252_shp_encoding(str TEXT) RETURNS TEXT
AS $$
BEGIN
RETURN convert_from(convert_to(str, 'WIN1252'), 'UTF-8');
EXCEPTION WHEN others THEN RETURN str;
END;
$$ LANGUAGE plpgsql IMMUTABLE;
CREATE TABLE IF NOT EXISTS state_label AS (
SELECT topoint(geom) AS geometry,
name_local, fix_win1252_shp_encoding(name) AS name_en,
abbrev, postal,
scalerank, labelrank,
shape_area, datarank, type
FROM ne_10m_admin_1_states_provinces_shp
WHERE type IN ('State', 'Avtonomnyy Okrug', 'Sheng', 'Estado')
AND scalerank <= 3 AND labelrank <= 2
);
CREATE INDEX IF NOT EXISTS state_label_geometry_idx ON state_label USING gist(geometry);
CREATE OR REPLACE VIEW state_z3 AS (
SELECT * FROM state_label
WHERE (scalerank <= 2 AND labelrank <= 1) OR type = 'Avtonomnyy Okrug'
);
CREATE OR REPLACE VIEW state_z4 AS (
SELECT * FROM state_label
);
CREATE OR REPLACE FUNCTION layer_state(bbox geometry, zoom_level int)
RETURNS TABLE(geometry geometry, name text, name_en text, abbrev text, postal text, scalerank int, labelrank int) AS $$
SELECT geometry,
COALESCE(name_local, name_en) AS name_local, name_en,
abbrev, postal, scalerank::int, labelrank::int FROM (
SELECT * FROM state_z3
WHERE zoom_level = 3
UNION ALL
SELECT * FROM state_z4
WHERE zoom_level >= 4
) AS t
WHERE geometry && bbox
ORDER BY scalerank ASC, labelrank ASC, shape_area DESC;
$$ LANGUAGE SQL IMMUTABLE;