Safeway is at the center of a battle among grocers to cash in on the Washington area’s boom. (Ben Nelms/Bloomberg)

In the District’s less prosperous days, Safeway — the city’s top grocery chain for decades — was often competing only with itself. After years of population declines, swaths of D.C. neighborhoods did not have any grocery stores at all, much less competitors on the same street.

Now that the city is undergoing an economic and demographic resurgence, Safeway is at the center of a battle among grocers to cash in on the Washington area’s boom.

The company, based in Pleasanton, Calif., is moving aggressively to keep its footing. At the moment, Safeway is building two new local stores, both with hundreds of apartments on top, and is planning to build or rebuild four stores beginning this year or the next in Alexandria, Rockville, Upper Marlboro and Hyattsville.

Another four stores are in the pre-planning stages for redevelopment, and the chain has already made clear to D.C. Mayor Vincent C. Gray (D) that it would like to build a new store at the former site of the Walter Reed hospital, which the District is planning to redevelop.

Much of this course was plotted in 2005, when Safeway — acknowledging that it had allowed some stores to flag — decided that it would do one of three things with each of its Washington area locations to address new competition: close the store, upgrade it with more offerings or rebuild it altogether.

“There’s a lot more choices than there were seven years ago, and the quality has certainly improved with the Harris Teeters and Whole Foods and the Wegmans,” said Tim Baker, vice president of real estate at Safeway. “A lot of them, if you go back seven years, there were a lot fewer or none [in the area] back then.”

Safeway has closed stores that were either underperforming or too small to remodel into the chain’s new format, such as the one located in the Watergate offices and the one near Dupont Circle that had so little visibility that people called it the “Secret Safeway.”

But for some older stores in prime locations, the company and its real estate arm, Property Development Centers, tested the waters with local real estate developers about their interest in building housing above the stores. With Washington’s red-hot apartment market, the response was massive. A new Safeway in Wheaton, with 486 apartments on top, will be complete in October. The new Petworth store, with 220 apartments, will open next summer.

Baker said the chain’s history with the city and neighbors make the projects easier. “We have lots of experience in D.C. and lots of experience in the neighborhoods and lots of experience developing,” he said.

Though it is tops in the District, Safeway does not have the most stores in the Washington area. That title belongs to Giant Food, which opened its first store in 1936 and was purchased by the Dutch firm Ahold in 1998. Harris Teeter, based in North Carolina, has been quickly adding new area stores, and it also may be changing hands; it said in February that it had been approached by private-equity firms and had hired J.P. Morgan as an adviser. Ahold has also been rumored to be interested. Whole Foods would like to open a new store in Riverdale Park and previously expressed interest in the area around Nationals Park.

But in the next 18 months, Safeway plans to begin construction on four new stores. Two are planned for Prince George’s County, on Route 301 in Upper Marlboro and at University Town Center in Hyattsville. The chain will build a brand-new store in tandem with developer JBG Rosenfeld at the Twinbrook Metro station, and it will rebuild its Alexandria store at 3526 King St. beginning this summer.