pythongraphnetworkxvertices

Faces in Planar Graph


I'm trying to find faces of an undirected planar graph.

Planar Graph (Networkx random layout of vertices

The graph is embedded in within the networkx Python Graph library and it's nodes have positions (GPS Data) such as:

[(0, {'pos': [12.54341, 54.10141]}), (1, {'pos': [12.5433, 54.10064]}), (2, {'pos': [12.54388, 54.10015]})...

.. which look like this:

Graph with GPS positions

What is the easiest way to find all "geographic" faces of the graph? With geographic I mean the cycles of the second visual presentation with start_node = end_node.

I'd appreciate a brief cookbook of how to proceed.


Solution

  • As @philbox2 mentions, you can't rely on cycle_basis or minimum_cycle_basis from NetworkX, since these functions serve a different purpose and might return overlapping areas. However, since you doing geography + Python, you can use Shapely's polygonize function for this. → https://shapely.readthedocs.io/en/stable/reference/shapely.polygonize.html#shapely.polygonize

    From the documentation (October 2025):

    Polygonizes an array of Geometries that contain linework which represents the edges of a planar graph. Any type of Geometry may be provided as input; only the constituent lines and rings will be used to create the output polygons.