public void Polygons()
        {
            GeoLineString line = new GeoLineString(
                new[]
                {
                    new GeoPosition(0, 0),
                    new GeoPosition(0, 1),
                    new GeoPosition(1, 1),
                    new GeoPosition(0, 0),
                });
            Assert.AreEqual(
                "geo.intersects(Foo, geography'POLYGON((0 0,0 1,1 1,0 0))')",
                SearchFilter.Create($"geo.intersects(Foo, {line})"));

            GeoPolygon polygon = new GeoPolygon(line.Coordinates);
            Assert.AreEqual(
                "geo.intersects(Foo, geography'POLYGON((0 0,0 1,1 1,0 0))')",
                SearchFilter.Create($"geo.intersects(Foo, {polygon})"));

            Assert.Throws<ArgumentException>(() => SearchFilter.Create(
                $"{new GeoLineString(new[] { new GeoPosition(0, 0) })}"));
            Assert.Throws<ArgumentException>(() => SearchFilter.Create(
                $"{new GeoLineString(new[] { new GeoPosition(0, 0), new GeoPosition(0, 0), new GeoPosition(0, 0), new GeoPosition(1, 1) })}"));
            Assert.Throws<ArgumentException>(() => SearchFilter.Create(
                $"{new GeoPolygon(new[] { new GeoLinearRing(line.Coordinates), new GeoLinearRing(line.Coordinates) })}"));
        }
Пример #2
0
        public void Can_Serialize_LineString_Feature()
        {
            var coordinates = new[]
            {
                new List <IGeoEntity>
                {
                    new GeoEntity(4.88925933837, 52.37072588121),
                    new GeoEntity(4.89526748657, 52.371145110),
                    new GeoEntity(4.89209175109, 52.3693109527),
                    new GeoEntity(4.88925933837, 52.37072588121)
                },
                new List <IGeoEntity>
                {
                    new GeoEntity(4.98925933837, 52.37072588121),
                    new GeoEntity(4.99526748657, 52.371145110),
                    new GeoEntity(4.99209175109, 52.3693109527),
                    new GeoEntity(4.98925933837, 52.37072588121)
                }
            };

            var geometry = new GeoLineString(coordinates[0]);

            var actualJson = JsonSerializer.SerializeToString(new Net.Feature.Feature(geometry));

            var expectedJson = GetExpectedJson();

            JsonAssert.AreEqual(expectedJson, actualJson);
        }
        public void FeatureCollectionSerialization()
        {
            var model = new FeatureCollection();

            for (var i = 10; i-- > 0;)
            {
                var geom = new GeoLineString(new[]
                {
                    new GeoEntity(-1.034, 51.010),
                    new GeoEntity(-0.034, 51.010)
                });

                var props = new Dictionary <string, object>
                {
                    { "test1", "1" },
                    { "test2", 2 }
                };

                var feature = new Net.Feature.Feature(geom, props);
                model.Features.Add(feature);
            }

            var actualJson = JsonSerializer.SerializeToString(model);

            Assert.IsNotNull(actualJson);

            Assert.IsFalse(string.IsNullOrEmpty(actualJson));
        }
Пример #4
0
 public void CreateLineString()
 {
     #region Snippet:CreateLineString
     var line = new GeoLineString(new[]
     {
         new GeoPosition(-122.108727, 47.649383),
         new GeoPosition(-122.081538, 47.640846),
         new GeoPosition(-122.078634, 47.576066),
         new GeoPosition(-122.112686, 47.578559),
     });
     #endregion
 }
Пример #5
0
        public void Is_Not_Closed()
        {
            var coordinates = new List <GeoEntity>
            {
                new GeoEntity(4.889259338378906, 52.370725881211314),
                new GeoEntity(4.895267486572266, 52.3711451105601),
                new GeoEntity(4.892091751098633, 52.36931095278263),
                new GeoEntity(4.889259338378955, 52.370725881211592)
            };

            var lineString = new GeoLineString(coordinates);

            Assert.IsFalse(lineString.IsClosed());
        }
Пример #6
0
        public void Can_Serialize()
        {
            var coordinates = new List <GeoEntity>
            {
                new GeoEntity(4.8892593383789, 52.370725881211),
                new GeoEntity(4.89526748657226, 52.371145110562),
                new GeoEntity(4.89209175109863, 52.369310952782),
                new GeoEntity(4.8892593383789, 52.370725881211)
            };

            var lineString = new GeoLineString(coordinates);

            var actualJson = JsonSerializer.SerializeToString(lineString);

            JsonAssert.AreEqual(GetExpectedJson(), actualJson);
        }
Пример #7
0
        public void Can_Deserialize()
        {
            var coordinates = new List <GeoEntity>
            {
                new GeoEntity(4.889259338378906, 52.370725881211314),
                new GeoEntity(4.895267486572266, 52.3711451105601),
                new GeoEntity(4.892091751098633, 52.36931095278263),
                new GeoEntity(4.889259338378906, 52.370725881211314)
            };

            var expectedLineString = new GeoLineString(coordinates);

            var json             = GetExpectedJson();
            var actualLineString = JsonSerializer.DeserializeFromString <GeoLineString>(json);

            Assert.AreEqual(expectedLineString, actualLineString);
        }
Пример #8
0
        // Support for both Azure.Core.GeoJson and Microsoft.Spatial encoding are duplicated
        // below to avoid extraneous allocations for adapters and to consolidate them to a single
        // source file for easier maintenance.

        /// <summary>
        /// Encodes a polygon for use in OData filters.
        /// </summary>
        /// <param name="line">The <see cref="GeoLineString"/> to encode.</param>
        /// <returns>The OData filter-encoded POLYGON string.</returns>
        /// <exception cref="ArgumentException">The <paramref name="line"/> has fewer than 4 points, or the first and last points do not match.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="line"/> or <see cref="GeoLineString.Coordinates"/> is null.</exception>
        public static string EncodePolygon(GeoLineString line)
        {
            Argument.AssertNotNull(line, nameof(line));

            if (line.Coordinates.Count < 4)
            {
                throw new ArgumentException(
                          $"A {nameof(GeoLineString)} must have at least four {nameof(GeoLineString.Coordinates)} to form a searchable polygon.",
                          $"{nameof(line)}");
            }
            else if (line.Coordinates[0] != line.Coordinates[line.Coordinates.Count - 1])
            {
                throw new ArgumentException(
                          $"A {nameof(GeoLineString)} must have matching first and last {nameof(GeoLineString.Coordinates)} to form a searchable polygon.",
                          $"{nameof(line)}");
            }

            return(EncodePolygon(new GeoLinearRing(line.Coordinates)));
        }
Пример #9
0
        public void LineCoordinatesWork()
        {
            var line = new GeoLineString(new GeoPosition[]
            {
                new GeoPosition(1, 2),
                new GeoPosition(3, 4),
                new GeoPosition(5, 6),
            });

            Assert.AreEqual(3, line.Coordinates.Count);
            Assert.AreEqual(3, line.Coordinates.Count());

            Assert.AreEqual(1, line.Coordinates[0][0]);
            Assert.AreEqual(2, line.Coordinates[0][1]);

            Assert.AreEqual(3, line.Coordinates[1][0]);
            Assert.AreEqual(4, line.Coordinates[1][1]);

            Assert.AreEqual(5, line.Coordinates[2][0]);
            Assert.AreEqual(6, line.Coordinates[2][1]);
        }
Пример #10
0
        /// <summary>
        /// Create an OData filter expression from an interpolated string.  The
        /// interpolated values will be quoted and escaped as necessary.
        /// </summary>
        /// <param name="filter">An interpolated filter string.</param>
        /// <param name="formatProvider">
        /// Format provider used to convert values to strings.
        /// <see cref="CultureInfo.InvariantCulture"/> is used as a default.
        /// </param>
        /// <returns>A valid OData filter expression.</returns>
        public static string Create(FormattableString filter, IFormatProvider formatProvider)
        {
            if (filter == null)
            {
                return(null);
            }
            formatProvider ??= CultureInfo.InvariantCulture;

            string[] args = new string[filter.ArgumentCount];
            for (int i = 0; i < filter.ArgumentCount; i++)
            {
                args[i] = filter.GetArgument(i) switch
                {
                    // Null
                    null => "null",

                    // Boolean
                    bool x => x.ToString(formatProvider).ToLowerInvariant(),

                    // Numeric
                    sbyte x => x.ToString(formatProvider),
                    byte x => x.ToString(formatProvider),
                    short x => x.ToString(formatProvider),
                    ushort x => x.ToString(formatProvider),
                    int x => x.ToString(formatProvider),
                    uint x => x.ToString(formatProvider),
                    long x => x.ToString(formatProvider),
                    ulong x => x.ToString(formatProvider),
                    decimal x => x.ToString(formatProvider),

                    // Floating point
                    float x => JsonSerialization.Float(x, formatProvider),
                    double x => JsonSerialization.Double(x, formatProvider),

                    // Dates as 8601 with a time zone
                    DateTimeOffset x => JsonSerialization.Date(x, formatProvider),
                    DateTime x => JsonSerialization.Date(x, formatProvider),

                    // Points
                    GeoPosition x => EncodeGeography(x),
                    GeoPoint x => EncodeGeography(x),

                    // Polygons
                    GeoLineString x => EncodeGeography(x),
                    GeoPolygon x => EncodeGeography(x),

                    // Text
                    string x => Quote(x),
                    char x => Quote(x.ToString(formatProvider)),
                    StringBuilder x => Quote(x.ToString()),

                    // Microsoft.Spatial types
                    object x when SpatialProxyFactory.TryCreate(x, out GeographyProxy proxy) => proxy.ToString(),

                    // Everything else
                    object x => throw new ArgumentException(
                              $"Unable to convert argument {i} from type {x.GetType()} to an OData literal.")
                };
            }
            string text = string.Format(formatProvider, filter.Format, args);
            return(text);
        }
Пример #11
0
 /// <summary>
 /// Convert a <see cref="GeoLineString"/> forming a polygon to an OData
 /// value.  A GeoLine must have at least four
 /// <see cref="GeoLineString.Coordinates"/> and the first and last must
 /// match to form a searchable polygon.
 /// </summary>
 /// <param name="line">The line forming a polygon.</param>
 /// <returns>The OData representation of the line.</returns>
 private static string EncodeGeography(GeoLineString line) =>
 SpatialFormatter.EncodePolygon(line);