/// <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> /// 写入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); }