A man has been shot dead after lunging at an officer in a police station near Barcelona, Spain, in what police said was a terrorist act.

Police said the man entered the station in Cornella, in the northeastern region of Catalonia, shortly before 4am, seeking information.

He then leapt towards an officer, brandishing a large knife and shouting "Allahu Akbar - God is great". The attacker, identified only as living in Cornella, was shot dead.

"However you look at it, it was a knife attack against an officer (by a man who) shouted "Allah is great" and other things that were impossible to make out," said regional investigating police chief Rafel Comas.

"For now, we are treating it as a terrorist attack," he added.

The attacker "wanted to die himself", police added on Twitter.

Mr Comas did not confirm Spanish media reports that he was a 29-year-old Algerian, saying only that he had been in the country "for years".

There was no evidence linking the attack with any terrorist cell, Mr Comas said,

He added that Spain would maintain its security alert at level 4, one notch below the maximum level that signals an attack is imminent.

Last Friday, Barcelona commemorated the first anniversary of an Islamist assault in which a young man drove a van down a busy central boulevard, killing 14 people and injuring more than 100 in Spain's worst attack in more than a decade.

Separately, three people were injured when a car mounted the pavement in the northern town of Casetas, in what a government spokeswoman later said was being treated as an accident.

The two occupants of the car fled the scene and were later detained.