D.C. police must begin tracking the race of every person officers stop in a vehicle or as a pedestrian within the next 28 days, a court ruled Thursday.

The order applies to stops whether or not they result in an arrest or search.

It has been three years since the D.C. Council mandated the collection of racial data along with some other details of stops and included the requirement in a much broader piece of crime legislation.

But the department has yet to comply with the law, sparking a lawsuit by several advocacy groups who say the data is needed to further shape the District’s approach to law enforcement.

On Thursday, Superior Court Judge John M. Campbell sided with those groups, saying that unless and until D.C. police come up with their own system of data collection they must use one created by the plaintiffs: a one-page form detailing each officer’s stop.

“The Court has already reviewed the defendants’ inability, over the past three years, to offer any substantial explanation for their non-action,” Campbell wrote in his opinion. “Frankly, the Court has delayed making a final decision in the hope that the defendants, having seen the writing on the wall, as it were” would find a way to comply with the law. “It appears, however, that this hope was not well-founded, and that judicial intervention is now both warranted and necessary.”

The D.C. attorney general’s office could try to delay the new policy by asking for an emergency stay from the D.C. Court of Appeals.

“We are taking a close look at the court’s ruling and considering all of our options,” a spokeswoman for the office said.

Scott Michelman, legal co-director for the American Civil Liberties Union of D.C., said it’s time for the city to stop fighting.

“The Metropolitan Police Department has obfuscated, dragged their feet, delayed and even misled the public over the course of three years of resistance to a data collection law passed back in 2016,” he said. “Ultimately the judge’s patience quite reasonably ran out, and D.C. community members’ patience has more than run out.”

Along with the ACLU, Black Lives Matter and the Stop Police Terror Project sued last year after filing a public records request for the data and getting nothing.

“This ruling is an important step forward for transparency and accountability in the District of Columbia,” said Eugene Puryear of Stop Police Terror Project DC. “As the court noted, this data is tremendously important and obtaining it is essential for constructing policies that combat racial inequities in the District.”

Police have released some data, including a spreadsheet listing the race and other information about every person arrested over a five-year period. The city says that shows a good-faith effort to meet the law’s requirements.

But to fully comply, city officials argue they need substantial upgrades to computers in the police department and the Motor Vehicle Administration, which logs in traffic tickets. The undertaking has been more complicated than first thought, they say.

Kevin Donahue, the deputy mayor for public safety and the judiciary, has testified to the council that the upgrades could exceed the $150,000 initially allocated. Donahue’s office issued a statement earlier this month saying the administration “has been transparent about the data system requirements that are needed.” The statement adds that D.C. police and the Department of Motor Vehicles “expect to complete the necessary IT system upgrades this summer.”

Police spokesman Dustin Sternbeck said that “we are perplexed that the court would issue an injunction at this time as they are aware that our IT systems would be in full compliance in July.”

Campbell called the explanation “opaque” and questioned why the city “had given no thought to a low-tech, short-term, temporary measure that could collect the required data, even in non-digital form, while the digital systems were upgraded.”

The dispute is over one part of an effort called the Neighborhood Engagement Achieves Results law, otherwise known as the NEAR Act, which passed the council unanimously in 2016. The legislation transformed the District’s approach to fighting crime by seeking to address the underlying causes of violence and confront those challenges using a health-based model.

It set up a new city agency called the Office of Neighborhood Safety and Engagement to oversee a variety of programs, including violence interrupters who try to quiet disputes before gunfire starts. The law also requires D.C. police to collect a wide range of data when officers stop and frisk people or conduct stops, including the person’s race, ethnicity and gender, and duration of the stop.

Council members and advocacy groups view the data as essential to understanding who police stop and why, and whether there is evidence of racial bias. They say those numbers can help form legislation and rules to guide the District’s policing strategies under its new holistic approach to restoring safety to neighborhoods.

Most provisions have been implemented. But the data collection portion remained a sticking point.

Earlier this year, the District made public raw data on several years’ worth of arrests that showed a disproportionate number of black people are arrested for minor violations that include smoking marijuana in public, driving without a license and gambling. The data showed, for example, that of more than 10,000 people arrested for driving without a license in the District, 80 percent were black.

But while police track the race of people arrested, they do not track that detail for other types of detentions or traffic stops that result in citations. The ACLU argued that absent the information, it was impossible to assess whether a disproportionate number of black people are stopped and searched or pulled over for possible traffic infractions.

Earlier this month, the District responded to an ACLU request for six months of racial data on traffic stops by saying the information was available only by watching videos from body cameras worn by police officers. The District noted there were 31,521 videos from stops over the period requested.

On June 21, the ACLU said it received an invoice from the District indicating it would cost $310,362 for a small portion of the body camera footage being sought.

In a court filing, the ACLU estimated that it would cost more than $9 million to fulfill its complete request, and police later said they would work to find a less expensive solution. Police argue the costs are mostly due to having to redact faces and other personal information from videos, a tedious process that requires alterations in each frame.

In a court filing, the ACLU called the invoice and request that they watch video to discern a person’s race “a joke” and said “it is designed to thwart the will of the Council and obstruct D.C. residents’ access to information about how their police are carrying out their important responsibilities.”

Campbell agreed and said the video collected by body cameras is not a “record” in the way the NEAR Act intended.

“Having to pay huge sums to receive the videos on all stops, and then to spend countless hours viewing the videos and recording the pertinent information, puts this data, such as it is, effectively out of reach of the community and of organizations,” he wrote. “This undercuts the statute’s important purposes to promote transparency, police accountability, and community involvement.”

D.C. officials said their offer of body camera video was an interim solution as officials try to develop a permanent fix. The District later retracted the invoice it had sent the ACLU, saying in a court filing it was instead “exploring whether recently updated technology can be used to make the redactions in-house rather than having a vendor complete this task.” The filing says the new process “will be more economical and might be more efficient.”

Charles Allen (D-Ward 6), who chairs the public safety and judiciary committee, said that after three years, it is fair to ask District leaders whether the delay in collecting data “is an issue of capacity or an issue of will.”

“I don’t know what they’re afraid of,” Allen said. “With better data, the police can do a better job building our communities and public trust, and doing a better job speaking out if bias does exist . . . The NEAR Act holds a mirror up and makes sure we’re making the right decisions about our city and public safety.”