• 纯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;
                }
            }
  • 相关阅读:
    杭电 Problem
    杭电Problem 5053 the sum of cube 【数学公式】
    杭电 Problem 2089 不要62 【打表】
    杭电 Problem 4548 美素数【打表】
    杭电 Problem 2008 分拆素数和 【打表】
    杭电 Problem 1722 Cake 【gcd】
    杭电 Problem 2187 悼念512汶川大地震遇难同胞——老人是真饿了【贪心】
    杭电Problem 1872 稳定排序
    杭电 Problem 1753 大明A+B
    东北林业大 564 汉诺塔
  • 原文地址:https://www.cnblogs.com/mnxxz/p/11601818.html
Copyright © 2020-2023  润新知