How to efficiently create a polygon


Summary
A polygon can be built in numerous ways according to the user's input. This article shows the most commonly used approaches to efficiently create a new polygon.

Building a polygon using points

The following code shows how to build a polygon using a collection of points. This approach is preferred when the user has a sequence of vertices as input. The resulting polygon ring will only contain a straight line segment:
 
[Java]
static void createPolygonByPoints()throws Exception{
    //Build polygon from a sequence of points. 
    //At ArcGIS 9.2, the recommended way to add arrays of points to a geometry is to use
    //the IGeometryBridge2 interface on the GeometryEnvironment singleton object.
    IGeometryBridge2 geomBridge = new GeometryEnvironment();
    IPointCollection4 polygon = new Polygon();
    //TODO: Set the spatial reference of the newly created polygon

    int count = ...;
    _WKSPoint[] points = new _WKSPoint[count];
    //TODO: ITODO:TODO:nitialize the points array with proper coordinates
    geomBridge.setWKSPoints(polygon, points);
}

Building a polygon using segments

In this code example, the multipart polygon is built segment–by–segment. It gives the most control if you are using advanced construction techniques or curved segments (circular arcs, Bèzier curves, and so on). See the following:
[Java]
static void createPolygonBySegments()throws Exception{

    //Build a polygon segment-by-segment.
    IPolygon polygon = new Polygon();
    //TODO: Set the spatial reference of the newly created polygon

    //Create the segments and rings. If this were a single-part polygon, segments can be
    //added directly to the polygon and the ring created internally.
    //You cannot reuse the same ring object. Also, when rings are added to the polygon,
    //the polygon takes ownership of the rings. You cannot reuse a ring for building another polygon.
    //These same restrictions also apply to segments.
    ICircularArc circularArc = new CircularArc();
    IBezierCurveGEN bezCurve = new BezierCurve();
    ISegmentCollection ring1 = new Ring();
    ISegmentCollection ring2 = new Ring();
    ring1.addSegment((ISegment)circularArc, null, null);
    ring2.addSegment((ISegment)bezCurve, null, null);

    IGeometryCollection geoCollection = (IGeometryCollection)polygon;
    geoCollection.addGeometry((IGeometry)ring1, null, null);
    geoCollection.addGeometry((IGeometry)ring2, null, null);

    //At this point, a _shell_geometry has been constructed. The _shell_geometry consists of
    //one polygon containing two rings; each containing one segment.
    //However, the coordinates of those segments have not been defined.
    //Because references to those segments exist, their coordinates can be 
    //defined now.
    IPoint point = new Point();
    point.setX( - 10);
    point.setY(0);
    circularArc.putCoordsByAngle(point, 0, 2 * 3.14159265358979, 10.0);
    IPoint[] points = new IPoint[]{
        new Point(), new Point(), new Point(), new Point()
    };
    points[0].setX(10);
    points[0].setY(0);
    points[1].setX(10);
    points[1].setY(10);
    points[2].setX(20);
    points[2].setY(10);
    points[3].setX(10);
    points[3].setY(0);
    bezCurve.putCoords(points);

    //pPolygon has now been defined. When changing segment coordinates directly
    //like this, be careful to let the top-level geometry know that
    //things have changed underneath it so that it can delete any cached properties
    //that it maintains, such as envelope, length, area, and so on.
    //When you use certain methods on the top-level geometry implementation
    //of IGeometryCollection interface, like AddGeometry, it automatically
    //invalidates any cached properties.
    geoCollection.geometriesChanged();

}

Creating a polygon using existing geometries

A polygon can be created based on a topological relationship between existing geometries. In the following code example, a polygon is generated by uniting two existing polygons:
[Java]
static void createPolygonFromExistingGeometries(IPolygon pPolygon1, IPolygon
    pPolygon2)throws Exception{
    //Build a new polygon by uniting two existing polygons.
    ITopologicalOperator2 topoOp = (ITopologicalOperator2)pPolygon1;
    //Simplify.
    topoOp.setIsKnownSimple(false);
    topoOp.simplify();
    IPolygon newPolygon = (IPolygon)topoOp.union(pPolygon2);
    //newPolygon is the new generated polygon.
}