将GeoJSON响应转换为FeatureCollection

时间:2016-06-10 13:45:32

标签: c# json geojson nettopologysuite

您好我正在尝试使用GeoJson.Net解析来自OSM Web服务的响应到功能集合

我是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,但是当我使用下面的代码时,它给了我异常

&#34;预期令牌&#39;类型&#39;没找到。&#34;

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);

2 个答案:

答案 0 :(得分:3)

我讨厌回答我的问题但经过两天的打击后试用我使用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();
    }
   }

答案 1 :(得分:3)

//Steps to Converting GeoJSON response to FeatureCollection
//1. Add NetTopologySuite.IO.GeoJson package from Nuget Package Manager.
//2. Write Following Code Snap:


 string Filepath = "Your filepath here";
    var josnData = File.ReadAllText(Filepath);
                        var reader = new NetTopologySuite.IO.GeoJsonReader();
                        var featureCollection = 
      reader.Read<GeoJSON.Net.Feature.FeatureCollection>(josnData);

    //in my case i did it like this
     for (int fIndex = 0; fIndex < featureCollection.Features.Count; fIndex++)
                        {
                            var AreaDetails = 
     featureCollection.Features[fIndex].Properties;
                            for (int AIndex = 0; AIndex < AreaDetails.Count; 
     AIndex++)
                            {
                                var element = AreaDetails.ElementAt(AIndex);
                                var Key = element.Key;
                                var Value = element.Value;
                                if (Key == "GML_ID")
                                {
                                    areaDetails.StateCode = Value.ToString();
                                }
                                else if (Key == "STNAME")
                                {
                                    areaDetails.State = Value.ToString();
                                }
                                else if (Key == "DISTFULL")
                                {
                                    areaDetails.DistrictCode = Value.ToString();
                                }
                                else if (Key == "DTNAME")
                                {
                                    areaDetails.District = Value.ToString();
                                }
                                else if (Key == "IPCODE")
                                {
                                    areaDetails.TalukaCode = Value.ToString();
                                }
                                else if (Key == "IPNAME")
                                {
                                    areaDetails.Taluka = Value.ToString();
                                }
                                else if (Key == "VLGCD2001")
                                {
                                    areaDetails.AreaCode = Value.ToString();
                                }
                                else if (Key == "VILLNAME")
                                {
                                    areaDetails.AreaName = Value.ToString();
                                }
                            }
                            var AreaCoords = featureCollection.Features[fIndex].Geometry;
                            var Type = AreaCoords.Type;
                            LocationDetails locationDetails = new LocationDetails();

                            if (Type == GeoJSONObjectType.Polygon)
                            {
                                var polygon = AreaCoords as GeoJSON.Net.Geometry.Polygon;
                                var polygonCoords = polygon.Coordinates[0].Coordinates;

                                for (int AIndex = 0; AIndex < polygonCoords.Count; AIndex++)
                                {
                                    locationDetails.lat = Convert.ToDecimal(polygonCoords[AIndex].Latitude);
                                    locationDetails.lng = Convert.ToDecimal(polygonCoords[AIndex].Longitude);
                                    locationDetailsList.Add(locationDetails);
                                }
                            }