public void Constructor2()
        {
            var outer1 = new [] {
                new [] { 9.531275, 55.714505 },
                new [] { 9.531503, 55.714701 },
                new [] { 9.531278, 55.714791 },
                new [] { 9.531038, 55.714599 },
                new [] { 9.531275, 55.714505 }
            };

            var outer2 = new [] {
                new [] { 9.531355, 55.714570 },
                new [] { 9.531450, 55.714528 },
                new [] { 9.531613, 55.714660 },
                new [] { 9.531504, 55.714702 },
                new [] { 9.531355, 55.714570 }
            };

            var coordinates = new []  {
                new [] { outer1 },
                new [] { outer2 }
            };


            GeoJsonMultiPolygon multiPolygon = new GeoJsonMultiPolygon(coordinates);

            Assert1(multiPolygon);
        }
        public void Constructor5()
        {
            var polygon1 = new GeoJsonPolygon(
                new GeoJsonCoordinates(9.531275, 55.714505),
                new GeoJsonCoordinates(9.531503, 55.714701),
                new GeoJsonCoordinates(9.531278, 55.714791),
                new GeoJsonCoordinates(9.531038, 55.714599),
                new GeoJsonCoordinates(9.531275, 55.714505)
                );

            var polygon2 = new GeoJsonPolygon(
                new GeoJsonCoordinates(9.531355, 55.714570),
                new GeoJsonCoordinates(9.531450, 55.714528),
                new GeoJsonCoordinates(9.531613, 55.714660),
                new GeoJsonCoordinates(9.531504, 55.714702),
                new GeoJsonCoordinates(9.531355, 55.714570)
                );

            List <GeoJsonPolygon> polygons = new List <GeoJsonPolygon> {
                polygon1, polygon2
            };

            GeoJsonMultiPolygon multiPolygon = new GeoJsonMultiPolygon(polygons);

            Assert1(multiPolygon);
        }
        public void Constructor3()
        {
            IPolygon polygon1 = new Polygon(
                new Point(55.714505, 9.531275),
                new Point(55.714701, 9.531503),
                new Point(55.714791, 9.531278),
                new Point(55.714599, 9.531038),
                new Point(55.714505, 9.531275)
                );

            IPolygon polygon2 = new Polygon(
                new Point(55.714570, 9.531355),
                new Point(55.714528, 9.531450),
                new Point(55.714660, 9.531613),
                new Point(55.714702, 9.531504),
                new Point(55.714570, 9.531355)
                );

            IPolygon[] array = { polygon1, polygon2 };

            List <IPolygon> list = new List <IPolygon> {
                polygon1, polygon2
            };

            GeoJsonMultiPolygon multiPolygon1 = new GeoJsonMultiPolygon(array);

            GeoJsonMultiPolygon multiPolygon2 = new GeoJsonMultiPolygon(list);

            Assert1(multiPolygon1);

            Assert1(multiPolygon2);
        }
        //Not supporting Z and M values
        //check for cw and cww criteria
        private static EsriPolygon ToEsriPolygon(GeoJsonMultiPolygon geometry, bool isLongitudeFirst, int srid, Func <IPoint, IPoint> mapFunction)
        {
            if (geometry.IsNullOrEmpty())
            {
                return(new EsriPolygon());
            }

            int numberOfGeometries = geometry.NumberOfGeometries();

            List <EsriPoint> points = new List <EsriPoint>(geometry.NumberOfPoints());

            List <int> parts = new List <int>(numberOfGeometries);

            for (int i = 0; i < numberOfGeometries; i++)
            {
                var tempPolygon = geometry.Coordinates[i];

                var numberOfRings = tempPolygon == null ? 0 : tempPolygon.Length;

                for (int j = 0; j < numberOfRings; j++)
                {
                    parts.Add(points.Count);

                    points.AddRange(GetPoints(tempPolygon[j], isLongitudeFirst, srid, mapFunction));
                }
            }

            return(new EsriPolygon(points.ToArray(), parts.ToArray()));

            //for (int i = 0; i < numberOfGeometries; i++)
            //{
            //    int index = i + 1;

            //    SqlGeometry tempPolygon = geometry.STGeometryN(index);

            //    var exterior = tempPolygon.STExteriorRing();

            //    if (tempPolygon.IsNullOrEmpty() || exterior.IsNullOrEmpty())
            //        continue;

            //    parts.Add(points.Count);

            //    points.AddRange(GetPoints(exterior, mapFunction));

            //    for (int j = 0; j < tempPolygon.STNumInteriorRing(); j++)
            //    {
            //        var interior = tempPolygon.STInteriorRingN(j + 1);

            //        if (interior.IsNullOrEmpty())
            //            continue;

            //        parts.Add(points.Count);

            //        points.AddRange(GetPoints(interior, mapFunction));
            //    }
            //}

            //return new EsriPolygon(points.ToArray(), parts.ToArray());
        }
        public void Constructor1()
        {
            GeoJsonMultiPolygon multiPolygon = new GeoJsonMultiPolygon();

            Assert.AreEqual(GeoJsonType.MultiPolygon, multiPolygon.Type);

            Assert.AreEqual(0, multiPolygon.Count);
        }
Example #6
0
        private void TestRoundTrip <TCoordinates>(string expected, GeoJsonMultiPolygon <TCoordinates> multiPolygon) where TCoordinates : GeoJsonCoordinates
        {
            var json = multiPolygon.ToJson();

            Assert.AreEqual(expected, json);

            var rehydrated = BsonSerializer.Deserialize <GeoJsonMultiPolygon <TCoordinates> >(json);

            Assert.AreEqual(expected, rehydrated.ToJson());
        }
        /// <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);
            }
        }
Example #8
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);
            }
        }
        private void Assert1(GeoJsonMultiPolygon multiPolygon)
        {
            Assert.AreEqual(GeoJsonType.MultiPolygon, multiPolygon.Type);

            Assert.AreEqual(2, multiPolygon.Count);

            GeoJsonPolygon polygon1 = multiPolygon[0];

            Assert.AreEqual(5, polygon1.Outer.Count);
            Assert.AreEqual(0, polygon1.Inner.Count);

            Assert.AreEqual(9.531275, polygon1.Outer[0].X, "Polygon 1 - #1 X");
            Assert.AreEqual(55.714505, polygon1.Outer[0].Y, "Polygon 1 - #1 X");

            Assert.AreEqual(9.531503, polygon1.Outer[1].X, "Polygon 1 - #2 X");
            Assert.AreEqual(55.714701, polygon1.Outer[1].Y, "Polygon 1 - #2 X");

            Assert.AreEqual(9.531278, polygon1.Outer[2].X, "Polygon 1 - #3 X");
            Assert.AreEqual(55.714791, polygon1.Outer[2].Y, "Polygon 1 - #3 X");

            Assert.AreEqual(9.531038, polygon1.Outer[3].X, "Polygon 1 - #4 X");
            Assert.AreEqual(55.714599, polygon1.Outer[3].Y, "Polygon 1 - #4 X");

            Assert.AreEqual(9.531275, polygon1.Outer[4].X, "Polygon 1 - #5 X");
            Assert.AreEqual(55.714505, polygon1.Outer[4].Y, "Polygon 1 - #5 X");

            GeoJsonPolygon polygon2 = multiPolygon[1];

            Assert.AreEqual(5, polygon2.Outer.Count);
            Assert.AreEqual(0, polygon2.Inner.Count);

            Assert.AreEqual(9.531355, polygon2.Outer[0].X, "Polygon 2 - #1 X");
            Assert.AreEqual(55.714570, polygon2.Outer[0].Y, "Polygon 2 - #1 X");

            Assert.AreEqual(9.531450, polygon2.Outer[1].X, "Polygon 2 - #2 X");
            Assert.AreEqual(55.714528, polygon2.Outer[1].Y, "Polygon 2 - #2 X");

            Assert.AreEqual(9.531613, polygon2.Outer[2].X, "Polygon 2 - #3 X");
            Assert.AreEqual(55.714660, polygon2.Outer[2].Y, "Polygon 2 - #3 X");

            Assert.AreEqual(9.531504, polygon2.Outer[3].X, "Polygon 2 - #4 X");
            Assert.AreEqual(55.714702, polygon2.Outer[3].Y, "Polygon 2 - #4 X");

            Assert.AreEqual(9.531355, polygon2.Outer[4].X, "Polygon 2 - #5 X");
            Assert.AreEqual(55.714570, polygon2.Outer[4].Y, "Polygon 2 - #5 X");
        }
        public async Task Search_Intersects()
        {
            var repository = new PDVRepository(new Data.DatabaseContext());

            var coverageArea = new GeoJsonMultiPolygon <GeoJson2DCoordinates>
                               (
                new GeoJsonMultiPolygonCoordinates <GeoJson2DCoordinates>
                (
                    new GeoJsonPolygonCoordinates <GeoJson2DCoordinates>[]
            {
                new GeoJsonPolygonCoordinates <GeoJson2DCoordinates>
                (
                    new GeoJsonLinearRingCoordinates <GeoJson2DCoordinates>
                    (
                        new GeoJson2DCoordinates[]
                {
                    new GeoJson2DCoordinates(-46.944580078125, -23.51362636346272),
                    new GeoJson2DCoordinates(-46.8841552734375, -23.74009762440226),
                    new GeoJson2DCoordinates(-46.4996337890625, -23.810475327766568),
                    new GeoJson2DCoordinates(-46.219482421875, -23.55391651832161),
                    new GeoJson2DCoordinates(-46.59301757812499, -23.301901124188877),
                    new GeoJson2DCoordinates(-46.944580078125, -23.51362636346272)
                }
                    )
                )
            }
                )
                               );

            var adrress = new GeoJsonPoint <GeoJson2DCoordinates>(new GeoJson2DCoordinates(0, 0));

            var document = Guid.NewGuid().ToString();

            var pdv = new PDV("1", new LegalPeople("foo", "bar", document, new NaturalPeople("xyz")), coverageArea, adrress);

            await repository.Add(pdv);

            var ret = await repository.Search(-46.65893554687499, -23.54384513650583);

            Assert.IsNotNull(ret.Where(p => p.Id == pdv.Id));
        }
        public PDV(string code, LegalPeople company, GeoJsonMultiPolygon <GeoJson2DCoordinates> coverageArea, GeoJsonPoint <GeoJson2DCoordinates> address)
        {
            if (company == null)
            {
                throw new ArgumentException("A empresa é necessária.");
            }

            if (coverageArea == null)
            {
                throw new ArgumentException("A área de cobertura é necessária");
            }

            if (address == null)
            {
                throw new ArgumentException("O endereço é necessário.");
            }

            Code         = code;
            Id           = Guid.NewGuid();
            Company      = company;
            CoverageArea = coverageArea;
            Address      = address;
        }
        public void ToJson1()
        {
            GeoJsonPolygon polygon1 = new GeoJsonPolygon(
                new GeoJsonCoordinates(9.531275, 55.714505),
                new GeoJsonCoordinates(9.531503, 55.714701),
                new GeoJsonCoordinates(9.531278, 55.714791),
                new GeoJsonCoordinates(9.531038, 55.714599),
                new GeoJsonCoordinates(9.531275, 55.714505)
                );

            GeoJsonPolygon polygon2 = new GeoJsonPolygon(
                new GeoJsonCoordinates(9.531355, 55.714570),
                new GeoJsonCoordinates(9.531450, 55.714528),
                new GeoJsonCoordinates(9.531613, 55.714660),
                new GeoJsonCoordinates(9.531504, 55.714702),
                new GeoJsonCoordinates(9.531355, 55.714570)
                );

            GeoJsonMultiPolygon multiPolygon = new GeoJsonMultiPolygon(polygon1, polygon2);

            Assert.AreEqual(2, multiPolygon.Count);

            Assert.AreEqual(Json1, multiPolygon.ToJson(Formatting.None));
        }
Example #13
0
        private static void AddMultiPolygon(SqlGeographyBuilder builder, GeoJsonMultiPolygon geometry, bool isLongitudeFirst)
        {
            builder.BeginGeography(OpenGisGeographyType.MultiPolygon);

            foreach (var item in geometry.Coordinates)
            {
                builder.BeginGeography(OpenGisGeographyType.Polygon);

                foreach (var lines in item)
                {
                    if (lines.Length < minimumPolygonPoints)
                    {
                        Trace.WriteLine($"CreateMultiPolygon escape!");
                        continue;
                    }

                    AddLineStringOrRing(builder, lines, true, isLongitudeFirst);
                }

                builder.EndGeography();
            }

            builder.EndGeography();
        }
 /// <summary>
 /// Serializes a value.
 /// </summary>
 /// <param name="context">The serialization context.</param>
 /// <param name="value">The value.</param>
 protected override void SerializeValue(BsonSerializationContext context, GeoJsonMultiPolygon <TCoordinates> value)
 {
     _helper.SerializeMembers(context, value, SerializeDerivedMembers);
 }
 private void SerializeDerivedMembers(BsonSerializationContext context, GeoJsonMultiPolygon <TCoordinates> value)
 {
     SerializeCoordinates(context, value.Coordinates);
 }
        public void Deserialize1()
        {
            GeoJsonMultiPolygon multiPolygon = JsonConvert.DeserializeObject <GeoJsonMultiPolygon>(Json1);

            Assert1(multiPolygon);
        }
        public void Parse1()
        {
            GeoJsonMultiPolygon multiPolygon = GeoJsonMultiPolygon.Parse(Json1);

            Assert1(multiPolygon);
        }