arcgissamples\geometry\ConvertShapeToString.java—ArcObjects 10.4 Help for Java | ArcGIS for Desktop
Convert shape to string
arcgissamples\geometry\ConvertShapeToString.java
/* Copyright 2015 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 at <your ArcGIS install location>/DeveloperKit10.4/userestrictions.txt.
* 
*/
package arcgissamples.geometry;

import java.io.File;
import java.io.IOException;

import com.esri.arcgis.datasourcesfile.ShapefileWorkspaceFactory;
import com.esri.arcgis.geodatabase.FeatureClass;
import com.esri.arcgis.geodatabase.IFeature;
import com.esri.arcgis.geodatabase.Workspace;
import com.esri.arcgis.geometry.IGeometry;
import com.esri.arcgis.geometry.IPointCollection;
import com.esri.arcgis.geometry.Multipoint;
import com.esri.arcgis.geometry.Point;
import com.esri.arcgis.geometry.Polygon;
import com.esri.arcgis.geometry.Polyline;
import com.esri.arcgis.geometry.esriGeometryType;
import com.esri.arcgis.interop.AutomationException;
import com.esri.arcgis.system.AoInitialize;
import com.esri.arcgis.system.EngineInitializer;
import com.esri.arcgis.system.esriLicenseProductCode;
import com.esri.arcgis.system.esriLicenseStatus;

public class ConvertShapeToString
{
  /*
   * This method summarizes much of the information held in a Geometry...
   */
  public static String getStringFromShape(IGeometry geometry) throws AutomationException, IOException
  {
    String stringRepresentation = "\tShape: ";
    if (geometry == null)
    {
      stringRepresentation += "null";
      return stringRepresentation;
    }
    if (geometry.isEmpty())
    {
      stringRepresentation += "Empty";
      return stringRepresentation;
    }
    switch (geometry.getGeometryType())
    {
    case esriGeometryType.esriGeometryPoint:
      // For a Point, simply report the attribute awareness and coordinates.
      stringRepresentation += "Point";
      Point point = (Point) geometry;
      if (point.isZAware())
        stringRepresentation += "Z";
      if (point.isMAware())
        stringRepresentation += "M";
      if (point.isPointIDAware())
        stringRepresentation += " +IDs";

      stringRepresentation += "\n" + pointAsString(point);
      break;
    case esriGeometryType.esriGeometryMultipoint:
      // For a Multipoint, report attribute awareness and coordinates of each point.
      stringRepresentation += "MultiPoint";
      Multipoint multipt = (Multipoint) geometry;
      if (multipt.isZAware())
        stringRepresentation += "Z";
      if (multipt.isMAware())
        stringRepresentation += "M";
      if (multipt.isPointIDAware())
        stringRepresentation += " +IDs";
      stringRepresentation += "\n" + pointCollAsString(multipt);
      break;
    case esriGeometryType.esriGeometryPolyline:
      // For a Polyline, report attribute awareness, the number of parts,
      // and the number of vertices and coordinates of each vertex in each part.
      stringRepresentation += "PolyLine";
      Polyline polyline = (Polyline) geometry; // A regular cast does not work for a polyline shapefile
      if (polyline.isZAware())
        stringRepresentation += "Z";
      if (polyline.isMAware())
        stringRepresentation += "M";
      if (polyline.isPointIDAware())
        stringRepresentation += " +IDs";

      int polylineGeometryCount = polyline.getGeometryCount();
      stringRepresentation += "\n\tGeometries: " + polylineGeometryCount + "\n";

      for (int i = 0; i < polylineGeometryCount; i++)
      {
        stringRepresentation += pointCollAsString(polyline) + "\n";
        if (i >= 9)
        {
          break; // 10 is enough
        }
      }
      break;
    case esriGeometryType.esriGeometryPolygon:
      // For a Polygon, report attribute awareness, the number of parts,
      // and the number of vertices and coordinates of each vertex in each part.
      stringRepresentation += "Polygon";
      Polygon polygon = (Polygon) geometry; // A regular cast does not work for a polyline shapefile
      if (polygon.isZAware())
        stringRepresentation += "Z";
      if (polygon.isMAware())
        stringRepresentation += "M";
      if (polygon.isPointIDAware())
        stringRepresentation += " +IDs";

      int polygonGeometryCount = polygon.getGeometryCount();
      stringRepresentation += "\n\tGeometries: " + polygonGeometryCount + "\n";

      for (int i = 0; i < polygonGeometryCount; i++)
      {
        stringRepresentation += pointCollAsString(polygon) + "\n";
        if (i >= 9)
        {
          break; // 10 is enough
        }
      }
      break;
    default:
      stringRepresentation += "This geometry type is not yet handled by this sample.";
      break;
    }
    return stringRepresentation;
  }

  /*
   * This method creates a string of infomation for a PointCollection.
   */
  private static String pointCollAsString(IPointCollection pointCollection) throws IOException
  {
    String str = "";

    int count = pointCollection.getPointCount();
    str = "\tPoints: " + count + "\n";
    for (int i = 0; i < count; i++)
    {
      Point pt = (Point) pointCollection.getPoint(i);
      str += "\t\t" + pointAsString(pt) + "\n";
      if (i >= 9)
      {
        break; // 10 is enough
      }
    }
    return str;
  }

  /*
   * This method creates a string of infomation for a single Point
   * @param geometry
   * @return String
   */
  private static String pointAsString(Point point) throws IOException
  {
    String str = "";

    str = "(" + point.getX() + ", " + point.getY();
    if (point.isZAware())
      str += ", " + point.getZ();
    if (point.isMAware())
      str += point.getM();
    if (point.isPointIDAware())
      str += point.getID();
    str += ")";

    return str;
  }

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

      AoInitialize aoInit = new AoInitialize();
      initializeArcGISLicenses(aoInit);

      //Get DEVKITHOME Home
      String devKitHome = System.getenv("AGSDEVKITJAVA");

      // Change the following lines if you want to use different data
      String workspacePath = devKitHome + "java" + File.separator + "samples" + File.separator + "data" +
                             File.separator + "usa";
      
      String featureClassName = "states.shp";
      File shapefileFile = new File(workspacePath, featureClassName);
      if (!shapefileFile.exists())
      {
        System.out.println("Shapefile " + shapefileFile.getAbsolutePath() + " doesn't exist.  Exiting.");
        System.exit(-1);
      }
      System.out.println("Shapefile: " + shapefileFile.getAbsolutePath());

      ShapefileWorkspaceFactory shapefileWorkspaceFactory = new ShapefileWorkspaceFactory();
      Workspace workspace = new Workspace(shapefileWorkspaceFactory.openFromFile(workspacePath, 0));
      FeatureClass featureClass = new FeatureClass(workspace.openFeatureClass(featureClassName));
      int numFeatures = featureClass.featureCount(null);
      System.out.println("There are " + numFeatures + " features:");

      for (int i = 0; i < numFeatures; i++)
      {
        IFeature feature = featureClass.getFeature(i);
        IGeometry geometry = feature.getShapeCopy();
        System.out.println("Feature " + i + ": ");
        System.out.println(getStringFromShape(geometry));
        if (i >= 9)
        {
          break; // 10 is enough
        }
      }
      
      aoInit.shutdown();
      System.out.println("Sample finished.");
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }
  
  /**
   * 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.esriLicenseProductCodeBasic) == esriLicenseStatus.esriLicenseAvailable)
      {
        aoInit.initialize(esriLicenseProductCode.esriLicenseProductCodeBasic);
      }
      else
      {
        System.err.println("Could not initialize an Engine or Basic License. Exiting application.");
        System.exit(-1);
      }
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
  }  
}