A United States judge has blocked the construction of a controversial oil pipeline from Canada to the US.

The judge in the state of Montana said the Trump administration had "discarded" facts when it approved the Keystone XL Pipeline in 2017.

It had been rejected two years earlier by the Obama administration, mainly on environmental grounds.

Speaking outside the White House on Friday, President Donald Trump said the ruling was a disgrace.

The state department has now been ordered to do a more thorough review of the effect on issues like the climate.

The administration can appeal against the decision. But groups that have been seeking to block the $8bn (£6bn) project are celebrating.

Doug Hayes, a lawyer for the Sierra Club environmental group, said the ruling made clear it was time to give up on the "Keystone XL pipe dream".

"The Trump administration tried to force this dirty pipeline project on the American people, but they can't ignore the threats it would pose to our clean water, our climate, and our communities," Mr Hayes added.

What is the Keystone XL Pipeline?
The privately financed pipeline is projected to stretch 1,179-miles (1,897km) from the oil sands of Canada's Alberta province, through Montana and South Dakota, to rejoin an existing pipeline to Texas.

But it has been the subject of protests for more than a decade, both from environmentalists and Native American groups, who say it will cut through their sovereign lands.

Why is the Keystone XL pipeline so disputed?
Mr Obama rejected the scheme in 2015 following a recommendation from the Environmental Protection Agency, citing concerns over it increasing US dependence on fossil fuel.

However, President Donald Trump reversed the decision shortly after taking office, saying it would bring thousands of jobs.

Construction on the US section was due to begin next year.

What did the ruling say?
Judge Brian Morris, of the US District Court for the District of Montana, said construction could not go ahead until a more thorough review of the impact on the climate, cultural resources and wildlife was conducted.

He also accused the state department of having "discarded prior factual findings related to climate change to support its course reversal".

"An agency cannot simply disregard contrary or inconvenient factual determinations that it made in the past," Judge Morris said in his ruling.

He said the decision also fell short in other areas, including the impact on Native American lands, and did not take into proper consideration issues like oil spills and low prices.

How has Canada responded?
A spokeswoman for Canada's Natural Resources Minister Amarjeet Sohi said on Friday: "Our government has always supported the Keystone XL project, and we are disappointed by this decision."

She added that the project is important for jobs in Canada and "for a successful energy export market".

The Keystone XL project has received all necessary approvals in Canada.

A spokeswoman for the Canadian Association of Petroleum Producers told CBC News that the ruling shows the need for Canadian energy producers to look for other potential markets.

"This decision further reinforces the need for Canada to export its oil and natural gas to new global markets - ensuring fair market value for our natural resources, helping to meet growing global demand and expanding our customer base beyond the US," Tonya Zelinsky said.