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

Posted mnxxz

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了纯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":"",
");
            sb.Append(""fieldAliases":{"FID":"FID","Id" : "Id"},
");
            sb.Append(""geometryType" : "esriGeometryPolygon",
");
            sb.Append(" "spatialReference" : {"wkid" : 4326,"latestWkid" : 4326},
");
            sb.Append(""fields" : [{"name" : "FID","type" : "esriFieldTypeOID","alias" : "FID"}],
");
            sb.Append(""features" : [
");


            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 + "},
"geometry" : {
"rings" : [[
");

                            //sb.Append("
[");
                            //sb.Append(""Name":"" + nameArr[index - 1] + "",
");
                            //sb.Append(""Value":0,
");
                            //sb.Append(""PointCollection":
[");

                            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("]
],
");

                            sb.Append("]]}},
");

                            //string jstring = "";
                            //jstring += "
{";
                            //jstring += ""Name":"" + nameArr[index] + "",
";
                            //jstring += ""Value":0,
";
                            //jstring += ""PointCollection":
[";
                        }


                        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;
            }
        }

 

以上是关于纯C#,读取shp文件转json,arcgis api for js 3.x可加载的主要内容,如果未能解决你的问题,请参考以下文章

arcgis怎么把cad转成shp

如何把shp格式的文件转成CAD的dwg格式的文件

ArcGis系统如何读取shp文件

arcgis打开shp文件时出现这样的提示该怎么办?

arcgis两个shp文件相减

arcgis快速导出geojson的文件大小