基于ArcEngine由散点生成featureclass,再生成tin

1 篇文章 0 订阅
private void tinUDX2TinToolStripMenuItem_Click(object sender, EventArgs e)
         {
              //1、提取散点
              //2、先由散点创建FeatureClass
              //3、由FeatureClass创建TIN
              IFeatureClass pFeatureClass = GetFeatureCLass(@"D:\Xge.UDX.ExchangeProjection\DATA\TinTest.xml");
              IField pField = pFeatureClass.Fields.get_Field(pFeatureClass.FindField("elevation"));
              ITin pTin = CreateTin(pFeatureClass, pField, @"D:\Xge.UDX.ExchangeProjection\DATA\TinFromUDX");
              ITinLayer pTinLayer = new TinLayerClass();
             pTinLayer.Dataset = pTin;
             axMapControl1.Map.AddLayer(pTinLayer as ILayer);
         }

                           //获取散点,生成FeatureClass
          public static IFeatureClass GetFeatureCLass(string UDXpath)
          {
              IWorkspaceFactory pWorkspaceFactory = new InMemoryWorkspaceFactoryClass();
              IWorkspaceName pWorkspaceName = pWorkspaceFactory.Create("", "pWorkspace", null, 0);
              IName pName = (IName)pWorkspaceName;
              IWorkspace pWorkspace = (IWorkspace)pName.Open();
              IFeatureWorkspace pFeatureWorkspace = pWorkspace as IFeatureWorkspace;
              IFields pFields = new FieldsClass();
             IFieldsEdit pFieldsEdit = pFields as IFieldsEdit;
             IField pField = new FieldClass();
             IFieldEdit pFieldEdit = pField as IFieldEdit;
             pFieldEdit.Name_2 = "SHAPE";
             pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
             IGeometryDef pGeometryDef = new GeometryDefClass();
             IGeometryDefEdit pGeometryDefEdit = pGeometryDef as IGeometryDefEdit;
             pGeometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint;
             //为FeatureClass赋参考系,不写会出错***************************************
             ISpatialReferenceFactory pSpatialReferenceFactory = new SpatialReferenceEnvironmentClass();
             ISpatialReference pSpatialReference = pSpatialReferenceFactory.CreateGeographicCoordinateSystem((int)esriSRGeoCSType.esriSRGeoCS_WGS1984);
             pGeometryDefEdit.SpatialReference_2 = pSpatialReference;
             //************************************************************************
             pFieldEdit.GeometryDef_2 = pGeometryDef;
             pFieldsEdit.AddField(pField);
             pField = new FieldClass();//不要省略写!容易出问题
             pFieldEdit = pField as IFieldEdit;
             pFieldEdit.AliasName_2 = "高程";
             pFieldEdit.Name_2 = "elevation";
             pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble;
             pFieldsEdit.AddField(pField);
             IFeatureClass pFeatureClass = pFeatureWorkspace.CreateFeatureClass("TIN", pFields, null, null, esriFeatureType.esriFTSimple, "Shape", "");
 
             //从UDX中获取散点值
             Dictionary<int, IPoint> pointDictionary = new Dictionary<int, IPoint>();
             WriteIn wi = new WriteIn(UDXpath, "TIN");
             DTDataset sourceDT = wi.GetUDXFormXML(wi.filePath, wi.DTDatasetName);
             List<DTNode> vertexList = new List<DTNode>();
             vertexList.AddRange(SelectNodeClass.SelectNodes(sourceDT, "Vertex"));
             for (int i = 0; i < vertexList.Count;i++ )
             {
                 IPoint pPoint=new PointClass();
                 pPoint.X = double.Parse(vertexList[i].Kernel.Value.ToString().Split(',')[0]);
                 pPoint.Y = double.Parse(vertexList[i].Kernel.Value.ToString().Split(',')[1]);
                 pPoint.Z = double.Parse(vertexList[i].Kernel.Value.ToString().Split(',')[2]);
                 if (pPoint.Z.ToString() != "非数字")
                 {
                     pointDictionary.Add(i, pPoint);
                 }
             }
 
             //插入到新建的FeatureClass中
             IWorkspaceEdit pWorkspaceEdit = pWorkspace as IWorkspaceEdit;
             pWorkspaceEdit.StartEditing(true);
                 pWorkspaceEdit.StartEditOperation();
             IFeatureBuffer pFeatureBuffer = pFeatureClass.CreateFeatureBuffer();
             IFeatureCursor pFeatureCursor = pFeatureClass.Insert(true);
             for (int featureNum = 4; featureNum < pointDictionary.Count;featureNum++ )
            {
                 pFeatureBuffer.Shape = pointDictionary[featureNum] as IPoint;//出错点,在于新建字段的错误
                pFeatureBuffer.set_Value(pFeatureClass.Fields.FindField("elevation"), pointDictionary[featureNum].Z);
                 pFeatureCursor.InsertFeature(pFeatureBuffer);

             }
             pFeatureCursor.Flush();
             pWorkspaceEdit.StopEditOperation();
             pWorkspaceEdit.StopEditing(true);
 
                return pFeatureClass;
         }

            public ITin CreateTin(IFeatureClass pFeatureClass, IField pField, string pPath)
          {
              IGeoDataset pGeoDataset = pFeatureClass as IGeoDataset;
              ITinEdit pTinEdit = new TinClass();
              pTinEdit.InitNew(pGeoDataset.Extent);
              object pObj = Type.Missing;
              pTinEdit.AddFromFeatureClass(pFeatureClass, null, pField, null, esriTinSurfaceType.esriTinMassPoint, ref pObj);
              pTinEdit.SaveAs(pPath, ref pObj);
              pTinEdit.Refresh();
             return pTinEdit as ITin;
         }





出处:http://www.cnblogs.com/zhuyuchen/archive/2013/01/14/2860475.html  原作者:小猪同学


      好东西大家可以很好借鉴,谢谢原作者的分享.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值