Пример #1
0
        /// <summary>
        /// Read the GeoJSON type value.
        /// </summary>
        /// <param name="jsonObject">
        /// The JSON object.
        /// </param>
        /// <param name="coordinateSystem">
        /// The coordinate System.
        /// </param>
        /// <returns>
        /// A function that can read the value.
        /// </returns>
        /// <exception cref="ArgumentException">
        /// Unexpected JSON.
        /// </exception>
        /// <remarks>
        /// Leaves the reader positioned where the value should start.
        /// </remarks>
        public static GeoBase ParseJsonObjectToGeoBase(DbGeographyGeoJsonConverter converter, JObject jsonObject, out int? coordinateSystem)
        {
            var type = jsonObject["type"];
            if (type == null)
            {
                throw new ArgumentException(string.Format("Expected a \"type\" property, found [{0}]", string.Join(", ", jsonObject.Properties().Select(p => p.Name))));
            }

            if (type.Type != JTokenType.String)
            {
                throw new ArgumentException(string.Format("Expected a string token for the type of the GeoJSON type, got {0}", type.Type), "jsonObject");
            }

            var crs = jsonObject["crs"];
            coordinateSystem = crs != null ? converter.GetCoordinateSystem(crs.Value<JObject>()) : null;

            Func<GeoBase> geoType;
            if (!GeoBases.TryGetValue(type.Value<string>(), out geoType))
            {
                throw new ArgumentException(
                string.Format(
                "Got unsupported GeoJSON object type {0}. Expected one of [{1}]",
                type.Value<string>(),
                string.Join(", ", GeoBases.Keys)),
                "jsonObject");
            }

            var geo = geoType();
            geo.CoordinateSystem = coordinateSystem;
            var isCollection = typeof(Collection).IsAssignableFrom(geo.GetType());
            var geoObject = isCollection ? jsonObject["geometries"] : jsonObject["coordinates"];
            if (geoObject == null)
            {
                throw new ArgumentException(
                string.Format(
                "Expected a field named \"{0}\", found [{1}]",
                isCollection ? "geometries" : "coordinates",
                string.Join(", ", jsonObject.Properties().Select(p => p.Name))),
                "jsonObject");
            }

            geo.ParseJson(converter, geoObject.Value<JArray>());

            coordinateSystem = geo.CoordinateSystem;
            return geo;
        }