Create valid polylines
arcgissamples\geometry\CreateValidPolylines.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.GeometryEnvironment;
import com.esri.arcgis.geometry.Line;
import com.esri.arcgis.geometry.Path;
import com.esri.arcgis.geometry.Point;
import com.esri.arcgis.geometry.Polyline;
import com.esri.arcgis.geometry.UnknownCoordinateSystem;
import com.esri.arcgis.system.AoInitialize;
import com.esri.arcgis.system.EngineInitializer;
import com.esri.arcgis.system.esriLicenseProductCode;
import com.esri.arcgis.system.esriLicenseStatus;

/**
 * The following demonstrates how to create valid polylines efficiently
 */
public class CreateValidPolylines
{

  public CreateValidPolylines()
  {
    
  }
  
  public static void main(String[] args)
  {
    System.out.println("Starting CreateValidPolylines - An ArcObjects Java SDK Developer Sample");
    try
    {
      // Initialize the engine and licenses.
      EngineInitializer.initializeEngine();

      AoInitialize aoInit = new AoInitialize();
      initializeArcGISLicenses(aoInit);
      
      CreateValidPolylines polylines = new CreateValidPolylines();
      polylines.createMultipartPolylinePathSegmentCollection();
      polylines.createMultipartPolylinePathPointCollection();

      aoInit.shutdown();
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
  }  

  /**
   * creates multi part polyline path from segment collection
   * @throws Exception
   */
  private void createMultipartPolylinePathSegmentCollection() throws Exception
  {
    Point[] pointsPath0 = new Point[2];
    Point[] pointsPath1 = new Point[2];
    Point[] pointsPath2 = new Point[2];
    Point[] pointsPath3 = new Point[2];
    Point[] pointsPath4 = new Point[2];
    Point[] pointsPath5 = new Point[2];
    Point[] pointsPath6 = new Point[2];
    Path[] pPathColl = new Path[7];
    Line pLine0, pLine1, pLine2, pLine3, pLine4, pLine5, pLine6;

    // Create a new polyline
    Polyline pPolColl = new Polyline();

    // *********************************************************
    // THE SPATIAL REFERENCE SHOULD BE SET HERE ON THE POLYLINE
    // 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);
    pPolColl.setSpatialReferenceByRef(pspref);

    // *********************************************************
    // Initialize points
    for (int i = 0; i <= 1; i++)
    {
      pointsPath0[i] = new Point();
      pointsPath1[i] = new Point();
      pointsPath2[i] = new Point();
      pointsPath3[i] = new Point();
      pointsPath4[i] = new Point();
      pointsPath5[i] = new Point();
      pointsPath6[i] = new Point();
    }
    // Initialize path
    for (int i = 0; i <= 6; i++)
    {
      pPathColl[i] = new Path();
    }

    // Initialize lines, putcoords of points and add each line to a separate path
    pLine0 = new Line();
    pointsPath0[0].putCoords(100, 100);
    pointsPath0[1].putCoords(100, 150);
    pLine0.putCoords(pointsPath0[0], pointsPath0[1]);
    pPathColl[0].addSegment(pLine0, null, null);
    pLine1 = new Line();
    pointsPath1[0].putCoords(100, 150);
    pointsPath1[1].putCoords(100, 250);
    pLine1.putCoords(pointsPath1[0], pointsPath1[1]);
    pPathColl[1].addSegment(pLine1, null, null);
    pLine2 = new Line();
    pointsPath2[0].putCoords(100, 250);
    pointsPath2[1].putCoords(75, 300);
    pLine2.putCoords(pointsPath2[0], pointsPath2[1]);
    pPathColl[2].addSegment(pLine2, null, null);
    pLine3 = new Line();
    pointsPath3[0].putCoords(100, 250);
    pointsPath3[1].putCoords(150, 300);
    pLine3.putCoords(pointsPath3[0], pointsPath3[1]);
    pPathColl[3].addSegment(pLine3, null, null);
    pLine4 = new Line();
    pointsPath4[0].putCoords(100, 150);
    pointsPath4[1].putCoords(100, 200);
    pLine4.putCoords(pointsPath4[0], pointsPath4[1]);
    pPathColl[4].addSegment(pLine4, null, null);
    pLine5 = new Line();
    pointsPath5[0].putCoords(100, 200);
    pointsPath5[1].putCoords(150, 250);
    pLine5.putCoords(pointsPath5[0], pointsPath5[1]);
    pPathColl[5].addSegment(pLine5, null, null);
    pLine6 = new Line();
    pointsPath6[0].putCoords(100, 200);
    pointsPath6[1].putCoords(150, 175);
    pLine6.putCoords(pointsPath6[0], pointsPath6[1]);
    pPathColl[6].addSegment(pLine6, null, null);
    // Add all the paths to the polyline using AddGeometries method of GeometryEnvironment
    GeometryEnvironment gBridge = new GeometryEnvironment();
    gBridge.addGeometries(pPolColl, pPathColl);
    // You can draw, store or use the polygon (pPolColl) in other geometry operations at this point

    System.out.println("Polyline length =" + pPolColl.getLength());
  }

  /**
   * creates multi part polyline path from point collection
   * @throws Exception
   */
  private void createMultipartPolylinePathPointCollection() throws Exception
  {
    Point[] pointsPath0 = new Point[2];
    Point[] pointsPath1 = new Point[2];
    Point[] pointsPath2 = new Point[2];
    Point[] pointsPath3 = new Point[2];
    Point[] pointsPath4 = new Point[2];
    Point[] pointsPath5 = new Point[2];
    Point[] pointsPath6 = new Point[2];
    Path pathColl[] = new Path[7];
    Polyline polColl = new Polyline();
    // *********************************************************
    // THE SPATIAL REFERENCE SHOULD BE SET HERE ON THE POLYLINE
    // 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);
    polColl.setSpatialReferenceByRef(pspref);

    // *********************************************************
    // Initialize points
    for (int i = 0; i <= 1; i++)
    {
      pointsPath0[i] = new Point();
      pointsPath1[i] = new Point();
      pointsPath2[i] = new Point();
      pointsPath3[i] = new Point();
      pointsPath4[i] = new Point();
      pointsPath5[i] = new Point();
      pointsPath6[i] = new Point();
    }
    // Initialize path
    for (int i = 0; i <= 6; i++)
    {
      pathColl[i] = new Path();

    }
    // Putcoords on the points
    pointsPath0[0].putCoords(100, 100);
    pointsPath0[1].putCoords(100, 150);
    pointsPath1[0].putCoords(100, 150);
    pointsPath1[1].putCoords(100, 250);
    pointsPath2[0].putCoords(100, 250);
    pointsPath2[1].putCoords(75, 300);
    pointsPath3[0].putCoords(100, 250);
    pointsPath3[1].putCoords(150, 300);
    pointsPath4[0].putCoords(100, 150);
    pointsPath4[1].putCoords(100, 200);
    pointsPath5[0].putCoords(100, 200);
    pointsPath5[1].putCoords(150, 250);
    pointsPath6[0].putCoords(100, 200);
    pointsPath6[1].putCoords(150, 175);

    // Add points to separate paths
    GeometryEnvironment gBridge = new GeometryEnvironment();
    gBridge.addPoints(pathColl[0], pointsPath0);
    gBridge.addPoints(pathColl[1], pointsPath1);
    gBridge.addPoints(pathColl[2], pointsPath2);
    gBridge.addPoints(pathColl[3], pointsPath3);
    gBridge.addPoints(pathColl[4], pointsPath4);
    gBridge.addPoints(pathColl[5], pointsPath5);
    gBridge.addPoints(pathColl[6], pointsPath6);

    // Add all the paths to the polyline using AddGeometries method
    gBridge.addGeometries(polColl, pathColl);
    // You can draw, store or use the polygon (polColl) in other geometry operations at this point

    System.out.println("Polyline length =" + polColl.getLength());
  }

  /**
   * 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();
    }
  }

}