注册 登录  
 加关注
查看详情
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

程序员驿站

淘宝店:http://shop106888457.taobao.com

 
 
 

日志

 
 

shp to txt arcgis  

2013-08-12 17:28:55|  分类: ArcEngine |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Geodatabase;
  
namespace ShpTxt
{
    public partial class FrmShapfile : Form
    {
        public FrmShapfile()
        {
            InitializeComponent();
        }
        string InTxtpath;
        string InTxtName;
        string OutShapepath;
        string OutShapeName;
  
        string InShapepath;
        string InShapeName;
        string OutTxtPath;
        string OutTxtName;
  
        ISpatialReference pSRef = new ProjectedCoordinateSystemClass();
         
        private void btnCreate_Click(object sender, EventArgs e)
        {
            //获取要存储的txt文件
            string filepath = OutTxtPath+"\\"+OutTxtName+".txt";
            string AttributePth = OutTxtPath + "\\"+OutTxtName + "Attr.txt";          
            FileStream myStream = new FileStream(filepath, FileMode.Create, FileAccess.ReadWrite);
            StreamWriter myWrite = new StreamWriter(myStream);
            FileStream attrStream = new FileStream(AttributePth, FileMode.Create, FileAccess.Write);
            StreamWriter attriWrite = new StreamWriter(attrStream);
  
            //打开工作空间
            string fullpath = InShapepath;
            string filename =InShapeName ;
            IWorkspaceFactory pWSF = new ShapefileWorkspaceFactoryClass();
            IWorkspace pWS = pWSF.OpenFromFile(fullpath, 0);
            IFeatureWorkspace pFWS = pWS as IFeatureWorkspace;
            IFeatureClass pFeatClass = pFWS.OpenFeatureClass(filename);
            IFields pFields = pFeatClass.Fields;
            //获取字段的名称
            string FieldNameCol = "";
            for (int j = 2; j < pFields.FieldCount-1; j++)
            {                
                IField pField = pFields.get_Field(j);
                if (pField.Type != esriFieldType.esriFieldTypeGeometry)
                {
                    string Name = pField.Name.Trim ();
                    FieldNameCol += Name + "\t"
                }                
            }
            FieldNameCol += pFields.get_Field(pFields.FieldCount - 1).Name.Trim ();
            attriWrite.WriteLine(FieldNameCol);
            //获取字段的类型
            string FieldTypeCol = "";
            for (int j = 2; j < pFields.FieldCount-1; j++)
            {
                IField pField = pFields.get_Field(j);
                if (pField.Type != esriFieldType.esriFieldTypeGeometry)
                {
                    string type = pField.Type.ToString().Trim();
                    FieldTypeCol += type + "\t";
                }
            }
            FieldTypeCol += pFields.get_Field(pFields.FieldCount - 1).Type.ToString ().Trim ();
            attriWrite.WriteLine(FieldTypeCol);
  
            IFeatureCursor pFeatCursor = pFeatClass.Search(null, false);
            IFeature pFeat = pFeatCursor.NextFeature();
           myWrite.WriteLine( pFeat.Shape.GeometryType.ToString().Remove(0, 12));          
            while (pFeat != null)
            {
                IGeometry pGeo = pFeat.Shape;
                IPointCollection pPointCol = pGeo as IPointCollection;
                string filedvalue = pFeat.get_Value(0).ToString().Trim();
                myWrite.WriteLine(filedvalue);
                attriWrite.WriteLine(filedvalue);
                for (int i = 0; i < pPointCol.PointCount; i++)
                {
                    IPoint pPoint = pPointCol.get_Point(i);
                    myWrite.Write(i.ToString() + "\t");
                    string corrdinate = "";
                    corrdinate += pPoint.X.ToString() + "\t";
                    corrdinate += pPoint.Y.ToString();
                    myWrite.WriteLine(corrdinate);
                }
                myWrite.Flush();
                //获得属性值
                string ValueCol = "";
                for (int k = 2; k < pFeat.Fields.FieldCount - 1; k++)
                {
                    ValueCol += pFeat.get_Value(k).ToString().Trim() + "\t";
                }
                ValueCol += pFeat.get_Value(pFeat.Fields.FieldCount - 1).ToString().Trim();
                attriWrite.WriteLine(ValueCol);
                attriWrite.Flush();
                pFeat = pFeatCursor.NextFeature();
  
            }
            attriWrite.Close();
            attrStream.Close();
  
            myWrite.Close();
            myStream.Close();
            MessageBox.Show("成功!");
            //this.Close();
  
  
        }
  
        private void btnTransfer_Click(object sender, EventArgs e)
        {
            //创建一个新的shapfile工作空间
            string fullpath = OutShapepath;
            IWorkspaceFactory pWSF = new ShapefileWorkspaceFactoryClass();
            IWorkspace pWS = pWSF.OpenFromFile(fullpath, 0);
  
            IFields pFields = new FieldsClass();
            IFieldsEdit pFEdits = pFields as IFieldsEdit;
            IField pField = new FieldClass();
            IFieldEdit pFEdit = pField as IFieldEdit ;
            pFEdit.Name_2 = "Shape";
            pFEdit.Type_2  = esriFieldType.esriFieldTypeGeometry;
              
           
            IGeometryDef pGeoDef = new GeometryDefClass();
            IGeometryDefEdit pGeoDefEdit = pGeoDef as IGeometryDefEdit ;
            //获取几何类型
            string filepath = InTxtpath + "\\" + InTxtName + ".txt";  
            StreamReader myReader = new StreamReader(filepath, System.Text.Encoding.GetEncoding("GB2312"), true);
            string data = myReader.ReadLine().Trim();
            if (data == "Polyline")
                pGeoDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolyline;
            else if (data == "Polygon")
                pGeoDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolygon;
            else if (data == "Point")
                pGeoDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint; 
  
            pGeoDefEdit.SpatialReference_2 = pSRef;
            pFEdit.GeometryDef_2 = pGeoDef;
            pFEdits.AddField(pField);
            //获取字段的名称和类型
            string AtrtiPath = InTxtpath + "\\" + InTxtName + "Attr.txt";
            StreamReader AttriReader = new StreamReader(AtrtiPath, System.Text.Encoding.Default, true);
            AttriReader.BaseStream.Seek(0, SeekOrigin.Begin);
            string AReader = AttriReader.ReadLine();
            string[] NameCol = AReader.Split('\t');
            AReader = AttriReader.ReadLine();
            string[] TypeCol = AReader.Split('\t');        
            for (int i = 0; i < NameCol.GetLength(0); i++)
            {
                pField = new FieldClass();
                pFEdit = pField as IFieldEdit;
                pFEdit.Name_2 = NameCol[i].Trim();
                string type = TypeCol[i].Trim() ;
                switch (type)
                {
                    case "esriFieldTypeString":
                        pFEdit.Type_2 = esriFieldType.esriFieldTypeString;
                        break ;
                    case "esriFieldTypeDouble":
                        pFEdit.Type_2 = esriFieldType.esriFieldTypeDouble;
                        break ;
                    case "esriFieldTypeDate":
                        pFEdit.Type_2 = esriFieldType.esriFieldTypeDate;
                        break ;
                    case "esriFieldTypeInteger":
                        pFEdit.Type_2= esriFieldType.esriFieldTypeInteger ;
                        break ;
                    case "esriFieldTypeSmallInteger":
                        pFEdit.Type_2 = esriFieldType.esriFieldTypeSmallInteger;
                        break;
                      
                }
                pFEdits.AddField(pField);
            }
  
            IFeatureWorkspace pFeatWS = pWS as IFeatureWorkspace;
            IFeatureClass pFeatClass = pFeatWS.CreateFeatureClass(OutShapeName, pFields, null, null, esriFeatureType.esriFTSimple, "Shape", "");
                   
            IPointCollection pPointCol = new MultipointClass();
            while (data !="" && data !=null)
            {              
                string[] d = data.Split('\t');
                if (d.GetLength(0) == 1)
                {                   
                    if (pPointCol.PointCount > 0)
                    {
                        AReader = AttriReader.ReadLine();
                        AReader = AttriReader.ReadLine();
                        string []AttriCol = AReader.Split('\t');
                        addFeature(pPointCol, pWS, pFeatClass,AttriCol);
                        pPointCol = new MultipointClass();                     
  
                    }                  
                }
                else
                {
                    string X = d[1];
                    string Y = d[2];
                    IPoint pPoint = new PointClass();
                    pPoint.X = Convert.ToDouble(X);
                    pPoint.Y = Convert.ToDouble(Y);
                   object missing= Type.Missing ;
                    pPointCol.AddPoint(pPoint,ref  missing, ref missing);  
                }
                data = myReader.ReadLine();
            }
  
            if (pPointCol.PointCount > 0)
            {
                AReader = AttriReader.ReadLine();
                AReader = AttriReader.ReadLine();
                string[] AttriCol = AReader.Split('\t');
                addFeature(pPointCol, pWS, pFeatClass,AttriCol);
                pPointCol = new MultipointClass();
            }
  
            myReader.Close();
            MessageBox.Show("完成!");
            //this.Close();
  
        }
  
        private void addFeature(IPointCollection pPointCol, IWorkspace pWS, IFeatureClass pFeatClass, string[] ACol)
        {
            IWorkspaceEdit pWSEdit = pWS as IWorkspaceEdit;
            pWSEdit.StartEditOperation();
            IFeature pFeat = pFeatClass.CreateFeature();
            if (pFeat.Shape.GeometryType == esriGeometryType.esriGeometryPolygon)
                pFeat.Shape = AddPloygon(pPointCol);
            else if (pFeat.Shape.GeometryType == esriGeometryType.esriGeometryPolyline)
                pFeat.Shape = AddPolyline(pPointCol);
            else if (pFeat.Shape.GeometryType == esriGeometryType.esriGeometryPoint)
                pFeat.Shape = pPointCol.get_Point(0) as IGeometry; 
  
            for (int i = 0; i < ACol.GetLength(0); i++)
            {
                pFeat.set_Value(i + 2, ACol[i].ToString());
            }
  
            pFeat.Store();
            pWSEdit.StopEditOperation();
        }
  
        private IGeometry AddPloygon(IPointCollection pPointCol)
        {
            object missing = Type.Missing;
            IPolygon pPolygon = new PolygonClass();
            ISegmentCollection pSegCol = pPolygon as ISegmentCollection;
            for (int i = 0; i < pPointCol.PointCount - 1; i++)
            {
                ILine pLine = new LineClass();
                IPoint pFrom = pPointCol.get_Point(i);
                IPoint pTo = pPointCol.get_Point(i + 1);
                pLine.FromPoint = pFrom;
                pLine.ToPoint = pTo;
                ISegment pSeg = pLine as ISegment;
                pSegCol.AddSegment(pSeg, ref missing, ref missing);
            }
            ILine pLastLine = new LineClass();
            IPoint pFromP = pPointCol.get_Point(pPointCol.PointCount - 1);
            IPoint pToP = pPointCol.get_Point(0);
            pLastLine.FromPoint = pFromP;
            pLastLine.ToPoint = pToP;
            ISegment pLastSeg = pLastLine as ISegment;
            pSegCol.AddSegment(pLastSeg, ref missing, ref missing);
            pPolygon.Close();
            IGeometry pGeometry = pPolygon as IGeometry;
            return pGeometry;
   
        }
        private IGeometry AddPolyline(IPointCollection pPointCol)
        {
            object missing = Type.Missing;
            IPolyline pPolline = new PolylineClass();
            ISegmentCollection pSegCol = pPolline as ISegmentCollection;
            for (int i = 0; i < pPointCol.PointCount - 1; i++)
            {
                ILine pLine = new LineClass();
                IPoint pFrom = pPointCol.get_Point(i);
                IPoint pTo = pPointCol.get_Point(i + 1);
                pLine.FromPoint = pFrom;
                pLine.ToPoint = pTo;
                ISegment pSeg = pLine as ISegment;
                pSegCol.AddSegment(pSeg, ref missing, ref missing);
            }
            IGeometry pGeometry = pPolline as IGeometry;
            return pGeometry; 
        }
         
        private void btnTxtOpen_Click(object sender, EventArgs e)
        {
            OpenFileDialog InputTxt = new OpenFileDialog();
            InputTxt.Filter = "Text file(*.txt)|*.txt";
            InputTxt.Multiselect = false;
            DialogResult txtResult = InputTxt.ShowDialog();
            if (txtResult != DialogResult.OK)
                return;
            string    InTxtfullpath = InputTxt.FileName;
            InTxtpath = System.IO.Path.GetDirectoryName(InTxtfullpath);
            InTxtName = System.IO.Path.GetFileNameWithoutExtension(InTxtfullpath);
             txtInputTxt.Text = InTxtfullpath;
  
           
        }
  
        private void btnInputShape_Click(object sender, EventArgs e)
        {
            SaveFileDialog OutPutShape = new SaveFileDialog();
            OutPutShape.Filter = "Shapefile(*.shp)|*.shp";
            DialogResult shapeResult = OutPutShape.ShowDialog();
            if (shapeResult != DialogResult.OK)
                return;
            string Shapefullpath = OutPutShape.FileName;
            OutShapepath = System.IO.Path.GetDirectoryName(Shapefullpath);
            OutShapeName = System.IO.Path.GetFileNameWithoutExtension(Shapefullpath);
            txtOutputShape.Text = Shapefullpath;
  
        }
  
        private void btnCnacel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
  
        private void btnCoordinate_Click(object sender, EventArgs e)
        {
             
            OpenFileDialog OpenRef = new OpenFileDialog();
            OpenRef.Filter = "Shapefile(*.shp)|*.shp|坐标系统(*.prj)|*.prj";
            OpenRef.Multiselect = false;
            DialogResult RefResult = OpenRef.ShowDialog();
            if (RefResult != DialogResult.OK)
                return;
            string fullpath = OpenRef.FileName;
            string filepath = System.IO.Path.GetDirectoryName(fullpath);
            string filename = System.IO.Path.GetFileNameWithoutExtension(fullpath);
            if (OpenRef.FilterIndex == 1)
            {
                IWorkspaceFactory pWSF = new ShapefileWorkspaceFactoryClass();
                IWorkspace pWS = pWSF.OpenFromFile(filepath, 0);
                IFeatureWorkspace pFWS = pWS as IFeatureWorkspace;
                IFeatureClass pFeatClass = pFWS.OpenFeatureClass(filename);
                IGeoDataset pGeoDataset = pFeatClass as IGeoDataset;
                pSRef = pGeoDataset.SpatialReference;
                ISpatialReferenceInfo pSRFInfo = pSRef as ISpatialReferenceInfo;
                txtCoordinate.Text = pSRFInfo.Name;
  
            }
            else
            {
                ISpatialReferenceFactory pSRF = new SpatialReferenceEnvironmentClass();
                pSRef= pSRF.CreateESRISpatialReferenceFromPRJFile(fullpath);
                txtCoordinate.Text = filename;
            }
  
             
  
  
        }
  
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
  
        private void btnInShape_Click(object sender, EventArgs e)
        {
            OpenFileDialog OpenShape = new OpenFileDialog();
            OpenShape.Filter = "Shapefile(*.shp)|*.shp";
            OpenShape.Multiselect = false;
            DialogResult OpenResult = OpenShape.ShowDialog();
            if (OpenResult != DialogResult.OK)
                return;
            string fullpath = OpenShape.FileName;
            InShapepath = System.IO.Path.GetDirectoryName(fullpath);
            InShapeName = System.IO.Path.GetFileNameWithoutExtension(fullpath);
            txtInShape.Text = fullpath;
  
        }
  
        private void btnOutTxt_Click(object sender, EventArgs e)
        {
            SaveFileDialog SaveTxt = new SaveFileDialog();
            SaveTxt.Filter = "Text file(*.txt)|*.txt";
            DialogResult SaveResult = SaveTxt.ShowDialog();
            if (SaveResult != DialogResult.OK)
                return;
            string fullpath = SaveTxt.FileName;
            OutTxtPath = System.IO.Path.GetDirectoryName(fullpath);
            OutTxtName = System.IO.Path.GetFileNameWithoutExtension(fullpath);
            txtOutTxt.Text = fullpath;
  
        }
  
  
  
    }
} ...
  评论这张
 
阅读(556)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018