Esempio n. 1
0
        /// <summary>
        /// Create a <see cref="DbGeography"/> or <see cref="DbGeometry"/> from <paramref name="jsonObject"/>.
        /// </summary>
        /// <param name="jsonObject">
        /// The JSON object.
        /// </param>
        /// <param name="objectType">
        /// The object type.
        /// </param>
        /// <returns>
        /// The <see cref="DbGeography"/> or <see cref="DbGeometry"/> 
        /// </returns>
        /// <exception cref="ArgumentException">
        /// <paramref name="objectType"/> is not a <see cref="DbGeography"/> or <see cref="DbGeometry"/>.
        /// </exception>
        private static object CreateDbGeo(DbGeographyGeoJsonConverter converter, JObject jsonObject, Type objectType)
        {
            Func<Tuple<byte[], int>, object> returnValue;
            int defaultCoordinateSystemId;
            if (typeof(DbGeography).IsAssignableFrom(objectType))
            {
                returnValue = x => (object)DbGeography.FromBinary(x.Item1, x.Item2);
                defaultCoordinateSystemId = DbGeography.DefaultCoordinateSystemId;
            }
            else if (typeof(DbGeometry).IsAssignableFrom(objectType))
            {
                returnValue = x => (object)DbGeometry.FromBinary(x.Item1, x.Item2);
                defaultCoordinateSystemId = DbGeometry.DefaultCoordinateSystemId;
            }
            else
            {
                throw new ArgumentException(string.Format("Expected a DbGeography or DbGeometry objectType. Got {0}", objectType.Name), "objectType");
            }

            return jsonObject.Type == JTokenType.Null || jsonObject.Type == JTokenType.None ? null : returnValue(GetWellKnownBinary(converter, jsonObject, defaultCoordinateSystemId));
        }
Esempio n. 2
0
        /// <summary>
        /// Get well known binary from a <see cref="JsonReader"/>.
        /// </summary>
        /// <param name="jsonObject">
        /// The JSON object.
        /// </param>
        /// <param name="defaultCoordinateSystemId">
        /// The default coordinate system id.
        /// </param>
        /// <returns>
        /// A tuple of the well-known-binary and the coordinate system identifier.
        /// </returns>
        /// <exception cref="ArgumentException">
        /// Unexpected JSON.
        /// </exception>
        private static Tuple<byte[], int> GetWellKnownBinary(DbGeographyGeoJsonConverter converter, JObject jsonObject, int defaultCoordinateSystemId)
        {
            var ob = new MemoryStream();

            int? coordinateSystemId;
            var geoBase = ParseJsonObjectToGeoBase(converter, jsonObject, out coordinateSystemId);
            geoBase.WellKnownBinary(ob);
            return new Tuple<byte[], int>(ob.ToArray(), coordinateSystemId.HasValue ? coordinateSystemId.Value : defaultCoordinateSystemId);
        }
Esempio n. 3
0
 /// <inheritdoc/>
 public override void ParseJson(DbGeographyGeoJsonConverter converter,JArray array)
 {
     this.Rings = GeoBase.ParseListListPosition(array);
 }
Esempio n. 4
0
            /// <inheritdoc/>
            public override void ParseJson(DbGeographyGeoJsonConverter converter,JArray array)
            {
                if (array.Count < 2)
                {
                    throw new ArgumentException(string.Format("Expected at least 2 points for a position, got {0}", array.Count), "array");
                }

                this.P1 = (double)array[0];
                this.P2 = (double)array[1];
                if (array.Count > 2)
                {
                    this.P3 = (double)array[2];
                    if (array.Count > 3)
                    {
                        this.P4 = (double)array[3];
                    }
                }
            }
Esempio n. 5
0
 /// <inheritdoc/>
 public override void ParseJson(DbGeographyGeoJsonConverter converter,JArray array)
 {
     this.Position = new Position(array);
 }
Esempio n. 6
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;
        }
Esempio n. 7
0
            /// <inheritdoc/>
            public override void ParseJson(DbGeographyGeoJsonConverter converter,JArray array)
            {
                if (array.Cast<JArray>().Any(l => l.Count < 2))
                {
                    throw new ArgumentException(
                    string.Format(
                    "Expected all points to have greater than two points, got {0} with zero and {1} with one",
                    array.Cast<JArray>().Count(l => l.Count == 0),
                    array.Cast<JArray>().Count(l => l.Count == 1)),
                    "array");
                }

                this.Points = array.Cast<JArray>().Select(l => new Point { Position = new Position(l) }).ToList();
            }
Esempio n. 8
0
 /// <summary>
 /// Parse JSON into the <see cref="GeoBase"/>-derived type.
 /// </summary>
 /// <param name="array">
 /// Get JSON from this.
 /// </param>
 public abstract void ParseJson(DbGeographyGeoJsonConverter converter, JArray array);
Esempio n. 9
0
            /// <inheritdoc/>
            public override void ParseJson(DbGeographyGeoJsonConverter converter, JArray array)
            {
                this.Entries = new List<GeoBase>();
                foreach (var elem in array)
                {
                    if (elem.Type != JTokenType.Object)
                    {
                        throw new ArgumentException(
                        string.Format("Expected object elements of the collection array, got {0}", elem.Type),
                        "array");
                    }

                    int? dummyCoordinateSystem;
                    this.Entries.Add(DbGeographyGeoJsonConverter.ParseJsonObjectToGeoBase(converter, (JObject)elem, out dummyCoordinateSystem));
                }
            }