presto-geospatial-toolkit/src/main/java/io/prestosql/geospatial/serde/JtsGeometrySerde.java/JtsGeometrySerde/writePolygon
class JtsGeometrySerde:    private static void writePolygon(Geometry geometry, SliceOutput output, boolean multitype)
    {
        int numGeometries = geometry.getNumGeometries();
        int numParts = 0;
        int numPoints = geometry.getNumPoints();
        for (int i = 0; i < numGeometries; i++) {
            Polygon polygon = (Polygon) geometry.getGeometryN(i);
            if (polygon.getNumPoints() > 0) {
                numParts += polygon.getNumInteriorRing() + 1;
            }
        }

        if (multitype) {
            output.writeByte(GeometrySerializationType.MULTI_POLYGON.code());
        }
        else {
            output.writeByte(GeometrySerializationType.POLYGON.code());
        }

        output.writeInt(EsriShapeType.POLYGON.code);

        writeEnvelope(geometry, output);

        output.writeInt(numParts);
        output.writeInt(numPoints);

        if (numParts == 0) {
            return;
        }

        int[] partIndexes = new int[numParts];
        boolean[] shellPart = new boolean[numParts];

        int currentPart = 0;
        int currentPoint = 0;
        for (int i = 0; i < numGeometries; i++) {
            Polygon polygon = (Polygon) geometry.getGeometryN(i);

            partIndexes[currentPart] = currentPoint;
            shellPart[currentPart] = true;
            currentPart++;
            currentPoint += polygon.getExteriorRing().getNumPoints();

            int holesCount = polygon.getNumInteriorRing();
            for (int holeIndex = 0; holeIndex < holesCount; holeIndex++) {
                partIndexes[currentPart] = currentPoint;
                shellPart[currentPart] = false;
                currentPart++;
                currentPoint += polygon.getInteriorRingN(holeIndex).getNumPoints();
            }
        }

        for (int partIndex : partIndexes) {
            output.writeInt(partIndex);
        }

        Coordinate[] coordinates = geometry.getCoordinates();
        canonicalizePolygonCoordinates(coordinates, partIndexes, shellPart);
        writeCoordinates(coordinates, output);
    }