C#ジオプロとインメモリ


IGPUtilities2でメモリに保持するだけ。
しょうがない時で件数読めるとき用かな。

using System;

using AnalysisTools = ESRI.ArcGIS.AnalysisTools;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Geoprocessing;
using ESRI.ArcGIS.Geoprocessor;
using ESRI.ArcGIS.ADF;

class gptest
{
  public static void Test()
  {
    Geoprocessor gp = new Geoprocessor();

    Type t = Type.GetTypeFromProgID("esriGeoprocessing.GPUtilities");//ProgId
    var gpUtil = (IGPUtilities2)Activator.CreateInstance(t);
    using (ComReleaser com = new ComReleaser())
    {
      //メモリワークスペースの取得
      var inmemWorkspace = gpUtil.GetInMemoryWorkspace();
      com.ManageLifetime(inmemWorkspace);
      //フィーチャクラス作成
      var fc = testCreate((IFeatureWorkspace)inmemWorkspace, "TEST1");
      com.ManageLifetime(fc);

      var fb = fc.CreateFeatureBuffer();
      com.ManageLifetime(fb);
      var insCurosr = fc.Insert(true);
      com.ManageLifetime(insCurosr);
      IPoint pt = new PointClass();
      pt.PutCoords(0, 0);

      fb.Shape = pt;
      insCurosr.InsertFeature(fb);

      // Create the tool process object.
      AnalysisTools.Buffer bufferTool = new AnalysisTools.Buffer();

      // Set parameter values.
      bufferTool.in_features = @"in_memory\TEST1";
      bufferTool.out_feature_class = @"in_memory\TEST_OUT";
      bufferTool.buffer_distance_or_field = "100";

      // Execute the tool. 
      var gpResult = gp.Execute(bufferTool, null);
      com.ManageLifetime(gpResult);

      var outFc = ((IFeatureWorkspace)inmemWorkspace).OpenFeatureClass("TEST_OUT");
      var geodataset = (IGeoDataset)outFc;

      Console.WriteLine(
          "NAME:{0}" + Environment.NewLine +
          "RECORD:{1}" + Environment.NewLine +
          "EXTENT:{2}",
          outFc.AliasName,
          outFc.FeatureCount(null),
          string.Format("{0} ,{1} ,{2} ,{3}", geodataset.Extent.XMin, geodataset.Extent.YMin, geodataset.Extent.XMax, geodataset.Extent.YMax)
      );

    }//end com
  }//end method


  private static IFeatureClass testCreate(IFeatureWorkspace fw, string name)
  {
    using (ComReleaser com = new ComReleaser())
    {
      // Create a fields collection for the feature class.
      IFields fields = new FieldsClass();
      com.ManageLifetime(fields);

      IFieldsEdit fieldsEdit = (IFieldsEdit)fields;

      // Add an object ID field to the fields collection. This is mandatory for feature classes.
      IField oidField = new FieldClass();
      com.ManageLifetime(oidField);

      IFieldEdit oidFieldEdit = (IFieldEdit)oidField;
      oidFieldEdit.Name_2 = "OID";
      oidFieldEdit.Type_2 = esriFieldType.esriFieldTypeOID;
      fieldsEdit.AddField(oidField);

      // Create a geometry definition (and spatial reference) for the feature class.
      IGeometryDef geometryDef = new GeometryDefClass();
      com.ManageLifetime(geometryDef);

      IGeometryDefEdit geometryDefEdit = (IGeometryDefEdit)geometryDef;
      geometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint;

      Type t = Type.GetTypeFromProgID("esriGeometry.SpatialReferenceEnvironment");
      ISpatialReferenceFactory spatialReferenceFactory = (ISpatialReferenceFactory)Activator.CreateInstance(t); // new SpatialReferenceEnvironmentClass();
      com.ManageLifetime(spatialReferenceFactory);

      ISpatialReference spatialReference = spatialReferenceFactory.CreateProjectedCoordinateSystem((int)esriSRProjCS2Type.esriSRProjCS_WGS1984SphereWebMercator);
      com.ManageLifetime(spatialReference);

      ISpatialReferenceResolution spatialReferenceResolution = (ISpatialReferenceResolution)spatialReference;
      spatialReferenceResolution.ConstructFromHorizon();
      ISpatialReferenceTolerance spatialReferenceTolerance = (ISpatialReferenceTolerance)spatialReference;
      spatialReferenceTolerance.SetDefaultXYTolerance();
      geometryDefEdit.SpatialReference_2 = spatialReference;

      // Add a geometry field to the fields collection. This is where the geometry definition is applied.
      IField geometryField = new FieldClass();
      com.ManageLifetime(geometryField);

      IFieldEdit geometryFieldEdit = (IFieldEdit)geometryField;
      geometryFieldEdit.Name_2 = "Shape";
      geometryFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
      geometryFieldEdit.GeometryDef_2 = geometryDef;
      fieldsEdit.AddField(geometryField);

      // Create a text field called "Name" for the fields collection.
      IField nameField = new FieldClass();
      com.ManageLifetime(nameField);

      IFieldEdit nameFieldEdit = (IFieldEdit)nameField;
      nameFieldEdit.Name_2 = "Name";
      nameFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
      nameFieldEdit.Length_2 = 20;
      fieldsEdit.AddField(nameField);

      // Use IFieldChecker to create a validated fields collection.
      IFieldChecker fieldChecker = new FieldCheckerClass();
      com.ManageLifetime(fieldChecker);

      IEnumFieldError enumFieldError = null;
      IFields validatedFields = null;
      fieldChecker.ValidateWorkspace = (IWorkspace)fw;
      fieldChecker.Validate(fields, out enumFieldError, out validatedFields);
      com.ManageLifetime(enumFieldError);
      com.ManageLifetime(validatedFields);

      // The enumFieldError enumerator can be inspected at this point to determine 
      // which fields were modified during validation.

      // Create the feature class. Note that the CLSID parameter is null - this indicates to use the
      // default CLSID, esriGeodatabase.Feature (acceptable in most cases for feature classes).
      IFeatureClass featureClass = fw.CreateFeatureClass(name, validatedFields, null, null, esriFeatureType.esriFTSimple, "Shape", "");
      return featureClass;
    }//end com
  }//end method

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