Example #1
0
        /// <summary>
        /// Read a shapefile Polygon record.
        /// </summary>
        /// <param name="stream">Input stream.</param>
        /// <param name="record">Shapefile record to be updated.</param>
        private static void ReadPolygon(Stream stream, ShapeFileRecord record)
        {
            // Bounding Box.
            record.XMin = ShapeFile.ReadDouble64_LE(stream);
            record.YMin = ShapeFile.ReadDouble64_LE(stream);
            record.XMax = ShapeFile.ReadDouble64_LE(stream);
            record.YMax = ShapeFile.ReadDouble64_LE(stream);

            // Num Parts and Points.
            int numParts  = ShapeFile.ReadInt32_LE(stream);
            int numPoints = ShapeFile.ReadInt32_LE(stream);

            // Parts.
            for (int i = 0; i < numParts; i++)
            {
                record.Parts.Add(ShapeFile.ReadInt32_LE(stream));
            }

            // Points.
            for (int i = 0; i < numPoints; i++)
            {
                PointF p = new PointF();
                p.X = (float)ShapeFile.ReadDouble64_LE(stream);
                p.Y = (float)ShapeFile.ReadDouble64_LE(stream);
                record.Points.Add(p);
            }
        }
Example #2
0
        /// <summary>
        /// Read a shapefile Point record.
        /// </summary>
        /// <param name="stream">Input stream.</param>
        /// <param name="record">Shapefile record to be updated.</param>
        private static void ReadPoint(Stream stream, ShapeFileRecord record)
        {
            // Points - add a single point.
            PointF p = new PointF();

            p.X = (float)ShapeFile.ReadDouble64_LE(stream);
            p.Y = (float)ShapeFile.ReadDouble64_LE(stream);
            record.Points.Add(p);

            // Bounding Box.
            record.XMin = p.X;
            record.YMin = p.Y;
            record.XMax = record.XMin;
            record.YMax = record.YMin;
        }
Example #3
0
        /// <summary>
        /// Read a shapefile record.
        /// </summary>
        /// <param name="stream">Input stream.</param>
        public ShapeFileRecord ReadShapeFileRecord(Stream stream)
        {
            ShapeFileRecord record = new ShapeFileRecord();

            // 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 ApplicationException(msg);
            }
            }

            // Add the record to our internal list.
            this.records.Add(record);

            return(record);
        }
Example #4
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 ApplicationException(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);
        }
Example #5
0
        private void importMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();

            ofd.Filter = "Shape Files (*.shp)|*.shp" +
                         "|All files (*.*)|*.*";
            ofd.FilterIndex     = 0;
            ofd.CheckFileExists = true;
            try
            {
                if (ofd.ShowDialog(this) == DialogResult.OK)
                {
                    map.ClearUndo();
                    ShapeFile sf = new ShapeFile();
                    sf.Read(ofd.FileName);
                    StringBuilder sb      = new StringBuilder("<MapItems>", sf.Records.Count * 100);
                    float         xOffset = (float)-sf.FileHeader.XMin;
                    float         yOffset = (float)-sf.FileHeader.YMin;
                    //PointF offset = this.MercatorConversion(new PointF((float)sf.FileHeader.XMin, (float)sf.FileHeader.YMin));
                    //float xOffset = (float)-offset.X;
                    //float yOffset = (float)-offset.Y;
                    foreach (ShapeFileRecord sfr in sf.Records)
                    {
                        if (sfr.ShapeType == (int)ShapeType.Polygon)
                        {
                            HandlePolygon(sb, xOffset, yOffset, sfr);
                        }
                    }

                    sb.Append("</MapItems>");
                    map.Paste(new Point(0, 0), sb.ToString());
                    map.ClearUndo();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, string.Format("Error opening {0}", ofd.FileName));
            }
            finally
            {
                ofd.Dispose();
            }
        }
Example #6
0
        private void importMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "Shape Files (*.shp)|*.shp" +
                "|All files (*.*)|*.*";
            ofd.FilterIndex = 0;
            ofd.CheckFileExists = true;
            try
            {
                if (ofd.ShowDialog(this) == DialogResult.OK)
                {
                    map.ClearUndo();
                    ShapeFile sf = new ShapeFile();
                    sf.Read(ofd.FileName);
                    StringBuilder sb = new StringBuilder("<MapItems>", sf.Records.Count * 100);
                    float xOffset = (float)-sf.FileHeader.XMin;
                    float yOffset = (float)-sf.FileHeader.YMin;
                    //PointF offset = this.MercatorConversion(new PointF((float)sf.FileHeader.XMin, (float)sf.FileHeader.YMin));
                    //float xOffset = (float)-offset.X;
                    //float yOffset = (float)-offset.Y;
                    foreach (ShapeFileRecord sfr in sf.Records)
                    {
                        if (sfr.ShapeType == (int)ShapeType.Polygon)
                        {
                            HandlePolygon(sb, xOffset, yOffset, sfr);
                        }
                    }

                    sb.Append("</MapItems>");
                    map.Paste(new Point(0, 0), sb.ToString());
                    map.ClearUndo();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, string.Format("Error opening {0}", ofd.FileName));
            }
            finally
            {
                ofd.Dispose();
            }
        }