karaGR's memo

FOSS4G R python さんぽ

グループ化したレイヤの紙芝居プラグインを作りました

ども、karaGRです。

FOSS4G Advent Calendar 2017 13日目の記事です。
https://qiita.com/advent-calendar/2017/foss4g


これまでの”標準地域メッシュ”はネタ切れ&凝ったことをしている時間が取れず・・・
ということで、ほんのちょっとだけQGISを便利にできればと、
グループ化したレイヤの紙芝居プラグインを作りました。
https://github.com/karaGR/GroupLayerSlideShow

使い方

紙芝居(スライドショー)したいレイヤをグループ化します。

ここでは国土数値情報からDLした北海道のDID人口集中地区データを使っています。

"GroupLayerSlideShow"プラグインをインストールしたQGISで、
"プラグイン"メニュー→"レイヤスライドショー"の
"レイヤスライドショー"を呼び出すと次の窓が開きます

紙芝居するグループを選択し、”SET”をクリック。

表示される窓のボタンをクリックすることで、表示レイヤを切り替えられます。

"Top":一番上のレイヤを表示
"Up":1つ上のレイヤを表示
”Down":1つ下のレイヤを表示
”Bottom":一番下のレイヤを表示

処理について

レイヤグループの排他的選択機能を利用して、単純に表示を入れ替えているだけです・・・

QgsLegendInterface.groups()でロードされているグループのリストを取得しておいて、

def run(self):   
    myLegendInterface = self.iface.legendInterface()              
    myGroupList = myLegendInterface.groups()

QCombobox.addItems()でコンボボックスに設定。

self.CoB_group.addItems(groupList)

グループとレイヤの関連性リストをQgsLegendInterface.groupLayerRelationship()で取得。
そこから内包表記を使ってレイヤ名のリストを取得。
選択したグループをQgsLayerTreeGroup.setIsMutuallyExclusive(True)で排他的に設定し、

def setButtonAction(self):
      groupName = self.CoB_group.currentText()
      layerNameList = [mylist[1] for mylist in QgsLegendInterface.groupLayerRelationship() if mylist[0] == groupName][0]
        
      if len(layerNameList) > 1:
          self.myLayerTreeView.setCurrentLayer(QgsMapLayerRegistry.instance().mapLayer(layerNameList[0]))
          self.myLayerTreeView.currentGroupNode().setIsMutuallyExclusive(True)

QgsLegendInterface.setLayerVisible(QgsMapLayerRegistry.mapLayer(レイヤ名))で、表示することにより、排他的に切り替えています。

def moveTop(self):
    self.myLegendInterface.setLayerVisible(QgsMapLayerRegistry.instance().mapLayer(self.layerNameList[0]),True)
    self.layerId = 0
        
def moveBottom(self):
    self.myLegendInterface.setLayerVisible(QgsMapLayerRegistry.instance().mapLayer(self.layerNameList[-1]),True)
    self.layerId = len(self.layerNameList)-1
        
def moveDown(self):
    self.resetLayerId()
    if self.layerId < len(self.layerNameList)-1:
        self.layerId += 1
        self.myLegendInterface.setLayerVisible(QgsMapLayerRegistry.instance().mapLayer(self.layerNameList[self.layerId]),True)
            
def moveUp(self):
    self.resetLayerId()
    if self.layerId > 0:
        self.layerId -= 1
        self.myLegendInterface.setLayerVisible(QgsMapLayerRegistry.instance().mapLayer(self.layerNameList[self.layerId]),True)             

おわりに

UbuntuのQGIS2.14.21環境以外ではテストしてません。
他の環境でのテスト&修正は追々やります。スミマセン。
→昨年も同じことを書いた・・・成長してない

もうじきQGIS3もでるようですが、
とりあえずいじってみてくださいね〜