ShapeFileの出力


まあ、サンプル通りだけど、この程度のコードならばジオプロに頼りたくない…。
不安定=ジオプロなイメージが払拭できない。

using ESRI.ArcGIS.ADF;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using System;

public class ShapeExporter
{
  public void Export(IFeatureClass srcFc, string shpFileFullPath)
  {
    using (ComReleaser com = new ComReleaser())
    {
      IWorkspaceName dstWorkspaceName = new WorkspaceNameClass();
      com.ManageLifetime(dstWorkspaceName);
      dstWorkspaceName.WorkspaceFactoryProgID = "esriDataSourcesFile.ShapefileWorkspaceFactory";
      dstWorkspaceName.PathName = System.IO.Path.GetDirectoryName(shpFileFullPath);

      IName dstWorkspaceIName = (IName)dstWorkspaceName;

      IDatasetName dstDatasetName = new FeatureClassNameClass();
      com.ManageLifetime(dstDatasetName);

      dstDatasetName.Name = System.IO.Path.GetFileNameWithoutExtension(shpFileFullPath);
      dstDatasetName.WorkspaceName = dstWorkspaceName;

      IFeatureClassName srcName = ((IDataset)srcFc).FullName as IFeatureClassName;
      // Find the shape field.
      String shapeFieldName = srcFc.ShapeFieldName;
      int shapeFieldIndex = srcFc.FindField(shapeFieldName);
      IField shapeField = srcFc.Fields.get_Field(shapeFieldIndex);

      // Get the geometry definition from the shape field and clone it.
      IGeometryDef geometryDef = shapeField.GeometryDef;

      IGeometryDef dstGeometryDef = ((IClone)geometryDef).Clone() as IGeometryDef;
      com.ManageLifetime(dstGeometryDef);

      // Create the objects and references necessary for field validation.
      IFieldChecker fieldChecker = new FieldCheckerClass();
      com.ManageLifetime(fieldChecker);

      IFields sourceFields = srcFc.Fields;
      IFields dstFields = null;
      IEnumFieldError enumFieldError = null;

      IWorkspace dstWorkspace = (IWorkspace)dstWorkspaceIName.Open();
      com.ManageLifetime(dstWorkspace);

      // Set the required properties for the IFieldChecker interface.
      fieldChecker.InputWorkspace = ((IDataset)srcFc).Workspace;
      fieldChecker.ValidateWorkspace = dstWorkspace;

      // Validate the fields and check for errors.
      fieldChecker.Validate(sourceFields, out enumFieldError, out dstFields);
      com.ManageLifetime(enumFieldError);
      com.ManageLifetime(dstFields);

      enumFieldError.Reset();
      IFieldError ferr = null;
      while ((ferr = enumFieldError.Next()) != null)
      {
        com.ManageLifetime(ferr);
        string msg = string.Format("フィールド変換:{0} , {1}", ferr.FieldIndex, ferr.FieldError.ToString("G"));
        Console.WriteLine(msg);
      }//end loop

      IFeatureDataConverter dataConverter = new FeatureDataConverterClass();
      com.ManageLifetime(dataConverter);

      IQueryFilter qFilter = null;
      /*
      new QueryFilterClass();
      com.ManageLifetime(qFilter);
      qFilter.WhereClause = query;
      */

      IEnumInvalidObject enumInv = dataConverter.ConvertFeatureClass
        (
          srcName,
          qFilter,
          null,
          (IFeatureClassName)dstDatasetName,
          dstGeometryDef,
          dstFields,
          "",
          1000,
          0
        );
      com.ManageLifetime(enumInv);

      enumInv.Reset();
      IInvalidObjectInfo objInfo = null;
      while ((objInfo = enumInv.Next()) != null)
      {
        Console.WriteLine("OID:{0}, ERR:{1}",
          objInfo.InvalidObjectID, objInfo.ErrorDescription);
      }//end loop
    }//end com
  }//end method

}//end class
カテゴリー: 開発 タグ: パーマリンク