#include <ogrsf_frmts.h>
#include <iostream>
#include <cassert>
using namespace std;

int main(int argc, char ** argv)
{
    OGRRegisterAll();

    // Creating the polygon from the WKT
    char* wkt = "POLYGON((0 0, 5 0, 5 5, 0 5), (1 1, 2 1, 2 2, 2 1))";
    OGRErr err = OGRERR_NONE;
    OGRGeometry* geom = NULL;
    err = OGRGeometryFactory::createFromWkt(&wkt, NULL, &geom);
    assert(OGRERR_NONE == err);
    assert(NULL != geom);
    
    cout << "--- BEFORE CLOSE ---\n";
    cout << "Geometry type (=3): " << geom->getGeometryType() << endl;
    cout << "Geometry coordinate dimension (=2): " << geom->getCoordinateDimension() << endl;
    cout << endl;

    // Check polygon and exterior ring BEFORE close operation
    OGRPolygon* poly = static_cast<OGRPolygon*>(geom);

    cout << "Polygon type (=3): " << poly->getGeometryType() << endl;
    cout << "Polygon coordinate dimension (=2): " << poly->getCoordinateDimension() << endl;
    cout << endl;

    OGRLinearRing const* ring = poly->getExteriorRing();
    cout << "Ring type (=2): " << ring->getGeometryType() << endl;
    cout << "Ring coordinate dimension (=2): " << ring->getCoordinateDimension() << endl;
    cout << endl;

    // Do the job!
    poly->closeRings();

    // Check polygon and exterior ring AFTER close operation
    cout << "--- AFTER CLOSE ---\n";
    cout << "Polygon type (=3): " << poly->getGeometryType() << endl;
    cout << "Polygon coordinate dimension (=2): " << poly->getCoordinateDimension() << endl;
    cout << endl;

    //
    // BUG: After closeRings() operation, geometry type of ring is reported as
    //      wkbLineString25D enumerator (value = 0x80000002)
    //      The closeRings() implicitly changes cooridnate dimensionality from
    //      X,Y to X,Y,Z
    //
    ring = poly->getExteriorRing();
    cout << "Ring type (=2): " << ring->getGeometryType() << endl;
    cout << "Ring coordinate dimension (=2): " << ring->getCoordinateDimension() << endl;
    cout << endl;

    return 0;
}

