Ejemplo n.º 1
0
        private void ParseSHP(string fileName)
        {
            BinaryReader br = new BinaryReader(File.Open(fileName, FileMode.Open));

            br.ReadBytes(24);
            int FileLength = br.ReadInt32();//<0代表数据长度未知
            int FileBanben = br.ReadInt32();

            ShapeType = br.ReadInt32();
            double xmin, ymin, xmax, ymax;

            xmin = br.ReadDouble();
            ymax = -1 * br.ReadDouble();
            xmax = br.ReadDouble();
            ymin = -1 * br.ReadDouble();
            double width  = xmax * xmin;
            double height = ymax * ymin;

            br.ReadBytes(32);

            switch (ShapeType)
            {
            case 1:
                m_points.Clear();
                while (br.PeekChar() != -1)
                {
                    PointF point = new PointF();

                    uint RecordNum  = br.ReadUInt32();
                    int  DataLength = br.ReadInt32();


                    //读取第i个记录
                    br.ReadInt32();
                    point.X = (float)br.ReadDouble();
                    point.Y = (float)br.ReadDouble();
                    m_points.Add(point);
                }
                break;

            case 3:
                m_polylines.Clear();
                while (br.PeekChar() != -1)
                {
                    PolyLine_shape polyline = new PolyLine_shape();
                    polyline.Box    = new double[4];
                    polyline.Parts  = new List <int>();
                    polyline.Points = new List <PointF>();

                    uint RecordNum  = br.ReadUInt32();
                    int  DataLength = br.ReadInt32();

                    //读取第i个记录
                    br.ReadInt32();
                    polyline.Box[0]    = br.ReadDouble();
                    polyline.Box[1]    = br.ReadDouble();
                    polyline.Box[2]    = br.ReadDouble();
                    polyline.Box[3]    = br.ReadDouble();
                    polyline.NumParts  = br.ReadInt32();
                    polyline.NumPoints = br.ReadInt32();
                    for (int i = 0; i < polyline.NumParts; i++)
                    {
                        polyline.Parts.Add(br.ReadInt32());
                    }
                    for (int j = 0; j < polyline.NumPoints; j++)
                    {
                        PointF pointtemp = new PointF();
                        pointtemp.X = (float)br.ReadDouble();
                        pointtemp.Y = (float)br.ReadDouble();

                        polyline.Points.Add(pointtemp);
                    }
                    m_polylines.Add(polyline);
                }

                break;

            case 5:
                m_polygons.Clear();
                while (br.PeekChar() != -1)
                {
                    Polygon_shape polygon = new Polygon_shape();
                    polygon.Box    = new double[4];
                    polygon.Parts  = new List <int>();
                    polygon.Points = new List <PointF>();

                    uint RecordNum  = br.ReadUInt32();
                    int  DataLength = br.ReadInt32();

                    //读取第i个记录
                    int m = br.ReadInt32();
                    polygon.Box[0]    = br.ReadDouble();
                    polygon.Box[1]    = br.ReadDouble();
                    polygon.Box[2]    = br.ReadDouble();
                    polygon.Box[3]    = br.ReadDouble();
                    polygon.NumParts  = br.ReadInt32();
                    polygon.NumPoints = br.ReadInt32();
                    for (int j = 0; j < polygon.NumParts; j++)
                    {
                        polygon.Parts.Add(br.ReadInt32());
                    }
                    for (int j = 0; j < polygon.NumPoints; j++)
                    {
                        PointF pointtemp = new PointF();
                        pointtemp.X = (float)br.ReadDouble();
                        pointtemp.Y = (float)br.ReadDouble();
                        polygon.Points.Add(pointtemp);
                    }
                    m_polygons.Add(polygon);
                }

                break;
            }
            br.Close();
        }
Ejemplo n.º 2
0
        private void 打开shp文件ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult dr = this.openFileDialog1.ShowDialog();

            if (dr == DialogResult.OK)
            {
                BinaryReader br = new BinaryReader(openFileDialog1.OpenFile());
                //读取文件过程

                br.ReadBytes(24);
                int FileLength = br.ReadInt32();//<0代表数据长度未知
                int FileBanben = br.ReadInt32();
                ShapeType = br.ReadInt32();
                xmin      = br.ReadDouble();
                ymax      = -1 * br.ReadDouble();
                xmax      = br.ReadDouble();
                ymin      = -1 * br.ReadDouble();
                double width  = xmax - xmin;
                double height = ymax - ymin;
                n1 = (float)(this.pictureBox1.Width * 0.9 / width);   //x轴放大倍数
                n2 = (float)(this.pictureBox1.Height * 0.9 / height); //y轴放大倍数
                br.ReadBytes(32);

                switch (ShapeType)
                {
                case 1:
                    points.Clear();
                    while (br.PeekChar() != -1)
                    {
                        Point_shape point = new Point_shape();

                        uint RecordNum  = br.ReadUInt32();
                        int  DataLength = br.ReadInt32();


                        //读取第i个记录
                        br.ReadInt32();
                        point.X = br.ReadDouble();
                        point.Y = -1 * br.ReadDouble();
                        points.Add(point);
                    }
                    StreamWriter sw = new StreamWriter("point.txt");

                    foreach (Point_shape p in points)
                    {
                        sw.WriteLine("{0},{1},{2} ", p.X, -1 * p.Y, 0);
                    }
                    sw.Close();
                    break;

                case 3:
                    polylines.Clear();
                    while (br.PeekChar() != -1)
                    {
                        PolyLine_shape polyline = new PolyLine_shape();
                        polyline.Box    = new double[4];
                        polyline.Parts  = new ArrayList();
                        polyline.Points = new ArrayList();

                        uint RecordNum  = br.ReadUInt32();
                        int  DataLength = br.ReadInt32();

                        //读取第i个记录
                        br.ReadInt32();
                        polyline.Box[0]    = br.ReadDouble();
                        polyline.Box[1]    = br.ReadDouble();
                        polyline.Box[2]    = br.ReadDouble();
                        polyline.Box[3]    = br.ReadDouble();
                        polyline.NumParts  = br.ReadInt32();
                        polyline.NumPoints = br.ReadInt32();
                        for (int i = 0; i < polyline.NumParts; i++)
                        {
                            int parts = new int();
                            parts = br.ReadInt32();
                            polyline.Parts.Add(parts);
                        }
                        for (int j = 0; j < polyline.NumPoints; j++)
                        {
                            Point_shape pointtemp = new Point_shape();
                            pointtemp.X = br.ReadDouble();
                            pointtemp.Y = -1 * br.ReadDouble();
                            polyline.Points.Add(pointtemp);
                        }
                        polylines.Add(polyline);
                    }
                    StreamWriter sw2 = new StreamWriter("line.txt");
                    count = 1;
                    foreach (PolyLine_shape p in polylines)
                    {
                        for (int i = 0; i < p.NumParts; i++)
                        {
                            int startpoint;
                            int endpoint;
                            if (i == p.NumParts - 1)
                            {
                                startpoint = (int)p.Parts[i];
                                endpoint   = p.NumPoints;
                            }
                            else
                            {
                                startpoint = (int)p.Parts[i];
                                endpoint   = (int)p.Parts[i + 1];
                            }
                            sw2.WriteLine("线" + count.ToString() + ":");
                            for (int k = 0, j = startpoint; j < endpoint; j++, k++)
                            {
                                Point_shape ps = (Point_shape)p.Points[j];
                                sw2.WriteLine("    {0},{1},{2} ", ps.X, -1 * ps.Y, 0);
                            }
                            count++;
                        }
                    }
                    sw2.Close();
                    break;

                case 5:
                    polygons.Clear();
                    while (br.PeekChar() != -1)
                    {
                        Polygon_shape polygon = new Polygon_shape();
                        polygon.Parts  = new ArrayList();
                        polygon.Points = new ArrayList();

                        uint RecordNum  = br.ReadUInt32();
                        int  DataLength = br.ReadInt32();

                        //读取第i个记录
                        int m = br.ReadInt32();
                        polygon.Box1      = br.ReadDouble();
                        polygon.Box2      = br.ReadDouble();
                        polygon.Box3      = br.ReadDouble();
                        polygon.Box4      = br.ReadDouble();
                        polygon.NumParts  = br.ReadInt32();
                        polygon.NumPoints = br.ReadInt32();
                        for (int j = 0; j < polygon.NumParts; j++)
                        {
                            int parts = new int();
                            parts = br.ReadInt32();
                            polygon.Parts.Add(parts);
                        }
                        for (int j = 0; j < polygon.NumPoints; j++)
                        {
                            Point_shape pointtemp = new Point_shape();
                            pointtemp.X = br.ReadDouble();
                            pointtemp.Y = -1 * br.ReadDouble();
                            polygon.Points.Add(pointtemp);
                        }
                        polygons.Add(polygon);
                    }
                    StreamWriter sw1 = new StreamWriter("polygon.txt");
                    count = 1;
                    foreach (Polygon_shape p in polygons)
                    {
                        for (int i = 0; i < p.NumParts; i++)
                        {
                            int startpoint;
                            int endpoint;
                            if (i == p.NumParts - 1)
                            {
                                startpoint = (int)p.Parts[i];
                                endpoint   = p.NumPoints;
                            }
                            else
                            {
                                startpoint = (int)p.Parts[i];
                                endpoint   = (int)p.Parts[i + 1];
                            }
                            sw1.WriteLine("多边形" + count.ToString() + ":");
                            for (int k = 0, j = startpoint; j < endpoint; j++, k++)
                            {
                                Point_shape ps = (Point_shape)p.Points[j];
                                sw1.WriteLine("    {0},{1},{2} ", ps.X, -1 * ps.Y, 0);
                            }
                            count++;
                        }
                    }
                    sw1.Close();
                    break;
                }
                br.Close();
            }
            this.pictureBox1.Refresh();
        }
Ejemplo n.º 3
0
        private void ParseSHP(string fileName)
        {
            BinaryReader br = new BinaryReader(File.Open(fileName, FileMode.Open));

            br.ReadBytes(24);
            int FileLength = br.ReadInt32();//<0代表数据长度未知
            int FileBanben = br.ReadInt32();
            ShapeType = br.ReadInt32();
            double xmin, ymin, xmax, ymax;
            xmin = br.ReadDouble();
            ymax = -1 * br.ReadDouble();
            xmax = br.ReadDouble();
            ymin = -1 * br.ReadDouble();
            double width = xmax * xmin;
            double height = ymax * ymin;
            br.ReadBytes(32);

            switch (ShapeType)
            {
                case 1:
                    m_points.Clear();
                    while (br.PeekChar() != -1)
                    {
                        PointF point = new PointF();

                        uint RecordNum = br.ReadUInt32();
                        int DataLength = br.ReadInt32();

                        //读取第i个记录
                        br.ReadInt32();
                        point.X = (float)br.ReadDouble();
                        point.Y = (float)br.ReadDouble();
                        m_points.Add(point);
                    }
                    break;
                case 3:
                    m_polylines.Clear();
                    while (br.PeekChar() != -1)
                    {
                        PolyLine_shape polyline = new PolyLine_shape();
                        polyline.Box = new double[4];
                        polyline.Parts = new List<int>();
                        polyline.Points = new List<PointF>();

                        uint RecordNum = br.ReadUInt32();
                        int DataLength = br.ReadInt32();

                        //读取第i个记录
                        br.ReadInt32();
                        polyline.Box[0] = br.ReadDouble();
                        polyline.Box[1] = br.ReadDouble();
                        polyline.Box[2] = br.ReadDouble();
                        polyline.Box[3] = br.ReadDouble();
                        polyline.NumParts = br.ReadInt32();
                        polyline.NumPoints = br.ReadInt32();
                        for (int i = 0; i < polyline.NumParts; i++)
                        {
                            polyline.Parts.Add(br.ReadInt32());
                        }
                        for (int j = 0; j < polyline.NumPoints; j++)
                        {

                            PointF pointtemp = new PointF();
                            pointtemp.X = (float)br.ReadDouble();
                            pointtemp.Y = (float)br.ReadDouble();

                            polyline.Points.Add(pointtemp);
                        }
                        m_polylines.Add(polyline);
                    }

                    break;
                case 5:
                    m_polygons.Clear();
                    while (br.PeekChar() != -1)
                    {
                        Polygon_shape polygon = new Polygon_shape();
                        polygon.Box = new double[4];
                        polygon.Parts = new List<int>();
                        polygon.Points = new List<PointF>();

                        uint RecordNum = br.ReadUInt32();
                        int DataLength = br.ReadInt32();

                        //读取第i个记录
                        int m = br.ReadInt32();
                        polygon.Box[0] = br.ReadDouble();
                        polygon.Box[1] = br.ReadDouble();
                        polygon.Box[2] = br.ReadDouble();
                        polygon.Box[3] = br.ReadDouble();
                        polygon.NumParts = br.ReadInt32();
                        polygon.NumPoints = br.ReadInt32();
                        for (int j = 0; j < polygon.NumParts; j++)
                        {
                            polygon.Parts.Add(br.ReadInt32());
                        }
                        for (int j = 0; j < polygon.NumPoints; j++)
                        {
                            PointF pointtemp = new PointF();
                            pointtemp.X = (float)br.ReadDouble();
                            pointtemp.Y = (float)br.ReadDouble();
                            polygon.Points.Add(pointtemp);
                        }
                        m_polygons.Add(polygon);
                    }

                    break;
            }
            br.Close();
        }