/// <summary> /// Parses the specified <paramref name="json"/> object into an instance deriving from <see cref="GeoJsonObject"/>. /// /// As the GeoJSON format specified the type of a /// </summary> /// <param name="json">The JSON object.</param> /// <returns>An instance that derives from <see cref="GeoJsonObject"/>.</returns> private static GeoJsonObject Parse(JObject json) { if (json == null) { return(null); } // Get the value of the "type" property string type = json.GetString("type"); if (string.IsNullOrWhiteSpace(type)) { throw new GeoJsonParseException("The JSON object doesn't specify a type", json); } // Parse the type into an enum if (EnumUtils.TryParseEnum(type, out GeoJsonType result) == false) { throw new GeoJsonParseException($"Unknown type {type}", json); } switch (result) { case GeoJsonType.Feature: return(GeoJsonFeature.Parse(json)); case GeoJsonType.FeatureCollection: return(GeoJsonFeatureCollection.Parse(json)); case GeoJsonType.Point: return(GeoJsonPoint.Parse(json)); case GeoJsonType.LineString: return(GeoJsonLineString.Parse(json)); case GeoJsonType.Polygon: return(GeoJsonPolygon.Parse(json)); case GeoJsonType.MultiPoint: return(GeoJsonMultiPoint.Parse(json)); //case GeoJsonType.MultiLineString: // return GeoJsonMultiLineString.Parse(obj); case GeoJsonType.MultiPolygon: return(GeoJsonMultiPolygon.Parse(json)); case GeoJsonType.GeometryCollection: return(GeoJsonGeometryCollection.Parse(json)); default: throw new GeoJsonParseException($"Unknown type {type}", json); } }
/// <inheritdoc /> public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { if (reader.TokenType == JsonToken.Null) { return(null); } if (reader.TokenType != JsonToken.StartObject) { return(null); } JObject obj = JObject.Load(reader); string type = obj.Value <string>("type"); switch (type?.ToLower()) { case "feature": return(GeoJsonFeature.Parse(obj)); case "featurecollection": return(GeoJsonFeatureCollection.Parse(obj)); case "point": return(GeoJsonPoint.Parse(obj)); case "multipoint": return(GeoJsonMultiPoint.Parse(obj)); case "linestring": return(GeoJsonLineString.Parse(obj)); case "multilinestring": return(GeoJsonMultiLineString.Parse(obj)); case "polygon": return(GeoJsonPolygon.Parse(obj)); case "multipolygon": return(GeoJsonMultiPolygon.Parse(obj)); default: if (objectType == typeof(GeoJsonProperties)) { return(ReadJsonProperties(reader)); } throw new GeoJsonParseException($"Unknown shape: {type}", obj); } }
public void Parse1() { GeoJsonMultiPoint mp = GeoJsonMultiPoint.Parse("{\"type\":\"MultiPoint\",\"coordinates\":[[10.0,40.0],[40.0,30.0],[20.0,20.0],[30.0,10.0]]}"); Assert.AreEqual(4, mp.Count); GeoJsonCoordinates p1 = mp[0]; GeoJsonCoordinates p2 = mp[1]; GeoJsonCoordinates p3 = mp[2]; GeoJsonCoordinates p4 = mp[3]; Assert.AreEqual(10, p1.X, "#1 X"); Assert.AreEqual(40, p1.Y, "#1 Y"); Assert.AreEqual(40, p2.X, "#2 X"); Assert.AreEqual(30, p2.Y, "#2 Y"); Assert.AreEqual(20, p3.X, "#3 X"); Assert.AreEqual(20, p3.Y, "#3 Y"); Assert.AreEqual(30, p4.X, "#4 X"); Assert.AreEqual(10, p4.Y, "#4 Y"); }