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 原作者:小猪同学
好东西大家可以很好借鉴,谢谢原作者的分享.