Ejemplo n.º 1
0
        /// <summary>
        /// Serializes a value.
        /// </summary>
        /// <param name="context">The serialization context.</param>
        /// <param name="value">The value.</param>
        protected override void SerializeValue(BsonSerializationContext context, GeoJsonMultiPolygonCoordinates <TCoordinates> value)
        {
            var bsonWriter = context.Writer;

            bsonWriter.WriteStartArray();
            foreach (var polygon in value.Polygons)
            {
                context.SerializeWithChildContext(_polygonCoordinatesSerializer, polygon);
            }
            bsonWriter.WriteEndArray();
        }
        // protected methods
        /// <summary>
        /// Deserializes a value.
        /// </summary>
        /// <param name="context">The deserialization context.</param>
        /// <returns>The value.</returns>
        protected override GeoJsonMultiPolygon <TCoordinates> DeserializeValue(BsonDeserializationContext context)
        {
            var args = new GeoJsonObjectArgs <TCoordinates>();
            GeoJsonMultiPolygonCoordinates <TCoordinates> coordinates = null;

            _helper.DeserializeMembers(context, (elementName, flag) =>
            {
                switch (flag)
                {
                case Flags.Coordinates: coordinates = DeserializeCoordinates(context); break;

                default: _helper.DeserializeBaseMember(context, elementName, flag, args); break;
                }
            });

            return(new GeoJsonMultiPolygon <TCoordinates>(args, coordinates));
        }
 private void SerializeCoordinates(BsonWriter bsonWriter, GeoJsonMultiPolygonCoordinates <TCoordinates> coordinates)
 {
     bsonWriter.WriteName("coordinates");
     _coordinatesSerializer.Serialize(bsonWriter, typeof(GeoJsonMultiPolygonCoordinates <TCoordinates>), coordinates, null);
 }
 private void SerializeCoordinates(BsonSerializationContext context, GeoJsonMultiPolygonCoordinates <TCoordinates> coordinates)
 {
     context.Writer.WriteName("coordinates");
     context.SerializeWithChildContext(_coordinatesSerializer, coordinates);
 }
Ejemplo n.º 5
0
        static public GeoJsonGeometry <T> ToGeoJsonGeometry <T>(this IGeometry geometry) where T : GeoJsonCoordinates
        {
            if (geometry is IPoint)
            {
                var point = (IPoint)geometry;

                return(new GeoJsonPoint <T>(
                           (T)Activator.CreateInstance(typeof(T), new object[] { point.X, point.Y })));
            }
            if (geometry is IPolyline)
            {
                var polyline = (IPolyline)geometry;

                if (polyline.PathCount == 1)
                {
                    var lineStringCoordinates = new GeoJsonLineStringCoordinates <T>(polyline[0].ToGeoJsonCoordinates <T>());
                    return(new GeoJsonLineString <T>(lineStringCoordinates));
                }
                if (polyline.PathCount > 1)
                {
                    var lineStrings = new List <GeoJsonLineStringCoordinates <T> >();
                    for (int p = 0, p_to = polyline.PathCount; p < p_to; p++)
                    {
                        lineStrings.Add(new GeoJsonLineStringCoordinates <T>(polyline[p].ToGeoJsonCoordinates <T>()));
                    }

                    var multiLineStrings = new GeoJsonMultiLineStringCoordinates <T>(lineStrings);
                    return(new GeoJsonMultiLineString <T>(multiLineStrings));
                }
            }
            if (geometry is IPolygon)
            {
                var polygon = (IPolygon)geometry;
                polygon.MakeValid();
                polygon.CloseAllRings();

                if (polygon.OuterRingCount == 1)
                {
                    var outerRing  = new GeoJsonLinearRingCoordinates <T>(polygon.OuterRings().First().ToGeoJsonCoordinates <T>());
                    var innerRings = new List <GeoJsonLinearRingCoordinates <T> >();
                    if (polygon.InnerRingCount > 0)
                    {
                        foreach (var hole in polygon.InnerRings(polygon.OuterRings().First()))
                        {
                            innerRings.Add(new GeoJsonLinearRingCoordinates <T>(hole.ToGeoJsonCoordinates <T>()));
                        }
                    }
                    var polygonCoordinates = innerRings.Count > 0 ? new GeoJsonPolygonCoordinates <T>(outerRing, innerRings) : new GeoJsonPolygonCoordinates <T>(outerRing);
                    return(new GeoJsonPolygon <T>(polygonCoordinates));
                }
                else if (polygon.OuterRingCount > 1)
                {
                    List <GeoJsonPolygonCoordinates <T> > polygonCoordinatesArray = new List <GeoJsonPolygonCoordinates <T> >();
                    foreach (var ring in polygon.OuterRings())
                    {
                        var outerRing  = new GeoJsonLinearRingCoordinates <T>(ring.ToGeoJsonCoordinates <T>());
                        var innerRings = new List <GeoJsonLinearRingCoordinates <T> >();
                        foreach (var hole in polygon.InnerRings(ring))
                        {
                            innerRings.Add(new GeoJsonLinearRingCoordinates <T>(hole.ToGeoJsonCoordinates <T>()));
                        }
                        polygonCoordinatesArray.Add(innerRings.Count > 0 ? new GeoJsonPolygonCoordinates <T>(outerRing, innerRings) : new GeoJsonPolygonCoordinates <T>(outerRing));
                    }

                    var multiPolygonCoordinates = new GeoJsonMultiPolygonCoordinates <T>(polygonCoordinatesArray);
                    return(new GeoJsonMultiPolygon <T>(multiPolygonCoordinates));
                }
            }

            return(null);
        }