LoginSignup
0
0

More than 1 year has passed since last update.

[mapbox]フィルタリングしたレイヤーをクラスター化した結果が0になるのを防ぐ

Last updated at Posted at 2022-12-19

具体的には下記のような症状を防ぐ方法です。
image.pngimage.png
動画だとわかりやすいのですが、レイヤーのフィルタリングでポイントが表示されていないにもかかわらず、クラスター化すると顕在化し'0'と表示されます。
https://youtu.be/XAmTpVcJfCQ

今回使用させてもらったのは国土数値情報の医療機関データです。
全国の医療機関一覧が位置情報とともに取得でき、かつ医療機関分類コードによって区分けされています。これを扱いやすい形にしてmapboxにて表示しました。

レイヤーのフィルタリング方法としては、まず

getFilterExpressionPart = function(_ckFlags){
    var tmpEpr = ['any'];
    if((_ckFlags&1)==1){ tmpEpr.push(['==',1,['get','code']]); } // P04_001
    if((_ckFlags&2)==2){ tmpEpr.push(['==',2,['get','code']]); }
    if((_ckFlags&4)==4){ tmpEpr.push(['==',3,['get','code']]); }
    return tmpEpr;
}

のような関数で表示する分類に応じたフィルターを作成し、

       map.addLayer({
            id: 'hospital-point',
            type: 'circle',
            source: 'hospitalSrc',
            filter: ['all',['!', ['has', 'point_count']],epr],
             :

で表示するようにしています。

クラスターレイヤーの設定には

        map.addSource('hospitalSrc', {
            'type': 'geojson',
            'data':hospitalAll,
            cluster: true,
            clusterMaxZoom: 10, // Max zoom to cluster points on
            clusterRadius: 20, // Radius of each cluster when clustering points (defaults to 50)
            clusterMinPoints:2,
            clusterProperties: {
                'sump': ['+', ['case', epr, 1,0 ]], //
            }
        });

のようにsumpを作成しこれを使用しました。

その結果が上記になるのですが、結論としては

        map.addSource('hospitalSrc', {
            'type': 'geojson',
            'data':hospitalAll,
            filter: epr,
            cluster: true,
            clusterMaxZoom: 10, // Max zoom to cluster points on
            clusterRadius: 20, // Radius of each cluster when clustering points (defaults to 50)
            clusterMinPoints:2,
            clusterProperties: {
                'sump': ['+', ['case', epr, 1,0 ]], //
            }
        });

のように、sourceにfilter: epr を追加することで回避することができました。
sourceにもfilter使えたんですね。。勉強になりました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0