This is an automated email from the ASF dual-hosted git repository.

bchapuis pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-baremaps.git


The following commit(s) were added to refs/heads/main by this push:
     new d6c37f8e Improve label ordering in basemap (#589)
d6c37f8e is described below

commit d6c37f8e2bd3a37b0b0c87ba57a8a2f328a00b5e
Author: Bertil Chapuis <[email protected]>
AuthorDate: Tue Feb 28 00:53:03 2023 +0100

    Improve label ordering in basemap (#589)
    
    * Improve the ordering of the labels
    
    * Prioritize capitals over cities
    
    * Use the population to prioritize city names
---
 basemap/layers/point/label.js | 21 ++++++++++++++++++---
 basemap/utils/utils.js        | 20 ++++++++++++++++++--
 2 files changed, 36 insertions(+), 5 deletions(-)

diff --git a/basemap/layers/point/label.js b/basemap/layers/point/label.js
index 0e7fa1fe..2a68feb6 100644
--- a/basemap/layers/point/label.js
+++ b/basemap/layers/point/label.js
@@ -1,10 +1,25 @@
-import {asLayerObject, withSortKeys} from "../../utils/utils.js";
+import {asLayerObject, withSymbolSortKeys} from "../../utils/utils.js";
 
 let directives = [
     {
-        filter: ['==', ['get', 'place'], 'city'],
+        filter: [
+            'all',
+            ['==', ['get', 'capital'], 'yes'],
+            ['==', ['get', 'place'], 'city']
+        ],
         'text-size': 16,
         'text-color': 'rgb(25, 25, 25)',
+        'symbol-sort-key': ["-", ["to-number", ['get', 'population'], 0]],
+    },
+    {
+        filter: [
+            'all',
+            ['!=', ['get', 'capital'], 'yes'],
+            ['==', ['get', 'place'], 'city']
+        ],
+        'text-size': 16,
+        'text-color': 'rgb(25, 25, 25)',
+        'symbol-sort-key': ["-", ["to-number", ['get', 'population'], 0]],
     },
     {
         filter: ['==', ['get', 'place'], 'town'],
@@ -41,7 +56,7 @@ let directives = [
 
 ];
 
-export default asLayerObject(withSortKeys(directives), {
+export default asLayerObject(withSymbolSortKeys(directives), {
     id: 'label',
     type: 'symbol',
     source: 'baremaps',
diff --git a/basemap/utils/utils.js b/basemap/utils/utils.js
index c2e128c1..32d2b6d7 100644
--- a/basemap/utils/utils.js
+++ b/basemap/utils/utils.js
@@ -8,17 +8,28 @@ export function withSortKeys(directives) {
 export function withFillSortKey(directive, index, array) {
     return directive['fill-color'] ?{
         ...directive,
-        'fill-sort-key': array.length - index - 1,
+        'fill-sort-key': array.length - index,
     } : directive;
 }
 
 export function withLineSortKey(directive, index, array) {
     return directive['line-width'] ? {
         ...directive,
-        'line-sort-key': array.length - index - 1,
+        'line-sort-key': array.length - index,
     } : directive;
 }
 
+export function withSymbolSortKeys(directives) {
+    return directives.map(withSymbolSortKey);
+}
+
+export function withSymbolSortKey(directive, index) {
+    return directive['symbol-sort-key'] ? directive : {
+        ...directive,
+        'symbol-sort-key': index,
+    };
+}
+
 export function asLayerObject(directives = [], baseLayer = {}) {
     return {
         ...baseLayer,
@@ -38,6 +49,7 @@ export function asLayoutProperty(directives = [], baseLayout 
= {}) {
             ...iconImage(directives),
             ...lineSortKey(directives),
             ...fillSortKey(directives),
+            ...symbolSortKey(directives),
         },
         baseLayout,
     )
@@ -142,6 +154,10 @@ function fillSortKey(directives) {
     return mergeDirectives(directives, 'fill-sort-key', 0)
 }
 
+function symbolSortKey(directives) {
+    return mergeDirectives(directives, 'symbol-sort-key', 0)
+}
+
 function mergeDirectives(directives, property, value) {
     let cases = directives.flatMap((rule) => {
         if (rule[property]) {

Reply via email to