纯C#,读取shp文件转json,arcgis api for js 3.x可加载

public static void FeaturesToJSON(string shpName)
        {
            int ShapeType;//shp文件类型,点1 线3 面5
            double[] fileBox = new double[4];

            List<int> partsArr = new List<int>();//多部分的
            List<double> coorsArr = new List<double>();//坐标

            fileBox[0] = double.MaxValue;
            fileBox[1] = double.MaxValue;
            fileBox[2] = double.MinValue;
            fileBox[3] = double.MinValue;

            FileStream fs = new FileStream(shpPath, 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);

            StringBuilder sb = new StringBuilder("{");
            sb.Append("\"displayFieldName\":\"\",\r\n");
            sb.Append("\"fieldAliases\":{\"FID\":\"FID\",\"Id\" : \"Id\"},\r\n");
            sb.Append("\"geometryType\" : \"esriGeometryPolygon\",\r\n");
            sb.Append(" \"spatialReference\" : {\"wkid\" : 4326,\"latestWkid\" : 4326},\r\n");
            sb.Append("\"fields\" : [{\"name\" : \"FID\",\"type\" : \"esriFieldTypeOID\",\"alias\" : \"FID\"}],\r\n");
            sb.Append("\"features\" : [\r\n");


            switch (ShapeType)
            {
                case 5:
                    while (br.PeekChar() != -1)
                    {
                        uint RecordNum = br.ReadUInt32();
                        int index = ChangeByteOrder((int)RecordNum) - 1;
                        //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);
                        }

                        for (int i = 0; i < partsArr.Count; i++)
                        {
                            sb.Append("{\"attributes\" : {\"FID\":" + index + "},\r\n\"geometry\" : {\r\n\"rings\" : [[\r\n");

                            //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 += "[" + coorsArr[startIndex++] + "," + coorsArr[startIndex++] + "],";
                                    //sb.Append();
                                }
                            }
                            else
                            {
                                while (startIndex < partsArr[i + 1] * 2)
                                {
                                    coorJson += "[" + coorsArr[startIndex++] + "," + coorsArr[startIndex++] + "],";
                                    //sb.Append();
                                }
                            }
                            coorJson = coorJson.Substring(0, coorJson.Length - 1);
                            sb.Append(coorJson);

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

                            sb.Append("]]}},\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;
                    
            }

            int start = sb.Length - 3;

            sb.Remove(start, 1);
            sb.Append("]}");
            WriteTxt(sb.ToString(), shpName);
            fs.Flush();
            fs.Close();
        }

        //转换位字节 大小位转换
        private static 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;
        }

        private static bool WriteTxt(string s, string shpName)
        {
            try
            {
                FileStream fs = new FileStream("E:\\HJProject\\ShangDong\\Json\\" + shpName + ".json", FileMode.Create);
                //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;
            }
        }
相关文章
相关标签/搜索