首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GeoJson c#示例解析世界上的国家并为每个国家生成Geojson

GeoJson c#示例解析世界上的国家并为每个国家生成Geojson
EN

Stack Overflow用户
提问于 2016-08-21 03:14:15
回答 2查看 4.7K关注 0票数 3

寻找如何使用geojson.net解析/反序列化Geojson文件的示例。由于某些原因,没有关于如何使用geojson.net包的示例。

我想在我的网站上使用谷歌地图api。目前我使用多边形形状,但我希望将geojson对象用于层,因为这似乎是一种更好的格式。

使用c#,我想序列化Geojson,选择特定的国家边界,并生成一个新的geojson文件,该文件可以作为参考并作为图层添加到谷歌地图中。

为了测试这一点,我创建了一个colsole来尝试反序列化GeoJson,这不起作用(请您给我一些关于反序列化Geojson的正确方法的指导?)

代码语言:javascript
复制
        static void Main(string[] args)
    {
        string Jsonstring = File.ReadAllText("c:/worldborders.json");
        JavaScriptSerializer ser = new JavaScriptSerializer();
        List<GeoJsonProperties> ns = (List<GeoJsonProperties>)ser.Deserialize(Jsonstring, typeof(List<GeoJsonProperties>));

?ns is Empty?


    }

我使用在线生成器http://json2csharp.com/为geojson文件创建了一个类(我曾认为GeoJson.net会将这个类作为它的标准),GeoJsonProperties,

代码语言:javascript
复制
 public class GeoJsonProperties
{
    public int scalerank { get; set; }
    public string featurecla { get; set; }
    public double labelrank { get; set; }
    public string sovereignt { get; set; }
    public string sov_a3 { get; set; }
    public double adm0_dif { get; set; }
    public double level { get; set; }
    public string type { get; set; }
    public string admin { get; set; }
    public string adm0_a3 { get; set; }
    public double geou_dif { get; set; }
    public string geounit { get; set; }
    public string gu_a3 { get; set; }
    public double su_dif { get; set; }
    public string subunit { get; set; }
    public string su_a3 { get; set; }
    public double brk_diff { get; set; }
    public string name { get; set; }
    public string name_long { get; set; }
    public string brk_a3 { get; set; }
    public string brk_name { get; set; }
    public object brk_group { get; set; }
    public string abbrev { get; set; }
    public string postal { get; set; }
    public string formal_en { get; set; }
    public string formal_fr { get; set; }
    public string note_adm0 { get; set; }
    public string note_brk { get; set; }
    public string name_sort { get; set; }
    public string name_alt { get; set; }
    public double mapcolor7 { get; set; }
    public double mapcolor8 { get; set; }
    public double mapcolor9 { get; set; }
    public double mapcolor13 { get; set; }
    public double pop_est { get; set; }
    public double gdp_md_est { get; set; }
    public double pop_year { get; set; }
    public double lastcensus { get; set; }
    public double gdp_year { get; set; }
    public string economy { get; set; }
    public string income_grp { get; set; }
    public double wikipedia { get; set; }
    public object fips_10 { get; set; }
    public string iso_a2 { get; set; }
    public string iso_a3 { get; set; }
    public string iso_n3 { get; set; }
    public string un_a3 { get; set; }
    public string wb_a2 { get; set; }
    public string wb_a3 { get; set; }
    public double woe_id { get; set; }
    public string adm0_a3_is { get; set; }
    public string adm0_a3_us { get; set; }
    public double adm0_a3_un { get; set; }
    public double adm0_a3_wb { get; set; }
    public string continent { get; set; }
    public string region_un { get; set; }
    public string subregion { get; set; }
    public string region_wb { get; set; }
    public double name_len { get; set; }
    public double long_len { get; set; }
    public double abbrev_len { get; set; }
    public double tiny { get; set; }
    public double homepart { get; set; }
}

public class Geometry
{
    public string type { get; set; }
    public List<List<List<object>>> coordinates { get; set; }
}

public class Feature
{
    public string type { get; set; }
    public GeoJsonProperties  properties { get; set; }
    public Geometry geometry { get; set; }
}

public class RootObject
{
    public string type { get; set; }
    public List<Feature> features { get; set; }
}

}

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-03-06 22:58:04

GeoJSON.Net与Newtonsoft.Json一起工作。您可以使用与使用该库相同的方式进行反序列化。

代码语言:javascript
复制
var geoJsonObject = JsonConvert.DeserializeObject<Point>(json);
票数 3
EN

Stack Overflow用户

发布于 2018-10-06 21:47:08

要使用GeoJSON.Net库反序列化FeatureCollection对象(就像您在问题中提到的对象),请使用以下代码:

代码语言:javascript
复制
var collection = JsonConvert.DeserializeObject<FeatureCollection>(json);
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39057828

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档