The city's ban on panhandlers in traffic medians is unconstitutional, a federal judge ruled Wednesday, setting aside the law that came under attack after it was adopted last summer.

The restriction, which barred people from loitering on medians, violated free speech rights, Judge George Singal ruled. He also said the law was not necessary to preserve public safety and unfairly targeted one group by allowing people to place campaign signs on medians.

"It defies logic and common sense to say that a person is more or less safe when placing a 'campaign' sign on a median than any other type of sign," the judge wrote.

The ordinance approved by the City Council in July doesn't specifically mention panhandling, but it banned loitering on medians and forced panhandlers seeking handouts from motorists to move.

It was on hold during the litigation.

The city argued that the ordinance was aimed at addressing legitimate public safety concerns. But the plaintiffs said the ordinance was too broad and that the city exaggerated the public safety hazards to justify the ban.

Zachary Heiden, legal director of the American Civil Liberties Union of Maine, said he hoped the judge's ruling would make other communities think twice before adopting similar laws. He said that the city's existing laws, including those banning public intoxication, were enough to protect the public.

"It's really a notable victory for freedom of speech and for everybody who uses public space to share their views with their fellow citizens," he said.

Police Chief Michael Sauschuck said safety was always the primary concern.

"Although we were hoping for a different outcome we will certainly abide by the judge's decision," he said.

City Attorney Danielle West-Chuhta said the city was reviewing the ruling and was determining whether to appeal.