【问题标题】:Format GeoJson based on IEnumerable<Users>基于 IEnumerable<Users> 格式化 GeoJson
【发布时间】:2019-07-01 14:36:49
【问题描述】:

我有一个基本的 API 正在运行,我可以像往常一样存储和获取数据。但是,我在格式化我的内容时遇到了问题。我可以使用 HTTPGet 获取我所有用户的列表,它看起来像这样......

[
    {
        "userId": "1                           ",
        "geoHash": "123456789",
        "latitude": 1.234,
        "longitude": 5.689,
        "locationDate": "2019-07-01T00:00:00"

    },
    {
        "userId": "2                           ",
        "geoHash": "123456789",
        "latitude": 1.234,
        "longitude": 5.689,
    "locationDate": "2019-07-01T00:00:00"

    },
    {
        "userId": "3                           ",
        "geoHash": "123456789",
        "latitude": 1.234,
        "longitude": 5.689,
    "locationDate": "2019-07-01T00:00:00"

    }
]

但是,我不知道如何将它变成一个 GeoJson (https://geojson.org/),看起来像这样......

{
  "type": "Feature",
  "geometry": {
    "type": "Point",
    "coordinates": [125.6, 10.1]
  },
  "properties": {
    "name": "Dinagat Islands"
  }
}

这是我获取用户列表的基本方法

        [HttpGet]
        public IEnumerable<Users> GetUsers()
        {
            return _context.Users;
        }

我已经搜索了几个小时,围绕这个主题的内容似乎让我有些困惑,我非常感谢这里的一些帮助。

【问题讨论】:

  • 您要返回所有用户的FeatureCollection 吗?
  • 是的,我相信是的。不确定是否可以将数组转换为“坐标”

标签: c# .net api ienumerable geojson


【解决方案1】:

编辑:尽管问题包含 Json 格式的用户数据,但 cmets 现在表明它包含在 IEnumerable&lt;Users&gt; 中。下面的代码假定这些值的属性名称与 Json 中显示的相同。

您需要将用户数据“投影”为 GeoJson,最简单的方法是使用匿名类型然后对其进行序列化:这将根据 this linter 创建有效的 GeoJson。

    var users = GetUsers();

    var userGeo = users.Select(u => new
        {
            type = "Feature",
            geometry = new
            {
                type = "Point",
                coordinates = new double[] { u.longitude, u.latitude }
            },
            properties = new {
                name = "User " + u.userId.Trim()
            }
        }
    );

    var featureCollection = new {
        type = "FeatureCollection",
        features = userGeo
    };

    var geoJson = JsonConvert.SerializeObject(featureCollection, Formatting.Indented);

输出:

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [
          5.689,
          1.234
        ]
      },
      "properties": {
        "name": "User 1"
      }
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [
          5.689,
          1.234
        ]
      },
      "properties": {
        "name": "User 2"
      }
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [
          5.689,
          1.234
        ]
      },
      "properties": {
        "name": "User 3"
      }
    }
  ]
}

【讨论】:

  • 冒着听起来非常愚蠢的风险,我怎样才能将我的 json 放入 public IEnumerable GetUsers() 中的字符串中?
  • 我不确定你的意思。在您的问题中,您将用户数据作为 Json,因此我将它们反序列化为用户对象。如果他们是已经用户,那么您不需要这样做。
  • 当然,很抱歉没有澄清它,这是我调用 get 方法时得到的输出。我的问题是,我该如何接受我的“return _context.Users;”作为字符串,因此您的代码可以在此处使用它 var users = JsonConvert.DeserializeObject>(MyString/return);
  • 还是一头雾水。如果 GetUsers() 返回一个 IEnumerable&lt;Users&gt; 你可以调用它的投影..?
  • 顺便说一句,忘记提了,但你的称呼效​​果很好。所以谢谢!把所有东西都连接起来,并且更好地理解事情是如何运作的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-13
  • 1970-01-01
  • 2014-02-07
  • 1970-01-01
相关资源
最近更新 更多