将属性添加到GeoJSON

时间:2017-05-31 13:18:08

标签: c# asp.net json json.net geojson

我有一个令人兴奋的GeoJSON文件,就像这样;

{
  "type": "FeatureCollection",
  "crs": {
    "type": "name",
    "properties": {
      "name": "urn:ogc:def:crs:OGC:1.3:CRS84"
    }
  },
  "features": [
    {
      "type": "Feature",
      "properties": {
        "Item": "Value"
      },
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              9.449194065548566,
              55.86046393906458,
              -999
            ],
            [
              9.460203211292942,
              55.8619238071893,
              -999
            ],
            [
              9.440463307997378,
              55.876740797773365,
              -999
            ]
          ]
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {
        "Item": "Value"
      },
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              8.59655725301728,
              55.53506085541584,
              -999
            ],
            [
              8.601439658322603,
              55.52856219238175,
              -999
            ]
          ]
        ]
      }
    }
  ]
}

我需要加载此文件,为每个功能添加属性,并将其另存为新的json文件。 在C#中最好的解决方法是什么?

我可以像这样加载文件;

using (StreamReader r = new StreamReader(Server.MapPath("~/test.json")))
       {
           string json = r.ReadToEnd();
           List<RootObject> ro = JsonConvert.DeserializeObject<List<RootObject>>(json);
       }

但是,那么呢?

1 个答案:

答案 0 :(得分:0)

您可以使用GDAL / OGR的OGR库部分阅读geojson文件。 OGR能够直接读取geojson格式: http://gdal.org/1.11/ogr/drv_geojson.html

不幸的是,C#GDAL / OGR绑定的文档很差。

但您可以在此示例中了解如何使用OGR访问geojson功能:

https://trac.osgeo.org/gdal/browser/trunk/gdal/swig/csharp/apps/OGRFeatureEdit.cs

所以你可以使用:

Ogr.RegisterAll(); // To register the OGR drivers especially geojson
DataSource ds = Ogr.Open(<path to your geojson>, 1 );
Layer layer = ds.GetLayerByName("the name of the layer");

// Finally iterate over all features you want to modify
Feature feature = layer.GetFeature(0);
feature.SetField(<set your fields>);

// write your feature back to your layer
layer.SetFeature(feature)