示例#1
0
        /// <summary>
        /// Read the file header of the shapefile.
        /// </summary>
        /// <param name="stream">Input stream.</param>
        public void ReadShapeFileHeader(Stream stream)
        {
            // File Code.
            this.fileHeader.FileCode = ShapeFile.ReadInt32_BE(stream);
            if (this.fileHeader.FileCode != ShapeFile.expectedFileCode)
            {
                string msg = String.Format(System.Globalization.CultureInfo.InvariantCulture, "Invalid FileCode encountered. Expecting {0}.", ShapeFile.expectedFileCode);
                throw new ArgumentException(msg);
            }

            // 5 unused values.
            ShapeFile.ReadInt32_BE(stream);
            ShapeFile.ReadInt32_BE(stream);
            ShapeFile.ReadInt32_BE(stream);
            ShapeFile.ReadInt32_BE(stream);
            ShapeFile.ReadInt32_BE(stream);

            // File Length.
            this.fileHeader.FileLength = ShapeFile.ReadInt32_BE(stream);

            // Version.
            this.fileHeader.Version = ShapeFile.ReadInt32_LE(stream);

            // Shape Type.
            this.fileHeader.ShapeType = ShapeFile.ReadInt32_LE(stream);

            // Bounding Box.
            this.fileHeader.XMin = ShapeFile.ReadDouble64_LE(stream);
            this.fileHeader.YMin = ShapeFile.ReadDouble64_LE(stream);
            this.fileHeader.XMax = ShapeFile.ReadDouble64_LE(stream);
            this.fileHeader.YMax = ShapeFile.ReadDouble64_LE(stream);

            // Adjust the bounding box in case it is too small.
            if (Math.Abs(this.fileHeader.XMax - this.fileHeader.XMin) < 1)
            {
                this.fileHeader.XMin -= 5;
                this.fileHeader.XMax += 5;
            }
            if (Math.Abs(this.fileHeader.YMax - this.fileHeader.YMin) < 1)
            {
                this.fileHeader.YMin -= 5;
                this.fileHeader.YMax += 5;
            }

            // Skip the rest of the file header.
            stream.Seek(100, SeekOrigin.Begin);
        }
示例#2
0
        /// <summary>
        /// Read a shapefile record.
        /// </summary>
        /// <param name="stream">Input stream.</param>



        public ShapeFileRecord ReadShapeFileRecord(Stream stream)
        {
            ShapeFileRecord record = new ShapeFileRecord();

            //  MainWindow mw = new MainWindow();
            // Record Header.

            record.RecordNumber  = ShapeFile.ReadInt32_BE(stream);
            record.ContentLength = ShapeFile.ReadInt32_BE(stream);


            // Shape Type.
            record.ShapeType = ShapeFile.ReadInt32_LE(stream);


            // Read the shape geometry, depending on its type.
            switch (record.ShapeType)
            {
            case (int)ShapeType.NullShape:
                // Do nothing.
                break;

            case (int)ShapeType.Point:
                ShapeFile.ReadPoint(stream, record);
                break;

            case (int)ShapeType.PolyLine:
                // PolyLine has exact same structure as Polygon in shapefile.
                ShapeFile.ReadPolygon(stream, record);
                break;

            case (int)ShapeType.Polygon:
                ShapeFile.ReadPolygon(stream, record);
                break;

            case (int)ShapeType.Multipoint:
                ShapeFile.ReadMultipoint(stream, record);
                break;

            default:
            {
                string msg = String.Format(System.Globalization.CultureInfo.InvariantCulture, "ShapeType {0} is not supported.", (int)record.ShapeType);
                throw new ArgumentException(msg);
            }
            }

            // Add the record to our internal list.
            // Check if list Points is empty or not.
            if (record.Points.Count < 1)
            {
                return(record);
            }
            else
            {
                //part polygon record
                //Check if list of int "Parts" is superior to 1.
                if (record.Parts.Count > 1)
                {
                    for (int a = 0; a < record.Parts.Count; a++)
                    {
                        ShapeFileRecord record1 = new ShapeFileRecord();
                        if (a + 1 < record.Parts.Count)
                        {
                            for (int b = record.Parts[a]; b < record.Parts[a + 1]; b++)
                            {
                                Vector2 p = new Vector2();
                                p.x = record.Points[b].x;
                                p.y = record.Points[b].y;
                                // p.z = record.Points[b].z;
                                record1.Points.Add(p);
                            }
                            record1.ShapeType = 5;
                            record1.Parts.Add(record.Parts[a]);
                            this.Myrecords.Add(record1);
                        }
                        else
                        {
                            for (int b = record.Parts[a]; b < record.Points.Count; b++)
                            {
                                Vector2 p = new Vector2();
                                p.x = record.Points[b].x;
                                p.y = record.Points[b].y;
                                // p.z = record.Points[b].z;
                                record1.Points.Add(p);
                            }
                            record1.ShapeType = 5;
                            record1.Parts.Add(0);
                            this.Myrecords.Add(record1);
                        }
                    }
                    this.records.Add(record);
                    return(record);
                }
                this.records.Add(record);
                this.Myrecords.Add(record);
                return(record);
            }
        }