/// <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);
            }
        }
Exemple #2
0
        /// <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()
        {
            const string json = "{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[9.536067,55.708116]}}";

            GeoJsonFeature feature = GeoJsonFeature.Parse(json);

            Assert.IsNotNull(feature);
            Assert.IsNotNull(feature.Geometry);
            Assert.IsNotNull(feature.Properties);

            GeoJsonPoint point = feature.Geometry as GeoJsonPoint;

            Assert.IsNotNull(point);
            Assert.AreEqual(9.536067, point.X, "X");
            Assert.AreEqual(55.708116, point.Y, "Y");
            Assert.AreEqual(0, point.Altitude, "Altitude");
        }
        public void Parse2()
        {
            const string json = "{\"type\":\"Feature\",\"properties\":{\"name\":\"My feature\"},\"geometry\":{\"type\":\"Point\",\"coordinates\":[9.536067,55.708116,100]}}";

            GeoJsonFeature feature = GeoJsonFeature.Parse(json);

            Assert.IsNotNull(feature);
            Assert.IsNotNull(feature.Geometry);
            Assert.IsNotNull(feature.Properties);

            GeoJsonPoint point = feature.Geometry as GeoJsonPoint;

            Assert.IsNotNull(point);
            Assert.AreEqual(9.536067, point.X, "X");
            Assert.AreEqual(55.708116, point.Y, "Y");
            Assert.AreEqual(100, point.Altitude, "Altitude");

            Assert.AreEqual("My feature", feature.Properties.Name);
            Assert.AreEqual(null, feature.Properties.Description);
        }