/// <summary> /// 读取record数据 /// 当读取时文件流位置在第一个记录处 /// </summary> /// <param name="reader">shx文件流</param> public void Read(BinaryReader reader) { //如果header为空先读取header shp和shx的文件头一样 if (mHeader == null) { mHeader.Read(reader); } if (mRecordDic != null) { mRecordDic.Clear(); } else { mRecordDic = new Dictionary <ulong, ShxData>(); } if (reader.BaseStream.Position == 0) { reader.BaseStream.Seek(100, SeekOrigin.Begin);//跳过文件头 } while (reader.PeekChar() != -1) { ShxData item = new ShxData(); int Offset = reader.ReadInt32(); int ContentLength = reader.ReadInt32(); ulong uOffset = ByteTransUtil.big2little(Offset); // ulong uContentLength = ByteTransUtil.big2little(ContentLength); // item.Offset = (int)uOffset; // item.ContentLength = (int)uContentLength; mRecordDic.Add((ulong)(mRecordDic.Count + 1), item); //key值从1开始 } }
/// <summary> /// Encoding must be ASCII for this binary writer. /// </summary> /// <param name="writer"></param> /// <remarks> /// 参考shp文件头文件结构. /// </remarks> public void Write(BinaryWriter writer) { writer.Flush(); //清除 byte[] lbtFileCode = BitConverter.GetBytes(mFileCode); //将int转变为byte byte[] bbtFileCode = ByteTransUtil.little2big(lbtFileCode); writer.Write(bbtFileCode); int Unused = 0; //未使用 一共5个 big byte[] lbtUnused = BitConverter.GetBytes(Unused); //将int转变为byte byte[] bbtUnused = ByteTransUtil.little2big(lbtUnused); for (int i = 0; i < 5; i++) { writer.Write(bbtUnused); } byte[] lbtFileLength = BitConverter.GetBytes(mFileLength); //将int转变为byte byte[] bbtFileLength = ByteTransUtil.little2big(lbtFileLength); writer.Write(bbtFileLength); writer.Write(mVersion); int tyeptemp = (int)mGeoType; writer.Write(tyeptemp);//--------------------- writer.Write(mXmin); writer.Write(mYmin); writer.Write(mXmax); writer.Write(mYmax); writer.Write(mZmin); writer.Write(mZmax); writer.Write(mMmin); writer.Write(mMmax); }
/// <summary> /// 写入record /// </summary> /// <param name="writer"></param> public void Write(BinaryWriter writer) { if (mRecordDic == null || mHeader == null) { return; } writer.Flush(); //清除 byte[] lbtFileCode = BitConverter.GetBytes(mHeader.FileCode); //将int转变为byte byte[] bbtFileCode = ByteTransUtil.little2big(lbtFileCode); writer.Write(bbtFileCode); int Unused = 0; //未使用 一共5个 big byte[] lbtUnused = BitConverter.GetBytes(Unused); //将int转变为byte byte[] bbtUnused = ByteTransUtil.little2big(lbtUnused); for (int i = 0; i < 5; i++) { writer.Write(bbtUnused); } int mFileLength = 50 + mRecordDic.Count * 4; //两个inter 4字 byte[] lbtFileLength = BitConverter.GetBytes(mFileLength); //将int转变为byte byte[] bbtFileLength = ByteTransUtil.little2big(lbtFileLength); writer.Write(bbtFileLength); writer.Write(mHeader.Version); int tyeptemp = (int)mHeader.GeoType; writer.Write(tyeptemp); writer.Write(mHeader.Xmin); writer.Write(mHeader.Ymin); writer.Write(mHeader.Xmax); writer.Write(mHeader.Ymax); writer.Write(mHeader.Zmin); writer.Write(mHeader.Zmax); writer.Write(mHeader.Mmin); writer.Write(mHeader.Mmax); //---------------------写入记录--------------------- for (ulong i = 0; i < (ulong)mRecordDic.Count; i++) { //写入shp byte[] lbtOffset = BitConverter.GetBytes(mRecordDic[i + 1].Offset); byte[] bbtOffset = ByteTransUtil.little2big(lbtOffset); writer.Write(bbtOffset); byte[] lbtContentLength = BitConverter.GetBytes(mRecordDic[i + 1].ContentLength); byte[] bbtContentLength = ByteTransUtil.little2big(lbtContentLength); writer.Write(bbtContentLength); } }
/// <summary> /// 读取header数据 /// 确定读取时文件流位置在开始处 /// 当读取完成文件流位置在第一个记录处 /// </summary> /// <param name="reader"></param> public void Read(BinaryReader reader) { // type of reader. mFileCode = reader.ReadInt32(); mFileCode = (int)ByteTransUtil.big2little(mFileCode); reader.ReadBytes(20); //预留5个inter mFileLength = reader.ReadInt32(); mFileLength = (int)ByteTransUtil.big2little(mFileLength); //以字为单位,一字等于2字节,等于16位 mVersion = reader.ReadInt32(); int typeTemp = reader.ReadInt32(); mGeoType = (ShapeType)typeTemp;//目前考虑点 线 面三种 mXmin = reader.ReadDouble(); mYmin = reader.ReadDouble(); mXmax = reader.ReadDouble(); mYmax = reader.ReadDouble(); mZmin = reader.ReadDouble(); mZmax = reader.ReadDouble(); mMmin = reader.ReadDouble(); mMmax = reader.ReadDouble(); }
/// <summary> /// 读取record数据 /// 当读取时文件流位置在第一个记录处 /// </summary> /// <param name="reader"></param> public void Read(BinaryReader reader) { //如果header为空先读取header if (mHeader == null) { mHeader.Read(reader); } if (mRecordDic != null) { mRecordDic.Clear(); } else { mRecordDic = new Dictionary <ulong, ShpData>(); } //目前只考虑点线面三种类型 switch (mHeader.GeoType) { case ShapeType.Point: while (reader.PeekChar() != -1) { EVPoint point = new EVPoint(); uint RecordNum = reader.ReadUInt32(); int DataLength = reader.ReadInt32(); ulong uRecordNum = ByteTransUtil.big2little((int)RecordNum); //读取第i个记录 //每一条的记录长度没有考虑RecordNum、DataLength 所以比实际长度少4字 int uDataLength = (int)ByteTransUtil.big2little(DataLength); //以字为单位,一字等于2字节,等于16位 长度不包括uRecordNum和uDataLength int geoType = reader.ReadInt32(); //每一个对象的类型 没有用! point.RecordNum = uRecordNum; point.DataLength = uDataLength; //固定10字 没有考虑RecordNum、DataLength 比实际长度少4字 point.GeoType = ShapeType.Point; point.X = reader.ReadDouble(); point.Y = reader.ReadDouble(); // ShpData item = new ShpData(); item.GeoShape = point; mRecordDic.Add(uRecordNum, item); } break; case ShapeType.PolyLine: while (reader.PeekChar() != -1) { EVPolyLine polyline = new EVPolyLine(); polyline.Parts = new ArrayList(); polyline.Points = new ArrayList(); uint RecordNum = reader.ReadUInt32(); //读取第i个记录 从1开始算 ulong uRecordNum = ByteTransUtil.big2little((int)RecordNum); int DataLength = reader.ReadInt32(); //以字为单位,一字等于2字节,等于16位 //每一条的记录长度没有考虑RecordNum、DataLength 所以比实际长度少4字 int uDataLength = (int)ByteTransUtil.big2little(DataLength); int geoType = reader.ReadInt32();//每一个对象的类型 没有用 polyline.RecordNum = uRecordNum; polyline.DataLength = uDataLength; //没有考虑RecordNum、DataLength 比实际长度少4字 polyline.GeoType = ShapeType.PolyLine; polyline.Xmin = reader.ReadDouble(); polyline.Ymin = reader.ReadDouble(); polyline.Xmax = reader.ReadDouble(); polyline.Ymax = reader.ReadDouble(); polyline.NumParts = reader.ReadInt32(); polyline.NumPoints = reader.ReadInt32(); for (int i = 0; i < polyline.NumParts; i++) { int parts = new int(); parts = reader.ReadInt32(); polyline.Parts.Add(parts); } for (int j = 0; j < polyline.NumPoints; j++) { EVPoint pointtemp = new EVPoint(); pointtemp.X = reader.ReadDouble(); pointtemp.Y = reader.ReadDouble(); polyline.Points.Add(pointtemp); } ShpData item = new ShpData(); item.GeoShape = polyline; mRecordDic.Add(uRecordNum, item); } break; case ShapeType.Polygon: while (reader.PeekChar() != -1) { EVPolygon polygon = new EVPolygon(); polygon.Parts = new ArrayList(); polygon.Points = new ArrayList(); uint RecordNum = reader.ReadUInt32(); int DataLength = reader.ReadInt32(); ulong uRecordNum = ByteTransUtil.big2little((int)RecordNum); //读取第i个记录 //每一条的记录长度没有考虑RecordNum、DataLength 所以比实际长度少4字 //以字为单位,一字等于2字节,等于16位 int uDataLength = (int)ByteTransUtil.big2little(DataLength); int geoType = reader.ReadInt32(); polygon.RecordNum = uRecordNum; polygon.DataLength = uDataLength; polygon.GeoType = ShapeType.Polygon; polygon.Xmin = reader.ReadDouble(); polygon.Ymin = reader.ReadDouble(); polygon.Xmax = reader.ReadDouble(); polygon.Ymax = reader.ReadDouble(); polygon.NumParts = reader.ReadInt32(); polygon.NumPoints = reader.ReadInt32(); for (int j = 0; j < polygon.NumParts; j++) { int parts = new int(); parts = reader.ReadInt32(); polygon.Parts.Add(parts); } for (int j = 0; j < polygon.NumPoints; j++) { EVPoint pointtemp = new EVPoint(); pointtemp.X = reader.ReadDouble(); pointtemp.Y = reader.ReadDouble(); polygon.Points.Add(pointtemp); } ShpData item = new ShpData(); item.GeoShape = polygon; mRecordDic.Add(uRecordNum, item); } break; } }
/// <summary> /// 写入record记录 /// 先判断是否获取wkt信息以及写入header /// 写入的同时 计算shx的记录内容 /// </summary> /// <param name="writer"></param> /// <returns>返回shx的文件内容</returns> public Dictionary <ulong, ShxData> Write(BinaryWriter writer) { Dictionary <ulong, ShxData> shxDic = new Dictionary <ulong, ShxData>(); if (isGetWKTInfo) { if (writer.BaseStream.Position == 0) { mHeader.Write(writer); } int offset = 50;//跳过头文件 switch (mHeader.GeoType) { case ShapeType.Point: for (int i = 0; i < mRecordDic.Count; i++) { EVPoint point = mRecordDic[(ulong)(i + 1)].GeoShape as EVPoint; ShxData shx = new ShxData(); shx.ContentLength = point.DataLength; shx.Offset = offset; offset += shx.ContentLength + 4; shxDic.Add(point.RecordNum, shx); //写入shp byte[] lbtRecorderNum = BitConverter.GetBytes(point.RecordNum); byte[] bbtRecorderNum = ByteTransUtil.little2big(lbtRecorderNum); writer.Write(bbtRecorderNum); byte[] lbtDataLength = BitConverter.GetBytes(point.DataLength); byte[] bbtDataLength = ByteTransUtil.little2big(lbtDataLength); writer.Write(bbtDataLength); writer.Write((int)ShapeType.Point); writer.Write(point.X); writer.Write(point.Y); } break; case ShapeType.PolyLine: for (int i = 0; i < mRecordDic.Count; i++) { EVPolyLine line = mRecordDic[(ulong)(i + 1)].GeoShape as EVPolyLine; ShxData shx = new ShxData(); shx.ContentLength = line.DataLength; shx.Offset = offset; offset += shx.ContentLength + 4; shxDic.Add(line.RecordNum, shx); //写入shp byte[] lbtRecorderNum = BitConverter.GetBytes(line.RecordNum); byte[] bbtRecorderNum = ByteTransUtil.little2big(lbtRecorderNum); writer.Write(bbtRecorderNum); byte[] lbtDataLength = BitConverter.GetBytes(line.DataLength); byte[] bbtDataLength = ByteTransUtil.little2big(lbtDataLength); writer.Write(bbtDataLength); writer.Write((int)line.GeoType); writer.Write(line.Xmin); writer.Write(line.Ymin); writer.Write(line.Xmax); writer.Write(line.Ymax); writer.Write(line.NumParts); writer.Write(line.NumPoints); for (int j = 0; j < line.NumParts; j++) { writer.Write((int)line.Parts[j]); } for (int j = 0; j < line.NumPoints; j++) { EVPoint point = line.Points[j] as EVPoint; writer.Write(point.X); writer.Write(point.Y); } } break; case ShapeType.Polygon: for (int i = 0; i < mRecordDic.Count; i++) { EVPolygon gon = mRecordDic[(ulong)(i + 1)].GeoShape as EVPolygon; ShxData shx = new ShxData(); shx.ContentLength = gon.DataLength; shx.Offset = offset; offset += shx.ContentLength + 4; shxDic.Add(gon.RecordNum, shx); //写入shp byte[] lbtRecorderNum = BitConverter.GetBytes(gon.RecordNum); byte[] bbtRecorderNum = ByteTransUtil.little2big(lbtRecorderNum); writer.Write(bbtRecorderNum); byte[] lbtDataLength = BitConverter.GetBytes(gon.DataLength); byte[] bbtDataLength = ByteTransUtil.little2big(lbtDataLength); writer.Write(bbtDataLength); writer.Write((int)gon.GeoType); writer.Write(gon.Xmin); writer.Write(gon.Ymin); writer.Write(gon.Xmax); writer.Write(gon.Ymax); writer.Write(gon.NumParts); writer.Write(gon.NumPoints); for (int j = 0; j < gon.NumParts; j++) { writer.Write((int)gon.Parts[j]); } for (int j = 0; j < gon.NumPoints; j++) { EVPoint point = gon.Points[j] as EVPoint; writer.Write(point.X); writer.Write(point.Y); } } break; } } return(shxDic); }