c# - 将 GeoJSON 响应转换为 FeatureCollection

标签 c# json geojson nettopologysuite

您好,我正在尝试使用 GeoJson.Net 将来自 OSM 网络服务的响应解析为特征集合

我是 GeoJSON 的新手,无法确定如何操作:

可以找到 Json 响应 here .我写的代码是:

  System.IO.StreamReader file = new System.IO.StreamReader(filepath);
  string content = file.ReadToEnd();
  file.Close();

  dynamic deserialized = JsonConvert.DeserializeObject(content);

  List<Feature> lstGeoLocation = new List<Feature>();
  foreach (JObject item in deserialized.features)
  {
    //var feature = new Feature();
    var geom = item.Property("geometry").Value;
  }

但这将是纯 JSON 解析,可能有更好的解析方法。

我也试过NetTopologySuite JSON extension但是当我使用下面的代码时,它给了我异常

“未找到预期的 token ‘类型’。”

System.IO.StreamReader file = new System.IO.StreamReader(filepath);
      string content = file.ReadToEnd();
      file.Close();


      var reader = new NetTopologySuite.IO.GeoJsonReader();

      var featureCollection = reader.Read <NetTopologySuite.Features.FeatureCollection>(content);

最佳答案

我不想回答我的问题,但经过两天的点击和试用后,我可以同时使用 NetTopology 和 GeoJson

// get the JSON file content
var josnData = File.ReadAllText(destinationFileName);

// create NetTopology JSON reader
var reader = new NetTopologySuite.IO.GeoJsonReader();

// pass geoJson's FeatureCollection to read all the features
var featureCollection = reader.Read<GeoJSON.Net.Feature.FeatureCollection>(josnData);

// if feature collection is null then return 
if (featureCollection == null)
{
   return;
}

// loop through all the parsed featurd   
for (int featureIndex = 0;
     featureIndex < featureCollection.Features.Count;
     featureIndex++)
{
  // get json feature
  var jsonFeature = featureCollection.Features[featureIndex];
  Geometry geom = null;

   // get geometry type to create appropriate geometry
    switch (jsonFeature.Geometry.Type)
    {
      case GeoJSONObjectType.Point:
        break;
      case GeoJSONObjectType.MultiPoint:
        break;
      case GeoJSONObjectType.LineString:
        break;
      case GeoJSONObjectType.MultiLineString:
        break;
      case GeoJSONObjectType.Polygon:
      {
        var polygon = jsonFeature.Geometry as GeoJSON.Net.Geometry.Polygon;

        var coordinates = new List <Point3D>();
        foreach (var ring in polygon.Coordinates)
        {
          if (ring.IsLinearRing())
          {
            foreach (var coordinate in ring.Coordinates)
            {
              var location = coordinate as GeographicPosition;

              if (location == null)
              {
                continue;
              }

              coordinates.Add(new Point3D(location.Longitude,
                                          location.Latitude,
                                          location.Altitude.HasValue ? location.Altitude.Value : 0 ));
            }
          }
        }


        geom = new Polygon(new LinearRing(new CoordinateSequence(coordinates.ToArray())),
                           null);
      }
       break;
      case GeoJSONObjectType.MultiPolygon:
        break;
      case GeoJSONObjectType.GeometryCollection:
        break;
      case GeoJSONObjectType.Feature:
        break;
      case GeoJSONObjectType.FeatureCollection:
        break;
      default:
        throw new ArgumentOutOfRangeException();
    }
   }

关于c# - 将 GeoJSON 响应转换为 FeatureCollection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37749891/

相关文章:

c# - WCF Web 服务上的 System.OutOfMemoryException

java - Gson如何动态设置序列化名称

javascript - 带传单的多边形标签

mapbox - 项目数据不更新/Mapbox

c# - CSharpCodeProvider 设置编译细节

c# - 使用 LINQ 加速查询

c# - C#-拆分字符串问题

javascript - 为什么我在 JSON.stringify(value, replacer) 中的替换函数在 Angular 应用程序中没有收到 key ?

java - 如何断言 JSON 数组中具有相同键但值不同的两个 JSON 对象

javascript - 从 API 加载 geoJSON 标记 - React Leaflet