Thousands of police from around the country spilled out of a church into the streets surrounding a slain New York City officer’s funeral on Friday, calling for respect and understanding at a time when law enforcement is being deeply scrutinized.

Busloads of officers arrived from as far as California, Louisiana and Chicago for Officer Brian Moore’s funeral on Long Island. As a hearse carried his coffin to the cemetery, they lined up 10 and 20 deep to salute him.

“Brian’s death comes at a time of great challenge” for officers nationwide, who are “increasingly bearing the brunt of loud criticism,” Police Commissioner William Bratton said.

Only five months earlier, the New York Police Department mourned two other officers who were killed in an ambush by a gunman who said he wanted revenge for police killings of civilians.

“What is lost in the shouting and the rhetoric is the context of what we do,” said Bratton, his voice cracking as he posthumously promoted the 25-year-old Moore to the rank of detective. “What is lost is the way we already work together, the ways we get it right. … What is lost is that public safety is a shared responsibility.”

Moore died Monday, two days after he was shot in Queens by a suspect who he and his partner had stopped on suspicion of carrying a handgun.

Moore’s death came amid a national debate about policing, race and deadly force following the recent killings of unarmed black men by officers in New York, Ferguson, Mo., North Charleston, S.C., and elsewhere.

Amid the outcry, New York City officers Rafael Ramos and Wenjian Liu, were killed in their patrol car in December. The man who gunned them down had boasted online that he would kill police in revenge for the chokehold death of Eric Garner on Staten Island and the shooting of Michael Brown in Ferguson.

Liu’s relatives were among the mourners at Moore’s funeral. Detective Omar Daza-Quiroz, 33, traveled from Oakland, Calif., to stand with his colleagues — and stand for law enforcement.

“Right now, it’s a tough time in law enforcement,” he said. “Sometimes people forget we are human and that we have lives.”

Moore was the son, nephew and cousin of NYPD officers. He was so determined to follow them that he took the police entrance exam at 17 and “devoted his whole being to the job,” Mayor Bill de Blasio said.

At Ramos’ and Liu’s funerals, hundreds of officers turned their backs to the mayor in a searing sign of disrespect. Police union leaders had said de Blasio fostered anti-NYPD sentiment by allowing protesters to march through the city’s streets after a grand jury decided not to indict an officer in Garner’s death.

There has been no similar sign of tensions in the wake of Moore’s death, and no backs were turned on de Blasio Friday.

De Blasio’s “words are measured and careful to know that there’s support, and that’s important,” said Patrolmen’s Benevolent Association President Patrick Lynch, who once said de Blasio had “blood on his hands” after Liu’s and Ramos’ deaths.