The Dakota Access Pipeline, an oil route from North Dakota to Illinois that has inspired intense protests and legal battles, must shut down pending an environmental review and be emptied of oil by Aug. 5, a district court ruled Monday.

The decision, which could be subject to appeal, is a victory for the Standing Rock Sioux Tribe and other Native American and environmental groups who have fought the project for years, and a significant defeat for President Donald Trump, who has sought to keep the Dakota Access Pipeline alive.

"Today is a historic day for the Standing Rock Sioux Tribe and the many people who have supported us in the fight against the pipeline," Mike Faith, chair of the Standing Rock Sioux Tribe, said.

"This pipeline should have never been built here," he added. "We told them that from the beginning."

The ruling, by U.S. District Judge James Boasberg in the District of Columbia, is the latest twist in a long-running legal battle. It essentially vacates a federal permit that had allowed the pipeline to operate while the U.S. Army Corps of Engineers, which had granted the permits for the pipeline, conducted an extensive environmental impact review.

Energy Transfer Partners, the Texas company that owns the pipeline, said in a statement Monday that it would file a motion to stay the decision, and if that failed, appeal to a higher court.

"We will be immediately pursuing all available legal and administrative processes and are confident that once the law and full record are fully considered, Dakota Access Pipeline will not be shut down and that oil will continue to flow," it said.

In his opinion, Boasberg wrote that the court was "mindful of the disruption such a shutdown will cause" but that it had to consider the "potential harm each day the pipeline operates."

"This is shocking news," said Ron Ness, president of the North Dakota Petroleum Council, adding that the pipeline has operated reliably for years, and that the ruling would hurt the state's economy and encourage other, less safe means of oil transportation.

Energy Transfer said in its statement that if the pipeline were to shut down, state, local and tribal governments would lose billions of dollars in taxes and royalties.

"Farmers will suffer as crude transportation will move to rail, displacing corn, wheat and soy crops that would normally be moved to market," it said. "Ironically, the counties along these rail lines will face increased environmental risks due to the increased amount of crude oil traveling by rail."

If the ruling stands and the oil flow is halted, the pipeline could still resume operations after the Army Corps of Engineers environmental review is completed. The Army Corps did not immediately respond to a request for comment.

The Dakota Access Pipeline has been carrying oil for three years. In 2016, it was the subject of monthslong protests, and legal battles and political campaigns continued after the remnants of the protest camp were razed.

"It took four long years," said Jan Hasselman, a lawyer with the environmental group Earthjustice who represents the Standing Rock Sioux Tribe. "But today justice has been served at Standing Rock."

Members of Standing Rock Sioux Tribe, whose reservation is less than 1 mile from the pipeline, have long argued that a spill under the nearby Missouri River could pollute the water they rely on for fishing, drinking and religious ceremonies.

In 2016, the tribe sued in U.S. District Court in Washington to stop construction and won an early victory under the Obama administration when the Army Corps of Engineers announced it would seek alternative routes.

Four days after Trump took office in 2017, he signed an executive memorandum directing the corps "to review and approve in an expedited manner" the pipeline. The move prompted rallies outside the White House and Trump International Hotel.

Oil was flowing through the pipeline by June 2017.

In March, Boasberg, who was appointed by President Barack Obama, ordered the federal government to conduct a sweeping new environmental review of the Dakota Access Pipeline. The ruling found that the pipeline's "effects on the quality of the human environment are likely to be highly controversial," and that the federal government had not done enough to study the risks of a major spill, or whether the pipeline's leak detection system was adequate.

He also ordered both the tribe and the federal government to submit briefs on whether the pipeline should continue operating during the new environmental review. The Earthjustice lawyer, Hasselman, said at the time that the tribe would ask for the pipeline to be shut down until the review was completed. Monday's ruling granted that request.

"The shutdown will remain in place pending completion of a full environmental review, which normally takes several years, and the issuance of new permits," Earthjustice said in a statement Monday. "It may be up to a new administration to make final permitting decisions."

Boasberg's ruling is the second triumph in two days for pipeline opponents. On Sunday, Duke Energy and Dominion Energy, two of the nation's largest utility companies, announced they had canceled the Atlantic Coast Pipeline, which would have carried natural gas across the Appalachian Trail. The companies said that lawsuits, mainly from environmentalists, had increased costs to as much as $8 billion from about $4.5 billion to $5 billion.

Joel Reynolds, a senior attorney for the Natural Resources Defense Council, said that despite the litany of environmental rollbacks during the Trump administration, the past several years also have provided a lesson on the limits of presidential power.

"Our system is set up so that it's very unusual for the president to simply be able to snap his or her fingers and make something change," Reynolds said. "Trump is a perfect example of that. He's issued so many executive orders that sound really dramatic but don't really short-circuit the public process all that much."

Reynolds said that despite the president's desire to fast-track controversial energy projects such as pipelines, there are hurdles that even the White House cannot avoid.

"We have checks and balances. We have statutes that lay out legal procedures that have to be followed," he said. "It's one thing to talk about deregulation; it's quite another thing to do it.

"The Trump administration has learned the hard way that its deregulatory agenda is not something over which it has complete control," he added. "The courts have something to say about it."

He said the courts, as they displayed again Monday, "are a very powerful impediment."

The new court ruling is also a blow to Energy Transfer Partners, which has close ties with the Trump administration. Last month, the company's CEO, Kelcy Warren, held a fund-raiser for the president in his home, and former energy secretary Rick Perry rejoined the company's board just months after stepping down from the Cabinet.

The company, however, has been involved in other controversial projects, including the Rover natural gas pipeline that spilled drilling fluids during construction, contrary to an order by the Federal Energy Regulatory Commission.

Recently Trump has sought to speed development of pipelines by signing new executive orders, this time seeking to waive environmental permitting laws by citing the need to bolster the economy during the pandemic.

The Washington Post contributed to this report.