function layerTreeDepth(layer) {
  var count = 0
  var parent = layer.parentGroup()

  while(parent) {
    count++
    parent = parent.parentGroup()
  }

  return count
}
Return the 'depth' of a layer, that is, the number of ancestors it has