基于IEnumerable格式化GeoJson<Users>
Format GeoJson based on IEnumerable<Users>
我有一个基本的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;
}
我已经搜索了几个小时,关于这个主题的内容似乎让我有些困惑,我真的很感激这里的一些帮助。
编辑:虽然问题包含用户数据 Json,但评论现在表明它包含在 IEnumerable<Users>
中。下面的代码假定这些值的 属性 名称与 Json.
中显示的名称相同
您需要 'project' 用户数据作为 GeoJson,最简单的方法是使用匿名类型然后将其序列化:这会根据 [= 创建有效的 GeoJson 13=].
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"
}
}
]
}
我有一个基本的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;
}
我已经搜索了几个小时,关于这个主题的内容似乎让我有些困惑,我真的很感激这里的一些帮助。
编辑:虽然问题包含用户数据 Json,但评论现在表明它包含在 IEnumerable<Users>
中。下面的代码假定这些值的 属性 名称与 Json.
您需要 'project' 用户数据作为 GeoJson,最简单的方法是使用匿名类型然后将其序列化:这会根据 [= 创建有效的 GeoJson 13=].
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"
}
}
]
}