hbhbice

导航

读SHP索引文件

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Text;
  4 using System.IO;
  5 
  6 
  7 namespace ReadIndexFile
  8 {
  9     public struct IndexContent
 10     {
 11         public int Offset;
 12         public int Length;
 13     }
 14     /// <summary>
 15     /// 代表SHX文件,用来读写SHX文件
 16     /// 
 17     /// </summary>
 18     class ShpIndex
 19     {
 20         #region Private Member
 21         /// <summary>
 22         /// 索引文件的文件名
 23         /// </summary>
 24         private string _fileName;
 25         private int _fileCode;
 26         private int _fileLength;
 27         private int _fileVersion;
 28         private int _shapeType;
 29         private double _XMin;
 30         private double _YMin;
 31         private double _XMax;
 32         private double _YMax;
 33         private List<IndexContent> _indexContents;
 34         #endregion
 35 
 36         public ShpIndex(string filename)
 37         {
 38             this._fileName = filename;
 39             this._indexContents = new List<IndexContent>();
 40         }
 41 
 42         /// <summary>
 43         /// 读索引文件
 44         /// </summary>
 45         public void Read()
 46         {
 47             FileStream fs = new FileStream(this._fileName, FileMode.Open);
 48             BinaryReader br = new BinaryReader(fs);
 49             
 50             #region 读头文件
 51             // br.ReadBytes(24);
 52             this._fileCode = ByteOrderExchange(br.ReadInt32());  // BIG BYTE ORDER 9994
 53             //以下几个没有用到的就转换字节序
 54             int Unused1 = br.ReadInt32();
 55             int Unused2 = br.ReadInt32();
 56             int Unused3 = br.ReadInt32();
 57             int Unused4 = br.ReadInt32();
 58             int Unused5 = br.ReadInt32();          //至此读了24个字节
 59 
 60             this._fileLength = ByteOrderExchange(br.ReadInt32());//<0代表数据长度未知  739442688
 61             this._fileVersion = br.ReadInt32();
 62             _shapeType = br.ReadInt32();           //至此读了36个字节
 63             this._XMin  = br.ReadDouble();
 64             this._YMax = -1 * br.ReadDouble();
 65             this._XMax  = br.ReadDouble();
 66             this._YMin = -1 * br.ReadDouble();      //至此读了68个字节
 67             //double width = xmax - xmin;
 68             //double height = ymax - ymin;
 69             //n1 = (float)(this.pictureBox1.Width * 0.9 / width);//x轴放大倍数
 70             //n2 = (float)(this.pictureBox1.Height * 0.9 / height);//y轴放大倍数
 71             br.ReadBytes(32);                       //至此读了100个字节
 72 
 73             #endregion
 74             IndexContent ic;
 75             for (int i = 0; i < (this._fileLength -50)/4; i++)
 76             {
 77                 
 78                 ic.Offset = ByteOrderExchange(br.ReadInt32());
 79                 ic.Length  = ByteOrderExchange(br.ReadInt32());
 80                 this._indexContents.Add(ic);
 81             }
 82             br.Close();
 83             fs.Close();
 84         }
 85 
 86         /// <summary>
 87         /// 转换Int32大字节码序与小字节码序
 88         /// </summary>
 89         /// <param name="formerByteOrder"></param>
 90         /// <returns></returns>
 91         private int ByteOrderExchange(int formerByteOrder)
 92         {
 93             byte[] bytes = BitConverter.GetBytes(formerByteOrder);
 94             byte temp;
 95             temp = bytes[3];
 96             bytes[3= bytes[0];
 97             bytes[0= temp;
 98             temp = bytes[2];
 99             bytes[2= bytes[1];
100             bytes[1= temp;
101             int laterByteOrder = BitConverter.ToInt32(bytes, 0);
102             return laterByteOrder;
103         }
104         
105     }
106 }

 

posted on 2010-05-19 14:26  hbhbice  阅读(547)  评论(0编辑  收藏  举报