When any of the improvements authorized by this chapter have been directed to be done by ordinance, as provided in this chapter, the legislative body of such city has the power to require any street or other railroad company to replace the rails that such company may have in such streets with other rails of a kind to be specified by the legislative body, when, in the judgment of the legislative body, the rails ordered to be removed are not suitable to be used with paving that is about to be put down by the body. Should the company refuse to comply with the requirements of the notice, the legislative body has the right, and it shall be its duty, to institute suitable legal proceedings against the company to compel and require the company to lay and replace the rails as are thus specified; and if successful in such legal proceedings, the city shall be entitled to recover from such company any and all costs, expenses, and losses incurred by it because of such refusal and failure of such company to comply with such order.