Officers Rafael Ramos, 40, and Wenjian Liu, 32, both of...

Officers Rafael Ramos, 40, and Wenjian Liu, 32, both of Brooklyn, were shot and killed on Dec. 20, 2014, as they sat in their patrol car in the Bedford-Stuyvesant section of Brooklyn. The gunman, Ismaaiyl Brinsley, fatally shot himself when other officers approached him in a nearby subway station. Credit: NYPD

Two NYPD officers were "assassinated" while sitting in their patrol car in Brooklyn Saturday afternoon by a gunman who had vowed online hours earlier to kill cops.

The gunman traveled to New York to carry out his plot after shooting his ex-girlfriend near Baltimore. His online postings said he was avenging the controversial deaths of two black men at police hands in Ferguson, Missouri, and Staten Island.

The officers, Wenjian Liu, 32, and Rafael Ramos, 40, were shot execution-style at point-blank range in the ambush attack near the intersection of Myrtle and Tompkins avenues in Bedford-Stuyvesant at 2:47 p.m., police said.

The killings happened just as the NYPD received word from police in Baltimore County, Maryland, that the gunman, Ismaaiyl Brinsley, 28, had shot his former girlfriend and might be on his way to New York to harm police.

"Two of New York's finest were shot and killed with no warning, no provocation," NYPD Commissioner William Bratton said during a news conference last night at Woodhull Medical Center, where both officers were pronounced dead. "They were quite simply assassinated, targeted for their uniform and for the responsibility they embraced to keep the people of the city safe."

The officers were working a critical response detail, aimed at combating a crime surge at the Tompkins Houses, a public housing project, when Brinsley walked up to the passenger side of the car, "took a shooting stance," and opened fire, Bratton said.

Ramos was in the driver's seat and Liu in the passenger seat when they were hit, Bratton said.

"They never had the opportunity to draw their weapons," Bratton said. "They may never even have actually seen their . . . murderer."

After the shootings, Brinsley fled into the nearby G train subway station and fatally shot himself on the platform as pursuing officers closed in, police said.

The murder weapon, a silver-colored semiautomatic Taurus pistol, was recovered in the subway station, police said.

Bratton said investigators believe Brinsley lived in Atlanta and had traveled to suburban Baltimore, where he shot his ex-girlfriend Saturday morning.

The gunman posted a photo of a silver handgun on Instagram about three hours before the Brooklyn shooting, writing "I'm Putting Wings on Pigs Today" and using a #ShootThePolice hashtag, a police source said. In another post, he wrote: "They Take 1 Of Ours . . . Let's Take 2 of Theirs. This May Be My Final Post," the source said.

The posts also made reference to Eric Garner and Michael Brown, both of whom were killed during encounters with police this year.

Brinsley, whose criminal history included numerous arrests on charges ranging from robbery to carrying a concealed weapon, had also urged his Instagram followers to "burn the Flag" in protest of police violence a few days before killing the officers, the source said.

Detectives were investigating whether he might have belonged to a street gang, the source said.

The ambush shootings of the two officers turned the busy commercial stretch of Bedford-Stuyvesant into a scene of violence and chaos.

Courtney Felix, 23, said he was at a friend's apartment near the Tompkins Houses when he heard the gunman open fire. He looked out the window and saw the two officers struggling to get out of their car, clutching at their wounds before falling to the ground.

"They looked like they were really hurt, they were struggling to get out of their car," Felix said.

Both officers were assigned to the 84th Precinct in Downtown Brooklyn but were working in the 79th Precinct as part of the special detail.

Ramos, formerly a school safety officer who fulfilled his dream of becoming a cop two years ago, had celebrated a birthday Dec. 12, officials said.

He had a wife and 13-year-old son, New York City Mayor Bill de Blasio said at the news conference Saturday night, adding that he and other officials prayed over the bodies of both men at the hospital.

Liu married his wife two months ago, officials said.

The mayor called their killings "a particularly despicable act" that "tears at the foundation of our society."

"It is an attack on all of us," he continued. "It is in attack on all we hold dear . . . on the very concept of decency. Therefore, every New Yorker should feel they were attacked . . . the entire city was attacked, by this heinous individual."

Officials also urged New Yorkers to immediately report any threats made against police on social media.

Brinsley's violent spree began about 5:45 a.m. in Owings Mills, Maryland, when he shot and wounded his ex-girlfriend, who has not been identified. Baltimore County police later received information that he was posting anti-police messages on the woman's Instagram account, Bratton said, and at 2:45 p.m. sent a wanted flier to the NYPD and other agencies warning that Brinsley might be headed their way.

"Tragically, this was at the same time they [Liu and Ramos] were being murdered," Bratton said.

The killing of the officers followed more than two weeks of protests in response to a grand jury's decision not to indict NYPD Officer Daniel Pantaleo in the apparent chokehold death of Garner during an arrest on July 17.

The decision in the Garner case came just days after a grand jury in Missouri declined to indict former police Officer Darren Wilson in the Aug. 9 shooting death of Michael Brown.

The killing of two officers was at least the third unprovoked attack on NYPD cops in recent months and the first line-of-duty shooting deaths since 2011.

Mets land Manaea ... Latest on Harborside sale ... Take holiday road to see 'Vacation' lights Credit: Newsday

Town asks for investigation into animal reserve ... CEO murder suspect pleads not guilty ... New boat is ferry nice ... Take holiday road to see 'Vacation' lights

Mets land Manaea ... Latest on Harborside sale ... Take holiday road to see 'Vacation' lights Credit: Newsday

Town asks for investigation into animal reserve ... CEO murder suspect pleads not guilty ... New boat is ferry nice ... Take holiday road to see 'Vacation' lights

SUBSCRIBE

Unlimited Digital AccessOnly 25¢for 6 months

ACT NOWSALE ENDS SOON | CANCEL ANYTIME