Beispiel #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, GeoJsonLineStringCoordinates <TCoordinates> value)
        {
            var bsonWriter = context.Writer;

            bsonWriter.WriteStartArray();
            foreach (var position in value.Positions)
            {
                context.SerializeWithChildContext(_coordinatesSerializer, position);
            }
            bsonWriter.WriteEndArray();
        }
Beispiel #2
0
        static public IPath ToPath <T>(this GeoJsonLineStringCoordinates <T> lineString) where T : GeoJsonCoordinates
        {
            var path = new Path();

            foreach (var position in lineString.Positions)
            {
                var values = position.Values;

                path.AddPoint(new Point(values[0], values[1]));
            }

            return(path);
        }
Beispiel #3
0
        // protected methods
        /// <summary>
        /// Deserializes a value.
        /// </summary>
        /// <param name="context">The deserialization context.</param>
        /// <returns>The value.</returns>
        protected override GeoJsonLineString <TCoordinates> DeserializeValue(BsonDeserializationContext context)
        {
            var args = new GeoJsonObjectArgs <TCoordinates>();
            GeoJsonLineStringCoordinates <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 GeoJsonLineString <TCoordinates>(args, coordinates));
        }
 private void SerializeCoordinates(BsonWriter bsonWriter, GeoJsonLineStringCoordinates <TCoordinates> coordinates)
 {
     bsonWriter.WriteName("coordinates");
     _coordinatesSerializer.Serialize(bsonWriter, typeof(GeoJsonLineStringCoordinates <TCoordinates>), coordinates, null);
 }
Beispiel #5
0
 private void SerializeCoordinates(BsonSerializationContext context, GeoJsonLineStringCoordinates <TCoordinates> coordinates)
 {
     context.Writer.WriteName("coordinates");
     context.SerializeWithChildContext(_coordinatesSerializer, coordinates);
 }
Beispiel #6
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);
        }