C#とPyt連係


ArcPy Toolboxも呼び出せた。
結構便利かも。

C#側

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

class Test
{
  public void testmethod()
  {
    using (ComReleaser com = new ComReleaser())
    {
      IGeoProcessor2 gp = new GeoProcessorClass();
      com.ManageLifetime(gp);

      gp.AddToolbox(@"c:\TEST.pyt");

      IVariantArray parameters = new VarArrayClass();
      com.ManageLifetime(parameters);

      parameters.Add(@"in_memory\TEST");

      var result = gp.Execute("RasterTest", parameters, null);
      com.ManageLifetime(result);

      Type t = Type.GetTypeFromProgID("esriGeoprocessing.GPUtilities");//ProgId
      var gpUtil = (IGPUtilities2)Activator.CreateInstance(t);
      com.ManageLifetime(gpUtil);

      //メモリワークスペースの取得
      var inmemWorkspace = gpUtil.GetInMemoryWorkspace();
      com.ManageLifetime(inmemWorkspace);

      var dsNames = inmemWorkspace.get_DatasetNames(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTRasterDataset);
      com.ManageLifetime(dsNames);

      dsNames.Reset();

      IDatasetName name = null;

      while ((name = dsNames.Next()) != null)
      {
        if (name != null)
          Console.WriteLine(name.Name);
      }
    }//end com

  }//end method
}//end class

Python側

import os.path
import arcpy
import numpy
import math

# Python ツールボックスのツール動作のカスタマイズ
# http://resources.arcgis.com/ja/help/main/10.1/index.html#//00150000002m000000
class Toolbox(object):
    def __init__(self):
        """Define the toolbox (the name of the toolbox is the name of the
        .pyt file)."""
        self.label = "MyToolbox"
        self.alias = ""

        # ツールのリスト設定 ( クラス設定 )
        self.tools = [RasterTest]

# 乱点出力
class RasterTest(object):
    def __init__(self):
        self.label = "テスト"
        self.description = "テスト"
        self.canRunInBackground = False

    def getParameterInfo(self):
        # Python ツールボックスでのパラメータの定義
        # http://resources.arcgis.com/ja/help/main/10.1/index.html#//001500000028000000
        # getParameterの型一覧
        # http://resources.arcgis.com/ja/help/main/10.1/index.html#//001500000035000000
        # パラメータリファレンス
        # http://resources.arcgis.com/ja/help/main/10.1/index.html#//018z00000063000000

        # 出力ラスタ
        param0 = arcpy.Parameter(
        displayName="出力ラスタ",
        name="in_outRaster",
        datatype="GPString",
        parameterType="Required",
        direction="Input")

        return [param0]

    def isLicensed(self):
        """Set whether tool is licensed to execute."""
        return True

    def updateParameters(self, parameters):
        """if parameters[1].valueAsText is None:
            parameters[2].enabled = True
        else:
            parameters[2].enabled = False
        """
        return


    def updateMessages(self, parameters):
        """Modify the messages created by internal validation for each tool
        parameter.  This method is called after internal validation."""
        return

    def execute(self, parameters, messages):
        # Python ツールボックスでのメッセージの書き込み
        # http://resources.arcgis.com/ja/help/main/10.1/index.html#//001500000036000000
        try:
            orgX = 120
            orgY = 20

            width = 1
            height = 1

            colX = 30
            rowY = 30
            outRaster = parameters[0].valueAsText

            # 数量分0埋め配列作って reshapeでXYにする。
            npArray = numpy.zeros(colX * rowY, numpy.int32).reshape(rowY, colX)

            raster = arcpy.NumPyArrayToRaster(npArray,
                                              arcpy.Point(orgX, orgY),
                                              width, height)

            raster.save(outRaster)
        except Exception as e:
            messages.AddErrorMessage(e.message)
        return
カテゴリー: 開発 タグ: , パーマリンク