Create valid polygons
arcgissamples\geometry\CreateValidPolygons.java
/* Copyright 2010 ESRI
* 
* All rights reserved under the copyright laws of the United States
* and applicable international laws, treaties, and conventions.
* 
* You may freely redistribute and use this sample code, with or
* without modification, provided you include the original copyright
* notice and use restrictions.
* 
* See the use restrictions.
* 
*/
package arcgissamples.geometry;

import com.esri.arcgis.geometry.*;
import com.esri.arcgis.system.*;

/**
 * This class demonstrates how to create valid polygons efficiently.
 */
public class CreateValidPolygons
{
  public CreateValidPolygons()
  {
    
  }
  
  public static void main(String[] args)
  {
    System.out.println("Starting CreateValidPolygons - An ArcObjects Java SDK Developer Sample");
    try
    {
      // Initialize the engine and licenses.
      EngineInitializer.initializeEngine();

      AoInitialize aoInit = new AoInitialize();
      initializeArcGISLicenses(aoInit);
      
      CreateValidPolygons polygons = new CreateValidPolygons();
      polygons.CreateRectanglePolygonFromEnvelope();
      polygons.createMultipartPolygonRingSegmentCollection();
      polygons.createMultipartPolygonRingPointCollection();
      polygons.createSinglepartPolygonPointCollection();
      polygons.createSinglepartPolygonSegmentCollection();

      aoInit.shutdown();
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
    
    System.out.println("Done!");
  }
  
  /**
   * Initializes the lowest available ArcGIS License
   */
  private static void initializeArcGISLicenses(AoInitialize aoInit)
  {
    try
    {
      if (aoInit.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeEngine) == esriLicenseStatus.esriLicenseAvailable)
      {
        aoInit.initialize(esriLicenseProductCode.esriLicenseProductCodeEngine);
      }
      else if (aoInit.isProductCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeArcView) == esriLicenseStatus.esriLicenseAvailable)
      {
        aoInit.initialize(esriLicenseProductCode.esriLicenseProductCodeArcView);
      }
      else
      {
        System.err.println("Could not initialize an Engine or ArcView license. Exiting application.");
        System.exit(-1);
      }
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }
  
  /**
   * Create a multipart polygon using rings via an array of Line segments
   */
  private void createMultipartPolygonRingSegmentCollection() throws Exception
  {
    Ring[] ringColl = new Ring[2];
    Line line0[] = new Line[4];
    Line line1[] = new Line[4];
    Line seg0[] = new Line[4];
    Line seg1[] = new Line[4];
    Ring geometry[] = new Ring[2];
    GeometryBag gonColl = new GeometryBag();

    // *********************************************************
    // THE SPATIAL REFERENCE SHOULD BE SET HERE ON THE POLYGON
    // Here the spatial reference is created in memory but could also come from various sources:
    // IMap, IGeodataset, IGeometry etc...
    UnknownCoordinateSystem pspref = new UnknownCoordinateSystem();

    // Set the false origin and units.
    // The XYUnits value is equivalent to the precision specified when creating a feature class
    pspref.setFalseOriginAndUnits(-10000, -10000, 100000);

    gonColl.setSpatialReferenceByRef(pspref);

    // *********************************************************
    // Initialize offset values
    double d0X0 = 0, d0Y0 = 0, d0X1 = 0, d0Y1 = 0, d0X2 = 0, d0Y2 = 0, d0X3 = 0, d0Y3 = 0, d1X0 = 10000, d1Y0 = 10000, d1X1 = 10000, d1Y1 = 10000, d1X2 = 10000, d1Y2 = 10000, d1X3 = 10000, d1Y3 = 10000;

    // Initialize the points

    Point[] pointsRing0 = new Point[4];
    Point[] pointsRing1 = new Point[4];

    for (int k = 0; k <= 3; k++)
    {
      pointsRing0[k] = new Point();
      pointsRing1[k] = new Point();
    }

    // Loop to change the coordinates of the points
    for (int i = 0; i <= 1000; i++)
    {
      ringColl[0] = new Ring();
      ringColl[1] = new Ring();

      // Lines are passed by reference to the polygon
      // so a new line has to be instantiated to avoid the polygon being degenerated
      for (int k = 0; k <= 3; k++)
      {
        line0[k] = new Line();
        line1[k] = new Line();
        seg0[k] = line0[k];
        seg1[k] = line1[k];
      }
      geometry[0] = ringColl[0];
      geometry[1] = ringColl[1];
      d0X0 = d0X0 - 5;
      d0Y0 = d0Y0 - 5;
      d0X1 = d0X1 + 5;
      d0Y1 = d0Y1 - 5;
      d0X2 = d0X2 + 5;
      d0Y2 = d0Y2 + 5;
      d0X3 = d0X3 - 5;
      d0Y3 = d0Y3 + 5;

      // Put the coordinates of the points to use in the first ring
      pointsRing0[0].putCoords(d0X0, d0Y0);
      pointsRing0[1].putCoords(d0X1, d0Y1);
      pointsRing0[2].putCoords(d0X2, d0Y2);
      pointsRing0[3].putCoords(d0X3, d0Y3);
      d1X0 = d1X0 - 5;
      d1Y0 = d1Y0 - 5;
      d1X1 = d1X1 + 5;
      d1Y1 = d1Y1 - 5;
      d1X2 = d1X2 + 5;
      d1Y2 = d1Y2 + 5;
      d1X3 = d1X3 - 5;
      d1Y3 = d1Y3 + 5;

      // Put the coordinates of the points to use in the second ring
      pointsRing1[0].putCoords(d1X0, d1Y0);
      pointsRing1[1].putCoords(d1X1, d1Y1);
      pointsRing1[2].putCoords(d1X2, d1Y2);
      pointsRing1[3].putCoords(d1X3, d1Y3);

      // Put the coordinates of the lines
      line0[0].putCoords(pointsRing0[0], pointsRing0[1]);
      line0[1].putCoords(pointsRing0[1], pointsRing0[2]);
      line0[2].putCoords(pointsRing0[2], pointsRing0[3]);
      line0[3].putCoords(pointsRing0[3], pointsRing0[0]);
      line1[0].putCoords(pointsRing1[0], pointsRing1[1]);
      line1[1].putCoords(pointsRing1[1], pointsRing1[2]);
      line1[2].putCoords(pointsRing1[2], pointsRing1[3]);
      line1[3].putCoords(pointsRing1[3], pointsRing1[0]);

      GeometryEnvironment gBridge = new GeometryEnvironment();

      // Add the segments to the rings
      gBridge.addSegments(ringColl[0], seg0);
      gBridge.addSegments(ringColl[1], seg1);

      // Add the rings to the polygon
      gBridge.addGeometries(gonColl, geometry);
    }

    // You can draw, store or use the polygon (gonColl) in other geometry operations at this point
  }

  /**
   * Create a multipart polygon using rings via a collection of Points
   */

  private void createMultipartPolygonRingPointCollection() throws Exception
  {
    Point pointsRing0[] = new Point[5];
    Point pointsRing1[] = new Point[5];
    Ring ringsColl[] = new Ring[2];
    IGeometry[] geometry = new IGeometry[2];

    // Create the resulting polygon
    Polygon gonColl = new Polygon();

    // *********************************************************
    // THE SPATIAL REFERENCE SHOULD BE SET HERE ON THE POLYGON
    // Here the spatial reference is created in memory but could also come from various sources:
    // IMap, IGeodataset, IGeometry etc...
    ISpatialReference pspref = new UnknownCoordinateSystem();

    // Set the false origin and units.
    // The XYUnits value is equivalent to the precision specified when creating a feature class
    pspref.setFalseOriginAndUnits(-10000, -10000, 100000);
    gonColl.setSpatialReferenceByRef(pspref);

    // *********************************************************
    double d0X0 = 0, d0Y0 = 0, d0X1 = 0, d0Y1 = 0, d0X2 = 0, d0Y2 = 0, d0X3 = 0, d0Y3 = 0, d1X0 = 10000, d1Y0 = 10000, d1X1 = 10000, d1Y1 = 10000, d1X2 = 10000, d1Y2 = 10000, d1X3 = 10000, d1Y3 = 10000;
    // Loop to change the coordinates of the points
    for (int i = 0; i <= 1000; i++)
    {
      ringsColl[0] = new Ring();
      ringsColl[1] = new Ring();
      geometry[0] = ringsColl[0];
      geometry[1] = ringsColl[1];

      // Create the new points
      for (int k = 0; k <= 4; k++)
      {
        pointsRing0[k] = new Point();
        pointsRing1[k] = new Point();
      }
      d0X0 = d0X0 - 5;
      d0Y0 = d0Y0 - 5;
      d0X1 = d0X1 + 5;
      d0Y1 = d0Y1 - 5;
      d0X2 = d0X2 + 5;
      d0Y2 = d0Y2 + 5;
      d0X3 = d0X3 - 5;
      d0Y3 = d0Y3 + 5;

      // Put the coordinates of the points to use in the first ring
      pointsRing0[0].putCoords(d0X0, d0Y0);
      pointsRing0[1].putCoords(d0X1, d0Y1);
      pointsRing0[2].putCoords(d0X2, d0Y2);
      pointsRing0[3].putCoords(d0X3, d0Y3);
      pointsRing0[4].putCoords(d0X0, d0Y0);
      IGeometryBridge gBridge = new GeometryEnvironment();

      // Add the points to the ring
      gBridge.addPoints(ringsColl[0], pointsRing0);
      d1X0 = d1X0 - 5;
      d1Y0 = d1Y0 - 5;
      d1X1 = d1X1 + 5;
      d1Y1 = d1Y1 - 5;
      d1X2 = d1X2 + 5;
      d1Y2 = d1Y2 + 5;
      d1X3 = d1X3 - 5;
      d1Y3 = d1Y3 + 5;

      // Put the coordinates of the points to use in the second ring
      pointsRing1[0].putCoords(d1X0, d1Y0);
      pointsRing1[1].putCoords(d1X1, d1Y1);
      pointsRing1[2].putCoords(d1X2, d1Y2);
      pointsRing1[3].putCoords(d1X3, d1Y3);
      pointsRing1[4].putCoords(d1X0, d1Y0);

      // Add the points to the ring
      gBridge.addPoints(ringsColl[1], pointsRing1);

      // Add the rings to the polygon
      gBridge.addGeometries(gonColl, geometry);
    }
    // You can draw, store or use the polygon (gonColl) in other geometry operations at this point
  }

  /**
   * Create a single part polygon via a collection of Points
   */
  private void createSinglepartPolygonPointCollection() throws Exception
  {
    Point pt[] = new Point[5];
    Polygon gonColl = new Polygon();

    // *********************************************************
    // THE SPATIAL REFERENCE SHOULD BE SET HERE ON THE POLYGON
    // Here the spatial reference is created in memory but could also come from various sources:
    // IMap, IGeodataset, IGeometry etc...
    UnknownCoordinateSystem pspref = new UnknownCoordinateSystem();

    // Set the false origin and units.
    // The XYUnits value is equivalent to the precision specified when creating a feature class
    pspref.setFalseOriginAndUnits(-10000, -10000, 100000);
    gonColl.setSpatialReferenceByRef(pspref);

    // *********************************************************
    // Initialize the points
    for (int i = 0; i <= 4; i++)
    {
      pt[i] = new Point();
    }
    pt[0].putCoords(0, 0);
    pt[1].putCoords(10, 0);
    pt[2].putCoords(10, 10);
    pt[3].putCoords(0, 10);
    pt[4].putCoords(0, 0);
    GeometryEnvironment gBridge = new GeometryEnvironment();

    // Add the points to the polygon
    gBridge.addPoints(gonColl, pt);

    // You can draw, store or use the polygon (gonColl) in other geometry operations at this point
  }

  /**
   * Create a single part polygon via a collection of Line segments.
   */
  private void createSinglepartPolygonSegmentCollection() throws Exception
  {
    Point pt[] = new Point[5];
    Line[] line = new Line[4];
    Line[] segment = new Line[4];
    Polygon gonColl = new Polygon();
    // *********************************************************
    // THE SPATIAL REFERENCE SHOULD BE SET HERE ON THE POLYGON
    // Here the spatial reference is created in memory but could also come from various sources:
    // IMap, IGeodataset, IGeometry etc...
    UnknownCoordinateSystem pspref = new UnknownCoordinateSystem();
    pspref.setFalseOriginAndUnits(-10000, -10000, 100000);
    // Set the false origin and units.
    // The XYUnits value is equivalent to the precision specified when creating a feature class
    gonColl.setSpatialReferenceByRef(pspref);

    // *********************************************************
    // Initialize things
    for (int i = 0; i <= 3; i++)
    {
      line[i] = new Line();
      pt[i] = new Point();
      segment[i] = line[i];
    }
    // Put the coordinates of the points
    pt[0].putCoords(0, 0);
    pt[1].putCoords(10, 0);
    pt[2].putCoords(10, 10);
    pt[3].putCoords(0, 10);
    // Put the coordinates of the line
    line[0].putCoords(pt[0], pt[1]);
    line[1].putCoords(pt[1], pt[2]);
    line[2].putCoords(pt[2], pt[3]);
    line[3].putCoords(pt[3], pt[0]);
    GeometryEnvironment gBridge = new GeometryEnvironment();
    // Add the segments in the polygon via the ISegmentCollection
    gBridge.addSegments(gonColl, segment);
    // You can draw or store the polygon (gonColl)
  }

  /**
   * Convert an envelope to a polygon
   */
  private void CreateRectanglePolygonFromEnvelope() throws Exception
  {
    Envelope pEnvelope = new Envelope();

    // *********************************************************
    // THE SPATIAL REFERENCE SHOULD BE SET HERE ON THE ENVELOPE
    // Here the spatial reference is created in memory but could also come from various sources:
    // IMap, IGeodataset, IGeometry etc...
    UnknownCoordinateSystem pspref = new UnknownCoordinateSystem();

    // Set the false origin and units.
    // The XYUnits value is equivalent to the precision specified when creating a feature class
    pspref.setFalseOriginAndUnits(-10000, -10000, 100000);

    pEnvelope.setSpatialReferenceByRef(pspref);

    // *********************************************************
    pEnvelope.putCoords(0, 0, 100, 100);
    Polygon segmentColl = new Polygon();
    segmentColl.setRectangle(pEnvelope);
    // This is transferring the spatial reference
    // You can draw or store the polygon (segmentColl)
  }

}