读写ESRI Shapefile格式的C#代码

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;


using System.Data.Odbc;  //add by hand,which is needed when load the layer attribute information
using System.Data.OleDb;
using System.Collections;
using System.Data;
using System.Xml;


namespace CGCL.CGFiles
{
    public class CGShapeFileParser
    {
        public class ESRI_ShxHeader
        {
            int FileCode; //9994
            int[] Unused2 = new int[5];
            int FileLength;
            int Version; //1000
            int ShapeType; // 0- Null shape
            // 1- Point
            // 3-Arc
            // 5-Polygon
            // 8-MultiPoint
            double XMin;
            double YMin;
            double XMax;
            double YMax;
            int[] Unused3 = new int[8];
        }

        class ESRI_ShapeFile
        {
            int FileCode; //9994
            int[] Unused = new int[5];
            int FileLength;
            int Version; //1000
            int ShapeType; // 0- Null shape
            // 1- Point
            // 3-Arc
            // 5-Polygon
            // 8-MultiPoint
            double XMin;
            double YMin;
            double XMax;
            double YMax;
            int[] Unused1 = new int[8];
        }


        class ESRI_RecordHeader
        {
            int RecNumber;
            int ContentLength;
        }

        class ESRI_PointContent
        {
            int ShapeType;
            double X;
            double Y;
        }
        class ESRI_IndexRec//索引文件
        {
            int Offset;
            int ContentLen;
        }

        class ESRI_ArcContent
        {
            int ShapeType;
            double xmin;
            double ymin;
            double xmax;
            double ymax;
            int NumParts;
            int NumPoints;
        }

        class ESRI_PolygonContent
        {
            int ShapeType;
            double xmin;
            double ymin;
            double xmax;
            double ymax;
            int NumParts;
            int NumPoints;
        }    
              
        public bool LoadShapeFile(CGDataAdapter.CGLocalGeoDataAdapter adapter)
        {

            string connectionString;
            OdbcConnection connection;
            OdbcDataAdapter OdbcAdapter;


            CGMap.CGGeoLayer geolayer = adapter.getMasterGeoLayer();           

            string shpfilepath = adapter.getPath();
            string shpfilename = adapter.getFileName();
            string shxfilepath = shpfilepath.Substring(0, shpfilepath.LastIndexOf("//") + 1) + adapter.getFileName() + ".shx";

            //read out the layer attribute infomation
            connectionString = "Dsn=Visual FoxPro Database;sourcedb=" + shpfilepath + ";sourcetype=DBF;exclusive=No;backgroundfetch=Yes;collate=Machine";
            connection = new OdbcConnection(connectionString);
            connection.Open();
            OdbcAdapter = new OdbcDataAdapter("select * from " + shpfilename, connectionString);

            // Create new DataTable and DataSource objects.
            DataSet ds = new DataSet();
            OdbcAdapter.Fill(ds);
            connection.Close();

            if (geolayer == null) return false;

            try
            {
                //先读取.shx文件,得到文件的总字节长度
                FileStream fs = new FileStream(shxfilepath, FileMode.Open, FileAccess.Read);   //文件流形式 
                BinaryReader BinaryFile = new BinaryReader(fs);  //二进制读取文件的对象
                long BytesSum = fs.Length;  //得到文件的字节总长 
                int shapecount = (int)(BytesSum - 100) / 8;  //得以总记录数目            

                BinaryFile.Close();
                fs.Close();

                //打开shp文件
                if (shxfilepath == "")
                {
                    //  MessageBox.Show("索引文件打开出错");
                    return false;
                }
                //打开.shp文件,读取x,y坐标的信息
                fs = new FileStream(shpfilepath, FileMode.Open, FileAccess.Read);   //文件流形式
                BinaryFile = new BinaryReader(fs);     //打开二进制文件  

                BinaryFile.ReadBytes(32);  //先读出36个字节,紧接着是Box边界合
                int shapetype = BinaryFile.ReadInt32();             
               
                geolayer.envlope.left = BinaryFile.ReadDouble();   //读出整个shp图层的边界合
                geolayer.envlope.bottom = BinaryFile.ReadDouble();
                geolayer.envlope.right = BinaryFile.ReadDouble();
                geolayer.envlope.top = BinaryFile.ReadDouble();


                BinaryFile.ReadBytes(32);  //   shp中尚未使用的边界盒   


                //Get Shape Data From Here On
                int stype;
                double x, y;
                double left, right, top, bottom;

                int partcount;
                int pointcount;

                switch (shapetype)
       

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值