private int GetPointCountByMultiPoint(FileStream fs, BinaryReader br, int oid) { int pointCount = ToLocalEndian.ToInt32FromLittle(br.ReadBytes(4)); //fs.Seek(pointCount * 2 * 8, SeekOrigin.Current); return(pointCount); }
private int GetPointCount(byte[] contentBytes, int oid, FileStream fs) { using (MemoryStream ms = new MemoryStream(contentBytes)) { using (BinaryReader br = new BinaryReader(ms)) { enumShapeType shapeType = (enumShapeType)ToLocalEndian.ToInt32FromLittle(br.ReadBytes(4)); switch (shapeType) { case enumShapeType.NullShape: break; case enumShapeType.Point: return(GetPointCountByPoint(fs, br, oid)); case enumShapeType.MultiPoint: return(GetPointCountByMultiPoint(fs, br, oid)); case enumShapeType.Polyline: return(GetPointCountByPolyline(fs, br, oid)); case enumShapeType.PolylineM: return(GetPointCoungByPolylineM(fs, br, oid)); case enumShapeType.Polygon: return(GetPointCountByPolygon(fs, br, oid)); default: return(0); } return(0); } } }
private long GetHeaderSizeAndFileLength(FileStream fsMainFile, BinaryReader brMainFile, out int fileLength) { //FileCode int fileCode = ToLocalEndian.ToInt32FromBig(brMainFile.ReadBytes(4)); if (fileCode != 9994) { throw new Exception("文件不是ESRI Shape Files文件头!"); } //Skip 20 bytes unused (5 integer) brMainFile.ReadBytes(20); //File length fileLength = ToLocalEndian.ToInt32FromBig(brMainFile.ReadBytes(4)) * 2; if (fileLength != fsMainFile.Length) { throw new Exception("ESRI Shape Files文件未正确结束!"); } //Version int Version = ToLocalEndian.ToInt32FromLittle(brMainFile.ReadBytes(4)); //Shape Type int ShapeType = ToLocalEndian.ToInt32FromLittle(brMainFile.ReadBytes(4)); // Envelope evp = new Envelope(); evp.MinX = ToLocalEndian.ToDouble64FromLittle(brMainFile.ReadBytes(8)); evp.MinY = ToLocalEndian.ToDouble64FromLittle(brMainFile.ReadBytes(8)); evp.MaxX = ToLocalEndian.ToDouble64FromLittle(brMainFile.ReadBytes(8)); evp.MaxY = ToLocalEndian.ToDouble64FromLittle(brMainFile.ReadBytes(8)); //Skip minZ,maxZ,minM,maxM brMainFile.ReadBytes(8 * 4); return(fsMainFile.Position); }
protected override object ConstructFeature(BinaryReader br, int oid) { enumShapeType shapeType = (enumShapeType)ToLocalEndian.ToInt32FromLittle(br.ReadBytes(4)); switch (shapeType) { case enumShapeType.NullShape: break; case enumShapeType.Point: return(ConstructPoint(br, oid)); case enumShapeType.MultiPoint: return(ConstructMultiPoint(br, oid)); case enumShapeType.Polyline: return(ConstructPolyline(br, oid)); case enumShapeType.PolylineM: return(ConstructPolylineM(br, oid)); case enumShapeType.Polygon: return(ConstructPolygon(br, oid)); default: return(null); } return(null); }
private void ReadHeader() { //FileCode int fileCode = ToLocalEndian.ToInt32FromBig(_brMainFile.ReadBytes(4)); if (fileCode != 9994) { throw new Exception("文件不是ESRI Shape Files文件头!"); } //Skip 20 bytes unused (5 integer) _brMainFile.ReadBytes(20); //File length _fileLength = ToLocalEndian.ToInt32FromBig(_brMainFile.ReadBytes(4)) * 2; if (_fileLength != _fsMainFile.Length) { throw new Exception("ESRI Shape Files文件未正确结束!"); } //Version int Version = ToLocalEndian.ToInt32FromLittle(_brMainFile.ReadBytes(4)); //Shape Type int ShapeType = ToLocalEndian.ToInt32FromLittle(_brMainFile.ReadBytes(4)); _shapeType = (enumShapeType)ShapeType; _envelope.MinX = ToLocalEndian.ToDouble64FromLittle(_brMainFile.ReadBytes(8)); _envelope.MinY = ToLocalEndian.ToDouble64FromLittle(_brMainFile.ReadBytes(8)); _envelope.MaxX = ToLocalEndian.ToDouble64FromLittle(_brMainFile.ReadBytes(8)); _envelope.MaxY = ToLocalEndian.ToDouble64FromLittle(_brMainFile.ReadBytes(8)); AdjustZeroWidthEnvelope(_envelope); //Skip minZ,maxZ,minM,maxM _brMainFile.ReadBytes(8 * 4); _fileHeaderSize = _fsMainFile.Position; }
private void WriteShapeMultiPointRecords(Feature[] vfs) { //the fist record offset in 16-bit word byte[] byteArray; ShapeMultiPoint mulPoint; Int32 contentLength; if (_currentShxRecondPosition != 0) { _fsShxFile.Seek(_currentShxRecondPosition, SeekOrigin.Begin); } if (_currentShpRecondPosition != 0) { _fsMainFile.Seek(_currentShpRecondPosition, SeekOrigin.Begin); } for (int i = 0; i < vfs.Length; i++) { mulPoint = vfs[i].Geometry as ShapeMultiPoint; contentLength = (mulPoint.Points.Length * 16 + 48) / 2; byteArray = BitConverter.GetBytes(_featuresCount + i + 1); _bwMainFile.Write(ToLocalEndian.ToInt32FromBig(byteArray)); byteArray = BitConverter.GetBytes(contentLength); _bwMainFile.Write(ToLocalEndian.ToInt32FromBig(byteArray)); _bwShxFile.Write(ToLocalEndian.ToInt32FromBig(byteArray)); byteArray = BitConverter.GetBytes(_offset); _bwShxFile.Write(ToLocalEndian.ToInt32FromBig(byteArray)); _offset += contentLength; byteArray = BitConverter.GetBytes((int)_shapeType); _bwMainFile.Write(ToLocalEndian.ToInt32FromLittle(byteArray)); byteArray = BitConverter.GetBytes(mulPoint.Envelope.MinX); _bwMainFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray)); byteArray = BitConverter.GetBytes(mulPoint.Envelope.MinY); _bwMainFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray)); byteArray = BitConverter.GetBytes(mulPoint.Envelope.MaxX); _bwMainFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray)); byteArray = BitConverter.GetBytes(mulPoint.Envelope.MaxY); _bwMainFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray)); byteArray = BitConverter.GetBytes(mulPoint.Points.Length); _bwMainFile.Write(ToLocalEndian.ToInt32FromLittle(byteArray)); foreach (ShapePoint sp in mulPoint.Points) { byteArray = BitConverter.GetBytes(sp.X); _bwMainFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray)); byteArray = BitConverter.GetBytes(sp.Y); _bwMainFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray)); } } _currentShpRecondPosition = _fsMainFile.Position; _currentShxRecondPosition = _fsShxFile.Position; }
private void WriteShapePointRecords(Feature[] vfs) { //the fist record offset in 16-bit word Int32 Offset = 50; byte[] byteArray; ShapePoint point; for (int i = 0; i < vfs.Length; i++) { point = vfs[i].Geometry as ShapePoint; byteArray = BitConverter.GetBytes(i + 1); _bwMainFile.Write(ToLocalEndian.ToInt32FromBig(byteArray)); byteArray = BitConverter.GetBytes(14); _bwMainFile.Write(ToLocalEndian.ToInt32FromBig(byteArray)); //// _bwShxFile.Write(ToLocalEndian.ToInt32FromBig(byteArray)); byteArray = BitConverter.GetBytes(Offset); _bwShxFile.Write(ToLocalEndian.ToInt32FromBig(byteArray)); Offset += 14; //// byteArray = BitConverter.GetBytes((int)_shapeType); _bwMainFile.Write(ToLocalEndian.ToInt32FromLittle(byteArray)); byteArray = BitConverter.GetBytes(point.X); _bwMainFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray)); byteArray = BitConverter.GetBytes(point.Y); _bwMainFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray)); } }
private void WriteShapeMultiPointRecords(Feature[] vfs) { //the fist record offset in 16-bit word Int32 Offset = 50; byte[] byteArray; ShapeMultiPoint mulPoint; Int32 contentLength; for (int i = 0; i < vfs.Length; i++) { mulPoint = vfs[i].Geometry as ShapeMultiPoint; contentLength = (mulPoint.Points.Length * 16 + 48) / 2; byteArray = BitConverter.GetBytes(i + 1); _bwMainFile.Write(ToLocalEndian.ToInt32FromBig(byteArray)); byteArray = BitConverter.GetBytes(contentLength); _bwMainFile.Write(ToLocalEndian.ToInt32FromBig(byteArray)); //// _bwShxFile.Write(ToLocalEndian.ToInt32FromBig(byteArray)); byteArray = BitConverter.GetBytes(Offset); _bwShxFile.Write(ToLocalEndian.ToInt32FromBig(byteArray)); Offset += contentLength; //// byteArray = BitConverter.GetBytes((int)_shapeType); _bwMainFile.Write(ToLocalEndian.ToInt32FromLittle(byteArray)); byteArray = BitConverter.GetBytes(mulPoint.Envelope.MinX); _bwMainFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray)); byteArray = BitConverter.GetBytes(mulPoint.Envelope.MinY); _bwMainFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray)); byteArray = BitConverter.GetBytes(mulPoint.Envelope.MaxX); _bwMainFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray)); byteArray = BitConverter.GetBytes(mulPoint.Envelope.MaxY); _bwMainFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray)); byteArray = BitConverter.GetBytes(mulPoint.Points.Length); _bwMainFile.Write(ToLocalEndian.ToInt32FromLittle(byteArray)); foreach (ShapePoint sp in mulPoint.Points) { byteArray = BitConverter.GetBytes(sp.X); _bwMainFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray)); byteArray = BitConverter.GetBytes(sp.Y); _bwMainFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray)); } } }
private int GetPointCountByPolygon(FileStream fs, BinaryReader br, int oid) { Envelope evp = new Envelope(ToLocalEndian.ToDouble64FromLittle(br.ReadBytes(8)), ToLocalEndian.ToDouble64FromLittle(br.ReadBytes(8)), ToLocalEndian.ToDouble64FromLittle(br.ReadBytes(8)), ToLocalEndian.ToDouble64FromLittle(br.ReadBytes(8))); int nParts = ToLocalEndian.ToInt32FromLittle(br.ReadBytes(4)); int nPoints = ToLocalEndian.ToInt32FromLittle(br.ReadBytes(4)); //fs.Seek(nParts * 4, SeekOrigin.Current); //fs.Seek(nPoints * 2 * 8, SeekOrigin.Current); return(nPoints); }
private object ConstructMultiPoint(BinaryReader br, int oid) { int pointCount = ToLocalEndian.ToInt32FromLittle(br.ReadBytes(4)); ShapePoint[] pts = new ShapePoint[pointCount]; for (int i = 0; i < pts.Length; i++) { pts[i] = new ShapePoint(ToLocalEndian.ToDouble64FromLittle(br.ReadBytes(8)), ToLocalEndian.ToDouble64FromLittle(br.ReadBytes(8))); } ShapeMultiPoint multiPoint = new ShapeMultiPoint(pts); Feature f = new Feature(oid, multiPoint, GetFieldNames(), GetFieldValues(oid), GetAnnotation(oid)); return(f); }
private object ConstructPolylineM(BinaryReader br, int oid) { Envelope evp = new Envelope(ToLocalEndian.ToDouble64FromLittle(br.ReadBytes(8)), ToLocalEndian.ToDouble64FromLittle(br.ReadBytes(8)), ToLocalEndian.ToDouble64FromLittle(br.ReadBytes(8)), ToLocalEndian.ToDouble64FromLittle(br.ReadBytes(8))); int nParts = ToLocalEndian.ToInt32FromLittle(br.ReadBytes(4)); int nPoints = ToLocalEndian.ToInt32FromLittle(br.ReadBytes(4)); int[] firstPoints = new int[nParts]; for (int i = 0; i < nParts; i++) { firstPoints[i] = ToLocalEndian.ToInt32FromLittle(br.ReadBytes(4)); } ShapePoint[] pts = new ShapePoint[nPoints]; for (int i = 0; i < nPoints; i++) { pts[i] = new ShapePoint(ToLocalEndian.ToDouble64FromLittle(br.ReadBytes(8)), ToLocalEndian.ToDouble64FromLittle(br.ReadBytes(8))); } //ShapeLineString ShapeLineString[] Lines = new ShapeLineString[nParts]; for (int i = 0; i < nParts; i++) { int bIdx = firstPoints[i]; int eIdx = 0; if (nParts == 1 || i == nParts - 1) { eIdx = nPoints; } else { eIdx = firstPoints[i + 1]; } ShapePoint[] rpts = new ShapePoint[eIdx - bIdx]; for (int j = bIdx; j < eIdx; j++) { rpts[j - bIdx] = pts[j]; } Lines[i] = new ShapeLineString(rpts); } // br.ReadBytes(2 * 8); //M Range br.ReadBytes(nPoints * 8); //M Array // ShapePolyline ply = new ShapePolyline(Lines, evp); Feature f = new Feature(oid, ply, GetFieldNames(), GetFieldValues(oid), GetAnnotation(oid)); return(f); }
private void WriteShapePointRecords(Feature[] vfs) { //the fist record offset in 16-bit word byte[] byteArray; ShapePoint point; if (_currentShxRecondPosition != 0) { _fsShxFile.Seek(_currentShxRecondPosition, SeekOrigin.Begin); } if (_currentShpRecondPosition != 0) { _fsMainFile.Seek(_currentShpRecondPosition, SeekOrigin.Begin); } for (int i = 0; i < vfs.Length; i++) { point = vfs[i].Geometry as ShapePoint; byteArray = BitConverter.GetBytes(_featuresCount + i + 1); _bwMainFile.Write(ToLocalEndian.ToInt32FromBig(byteArray)); byteArray = BitConverter.GetBytes(10); _bwMainFile.Write(ToLocalEndian.ToInt32FromBig(byteArray)); _bwShxFile.Write(ToLocalEndian.ToInt32FromBig(byteArray)); byteArray = BitConverter.GetBytes(_offset); _bwShxFile.Write(ToLocalEndian.ToInt32FromBig(byteArray)); _offset += 14; byteArray = BitConverter.GetBytes((int)_shapeType); _bwMainFile.Write(ToLocalEndian.ToInt32FromLittle(byteArray)); byteArray = BitConverter.GetBytes(point.X); _bwMainFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray)); byteArray = BitConverter.GetBytes(point.Y); _bwMainFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray)); } _currentShpRecondPosition = _fsMainFile.Position; _currentShxRecondPosition = _fsShxFile.Position; }
private void WriteShapePolygonRecords(Feature[] vfs) { //the fist record offset in 16-bit word byte[] byteArray; ShapePolygon polygon; Int32 contentLength; Int32 pointsNum = 0; Int32 index = 0; if (_currentShxRecondPosition != 0) { _fsShxFile.Seek(_currentShxRecondPosition, SeekOrigin.Begin); } if (_currentShpRecondPosition != 0) { _fsMainFile.Seek(_currentShpRecondPosition, SeekOrigin.Begin); } for (int i = 0; i < vfs.Length; i++) { polygon = vfs[i].Geometry as ShapePolygon; pointsNum = 0; index = 0; contentLength = (44 + polygon.Rings.Length * 4) / 2; foreach (ShapeRing part in polygon.Rings) { contentLength += part.Points.Length * 8; pointsNum += part.Points.Length; } byteArray = BitConverter.GetBytes(_featuresCount + i + 1); _bwMainFile.Write(ToLocalEndian.ToInt32FromBig(byteArray)); byteArray = BitConverter.GetBytes(_offset); _bwShxFile.Write(ToLocalEndian.ToInt32FromBig(byteArray)); _offset += contentLength + 4; byteArray = BitConverter.GetBytes(contentLength); _bwMainFile.Write(ToLocalEndian.ToInt32FromBig(byteArray)); _bwShxFile.Write(ToLocalEndian.ToInt32FromBig(byteArray)); byteArray = BitConverter.GetBytes((int)_shapeType); _bwMainFile.Write(ToLocalEndian.ToInt32FromLittle(byteArray)); byteArray = BitConverter.GetBytes(polygon.Envelope.MinX); _bwMainFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray)); byteArray = BitConverter.GetBytes(polygon.Envelope.MinY); _bwMainFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray)); byteArray = BitConverter.GetBytes(polygon.Envelope.MaxX); _bwMainFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray)); byteArray = BitConverter.GetBytes(polygon.Envelope.MaxY); _bwMainFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray)); byteArray = BitConverter.GetBytes(polygon.Rings.Length); _bwMainFile.Write(ToLocalEndian.ToInt32FromLittle(byteArray)); byteArray = BitConverter.GetBytes(pointsNum); _bwMainFile.Write(ToLocalEndian.ToInt32FromLittle(byteArray)); foreach (ShapeRing sr in polygon.Rings) { byteArray = BitConverter.GetBytes(index); _bwMainFile.Write(ToLocalEndian.ToInt32FromLittle(byteArray)); index += sr.Points.Length; } foreach (ShapeRing sr in polygon.Rings) { foreach (ShapePoint sp in sr.Points) { byteArray = BitConverter.GetBytes(sp.X); _bwMainFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray)); byteArray = BitConverter.GetBytes(sp.Y); _bwMainFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray)); } } } _currentShpRecondPosition = _fsMainFile.Position; _currentShxRecondPosition = _fsShxFile.Position; }
private void WriteVectorDataToFiles(Feature[] features) { byte[] byteArray; Envelope env = new Envelope(double.MaxValue, double.MaxValue, double.MinValue, double.MinValue); foreach (Feature vf in features) { env.UnionWith(vf.Geometry.Envelope); } ////write main file //write main file and shx file header byteArray = BitConverter.GetBytes(9994); _bwMainFile.Write(ToLocalEndian.ToInt32FromBig(byteArray)); _bwShxFile.Write(ToLocalEndian.ToInt32FromBig(byteArray)); byteArray = new byte[20]; _bwMainFile.Write(byteArray); _bwShxFile.Write(byteArray); //// byteArray = BitConverter.GetBytes(CalculateMainfileLengthInByte(features) / 2); _bwMainFile.Write(ToLocalEndian.ToInt32FromBig(byteArray)); byteArray = BitConverter.GetBytes(CalculateShxfileLenthInByte(features) / 2); _bwShxFile.Write(ToLocalEndian.ToInt32FromBig(byteArray)); //// byteArray = BitConverter.GetBytes(1000); _bwMainFile.Write(ToLocalEndian.ToInt32FromLittle(byteArray)); _bwShxFile.Write(ToLocalEndian.ToInt32FromLittle(byteArray)); byteArray = BitConverter.GetBytes((int)_shapeType); _bwMainFile.Write(ToLocalEndian.ToInt32FromLittle(byteArray)); _bwShxFile.Write(ToLocalEndian.ToInt32FromLittle(byteArray)); byteArray = BitConverter.GetBytes(env.MinX); _bwMainFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray)); _bwShxFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray)); byteArray = BitConverter.GetBytes(env.MinY); _bwMainFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray)); _bwShxFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray)); byteArray = BitConverter.GetBytes(env.MaxX); _bwMainFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray)); _bwShxFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray)); byteArray = BitConverter.GetBytes(env.MaxY); _bwMainFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray)); _bwShxFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray)); byteArray = new byte[32]; _bwMainFile.Write(byteArray); _bwShxFile.Write(byteArray); //write main file and Shx file records switch (_shapeType) { case enumShapeType.Point: WriteShapePointRecords(features); break; case enumShapeType.MultiPoint: WriteShapeMultiPointRecords(features); break; case enumShapeType.Polyline: WriteShapePolylineRecords(features); break; case enumShapeType.Polygon: WriteShapePolygonRecords(features); break; default: break; } ////write prj file,leave it now,no spec CloseVectorDataFiles(); }
private void WriteShapePolygonRecords(Feature[] vfs) { //the fist record offset in 16-bit word Int32 Offset = 50; byte[] byteArray; ShapePolygon polygon; Int32 contentLength; Int32 pointsNum = 0; Int32 index = 0; for (int i = 0; i < vfs.Length; i++) { polygon = vfs[i].Geometry as ShapePolygon; pointsNum = 0; index = 0; contentLength = (44 + polygon.Rings.Length * 4) / 2; foreach (ShapeRing part in polygon.Rings) { contentLength += part.Points.Length * 8; pointsNum += part.Points.Length; } byteArray = BitConverter.GetBytes(i + 1); _bwMainFile.Write(ToLocalEndian.ToInt32FromBig(byteArray)); //// byteArray = BitConverter.GetBytes(Offset); _bwShxFile.Write(ToLocalEndian.ToInt32FromBig(byteArray)); Offset += contentLength + 4; //// byteArray = BitConverter.GetBytes(contentLength); _bwMainFile.Write(ToLocalEndian.ToInt32FromBig(byteArray)); //// _bwShxFile.Write(ToLocalEndian.ToInt32FromBig(byteArray)); //// byteArray = BitConverter.GetBytes((int)_shapeType); _bwMainFile.Write(ToLocalEndian.ToInt32FromLittle(byteArray)); byteArray = BitConverter.GetBytes(polygon.Envelope.MinX); _bwMainFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray)); byteArray = BitConverter.GetBytes(polygon.Envelope.MinY); _bwMainFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray)); byteArray = BitConverter.GetBytes(polygon.Envelope.MaxX); _bwMainFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray)); byteArray = BitConverter.GetBytes(polygon.Envelope.MaxY); _bwMainFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray)); byteArray = BitConverter.GetBytes(polygon.Rings.Length); _bwMainFile.Write(ToLocalEndian.ToInt32FromLittle(byteArray)); byteArray = BitConverter.GetBytes(pointsNum); _bwMainFile.Write(ToLocalEndian.ToInt32FromLittle(byteArray)); foreach (ShapeRing sr in polygon.Rings) { byteArray = BitConverter.GetBytes(index); _bwMainFile.Write(ToLocalEndian.ToInt32FromLittle(byteArray)); index += sr.Points.Length; } foreach (ShapeRing sr in polygon.Rings) { foreach (ShapePoint sp in sr.Points) { byteArray = BitConverter.GetBytes(sp.X); _bwMainFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray)); byteArray = BitConverter.GetBytes(sp.Y); _bwMainFile.Write(ToLocalEndian.ToInt64FromLittle(byteArray)); } } } }