加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 01:18:57 所属栏目:百科 来源:网络整理
导读: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 (); // 坐标 fileB
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":"",rn");
            sb.Append(""fieldAliases":{"FID":"FID","Id" : "Id"},rn");
            sb.Append(""geometryType" : "esriGeometryPolygon",rn");
            sb.Append(" "spatialReference" : {"wkid" : 4326,"latestWkid" : 4326},rn");
            sb.Append(""fields" : [{"name" : "FID","type" : "esriFieldTypeOID","alias" : "FID"}],rn");
            sb.Append(""features" : [rn");


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

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

                            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++] + ",";
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //sb.Append();
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
                            }
                            coorJson = coorJson.Substring(0,coorJson.Length - 1);
                            sb.Append(coorJson);

                            //sb.Append("]rn],rn");

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

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


                        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:HJProjectShangDongJson" + 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;
            }
        }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读