/// <summary> /// Reads GeoJson and returns the feature collection. /// </summary> /// <param name="jsonReader"></param> /// <returns></returns> internal static FeatureCollection ReadFeatureCollection(JsonReader jsonReader) { var type = string.Empty; List<Feature> features = null; while (jsonReader.Read()) { if (jsonReader.TokenType == JsonToken.EndObject) { // end of geometry. break; } if (jsonReader.TokenType == JsonToken.PropertyName) { if ((string)jsonReader.Value == "type") { // the geometry type. type = jsonReader.ReadAsString(); } else if ((string)jsonReader.Value == "features") { // the geometry. features = GeoJsonConverter.ReadFeatureArray(jsonReader); } } } switch (type) { case "FeatureCollection": if (features == null) { return new FeatureCollection(); } return new FeatureCollection(features); } throw new Exception("Invalid type."); }
/// <summary> /// Reads GeoJson and returns the geometry. /// </summary> /// <param name="jsonReader"></param> /// <returns></returns> internal static Geometry ReadGeometry(JsonReader jsonReader) { var geometryType = string.Empty; var coordinates = new List<object>(); List<Geometry> geometries = null; while (jsonReader.Read()) { if (jsonReader.TokenType == JsonToken.EndObject) { // end of geometry. break; } if (jsonReader.TokenType == JsonToken.PropertyName) { if ((string)jsonReader.Value == "type") { // the geometry type. geometryType = jsonReader.ReadAsString(); } else if ((string)jsonReader.Value == "geometries") { // the geometries if a geometry collection. geometries = GeoJsonConverter.ReadGeometryArray(jsonReader); } else if ((string)jsonReader.Value == "coordinates") { // the coordinates. jsonReader.Read(); // move to first array start. coordinates = GeoJsonConverter.ReadCoordinateArrays(jsonReader); } } } // data has been read, instantiate the actual object. switch(geometryType) { case "Point": return GeoJsonConverter.BuildPoint(coordinates); case "LineString": return GeoJsonConverter.BuildLineString(coordinates); case "Polygon": return GeoJsonConverter.BuildPolygon(coordinates); case "MultiPoint": return GeoJsonConverter.BuildMultiPoint(coordinates); case "MultiLineString": return GeoJsonConverter.BuildMultiLineString(coordinates); case "MultiPolygon": return GeoJsonConverter.BuildMultiPolygon(coordinates); case "GeometryCollection": return GeoJsonConverter.BuildGeometryCollection(geometries); } throw new Exception(string.Format("Unknown geometry type: {0}", geometryType)); }
/// <summary> /// Reads GeoJson and returns the feature. /// </summary> /// <param name="jsonReader"></param> /// <returns></returns> internal static Feature ReadFeature(JsonReader jsonReader) { var type = string.Empty; Geometry geometry = null; GeometryAttributeCollection attributes = null; while (jsonReader.Read()) { if (jsonReader.TokenType == JsonToken.EndObject) { // end of geometry. break; } if (jsonReader.TokenType == JsonToken.PropertyName) { if ((string)jsonReader.Value == "type") { // the geometry type. type = jsonReader.ReadAsString(); } else if ((string)jsonReader.Value == "geometry") { // the geometry. geometry = GeoJsonConverter.ReadGeometry(jsonReader); } else if ((string)jsonReader.Value == "properties") { // the properties/attributes. attributes = GeoJsonConverter.ReadAttributes(jsonReader); } } } switch(type) { case "Feature": if(geometry == null) { throw new Exception("No geometry found."); } if(attributes != null) { return new Feature(geometry, attributes); } return new Feature(geometry); } throw new Exception("Invalid type."); }