ArcEngine:由散点(点文本XML)生成TIN(利用内存FeatureClass)

事件触发函数:UDX为某一XML格式文档,有自己开发的底层API,关系不大,这里不谈。

 1 private void tinUDX2TinToolStripMenuItem_Click(object sender, EventArgs e)
 2         {
 3             //1、提取散点
 4             //2、先由散点创建FeatureClass
 5             //3、由FeatureClass创建TIN
 6             IFeatureClass pFeatureClass = GetFeatureCLass(@"D:\Xge.UDX.ExchangeProjection\DATA\TinTest.xml");
 7             IField pField = pFeatureClass.Fields.get_Field(pFeatureClass.FindField("elevation"));
 8             ITin pTin = CreateTin(pFeatureClass, pField, @"D:\Xge.UDX.ExchangeProjection\DATA\TinFromUDX");
 9             ITinLayer pTinLayer = new TinLayerClass();
10             pTinLayer.Dataset = pTin;
11             axMapControl1.Map.AddLayer(pTinLayer as ILayer);
12         }
GetFeatureClassInMemory
 1 //获取散点,生成FeatureClass
 2         public static IFeatureClass GetFeatureCLass(string UDXpath)
 3         {
 4             IWorkspaceFactory pWorkspaceFactory = new InMemoryWorkspaceFactoryClass();
 5             IWorkspaceName pWorkspaceName = pWorkspaceFactory.Create("", "pWorkspace", null, 0);
 6             IName pName = (IName)pWorkspaceName;
 7             IWorkspace pWorkspace = (IWorkspace)pName.Open();
 8             IFeatureWorkspace pFeatureWorkspace = pWorkspace as IFeatureWorkspace;
 9             IFields pFields = new FieldsClass();
10             IFieldsEdit pFieldsEdit = pFields as IFieldsEdit;
11             IField pField = new FieldClass();
12             IFieldEdit pFieldEdit = pField as IFieldEdit;
13             pFieldEdit.Name_2 = "SHAPE";
14             pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
15             IGeometryDef pGeometryDef = new GeometryDefClass();
16             IGeometryDefEdit pGeometryDefEdit = pGeometryDef as IGeometryDefEdit;
17             pGeometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint;
18             //为FeatureClass赋参考系,不写会出错***************************************
19             ISpatialReferenceFactory pSpatialReferenceFactory = new SpatialReferenceEnvironmentClass();
20             ISpatialReference pSpatialReference = pSpatialReferenceFactory.CreateGeographicCoordinateSystem((int)esriSRGeoCSType.esriSRGeoCS_WGS1984);
21             pGeometryDefEdit.SpatialReference_2 = pSpatialReference;
22             //************************************************************************
23             pFieldEdit.GeometryDef_2 = pGeometryDef;
24             pFieldsEdit.AddField(pField);
25             pField = new FieldClass();//不要省略写!容易出问题
26             pFieldEdit = pField as IFieldEdit;
27             pFieldEdit.AliasName_2 = "高程";
28             pFieldEdit.Name_2 = "elevation";
29             pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble;
30             pFieldsEdit.AddField(pField);
31             IFeatureClass pFeatureClass = pFeatureWorkspace.CreateFeatureClass("TIN", pFields, null, null, esriFeatureType.esriFTSimple, "Shape", "");
32 
33             //从UDX中获取散点值
34             Dictionary<int, IPoint> pointDictionary = new Dictionary<int, IPoint>();
35             WriteIn wi = new WriteIn(UDXpath, "TIN");
36             DTDataset sourceDT = wi.GetUDXFormXML(wi.filePath, wi.DTDatasetName);
37             List<DTNode> vertexList = new List<DTNode>();
38             vertexList.AddRange(SelectNodeClass.SelectNodes(sourceDT, "Vertex"));
39             for (int i = 0; i < vertexList.Count;i++ )
40             {
41                 IPoint pPoint=new PointClass();
42                 pPoint.X = double.Parse(vertexList[i].Kernel.Value.ToString().Split(',')[0]);
43                 pPoint.Y = double.Parse(vertexList[i].Kernel.Value.ToString().Split(',')[1]);
44                 pPoint.Z = double.Parse(vertexList[i].Kernel.Value.ToString().Split(',')[2]);
45                 if (pPoint.Z.ToString() != "非数字")
46                 {
47                     pointDictionary.Add(i, pPoint);
48                 }
49             }
50 
51             //插入到新建的FeatureClass中
52             IWorkspaceEdit pWorkspaceEdit = pWorkspace as IWorkspaceEdit;
53             pWorkspaceEdit.StartEditing(true);
54             pWorkspaceEdit.StartEditOperation();
55             IFeatureBuffer pFeatureBuffer = pFeatureClass.CreateFeatureBuffer();
56             IFeatureCursor pFeatureCursor = pFeatureClass.Insert(true);
57             for (int featureNum = 4; featureNum < pointDictionary.Count;featureNum++ )
58             {
59                 pFeatureBuffer.Shape = pointDictionary[featureNum] as IPoint;//出错点,在于新建字段的错误
60                 pFeatureBuffer.set_Value(pFeatureClass.Fields.FindField("elevation"), pointDictionary[featureNum].Z);
61                 pFeatureCursor.InsertFeature(pFeatureBuffer);
62             }
63             pFeatureCursor.Flush();
64             pWorkspaceEdit.StopEditOperation();
65             pWorkspaceEdit.StopEditing(true);
66 
67             return pFeatureClass;
68         }
GetTINFormFeatureClass
 1 public ITin CreateTin(IFeatureClass pFeatureClass, IField pField, string pPath)
 2         {
 3             IGeoDataset pGeoDataset = pFeatureClass as IGeoDataset;
 4             ITinEdit pTinEdit = new TinClass();
 5             pTinEdit.InitNew(pGeoDataset.Extent);
 6             object pObj = Type.Missing;
 7             pTinEdit.AddFromFeatureClass(pFeatureClass, null, pField, null, esriTinSurfaceType.esriTinMassPoint, ref pObj);
 8             pTinEdit.SaveAs(pPath, ref pObj);
 9             pTinEdit.Refresh();
10             return pTinEdit as ITin;
11         }

不解释太多了,其中易出错点在于InMemoryWorkspaceFactoryClass的使用。

posted on 2013-01-14 23:22  小猪同学  阅读(2181)  评论(0编辑  收藏  举报

导航