使用C#从已知文本创建GeoJSON输出

本文关键字:创建 GeoJSON 输出 文本 使用 | 更新日期: 2023-09-27 18:25:11

我正在尝试创建类似于此处描述的GeoJSON格式的JSON输出:http://geojson.org/geojson-spec.html

特别是,我的数据源以text格式返回了文本,我想将我的DTO转换为JSON,格式如下面评论中所示。我遇到的主要问题是试图创建没有属性名称的坐标数组[[…]]。

代码:

/*
                Geometry Text Format from database:  POLYGON ((319686.3666000003 7363726.7955, 319747.05190000031 7363778.9233, 319700.78849999979 7363832.7814, 319640.10329999961 7363780.6536, 319686.3666000003 7363726.7955))
                And we want format:
                "geometry": {
                    "type": "Polygon",
                    "coordinates": [[
                        [319686.3666000003, 7363726.795],
                        [319747.0519000003, 7363778.9233],
                        [319700.78849999979, 7363832.7814],
                        [319640.10329999961, 7363780.6536],
                        [319686.3666000003, 7363726.795]
                    ]]
                }
                */

                // Strip out everything except the coordinates
                var coordRawText = myWkt.Replace("POLYGON ((", "");
                coordRawText = coordRawText.Replace("))", "");
                coordRawText = coordRawText.Replace(", ", ",");
                // Seperate coordinates to iterate through
                var coordsArray = coordRawText.Split(',');
                var coordsEnumerable = coordsArray.Select(coord => coord.Replace(" ", ","));
                // Build list of coordinates
                var coordsList = new List<CoordinateDto>();
                foreach (var coord in coordsEnumerable)
                {
                    var splt = coord.Split(',');
                    var x = double.Parse(splt[0]);
                    var y = double.Parse(splt[1]);
                    coordsList.Add(new CoordinateDto {X = x, Y = y});
                }
                myDto.Geometry = new GeometryDto
                {
                    Type =  "Polygon",
                    Coordinates = coordsList
                };

上面的输出"几乎"是我想要的,但并不完全是。输出如下所示:

"geometry":{"type":"Polygon","coordinates":[{"x":319686.3666000003,"y":7363726.7955},{"x":319747.05190000031,"y":7363778.9233},{"x":319700.78849999979,"y":7363832.7814},{"x":319640.10329999961,"y":7363780.6536},{"x":319686.3666000003,"y":7363726.7955}]}

我的DTO定义如下:

[DataContract]
public class GeometryDto
{
    [DataMember]
    public string Type { get; set; }
    [DataMember]
    public List<CoordinateDto> Coordinates { get; set; }

}

[DataContract]
public class CoordinateDto
{
    [DataMember]
    public double X { get; set; }
    [DataMember]
    public double Y { get; set; }

}

我曾尝试使用元组而不是坐标类,但只是插入了"item1"answers"item2"属性名,而不是"x"answers"y"。

我唯一还没有尝试的是创建我自己的JSON转换器?

提前感谢您的帮助,

问候,

Stefan

解决方案更新

由于这里选择了关于多维数组的答案(来自Dhanuka777),我找到了一个解决方案,但为了完整性,如果它有帮助的话:

我不得不创建一个新的助手函数(Jon Skeet的create Rectangular Array函数的略微修改版本:如何将数组列表转换为多维数组)

代码解决方案如下所示:

/*
                Geometry Text Format from database:  POLYGON ((319686.3666000003 7363726.7955, 319747.05190000031 7363778.9233, 319700.78849999979 7363832.7814, 319640.10329999961 7363780.6536, 319686.3666000003 7363726.7955))
                And we want format:
                "geometry": {
                    "type": "Polygon",
                    "coordinates": [[
                        [319686.3666000003, 7363726.795],
                        [319747.0519000003, 7363778.9233],
                        [319700.78849999979, 7363832.7814],
                        [319640.10329999961, 7363780.6536],
                        [319686.3666000003, 7363726.795]
                    ]]
                }
                */

                // Strip out everything except the coordinates
                var coordRawText = myWkt.Replace("POLYGON ((", "");
                coordRawText = coordRawText.Replace("))", "");
                coordRawText = coordRawText.Replace(", ", ",");
                // Seperate coordinates to iterate through
                var coordsArray = coordRawText.Split(',');
                var coordsEnumerable = coordsArray.Select(coord => coord.Replace(" ", ","));
                // Build list of coordinates
                var coordsList = new List<double[,]>();
                foreach (var coord in coordsEnumerable)
                {
                    var splt = coord.Split(',');
                    var x = double.Parse(splt[0]);
                    var y = double.Parse(splt[1]);
                    coordsList.Add(new[,] {{ x, y }});
                }
                myDto.Geometry = new GeometryDto
                {
                    Type =  "Polygon",
                    Coordinates = CreateRectangularArray(coordsList)
                };

创建矩形阵列定义的一个稍微修改的版本如下:

static T[,] CreateRectangularArray<T>(IList<T[,]> arrays)
        {
            // TODO: Validation and special-casing for arrays.Count == 0
            int minorLength = arrays[0].Length;
            T[,] ret = new T[arrays.Count, minorLength];
            for (int i = 0; i < arrays.Count; i++)
            {
                var array = arrays[i];
                if (array.Length != minorLength)
                {
                    throw new ArgumentException
                        ("All arrays must be the same length");
                }
                for (int j = 0; j < minorLength; j++)
                {
                    ret[i, j] = array[0, j];
                }
            }
            return ret;
        }

更新后的GeometryDto如下:

[DataContract]
    public class GeometryDto
    {
        [DataMember]
        public string Type { get; set; }
        [DataMember]
        public double[,] Coordinates { get; set; }

    }

Web API将使用Newtonsoft Json以所需的格式序列化对象。

使用C#从已知文本创建GeoJSON输出

我宁愿使用Newtonsoft Json序列化程序来实现这一点。将坐标定义为2D阵列即可。

public class GeometryDto
    {
        public string Type { get; set; }
        public double[,] coordinates { get; set; }            
    }
    class Program
    {
        static void Main()
        {
            var obj = new GeometryDto
            {
                Type = "Polygon",
                coordinates = new double[,] { { 319686.3666000003, 7363726.795 }, { 319747.0519000003, 7363778.9233 }, { 5.3434444, 6.423443 }, { 7.2343424234, 8.23424324 } }                     
            };
            var json = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
            Console.WriteLine(json);
            Console.ReadKey();
        }
    }

从这里得到Nuget。

输出:{"类型":"多边形","坐标":[[319686.36660000037363726.795],[319774.051190000317363778.9233],…]}