Section 101. A railroad corporation may alter the course of a public way to facilitate the crossing thereof by its railroad or to permit its railroad to pass at the side thereof without crossing, if, after notice to the city or town where the way is situated, and a hearing, the county commissioners decide that such alteration will not essentially injure the way, and make a decree prescribing the time and manner of such alteration. If it is necessary to take land for such alteration, the county commissioners shall take the same by eminent domain under chapter seventy-nine on behalf of the county, city or town having jurisdiction over the alteration of such way, and before entering upon, excavating or altering such way the corporation shall give to such county, city or town security satisfactory to the commissioners that it will indemnify such county, city or town for all damages and charges which it is obliged to pay by reason of such taking.