From f3e2d7f1637629f9c4c2224feb0da2735c022a72 Mon Sep 17 00:00:00 2001 From: lukasmartinelli Date: Fri, 28 Oct 2016 18:01:31 +0200 Subject: [PATCH] Add gridrank attribute --- layers/poi/class.sql | 30 ++++++++++++++++++++++++++++++ layers/poi/layer.sql | 13 +++++++++---- layers/poi/poi.yaml | 2 +- 3 files changed, 40 insertions(+), 5 deletions(-) diff --git a/layers/poi/class.sql b/layers/poi/class.sql index 7a915dab..d4f0795e 100644 --- a/layers/poi/class.sql +++ b/layers/poi/class.sql @@ -1,3 +1,33 @@ +CREATE OR REPLACE FUNCTION poi_class_rank(class TEXT) +RETURNS INT AS $$ + SELECT CASE class + WHEN 'hospital' THEN 20 + WHEN 'park' THEN 25 + WHEN 'cemetery' THEN 30 + WHEN 'bus' THEN 50 + WHEN 'attraction' THEN 70 + WHEN 'harbor' THEN 75 + WHEN 'college' THEN 80 + WHEN 'school' THEN 85 + WHEN 'stadium' THEN 90 + WHEN 'zoo' THEN 95 + WHEN 'town_hall' THEN 100 + WHEN 'campsite' THEN 110 + WHEN 'cemetery' THEN 115 + WHEN 'park' THEN 120 + WHEN 'library' THEN 130 + WHEN 'police' THEN 135 + WHEN 'post' THEN 140 + WHEN 'golf' THEN 150 + WHEN 'shop' THEN 400 + WHEN 'grocery' THEN 500 + WHEN 'fast_food' THEN 600 + WHEN 'clothing_store' THEN 700 + WHEN 'bar' THEN 800 + ELSE 1000 + END; +$$ LANGUAGE SQL IMMUTABLE; + CREATE OR REPLACE FUNCTION poi_class(subclass TEXT) RETURNS TEXT AS $$ SELECT CASE diff --git a/layers/poi/layer.sql b/layers/poi/layer.sql index 33ac661f..421e7761 100644 --- a/layers/poi/layer.sql +++ b/layers/poi/layer.sql @@ -1,8 +1,13 @@ -CREATE OR REPLACE FUNCTION layer_poi(bbox geometry, zoom_level integer) -RETURNS TABLE(osm_id bigint, geometry geometry, name text, name_en text, class text, subclass text) AS $$ - SELECT id, geometry, name, NULLIF(name_en, ''), poi_class(subclass) AS class, subclass +CREATE OR REPLACE FUNCTION layer_poi(bbox geometry, zoom_level integer, pixel_width numeric) +RETURNS TABLE(osm_id bigint, geometry geometry, name text, name_en text, class text, subclass text, gridrank bigint) AS $$ + SELECT id, geometry, name, NULLIF(name_en, ''), poi_class(subclass) AS class, subclass, + row_number() OVER ( + PARTITION BY LabelGrid(geometry, 100 * pixel_width) + ORDER BY poi_class_rank(poi_class(subclass)) ASC, length(name) DESC + ) AS gridrank FROM osm_poi_point WHERE geometry && bbox + AND zoom_level >= 14 AND name <> '' - AND (zoom_level >= 14); + ORDER BY gridrank; $$ LANGUAGE SQL IMMUTABLE; diff --git a/layers/poi/poi.yaml b/layers/poi/poi.yaml index fa996ea1..aab9e50b 100644 --- a/layers/poi/poi.yaml +++ b/layers/poi/poi.yaml @@ -9,7 +9,7 @@ layer: datasource: geometry_field: geometry srid: 900913 - query: (SELECT geometry, name, name_en, class, subclass FROM layer_poi(!bbox!, z(!scale_denominator!))) AS t + query: (SELECT geometry, name, name_en, class, subclass, gridrank FROM layer_poi(!bbox!, z(!scale_denominator!), !pixel_width!)) AS t schema: - ./class.sql - ./layer.sql