10

Hi I am trying to parse the response from a OSM webservice into feature collection using GeoJson.Net

I am new to GeoJSON and not able to identify how to do so:

The Json response can be find here. The code I have written is:

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

But this will be plain JSON parsing and there might be a better way to parse the same.

I also tried NetTopologySuite JSON extension but when i use following code it gives me exception

"Expected token 'type' not found."

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);
Mohit Vashistha
  • 1,824
  • 3
  • 22
  • 49

2 Answers2

16

I hate to answer my I question but after two days of hit & trial I get it working with both NetTopology and 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();
    }
   }
Mohit Vashistha
  • 1,824
  • 3
  • 22
  • 49
  • Hi there, been trying to get the geojson to work on my end found your solution. Only thing I'm missing is what is "Point3D" and "CoordinateSequence" I'm error'ng out on these two elements... thanks – Tim Maxey Jan 23 '17 at 19:23
  • Point3D is System.Media.Point3D structure to store point value in my application, you can use any of your structure to store your value. And CoordinateSequence is my collection class to store the points in a list and this class do bunch of operations on the points collection. But basically you can use any of your class. – Mohit Vashistha Jan 25 '17 at 10:49
  • Hi, I am having some problems with geoJson polygon values. Looking @Mohit example, to loop through the list of points or coordinates in the Polygon now returns an error with the new version of GeoJson (v 1.1.64) on Nuget. Can you please enlighten me on what the problem could be? – Kunbi Oct 23 '17 at 11:31
1
//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);
                                }
                            }



Rahul t.
  • 11
  • 4