C#,读取shp源文件 ,将要素生成JSON格式(面图层)

来源:互联网 发布:福特基金会 知乎 编辑:程序博客网 时间:2024/06/01 07:45
//转换位字节 大小位转换
        private int ChangeByteOrder(int indata)
        {
            byte[] src = new byte[4];
            src[0] = (byte)((indata >> 24) & 0xFF);
            src[1] = (byte)((indata >> 16) & 0xFF);
            src[2] = (byte)((indata >> 8) & 0xFF);
            src[3] = (byte)(indata & 0xFF);


            int value;
            value = (int)((src[0] & 0xFF) | ((src[1] & 0xFF) << 8) | ((src[2] & 0xFF) << 16) | ((src[3] & 0xFF) << 24));
            return value;
        }
        int ShapeType;//shp文件类型,点1 线3 面5


        double[] fileBox = new double[4];


        string[] nameArr = { "name1镇", "name2镇", "name3镇"};
        List<int> partsArr = new List<int>();//多部分的
        List<double> coorsArr = new List<double>();//坐标


        StringBuilder sb = new StringBuilder("[");

private void read()
        {
            fileBox[0] = double.MaxValue;
            fileBox[1] = double.MaxValue;
            fileBox[2] = double.MinValue;
            fileBox[3] = double.MinValue;


            //sb.Append("\r\n");
            //sb.Append("]");


            //writeTxt(sb.ToString());
            //return;


            string shpfilepath = @"F:\test\shp\test.shp";
            FileStream fs = new FileStream(shpfilepath, FileMode.Open, FileAccess.Read);
            BinaryReader br = new BinaryReader(fs);
            //Console.WriteLine("开始读取面文件...");
            //读取文件过程
            br.ReadBytes(24);
            int FileLength = br.ReadInt32();
            //Console.WriteLine("文件长度:" + ChangeByteOrder(FileLength));
            int FileBanben = br.ReadInt32();
            //Console.WriteLine("版本号:" + FileBanben);
            ShapeType = br.ReadInt32();
            //Console.WriteLine("几何类型:" + ShapeType);
            fileBox[0] = br.ReadDouble();
            //Console.WriteLine("空间数据所占空间范围的X方向最小值:" + fileBox[0]);
            fileBox[1] = br.ReadDouble();
            //Console.WriteLine("空间数据所占空间范围的Y方向最小值:" + fileBox[1]);
            fileBox[2] = br.ReadDouble();
            //Console.WriteLine("空间数据所占空间范围的X方向最大值:" + fileBox[2]);
            fileBox[3] = br.ReadDouble();
            //Console.WriteLine("空间数据所占空间范围的Y方向最大值:" + fileBox[3]);
            br.ReadBytes(32);
            switch (ShapeType)
            {
                case 5:
                    while (br.PeekChar() != -1)
                    {
                        uint RecordNum = br.ReadUInt32();
                        int index = ChangeByteOrder((int)RecordNum);
                        //Console.WriteLine("文件记录号为:" + index);
                        int DataLength = br.ReadInt32();
                        //Console.WriteLine("坐标长度为:" + ChangeByteOrder(DataLength));


                        //读取第i个记录
                        int m = br.ReadInt32();
                        //Console.WriteLine("几何类型:" + m);
                        for (int i = 0; i < 4; i++)
                        {
                            br.ReadDouble();
                            //Console.WriteLine("Box[" + i + "]:" + br.ReadDouble());
                        }


                        int numParts = br.ReadInt32();
                        //Console.WriteLine("子面个数:" + numParts);
                        int numPoints = br.ReadInt32();
                        //Console.WriteLine("坐标点个数:" + numPoints);


                        //Console.WriteLine("每个子环在坐标点内的起始位置:");
                        for (int j = 0; j < numParts; j++)
                        {
                            int parts = new int();
                            parts = br.ReadInt32();
                            partsArr.Add(parts);
                            //Console.WriteLine("parts[" + j + "]:" + parts);
                        }


                        //Console.WriteLine("Points数组:");
                        for (int j = 0; j < numPoints; j++)
                        {
                            double X = br.ReadDouble();
                            double Y = br.ReadDouble();
                            coorsArr.Add(X);
                            coorsArr.Add(Y);
                            //Console.WriteLine("Points[" + j + "]:" + X + " " + Y);
                            //Console.Write("{\"X\":" + X + ",\"Y\":" + Y + "},");
                        }
                        for (int i=0;i<partsArr.Count;i++)
                        {
                            sb.Append("\r\n{");
                            sb.Append("\"Name\":\""+nameArr[index-1]+"\",\r\n");
                            sb.Append("\"Value\":0,\r\n");
                            sb.Append("\"PointCollection\":\r\n[");
                            int startIndex = partsArr[i];
                            startIndex = startIndex * 2;
                            int endIndex = partsArr[partsArr.Count-1];
                            string coorJson = "";
                            if (i== partsArr.Count - 1)//最后一个走这里
                            {
                                while (startIndex < coorsArr.Count)
                                {
                                    coorJson += "{\"X\":" + coorsArr[startIndex++] + ",\"Y\":" + coorsArr[startIndex++] + "},";
                                    //sb.Append();
                                }
                            }
                            else {
                                while (startIndex< partsArr[i+1]*2)
                                {
                                    coorJson += "{\"X\":" + coorsArr[startIndex++] + ",\"Y\":" + coorsArr[startIndex++] + "},";
                                    //sb.Append();
                                }
                            }
                            coorJson = coorJson.Substring(0,coorJson.Length-1);
                            sb.Append(coorJson);
                            sb.Append("]\r\n},\r\n");
                            //string jstring = "";
                            //jstring += "\r\n{";
                            //jstring += "\"Name\":\"" + nameArr[index] + "\",\r\n";
                            //jstring += "\"Value\":0,\r\n";
                            //jstring += "\"PointCollection\":\r\n[";


                        }


                        partsArr.Clear();
                        coorsArr.Clear();


                        //Console.WriteLine();
                        //Console.WriteLine("--------------------------");


                    }
                    break;
                default:
                    Console.WriteLine("shp文件必须为面类型,且不带Z、M值");
                    break;




            }
            sb.Append("]");
            writeTxt(sb.ToString());
            fs.Flush();
            fs.Close();
        }


        private bool writeTxt(string s)
        {
            try {
                FileStream fs = new FileStream("C:\\json.txt",FileMode.Create);
                byte[] data = System.Text.Encoding.Default.GetBytes(s);
                fs.Write(data,0,data.Length);
                fs.Flush();
                fs.Close();
                return true;
            }
            catch  {
                return false;
            }
        }


以上是读取shp面图层转换成json字符串的代码。读取点和线文件类似。参照我之前的博客( C#不用ArcEngine生成shp文件 ) ,把代码改成点或者面的,就可以了

阅读全文
0 0
原创粉丝点击