enjoylives的GIS空间

利用ArcGIS Engine求算成本距离

下面写了一个封装了ArcGIS中Cost Distance求算功能的类,这个类的使用方法和ArcGIS Desktop中 Spatial Analyst---Cost Distance的调用方式一致,只需要传递cost surface,
source surface 和输出的栅格文件名。
关键要注意对IRasterAnalysisEnvironment的设置。
class RasterCostDistance
{
    private IRasterLayer costRasterlayer;

    public RasterCostDistance(IRasterLayer costRasterlayer)
    {
        this.costRasterlayer = costRasterlayer;
    }
    public IRasterLayer CalCostDistance(IRasterLayer sourceRasterlayer, string outputFileName)
    {
        if (System.IO.File.Exists(outputFileName))
        {
            System.IO.File.Delete(outputFileName);
        }

        IGeoDataset sourceRaster = sourceRasterlayer.Raster as IGeoDataset;

        IGeoDataset costRaster = costRasterlayer.Raster as IGeoDataset;

        IDistanceOp2 distanceOp = new RasterDistanceOpClass();
        IRasterAnalysisEnvironment rasAnaEnv = distanceOp as IRasterAnalysisEnvironment;

        //设置生成图层的工作空间
        IWorkspaceFactory wsf = new RasterWorkspaceFactoryClass();
        string outputPath = System.IO.Path.GetDirectoryName(outputFileName);
        string fileName = System.IO.Path.GetFileName(outputFileName);
        IWorkspace ws = wsf.OpenFromFile(outputPath, 0);
        rasAnaEnv.OutWorkspace = ws;

        //设置生成图层的范围
        object extent = (object)costRaster.Extent;
        object missing = System.Reflection.Missing.Value;
        rasAnaEnv.SetExtent(esriRasterEnvSettingEnum.esriRasterEnvValue, ref extent, ref missing);

        //设置生成图层的栅格大小
        rasAnaEnv.SetCellSize(esriRasterEnvSettingEnum.esriRasterEnvMinOf, ref cellsize);
        //进行成本距离的计算 
        object maxDistance = System.Reflection.Missing.Value;
        object valueRaster = System.Reflection.Missing.Value;
        IGeoDataset outputDataset = distanceOp.CostDistance(sourceRaster, costRaster, ref maxDistance, ref valueRaster);
        //生成的IRaster是保存在内存中的数据,要保存在硬盘文件中还应使用ISaveas
        ISaveAs pSaveAs = outputDataset as ISaveAs;
        pSaveAs.SaveAs(fileName, ws, "IMAGINE Image");
        IRasterLayer pCreatRalyr = new RasterLayerClass();
        pCreatRalyr.CreateFromFilePath(outputFileName);

        return pCreatRalyr;
    }
}

posted on 2010-01-13 16:28  enjoylives  阅读(1342)  评论(0编辑  收藏  举报

导航