The extraordinary video of a Peruvian woman coated in mud emerging from a brown sea of pallets and wooden poles was viewed around the world. Evangelina Chamorro, who had been feeding her pigs when she was swept for two miles downhill in a huge mudslide, became the poster girl for resilience during the country’s worst floods in living memory.

Remarkably, the 32-year-old was treated for minor injuries and left hospital just a week after the incident in March. The psychological scars, however, are taking longer to heal.

“It would have been too sad for my daughters if I had not survived. I just thought of them and prayed to God. Thanks to God I survived,” she says, her expression wavering.

Labelled “Mother Courage” by the national press, her ordeal, recorded on a camera phone by an onlooker, was viewed millions of times and broadcast around the world. But Chamorro’s story is not just one of remarkable survival. It revealed the shaky foundation of the Peruvian capital itself.

By many reckonings, Lima is one of the world’s greatest “self-built” cities. Most people here still build their own homes, pouring the concrete themselves. In fact, more cement is sold on the domestic market than in the commercial one.

But those homes are all too often built in vulnerable locations, sold by unscrupulous land dealers who misrepresent safety claims. As a result, when heavy rains hit the city, hundreds of thousands of homes were simply washed away.

With 10 million inhabitants, Peru’s capital spreads horizontally across a coastal desert plain and outward and upward into the foothills of the Andes mountains. It has grown sixfold since 1960, swelled by waves of migration from the provinces.

Until very recently, however, there was virtually no state-backed social housing. Successive governments simply allowed poor migrants to occupy desert land of little value, eventually giving in to the new slums’ demands for basic services such as electricity and water, and granting property titles. Villa El Salvador was among the first of these pueblos emergentes, or “emerging towns”, which started to spring up around Lima in the 1970s.

In Lima almost all the flood damage was caused by the illegal occupation of land

Alvaro Espinoza

This deliberate government policy – and the historic lack of access to credit – drove generations of families to build their own homes. They added floors when they could afford them; as the country’s economy boomed in the 2000s, so did cement companies and DIY stores. The result is a city of colourful, higgledy piggledy neighbourhoods where homes vary from the derelict to the picturesque – a testament to Peruvian resourcefulness.

The more established districts escaped the worst of the floods. But many others were not so lucky. The floods are estimated to have caused more than £7bn pounds worth of damage. President Pedro Pablo Kuczysnki has said that future construction, informal or not, will have to be strictly controlled.

“We must rebuild but we must do it much better than before,” he said in a national address in April. “No more roads which collapse, no more drains which get blocked, no more precarious buildings on dangerous floodplains. We have to change.”

“Self-building is not really a big deal. The problem is that the self-builder lacks technical assistance,” says Gustavo Riofrio, a town planner and adviser to Peru’s congressional housing commission. The result is homes that are less resistant to natural disasters such as earthquakes and floods.

Location also has a major role to play. “In Lima almost all the flood damage was caused by the illegal occupation of land,” says Alvaro Espinoza, an urban development investigator with thinktank Grade.

Damage caused by recent flash floods in Peru could cost up to £7bn to repair. Photograph: Ernesto Benavides/AFP/Getty Images

“The landslides happen every year. The problem isn’t the buildings, but that they are built on riverbeds – and people continue to live there even when they have seen what happens.”

Although this year’s floods were on a much greater scale, flood-induced landslides are a seasonal occurrence in the arid foothills that are the backbone of the low-lying Pacific-facing city.

Nueva Navarra, however – the new settlement near the popular beach resort Punta Hermosa where Chamorro’s partner, Armando, had bought their plot – had been dry for years. Like many Peruvians, the couple purchased terrain from an unregulated land dealer. They were not aware they were living on a time bomb waiting to triggered by a coastal El Niño.

“There are traffickers who sell land without explaining if it’s dangerous or not,” says Chamorro, who was a migrant from the Amazon city of Iquitos. “They should also share the blame for why we’re living in these places where no one should live.”

Chamorro is sitting in the living room of a portable home donated by a local modular construction company, Tecnofast, which she is set to move into with Armando and their daughters Doris, 10, and Daysy, five. The company claims the homes are insulated, fire-proof and earthquake-proof.

All the family needs now is a plot of land in a safe place. These remain in short supply in Lima.

New legislation from Kuczysnki promises to crack down on corrupt municipal officials and local mayors who facilitate such sales in high-risk zones. But the floods also laid bare how endemic corruption had left infrastructure weakened to the point of collapse.

Throughout the country, funds earmarked for disaster prevention were not used. In Lima, just a fraction of the budget for 2017 had been spent on actually preventing disaster. Instead, most of it was used to improve Lima’s seafront, despite widespread public disapproval.

Meanwhile, population growth in Lima continues – with very little good quality areas left on which to build, says Espinoza.

“Lima needs to densify. It’s too low-rise, and it needs to build upwards,” he says, arguing that only formal construction companies can make that transformation. While Peru’s emerging middle class are beginning to move into new-build apartments, helped by accessible mortgages from state-backed lenders such as Fondo MiVivienda, those remain beyond the reach of many. Until that changes, Lima’s poor will continue to build their own city – will all the risks that entails.

Chamorro urges her fellow settlers to choose wisely. “To all those people who are looking for a new place to live – find a safe place.”