c# - 在 C# 中创建 geoJson 对象

标签 c# geojson

我尝试通过仅将纬度和经度传递给在 PoCo 下面实例化的函数来创建 GeoJson FeatureCollection 对象。

namespace PoCo
{

public class LocalGeometry
{
    public string type { get; set; }
    public List<double> coordinates { get; set; }
}

public class Properties
{
    public string name { get; set; }
    public string address { get; set; }
    public string id { get; set; }
}

public class LocalFeature
{
    public string type { get; set; }
    public LocalGeometry geometry { get; set; }
    public Properties properties { get; set; }
}

public class geoJson
{
    public string type { get; set; }
    public List<LocalFeature> features { get; set; }
}

}

这就是创建对象的方式

var CorOrd = new LocalGeometry();
            CorOrd.coordinates.Add(Lat);
            CorOrd.coordinates.Add(Lang);
            CorOrd.type = "Point";


var geoJson = new geoJson
            {
                type = "FeatureCollection",
                features = new LocalFeature
                {
                    type = "Feature",
                    geometry = CorOrd
                }
            };

但我收到错误

CS0029 Cannot implicitly convert type 'PoCo' to 'System.Collections.Generic.List<PoCo.Local>'.

关于如何在此处创建 GeoJson 对象的任何建议。

最佳答案

以下分配无效 -

功能 = 新的 LocalFeature

它应该是一个LocalFeature列表 -

features = new List<LocalFeature>
{
   new LocalFeature { type = "Feature", geometry = CorOrd}
}

此外,您需要在添加之前实例化一个列表。否则,它将抛出NullReferenceException

ar CorOrd = new LocalGeometry();
CorOrd.coordinates = new List<double>(); // <=====
CorOrd.coordinates.Add(Lat);
CorOrd.coordinates.Add(Lang);
CorOrd.type = "Point";

关于c# - 在 C# 中创建 geoJson 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38731047/

相关文章:

c# - 创建 NHibernate Queryover 以获取两个字段乘积的总和

c# - Xamarin.Mac 中是否缺少 CloudKit?

c# - 来自 .Net 的异步电子邮件

r - 我正在尝试根据 ZevRoss 博客使用 LeafletR 包制作交互式 map 。但是代码有错误

javascript - 如何修改或删除 GeoJSON Leaflet 对象中的某些现有数据?

javascript - 如何根据属性值过滤 MapBox 热图中的点?

c# - Kernel32.dll 中的 CreateFile 返回无效句柄

c# - 使用 Dapper 的 IDbConnection 工厂

javascript - 如何在 GEOjson (Javascript) 中删除多余的 "

python - geojson转换为Elasticsearch:无法解析类型为[geo_shape]的字段[geometry.coordinates]