Convert From GDS to LinearRing
Whilst it might be possible to work directly with the gds files themselves, it made much more sense to convert them to an intermediate format that was easier to work with.
I’ve been using the Shapely Python library to work with polygons, so I decided to convert from gds to LinearRing, a shapely object type.
GDS vs LinearRing
The gds file contains all sorts of information about units, formats and layers. Thankfully the bit that we care about is just a simple list of coordinates. This list of coordinates is called a boundary.
It turns out that GDS boundaries and LinearRings are quite similar, so we don’t have to do do much to convert between them.
GDS Boundaries are of the format
XY: x1, y1, x2,y2, x3,y3, … , xn, yn
they must be explicitly closed, so that the first and last pairs of coordinate values are the same.
Shapely LinearRing objects are an ordered sequence of (x,y) tuples
e.g. ring = LinearRing([(0, 0), (1, 1), (1, 0)])
They can be explicitly closed, otherwise the LinearRing will be closed implicitly, by copying the first coordinate pair to the last position in the ring.
Convert GDS to LinearRing
To quickly demonstrate how to convert a shape from GDS to LinearRing I’ve put together a short python script.
As we saw above the key difference between a gds boundary and a LinearRing is that the LinearRing is a list of tuple coordinate pairs. To extract the pairs from the gds boundary list we can use the izip function from the itertools library.
In its simplest from conversion from gds to LinearRing could be something like:
from itertools import izip def pairwise(t): it = iter(t) return izip(it,it) # Dummy gds boundary data gds_test = (0, 0, 30000, 0, 15000, 15000, 5000, 15000, 0, 0) # Extract the coordinates pairwise from the gds boundary ring = list(pairwise(gds_test)) # Need to convert out list into a ring for the plotting function to work ring = LinearRing(ring)
In the script in my repository, I’ve added code from the shapely examples to let us plot the end result: