Virginia House Speaker Kirk Cox (R-Colonial Heights) supports creating a 12-member bipartisan commission on electoral districting in the commonwealth. (Steve Helber/AP)

VIRGINIA HAS been trending Democratic. A Republican hasn’t won a statewide race in nearly a decade. But Republicans continue to control the state legislature thanks to what federal judges have concluded is a racially gerrymandered electoral map drafted by GOP lawmakers in 2011.

Little wonder, then, that the party’s grandees in Richmond are reeling at what looks like a federal court’s imminent decision to impose a map that seems likely not only to flip both houses of the General Assembly to Democratic control in this fall’s elections but also to unseat several of the legislature’s top Republicans. The map, chosen by the court from configurations drafted by a professor in California, would shift six incumbent Republicans to newly drawn, and Democratic-leaning, districts. Among the probable casualties would be the current GOP House speaker, Kirk Cox (Colonial Heights).

Having bent the rules to establish what they presumed would be long-term political control, Republicans now wail that the referee is unfair and biased for having intervened and called foul. Desperate to reverse the tide, Mr. Cox is appealing to the Supreme Court. Nice try, but that court has refused to postpone the new map’s implementation while it considers the appeal.

It’s a sorry spectacle — and one that could as easily have befallen the Democrats in earlier decades when they controlled the political cartography and twisted the legislative district lines to their advantage. If there is any saving grace for the commonwealth, it is that Republicans, shocked at the map unelected judges are on the verge of imposing, are now finally moving toward the only balanced and equitable solution that favors actual voters: a bipartisan redistricting commission.

The plan they have put forward, drafted by Del. Mark L. Cole (Spotsylvania) and backed by Mr. Cox, would advance a constitutional amendment to establish a 12-member commission, evenly split between Republicans and Democrats, with four members each named by the House, the Senate and the governor. Whatever map it agreed on would get an up-or-down vote in the General Assembly; if lawmakers rejected both the first map and a second one from the commission, the problem would be punted to the state Supreme Court, which would see to drawing the lines itself.

A competing measure making its way through the state Senate would create a 16-member redistricting commission, evenly split between legislators and nonelected civilians. Unfortunately, senators rejected language that would explicitly prohibit gerrymandering — a feature of similar efforts in other states.

Other variations are kicking around the legislature’s current session in Richmond, including one backed by Gov. Ralph Northam, a Democrat. Any is likely preferable to the status quo, which gave rise to the current map’s racially skewed configuration; it packs African American voters into districts around Richmond and Hampton Roads, thereby diminishing their overall electoral muscle while keeping adjacent districts more heavily white — and Republican.

In Virginia and elsewhere, politicians empowered by supercomputers have perfected the dark art of choosing their voters and protecting their own jobs. The Supreme Court has been stymied until now in determining how much partisan gerrymandering is too much. Not so the lower courts, whose intervention has given legislatures in some states a wake-up call. In Virginia’s case, it’s a timely one.