Despite Chicago’s alarming, if select, murder statistics and routine singling out by President Donald Trump for gun violence, this lakeside metropolis was just ranked one of the safest cities in the world.

The Economist Intelligence Unit, a London-based research firm affiliated with the Economist magazine, named Chicago to its 2017 Safe Cities Index, issued Thursday — one of only three US cities to crack the top 20 — largely based on advances in digital security.

The annual ranking assesses 60 global cities using four factors: personal safety, health security, digital security and the safety of infrastructure.

Chicago, at 19th-safest, narrowly trailed the other top-20 US representatives, San Francisco at 15th and Los Angles at 18th. Tokyo, Singapore and Osaka topped the overall list, an order that has remained unchanged since 2015. Buenos Aires was the only city in the so-called developing world in the top half of the index.

Chicago fared best in digital security, jumping 12 places in the rankings and following just Tokyo and Singapore. The report gave props to an initiative in cybersecurity training through local colleges and the Department of Defense that Mayor Rahm Emanuel announced in January.

Latin American cities performed poorly, with Buenos Aires 23rd and Rio de Janiero and São Paolo sharing 49th place. “Brazil is one of the top sites for cybercrime in the world,” said Robert Muggah, co-founder of the Igarapé Institute, which participated in the report. “That has to do with the fact that internet banking came to Brazil quite early. So you have [a] sophisticated hacking community in Brazil.”

Major US cities tended to score below their developed-world counterparts when it came to infrastructure safety. But recent terrorist acts, including incidents involving the driving of vehicles into crowds in London and France, contributed to lowering their overall safety scores.

As for health, while many aspects of health care come at a price, income levels are not always the driving force behind the extent to which cities keep their residents healthy. Of the top 10 performing cities in the health security category, only two, Tokyo and Zurich, are high-income cities.

Broadly speaking, it’s with growth that safety risk tends to increase, the report stressed.

Today there are 31 so-called global megacities with populations greater than 10 million, but by 2030 that total is forecast to hit 41. The swelling in population is expected to directly affect water and power infrastructure, the housing supply and services including health care and housing, EIU wrote. According to the report, all of these pressure points have the potential to widen the equity divide, which will lead to greater tensions and even create increased threats of terrorism.

In its personal security ranking, the Safe Cities Index factored in terrorism in addition to other forms of urban violence. The report also confirmed that you are more likely to die from urban violence than terrorism, no matter where you are located around the world. Deaths from terrorism totaled 30,000 in 2015, whereas deaths by homicide were roughly 440,000.

Chicago was absent from the top 10 under the urban violence category; instead, five of the top-performing cities — Singapore, Osaka, Tokyo, Taipei and Hong Kong — are Asian, with two of them in Japan.

Chicago officials are quick to point to the ease of gun purchases in neighboring Indiana and Wisconsin, then used by gang offenders, as contributing to the statistics, as well as lenient punishment for gun offenders relative to sentencing in New York and other big cities. Still, the headline statistics have been hard for the city to shake.

Trump over the summer tweeted: “Crime and killings in Chicago have reached such epidemic proportions that I am sending in Federal help. 1,714 shootings in Chicago this year!” And the president, speaking to Fox’s Sean Hannity on Wednesday, once again said Chicago gun violence is “out of control.” (He also repeated an oft-told story about a Chicago police officer who claimed to have the answer to solving the city’s violence if only the force were allowed to do its job — a story Chicago police have previously called “discredited.”)