/// <summary> /// Читает запись представляющую полигон. /// </summary> /// <param name="file">Входной поток</param> /// <param name="record">Запись Shape-файла в которую будет помещена прочитанная информация</param> /// <param name="bounds">Ограничивающий прямоугольник, с которым должен пересекаться ограничивающий прямоугольник записи</param> public override bool Read(/*BigEndianBinaryReader*/ Stream file, BoundingRectangle bounds, ShapeFileRecord record) { record.MinX = file.ReadDouble(); // ShapeFile.ReadDouble64_LE(stream); record.MinY = file.ReadDouble(); //ShapeFile.ReadDouble64_LE(stream); record.MaxX = file.ReadDouble();; // ShapeFile.ReadDouble64_LE(stream); record.MaxY = file.ReadDouble();; // ShapeFile.ReadDouble64_LE(stream); int numParts = file.ReadInt32(); // ShapeFile.ReadInt32_LE(stream); int numPoints = file.ReadInt32(); //ShapeFile.ReadInt32_LE(stream); if (!ShapeHandler.IsRecordInView(bounds, record)) { file.Seek((long)numPoints * 16 + numParts * 4, SeekOrigin.Current); return(false); } for (int i = 0; i < numParts; i++) { record.Parts.Add(file.ReadInt32());//ShapeFile.ReadInt32_LE(stream)); } for (int i = 0; i < numPoints; i++) { ICoordinate p = PlanimetryEnvironment.NewCoordinate( file.ReadDouble(), //ShapeFile.ReadDouble64_LE(stream), file.ReadDouble()); //ShapeFile.ReadDouble64_LE(stream)); record.Points.Add(p); } return(true); }
/// <summary> /// Writes shapes. /// </summary> /// <param name="filename">The string value defining shape file name without .shp extension</param> /// <param name="geometryCollection"> MapAround.Geometry.GeometryCollection instance containing /// the geometries to write to shape file</param> public void WriteShapes(string filename, GeometryCollection geometryCollection) { if (geometryCollection.HasDifferentTypeInstances) { throw new ArgumentException("Geometries in the shape file should be the instances of the same type.", "geometryCollection"); } using (FileStream shpStream = new FileStream(filename + ".shp", FileMode.Create)) { using (FileStream shxStream = new FileStream(filename + ".shx", FileMode.Create)) { BigEndianBinaryWriter shpBinaryWriter = new BigEndianBinaryWriter(shpStream); //, Encoding.ASCII); BigEndianBinaryWriter shxBinaryWriter = new BigEndianBinaryWriter(shxStream); //, Encoding.ASCII); // body type and a handler Handlers.ShapeHandler handler = ShapeFile.GetShapeHandler(ShapeFile.GetShapeType(geometryCollection[0]));//.Geometries[0])); int numShapes = geometryCollection.Count; // calc the length of the shp file, so it can put in the header. int shpLength = 50; for (int i = 0; i < numShapes; i++) { IGeometry body = (IGeometry)geometryCollection[i]; //.Geometries[i]; shpLength += 4; // length of header in WORDS shpLength += handler.GetLength(body); // length of shape in WORDS } int shxLength = 50 + (4 * numShapes); // write the .shp header ShapeFileHeader shpHeader = new ShapeFileHeader(); shpHeader.FileLength = shpLength; // get envelope in external coordinates BoundingRectangle bounds = geometryCollection.GetBoundingRectangle(); shpHeader.SetBounds(bounds); shpHeader.FileCode = 9994; shpHeader.ShapeType = (int)ShapeFile.GetShapeType(geometryCollection[0]);//.Geometries[0]); shpHeader.Write(shpBinaryWriter, ShapeFile.GetShapeType(geometryCollection[0])); // write the .shx header ShapeFileHeader shxHeader = new ShapeFileHeader(); shxHeader.FileLength = shxLength; shxHeader.SetBounds(shpHeader);//.Bounds = shpHeader.Bounds; // assumes Geometry type of the first item will the same for all other items in the collection. shxHeader.FileCode = 9994; shxHeader.ShapeType = (int)ShapeFile.GetShapeType(geometryCollection[0]); shxHeader.Write(shxBinaryWriter, ShapeFile.GetShapeType(geometryCollection[0])); // write the individual records. int _pos = 50; // header length in WORDS for (int i = 0; i < numShapes; i++) { IGeometry body = geometryCollection[i];//.Geometries[i]; int recordLength = handler.GetLength(body); shpBinaryWriter.WriteIntBE(i + 1); shpBinaryWriter.WriteIntBE(recordLength); shxBinaryWriter.WriteIntBE(_pos); shxBinaryWriter.WriteIntBE(recordLength); _pos += 4; // length of header in WORDS handler.Write(body, shpBinaryWriter); //, geometryFactory); _pos += recordLength; // length of shape in WORDS } shxBinaryWriter.Flush(); shxBinaryWriter.Close(); shpBinaryWriter.Flush(); shpBinaryWriter.Close(); } } }