arcgissamples\geodatabase\simplepointdatasource\SimplePointDatasetHelper.java—ArcObjects 10.4 Help for Java | ArcGIS for Desktop
Point plug-in data source
arcgissamples\geodatabase\simplepointdatasource\SimplePointDatasetHelper.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.geodatabase.simplepointdatasource;

import java.io.File;
import java.io.IOException;
import com.esri.arcgis.geodatabase.FeatureClassDescription;
import com.esri.arcgis.geodatabase.Field;
import com.esri.arcgis.geodatabase.IField;
import com.esri.arcgis.geodatabase.IFieldEdit2;
import com.esri.arcgis.geodatabase.IFields;
import com.esri.arcgis.geodatabase.IFieldsEdit;
import com.esri.arcgis.geodatabase.IGeometryDefEdit;
import com.esri.arcgis.geodatabase.IObjectClassDescription;
import com.esri.arcgis.geodatabase.IPlugInCursorHelper;
import com.esri.arcgis.geodatabase.IPlugInDatasetHelper;
import com.esri.arcgis.geodatabase.IPlugInDatasetInfo;
import com.esri.arcgis.geodatabase.esriDatasetType;
import com.esri.arcgis.geodatabase.esriFieldType;
import com.esri.arcgis.geometry.Envelope;
import com.esri.arcgis.geometry.IEnvelope;
import com.esri.arcgis.geometry.IGeometry;
import com.esri.arcgis.geometry.IPoint;
import com.esri.arcgis.geometry.ISpatialReference;
import com.esri.arcgis.geometry.ISpatialReferenceFactory;
import com.esri.arcgis.geometry.Point;
import com.esri.arcgis.geometry.SpatialReferenceEnvironment;
import com.esri.arcgis.geometry.esriGeometryType;
import com.esri.arcgis.geometry.esriSRProjCSType;
import com.esri.arcgis.interop.AutomationException;
import com.esri.arcgis.interop.extn.ArcGISExtension;
import com.esri.arcgis.system.IClone;

@ArcGISExtension
public class SimplePointDatasetHelper implements IPlugInDatasetHelper, IPlugInDatasetInfo{

  /**
   * The name of the dataset in the workspace.
   */
  String datasetName;

  String workPathName;

  /**
   * The extent of the dataset.
   */
  IEnvelope bounds;

  /**
   * The full path of the dataset in the file system.
   */
  String fullPath;

  /**
   * This flag is used for debugging purpose
   */
  private boolean blnDebug = true;

  /**
   * The spatial reference of the dataset.
   */
  private ISpatialReference dataSpatial;

  public SimplePointDatasetHelper(){

  }

  /**
   * Creates a new dataset helper.
   */
  public SimplePointDatasetHelper(String pathName, String datasetName){
    this.datasetName = datasetName;
    this.workPathName = pathName;
    this.fullPath = pathName + File.separator + datasetName + SimplePointWorkspaceFactoryHelper.fileExtension;
  }

  /**
   * Gets all the records in the dataset. If an error occurs or an invalid parameter is provided,
   * a null value will be returned.
   */
  public IPlugInCursorHelper fetchAll(int arg0, String arg1, Object arg2) throws IOException, AutomationException {
    try {
      // Return null if a field map is not an array of integers.
      IFields fields = this.getFields(0);
      if (blnDebug)
        System.out.println("debug dataset 1: "+arg2);
      if (arg2 == null)
        return null;
      IPlugInCursorHelper pluginCurHelper = new SimplePointCursorHelper(this.fullPath,fields, -1, (int[]) arg2, null);
      return pluginCurHelper;
    } catch (Exception ex){
      ex.printStackTrace();
      return null;
    }
  }

  /**
   * Gets all of the records in the specified envelope, or all records if the envelope is null.
   */
  public IPlugInCursorHelper fetchByEnvelope(int arg0, IEnvelope arg1,boolean arg2, String arg3, Object arg4) throws IOException,AutomationException {
    try {
      IEnvelope boundEnv = this.getBounds();
      boundEnv.project(arg1.getSpatialReference());
      if (boundEnv.isEmpty())
        return null;
      if (blnDebug)
        System.out.println("debug dataset 2: "+arg2);
      // Get the fields for the specified class and return a cursor helper.
      IFields filelds = this.getFields(arg0);
      IPlugInCursorHelper pluginCurHelper = new SimplePointCursorHelper(this.fullPath,filelds, -1, (int[]) arg4, null);
      return pluginCurHelper;
    } catch (Exception ex){
      ex.printStackTrace();
      return null;
    }
  }

  /**
   * Returns a cursor helper that will fetch a single row by its OID.
   */
  public IPlugInCursorHelper fetchByID(int arg0, int arg1, Object arg2)throws IOException, AutomationException {
    try {
      if (blnDebug)
        System.out.println("debug dataset 3: "+arg1);
      IFields fileds = this.getFields(0);
      IPlugInCursorHelper pluginCurHelper = new SimplePointCursorHelper(this.fullPath,fileds, arg1, (int[]) arg2, null);
      return pluginCurHelper;
    }catch (Exception ex){
      ex.printStackTrace();
      return null;
    }
  }

  /**
   * Returns the extent of the dataset.
   */
  public IEnvelope getBounds() throws IOException, AutomationException {
    try {
      // If the dataset is non-spatial, return null.
      if (this.getDatasetType() == esriDatasetType.esriDTTable)
        return null;
      // If the extent of the dataset has not been previously determined, calculate it now.
      if (this.bounds == null) {
        bounds = new Envelope();
        bounds.setSpatialReferenceByRef(this.getSpatialReference());
        // Get an envelope for the extent of the dataset
        // We will have to calculate the extent by opening a cursor
        // on the dataset, and building up a minimum bounding rectangle

        // Prepare to open a cursor.
            // Make the fieldmap, with values of -1 (attributes do not need fetching)
        IFields fields = this.getFields(0);
        int count = fields.getFieldCount();
        int[] arrayFieldMap = new int[count];
        for (int i = 0; i < count; i++)
          arrayFieldMap[i] =-1;

        // Open the cursor
        IPlugInCursorHelper pluginCurHelper = this.fetchAll(0, "", arrayFieldMap);

        // loop through the data recording min/max X and Y values.
        double dxMin =  9999999;
        double dxMax = -9999999;
        double dyMin =  9999999;
        double dyMax = -9999999;
        double x,y;
        int numRecords = 0;

        IGeometry geometry = new Point();
        geometry.setSpatialReferenceByRef(this.getSpatialReference());
        while (!pluginCurHelper.isFinished()) {
          try {
            // Get the current geometry. If it's null, skip to the next record.
            pluginCurHelper.queryShape(geometry);
            if (geometry == null)
              continue;

            // Increment the record count and compare the point's X and Y values
            // with the known maximum and minimum values.
            numRecords++;
            IPoint point = (IPoint) geometry;
            double px = point.getX();
            double py = point.getY();
            if (px > dxMax)
              dxMax = px;
            if (px < dxMin)
              dxMin = px;
            if (py > dyMax)
              dyMax = py;
            if (py < dyMin)
              dyMin = py;
            // Move to the next record.
            pluginCurHelper.nextRecord();
          } catch (Exception ex){
            break;
          }
        }

        // Handle special case of single point in file
        // - add a small amount, so that we will end up with an envelope rather than a point
        if (numRecords == 1) {
          double dDelta = 0.01;
          if (dxMax != 0)
            dDelta = dxMax / 1000;
            dxMax += dDelta;
          dyMax += dDelta;
        }

        // Create the envelope object, setting the appropriate spatial reference
        this.bounds.putCoords(dxMin, dyMin, dxMax, dyMax);
      }
      // Return a clone of the stored bounds envelope.
      IClone cloneEnv = (IClone)bounds;
      return (IEnvelope)cloneEnv.esri_clone();
    } catch (Exception ex){
      return null;
    }
  }

  /**
   * Returns the number of classes in the dataset (one, in this implementation).
   */
  public int getClassCount() throws IOException, AutomationException {
    return 1;
  }

  /**
   * Returns the index of the named class.
   */
  public int getClassIndex(String arg0) throws IOException,AutomationException {
    return 0;
  }

  /**
   * Returns the name of the class at the specified index.
   */
  public String getClassName(int arg0) throws IOException,AutomationException {
    // There's only one class, so this will always be the local dataset name.
    this.datasetName = "Point";
    return this.datasetName;
  }

  /**
   * Returns the fields collection for the class at the specified index.
   */
  public IFields getFields(int arg0) throws IOException, AutomationException {
    // Create a new feature class description and get its required fields.
    IObjectClassDescription objClassDesc = new FeatureClassDescription();
    IFields fields = objClassDesc.getRequiredFields();
    IFieldEdit2 fieldsEdit = new Field();
    fieldsEdit.setLength(1);
    fieldsEdit.setName("ColumnOne");
    fieldsEdit.setType(esriFieldType.esriFieldTypeString);

    // Add a string column with a length of 1.
    IFieldEdit2 fieldsEdit2 = new Field();
    fieldsEdit2.setName("Extra");
    fieldsEdit2.setType(esriFieldType.esriFieldTypeInteger);
    IFieldsEdit fieldsedit3 = (IFieldsEdit) fields;
    fieldsedit3.addField(fieldsEdit);
    fieldsedit3.addField(fieldsEdit2);

    // Set shape field geometry definition
    if (this.getDatasetType() != esriDatasetType.esriDTTable) {
      IField field = fields.getField(fields.findField("Shape"));
      fieldsEdit = (IFieldEdit2)field;
      IGeometryDefEdit geomDefEdit = (IGeometryDefEdit)field.getGeometryDef();
      geomDefEdit.setGeometryType(geometryTypeByID(arg0));
      geomDefEdit.setSpatialReferenceByRef(this.getSpatialReference());
    }
    return fields;
  }

  /**
   * Returns the index of the OID field for the specified class.
   */
  public int getOIDFieldIndex(int arg0) throws IOException,AutomationException {
    // The OID field is always the first field.
    return 0;
  }

  /**
   * Returns the index of the shape field for the specified class.
   */
  public int getShapeFieldIndex(int arg0) throws IOException,AutomationException {
    // The shape field is always the second field.
    return 1;
  }

  /**
   * Returns the name of the dataset within the workspace.
   */
    public String getLocalDatasetName  () throws java.io.IOException, com.esri.arcgis.interop.AutomationException {
      return this.datasetName;
    }

  /**
   * Returns the dataset type.
   */
    public int getDatasetType  () throws java.io.IOException, com.esri.arcgis.interop.AutomationException{
      return esriDatasetType.esriDTFeatureClass;
    }

  /**
   * Returns the dataset's geometry type.
   */
    public int getGeometryType  () throws java.io.IOException, com.esri.arcgis.interop.AutomationException{
      return esriGeometryType.esriGeometryPoint;
    }

  /**
   * Returns the name of the dataset's shape field.
   */
    public String getShapeFieldName  () throws java.io.IOException, com.esri.arcgis.interop.AutomationException {
      return "Shape";
    }

    /**
     * Interal functions
     */
    private int geometryTypeByID(int ClassIndex)
    {
      try {
      if (this.getDatasetType() == esriDatasetType.esriDTTable)
        return esriGeometryType.esriGeometryNull;

      if (ClassIndex % 3 == 0)
        return esriGeometryType.esriGeometryPoint;
      else if (ClassIndex % 3 == 1)
        return esriGeometryType.esriGeometryPolyline;
      else
        return esriGeometryType.esriGeometryPolygon;
      } catch (Exception ex){
        ex.printStackTrace();
        return  esriGeometryType.esriGeometryPoint;
      }
    }


    private ISpatialReference getSpatialReference() throws IOException,AutomationException {
      if (dataSpatial == null){
        ISpatialReferenceFactory srFactory = new SpatialReferenceEnvironment();
        dataSpatial = srFactory.createProjectedCoordinateSystem(esriSRProjCSType.esriSRProjCS_World_Robinson);
      }
      return dataSpatial;
    }
}