用心计较般般错 安心自守事事宽

cgl 坚持、努力终有所获
  新随笔  :: 管理

c# 调用ArcEngine的GP工具

Posted on 2012-02-09 22:39  用心计较般般错 安心自守事事宽  阅读(6666)  评论(0编辑  收藏  举报

           IAoInitialize m_AoInitialize = new AoInitializeClass();

            esriLicenseStatus licenseStatus = esriLicenseStatus.esriLicenseUnavailable;

            licenseStatus = m_AoInitialize.Initialize(esriLicenseProductCode.esriLicenseProductCodeArcInfo);

            if (licenseStatus == esriLicenseStatus.esriLicenseCheckedOut)

            {

                Geoprocessor tGp = new Geoprocessor();

                tGp.OverwriteOutput = true;

                licenseStatus = m_AoInitialize.IsExtensionCodeAvailable(esriLicenseProductCode.esriLicenseProductCodeEngine,esriLicenseExtensionCode.esriLicenseExtensionCodeSpatialAnalyst);

                licenseStatus = m_AoInitialize.CheckOutExtension(esriLicenseExtensionCode.esriLicenseExtensionCodeSpatialAnalyst);

                

                //LICenced arcengine 首先是把ASCII文件转换为Raster文件

                ESRI.ArcGIS.ConversionTools.ASCIIToRaster tASC = new ESRI.ArcGIS.ConversionTools.ASCIIToRaster();

                tASC.data_type = "FLOAT";

                tASC.in_ascii_file = pWorkSpacePath + @"a1.txt";

                tASC.out_raster = pWorkSpacePath + "a1.img";

                //ScrollToBottom("ASCIIToRaster");

                IGeoProcessorResult tGeoResult = (IGeoProcessorResult)tGp.Execute(tASC, null);

                if (tGeoResult.Status == ESRI.ArcGIS.esriSystem.esriJobStatus.esriJobSucceeded)

                {

                    // 然后把转好的Raster文件重新分类

                    ESRI.ArcGIS.SpatialAnalystTools.Reclassify tReclass = new ESRI.ArcGIS.SpatialAnalystTools.Reclassify();

                    tReclass.in_raster = pWorkSpacePath + "a1.img";

                    tReclass.missing_values = "NODATA";

                    tReclass.out_raster = pWorkSpacePath + @"Out3.img";

                    tReclass.reclass_field = "VALUE";

                    tReclass.remap = "0 958 10;958 988 20;988 990 30;990 1000 40;1000 1100 50";

                   // ScrollToBottom("Reclassify");

                    tGeoResult = (IGeoProcessorResult)tGp.Execute(tReclass, null);

                    if (tGeoResult.Status == ESRI.ArcGIS.esriSystem.esriJobStatus.esriJobSucceeded)

                    {

                        // 把分类好的Raster文件转换为矢量文件

                        ESRI.ArcGIS.ConversionTools.RasterToPolygon tRTP = new ESRI.ArcGIS.ConversionTools.RasterToPolygon();

                        tRTP.in_raster = pWorkSpacePath + "Out3.img";

                        tRTP.out_polygon_features = pWorkSpacePath + "a2_shp.shp";

                        tRTP.raster_field = "VALUE";

                        tRTP.simplify = "SIMPLIFY";

                        //ScrollToBottom("RasterToPolygon");

                        tGeoResult = (IGeoProcessorResult)tGp.Execute(tRTP, null);

                        if (tGeoResult.Status == ESRI.ArcGIS.esriSystem.esriJobStatus.esriJobSucceeded)

                        {

                            licenseStatus = m_AoInitialize.CheckInExtension(esriLicenseExtensionCode.esriLicenseExtensionCodeSpatialAnalyst);

                            {

                // 把生成的矢量文件进行平滑处理

                                ESRI.ArcGIS.DataManagementTools.SmoothPolygon tSP = new ESRI.ArcGIS.DataManagementTools.SmoothPolygon();

                                tSP.algorithm = "PAEK";

                                tSP.in_features = pWorkSpacePath + "a2_shp.shp";

                                tSP.out_feature_class = pWorkSpacePath + "a2_shp_smooth.shp";

                                tSP.tolerance = "2";

                                tSP.endpoint_option = "FIXED_ENDPOINT";

                                tSP.error_option = "FLAG_ERRORS";

                                ScrollToBottom("SmoothPolygon");

                                tGeoResult = (IGeoProcessorResult)tGp.Execute(tSP, null);

                            }

                        }

                    }

                }

                // arcengine liencesd 当使用高版本的liencesd在使用中,如果需要低版本的liencesd,需要动态签出liencesd,不然将不会执行成功!

               // writeMessage(tGp);

            }

           

            MessageBox.Show("ok");