Section 104. A public way may be laid out across a railroad previously constructed, if the county commissioners, or the department of highways in the case of a state highway, adjudge that public necessity and convenience so require; and in such case, after notice to the railroad corporation and a hearing of all parties interested, said commissioners or division, or a city or town by authority of the county commissioners granted upon petition of its board of aldermen or selectmen, may thus lay out a way across a railroad, in such manner as not to injure or obstruct the railroad, and otherwise in conformity with sections ninety-seven and ninety-eight. A public way shall not be permitted to cross at a level with the railroad unless it is determined by the department of highways, in the case of a state highway, or the county commissioners, in the case of any other public way, after notice to all persons interested and a hearing, that public necessity so requires, and the department of telecommunications and energy consents thereto in writing. If it is necessary in the laying out, construction or alteration of a state highway across a railroad to alter or relocate the railroad, the department of highways may take by eminent domain under chapter seventy-nine, on behalf of the railroad corporation, land or rights in land necessary for such alteration or relocation with the written approval of the department of telecommunications and energy. Charges and expenses for such alteration or relocation shall be paid by the department of highways. A copy of the proceedings of the department of highways in laying out a state highway under this section, including a copy of the plan of so much of said way as lies within the location of the railroad, shall be filed in the office of the county commissioners of the county where such way is located.