Image copyright AP Image caption Police were called after reports of a shooting at a petrol station

President Obama has called for restraint after three police officers were shot dead in the city of Baton Rouge in Louisiana.

The gunman, Gavin Long, an African-American who had served for five years in the Marines, was also killed.

He had posted videos on the internet complaining about police treatment of African-Americans.

Tensions in Baton Rouge have been high since a black man, Alton Sterling, was shot dead by police two weeks ago.

That death - and a second police shooting in Minnesota - sparked protests across the United States and triggered a revenge attack by a black army veteran who shot dead five officers in the city of Dallas.

In one video, posted on YouTube, Long said that should "anything happen" to him, he was "not affiliated" with any group.

"I'm affiliated with the spirit of justice, nothing more nothing less. I thought my own thoughts, I made my own decisions," he said.

Media playback is unsupported on your device Media caption Barack Obama: 'Attacks on police are an attack on all of us'

In a live broadcast from the White House, President Obama called upon all Americans to unite and refrain from divisive language.

"Regardless of motive, the death of these three brave officers underscores the danger that police across the country confront every single day, and we as a nation have to be loud and clear that nothing justifies violence against law enforcement," he said.

"Everyone right now focus on words and actions that can unite this country rather than divide it further," he added, as the US begins two weeks of political conventions with Republicans meeting in Cleveland later on Monday.

"We need to temper our words and open our hearts... all of us," said the president.

A vigil was attended by police officers and members of the public on Sunday evening at Saint John the Baptist Church in Zachary, just north of Baton Rouge.

Image copyright Reuters Image caption Police and members of the public attended a vigil on Sunday evening

Exchange of fire

The incident began on Sunday morning with shots being fired at a petrol station on Airline Highway in Baton Rouge.

Police received reports of a man with an assault rifle.

Witness Brady Vancel told WAFB TV he saw what may have been gang members shooting at each other before police arrived.

Media playback is unsupported on your device Media caption An ABC reporter describes the scene near where the shootings took place

Another witness said she saw a gunman wearing a black mask and military-style clothing.

Shots were exchanged over a period of more than 15 minutes, leaving three police officers and the suspect dead, with three other officers wounded, one in a critical condition.

The dead officers were named as Montrell Jackson, 32, and Matthew Gerald, 41, of the Baton Rouge police department, and Sheriff's Deputy Brad Garafola, 45. All three men had families.

The suspect was named as 29-year-old Gavin Long, of Kansas City, Missouri, a former Marine.

He received an honourable discharge, and won several medals while in the military, including one for good conduct.

Media playback is unsupported on your device Media caption Louisiana Governor John Bel Edwards: "The violence and hatred has to stop"

Louisiana Governor John Bel Edwards told a news conference it was an "absolutely unspeakable, heinous attack."

Although no other suspects have been identified, police said they were investigating whether the gunman had help from unknown others.

"We are not ready to say he acted alone," said state police spokesman Major Doug Cain.

Baton Rouge Mayor Kip Holden said he had spoken to White House officials who had offered assistance. He said it was "a defining moment" for community relations.

But he also told local media the "rhetoric from some people" after the death of Alton Sterling in Baton Rouge may be connected to the shootings, without elaborating who.

"Everything's been anti-police," he said.