c# - 向 JSON 添加其他属性以创建 GeoJSON?
问题描述
在 SO 的帮助下,我能够创建一个让我更接近于创建 GeoJSON 对象的类:
这是课程:
var envelope = new
{
type = "FeatureCollection",
features = result.Tables[0]
};
string callback = JsonConvert.SerializeObject(envelope);
byte[] resultBytes = Encoding.UTF8.GetBytes(callback);
return new System.IO.MemoryStream(resultBytes);
返回此 JSON:
{
"type" : "FeatureCollection",
"features" : [
{
"Name " : "Joe",
"Status" : "Dev",
"imageUrl" : "markers/Dev.svg",
"lat" : 34.310100,
"Lon" : -77.215500
},
{
"Name " : "Joe",
"Status" : "Dev",
"imageUrl" : "markers/Dev.svg",
"lat" : 34.310100,
"Lon" : -77.215500
},
{
"Name " : "Mary",
"Status" : "Dev",
"imageUrl" : "markers/Dev.svg",
"lat" : 34.310100,
"Lon" : -77.215500
}
]
}
我需要向这个 JSON 对象添加另一组属性,但我不确定如何为此修改该类。
新的 JSON 必须如下所示;它本质上是 GeoJSON。在每个“记录”之前都会有另一组属性"type" : "Feature", "properties" : {
。在每一行之后还会有另一个子集"geometry" :
:
{
"type" : "FeatureCollection",
"features" : [
{
"type" : "Feature",
"properties" : {
"Name " : "Joe",
"Status" : "Dev",
"imageUrl" : "markers/Dev.svg",
"lat" : 34.21092,
"lon" : -77.59384
},
"geometry" : {
"type" : "Point",
"coordinates" : [ -77.59384, 34.21092 ]
}
},
{
"type" : "Feature",
"properties" : {
"Name " : "Mary",
"Status" : "Dev",
"imageUrl" : "markers/Dev.svg",
"lat" : 32.49323,
"lon" : -78.43144
},
"geometry" : {
"type" : "Point",
"coordinates" : [ -78.43144, 32.49323 ]
}
}
]
}
这是数据表:
Name Status imageUrl lat lon
Joe Dev markers/Dev.svg 34.21092 -77.59384
Mary Dev markers/Dev.svg 32.49323 -78.43144
解决方案
如果您实现一些类,这会容易得多
public abstract class TypedClass
{
public string type { get; set; }
}
public class Envelope : TypedClass
{
public List<Feature> features { get; set; }
public GeoJSONObject ToGeoJSON()
{
return new GeoJSONObject()
{
type = type,
features = new List<GeoJSONFeature>(features.Select(x => x.ToGeoJSONFeature()))
};
}
}
public class Feature
{
[JsonProperty("Name ")]
public string Name { get; set; }
public string Status { get; set; }
public string imageUrl { get; set; }
public double lat { get; set; }
public double Lon { get; set; }
public GeoJSONFeature ToGeoJSONFeature()
{
return new GeoJSONFeature()
{
type = "Feature",
properties = this,
geometry = new Geometry()
{
type = "Point",
coordinates = new List<double>() { Lon, lat }
}
};
}
}
public class GeoJSONObject : TypedClass
{
public List<GeoJSONFeature> features { get; set; }
}
public class GeoJSONFeature : TypedClass
{
public Feature properties { get; set; }
public Geometry geometry { get; set; }
}
public class Geometry : TypedClass
{
public List<double> coordinates { get; set; }
}
用法:
Envelope envelope = JsonConvert.DeserializeObject<Envelope>(json);
GeoJSONObject gjo = envelope.ToGeoJSON();
string geo_json = JsonConvert.SerializeObject(gjo);
推荐阅读
- javascript - 使用 webpack 将 20 多个入口点转换为 1 个或几个
- node.js - POST 请求失败,无法登录
- wso2 - WSO2 身份服务器 - 如何以编程方式更新密码历史设置
- sails.js - Sails.JS 模型无法全局访问,即使 Globals.JS 包含模型:从 0.12 升级到 1.4.3 后的 true
- python - 从 Python 3 中的嵌套文件夹导入库
- python-3.x - 我的测试有错误:预期值 ['Lambda', (None, 90), 0] 但我有:['TensorFlowOpLayer', [(None, 90)], 0]
- javascript - 如何删除垃圾邮件?
- python-3.x - 复制网站值并将其粘贴到文本文件 python
- sql - 关于 Postgres 和 oracle 在简单查询中的区别的问题
- informatica - Informatica Workflow Manager:如何根据某些条件控制工作流的流程