Exemplo n.º 1
0
 public static string GetPolygonAsGeoJSON()
 {
     var poly = GetAPolygon();
     var jsonSerializer = new GeoJsonSerializer();
     var sw = new System.IO.StringWriter();
     jsonSerializer.Serialize(sw, poly);
     return sw.ToString();
 }
Exemplo n.º 2
0
 /// <summary>
 /// Writes the specified feature collection.
 /// </summary>
 /// <param name="featureCollection">The feature collection.</param>
 /// <returns></returns>
 public string Write(FeatureCollection featureCollection)
 {
     JsonSerializer g = new GeoJsonSerializer();
     StringBuilder sb = new StringBuilder();
     using (StringWriter sw = new StringWriter(sb))
         g.Serialize(sw, featureCollection);
     return sb.ToString();
 }
Exemplo n.º 3
0
 /// <summary>
 /// Writes any specified object.
 /// </summary>
 /// <param name="value">Any object.</param>
 /// <returns></returns>
 public string Write(object value)
 {
     JsonSerializer g = new GeoJsonSerializer();
     StringBuilder sb = new StringBuilder();
     using (StringWriter sw = new StringWriter(sb))
         g.Serialize(sw, value);
     return sb.ToString();
 }
		/// <summary>
		/// Serializes the object to the stream as GeoJson.
		/// </summary>
		/// <param name="type"></param>
		/// <param name="value"></param>
		/// <param name="writeStream"></param>
		/// <param name="content"></param>
		public override void WriteToStream(Type type, object value, Stream writeStream, HttpContent content)
		{
			using (var writer = new StreamWriter(writeStream) { AutoFlush = true })
			{
				var serializer = new GeoJsonSerializer();
				serializer.Serialize(writer, value);
			}
		}
        /// <summary>
        /// Writes any specified object.
        /// </summary>
        /// <param name="value">Any object.</param>
        /// <returns></returns>
        public string Write(object value)
        {
            JsonSerializer g  = new GeoJsonSerializer();
            StringBuilder  sb = new StringBuilder();

            using (StringWriter sw = new StringWriter(sb))
                g.Serialize(sw, value);
            return(sb.ToString());
        }
 public void feature_collection_is_serialized_as_geojson_when_type_is_placed_as_first_property()
 {
     const string data = @"{ ""type"": ""FeatureCollection"", ""features"": [] }";
     JsonSerializer serializer = new GeoJsonSerializer();
     JsonTextReader reader = new JsonTextReader(new StringReader(data));
     FeatureCollection fc = serializer.Deserialize<FeatureCollection>(reader);
     Assert.That(fc, Is.Not.Null);
     Assert.That(fc.Count, Is.EqualTo(0));
 }
        /// <summary>
        /// Writes the specified feature.
        /// </summary>
        /// <param name="feature">The feature.</param>
        /// <returns>A string representing the feature's JSON representation</returns>
        public string Write(IFeature feature)
        {
            JsonSerializer g  = GeoJsonSerializer.Create(SerializerSettings, feature.Geometry.Factory);
            StringBuilder  sb = new StringBuilder();

            using (StringWriter sw = new StringWriter(sb))
                g.Serialize(sw, feature);
            return(sb.ToString());
        }
        /// <summary>
        /// Writes the specified feature.
        /// </summary>
        /// <param name="feature">The feature.</param>
        /// <returns></returns>
        public string Write(IFeature feature)
        {
            GeoJsonSerializer g  = new GeoJsonSerializer();
            StringBuilder     sb = new StringBuilder();

            using (StringWriter sw = new StringWriter(sb))
                g.Serialize(sw, feature);
            return(sb.ToString());
        }
        /// <summary>
        /// Writes the specified feature collection.
        /// </summary>
        /// <param name="featureCollection">The feature collection.</param>
        /// <returns>A string representing the feature collection's JSON representation</returns>
        public string Write(FeatureCollection featureCollection)
        {
            JsonSerializer g  = GeoJsonSerializer.Create(SerializerSettings, featureCollection.Features[0].Geometry.Factory);
            StringBuilder  sb = new StringBuilder();

            using (StringWriter sw = new StringWriter(sb))
                g.Serialize(sw, featureCollection);
            return(sb.ToString());
        }
        /// <summary>
        /// Writes the specified feature collection.
        /// </summary>
        /// <param name="featureCollection">The feature collection.</param>
        /// <returns></returns>
        public string Write(FeatureCollection featureCollection)
        {
            JsonSerializer g  = new GeoJsonSerializer();
            StringBuilder  sb = new StringBuilder();

            using (StringWriter sw = new StringWriter(sb))
                g.Serialize(sw, featureCollection);
            return(sb.ToString());
        }
        /// <summary>
        /// Writes any specified object.
        /// </summary>
        /// <param name="value">Any object.</param>
        /// <returns>A string representing the object's JSON representation</returns>
        public string Write(object value)
        {
            JsonSerializer g  = GeoJsonSerializer.Create(SerializerSettings, GeometryFactory.Default);
            StringBuilder  sb = new StringBuilder();

            using (StringWriter sw = new StringWriter(sb))
                g.Serialize(sw, value);
            return(sb.ToString());
        }
Exemplo n.º 12
0
        /// <summary>
        /// Reads the specified json.
        /// </summary>
        /// <typeparam name="TObject">The type of the object.</typeparam>
        /// <param name="json">The json.</param>
        /// <returns></returns>
        public TObject Read <TObject>(string json)
            where TObject : class
        {
            JsonSerializer g = new GeoJsonSerializer();

            using (StringReader sr = new StringReader(json))
            {
                return(g.Deserialize <TObject>(new JsonTextReader(sr)));
            }
        }
        /// <summary>
        /// Reads the specified json.
        /// </summary>
        /// <typeparam name="TObject">The type of the object.</typeparam>
        /// <param name="json">The json.</param>
        /// <returns></returns>
        public TObject Read <TObject>(string json)
            where TObject : class
        {
            var g = GeoJsonSerializer.Create(_serializerSettings, _factory);

            using (StringReader sr = new StringReader(json))
            {
                return(g.Deserialize <TObject>(new JsonTextReader(sr)));
            }
        }
        /// <summary>
        /// Writes the specified feature collection.
        /// </summary>
        /// <param name="featureCollection">The feature collection.</param>
        /// <returns>A string representing the feature collection's JSON representation</returns>
        public string Write(FeatureCollection featureCollection)
        {
            var            factory = SearchForFactory(featureCollection) ?? GeoJsonReader.Wgs84Factory;
            JsonSerializer g       = GeoJsonSerializer.Create(SerializerSettings, factory);
            StringBuilder  sb      = new StringBuilder();

            using (StringWriter sw = new StringWriter(sb))
                g.Serialize(sw, featureCollection);
            return(sb.ToString());
        }
Exemplo n.º 15
0
        /// <summary>
        /// Writes the specified geometry.
        /// </summary>
        /// <param name="geometry">The geometry.</param>
        /// <returns></returns>
        public string Write(IGeometry geometry)
        {
            if (geometry == null) 
                throw new ArgumentNullException("geometry");

            JsonSerializer g = new GeoJsonSerializer(geometry.Factory);
            StringBuilder sb = new StringBuilder();
            using (StringWriter sw = new StringWriter(sb))
                g.Serialize(sw, geometry);
            return sb.ToString();
        }
Exemplo n.º 16
0
 public static FeatureCollection ToFeatureCollection(this byte[] featureCollectionContent)
 {
     using (var stream = new MemoryStream(featureCollectionContent))
     {
         var serializer = new GeoJsonSerializer();
         using (var streamReader = new StreamReader(stream))
         using (var jsonTextReader = new JsonTextReader(streamReader))
         {
             return serializer.Deserialize<FeatureCollection>(jsonTextReader);
         }
     }
 }
Exemplo n.º 17
0
        /// <summary>
        /// Reads the specified json.
        /// </summary>
        /// <typeparam name="TObject">The type of the object.</typeparam>
        /// <param name="json">The json.</param>
        /// <returns></returns>
        public TObject Read <TObject>(JsonReader json)
            where TObject : class
        {
            if (json is null)
            {
                throw new ArgumentNullException(nameof(json));
            }

            var g = GeoJsonSerializer.Create(_serializerSettings, _factory, _dimension);

            return(g.Deserialize <TObject>(json));
        }
Exemplo n.º 18
0
 public static byte[] ToBytes(this FeatureCollection featureCollection)
 {
     using (var outputStream = new MemoryStream())
     {
         var writer = new StreamWriter(outputStream);
         var jsonWriter = new JsonTextWriter(writer);
         var serializer = new GeoJsonSerializer();
         serializer.Serialize(jsonWriter, featureCollection);
         jsonWriter.Flush();
         return outputStream.ToArray();
     }
 }
        public void feature_collection_with_crs_is_serialized_as_geojson()
        {
            const string data = @"
{ 
    ""type"": ""FeatureCollection"", 
    ""crs"": {""type"": ""name"", ""properties"": {""name"": ""urn:ogc:def:crs:OGC:1.3:CRS84""}}, 
    ""features"": [] 
}";
            JsonSerializer serializer = new GeoJsonSerializer();
            JsonTextReader reader = new JsonTextReader(new StringReader(data));
            FeatureCollection fc = serializer.Deserialize<FeatureCollection>(reader);
            Assert.That(fc, Is.Not.Null);
            Assert.That(fc.Count, Is.EqualTo(0));
        }
        /// <summary>
        /// Writes the specified geometry.
        /// </summary>
        /// <param name="geometry">The geometry.</param>
        /// <returns></returns>
        public string Write(IGeometry geometry)
        {
            if (geometry == null)
            {
                throw new ArgumentNullException("geometry");
            }

            JsonSerializer g  = new GeoJsonSerializer(geometry.Factory);
            StringBuilder  sb = new StringBuilder();

            using (StringWriter sw = new StringWriter(sb))
                g.Serialize(sw, geometry);
            return(sb.ToString());
        }
Exemplo n.º 21
0
        /// <summary>
        /// Writes the specified geometry.
        /// </summary>
        /// <param name="geometry">The geometry.</param>
        /// <returns>A string representing the geometry's JSON representation</returns>
        public string Write(IGeometry geometry)
        {
            if (geometry == null)
            {
                throw new ArgumentNullException(nameof(geometry));
            }

            var g = GeoJsonSerializer.Create(SerializerSettings, geometry.Factory);

            var sb = new StringBuilder();

            using (var sw = new StringWriter(sb))
                g.Serialize(sw, geometry);
            return(sb.ToString());
        }
        /// <summary>
        /// Writes the specified feature.
        /// </summary>
        /// <param name="feature">The feature.</param>
        /// <returns>A string representing the feature's JSON representation</returns>
        public string Write(IFeature feature)
        {
            if (feature == null)
            {
                throw new ArgumentNullException(nameof(feature));
            }

            var            factory = feature.Geometry?.Factory ?? GeoJsonReader.Wgs84Factory;
            JsonSerializer g       = GeoJsonSerializer.Create(SerializerSettings, factory);
            StringBuilder  sb      = new StringBuilder();

            using (StringWriter sw = new StringWriter(sb))
                g.Serialize(sw, feature);
            return(sb.ToString());
        }
        public void feature_collection_with_arbitrary_properties_is_serialized_as_geojson()
        {
            const string data = @"
{ 
    ""foo1"": ""bar1"",
    ""type"": ""FeatureCollection"",     
    ""foo2"": ""bar2"",
    ""features"": [],
    ""foo3"": ""bar3""
}";
            JsonSerializer serializer = new GeoJsonSerializer();
            JsonTextReader reader = new JsonTextReader(new StringReader(data));
            FeatureCollection fc = serializer.Deserialize<FeatureCollection>(reader);
            Assert.That(fc, Is.Not.Null);
            Assert.That(fc.Count, Is.EqualTo(0));
        }
Exemplo n.º 24
0
        /// <summary>
        /// Writes the specified geometry.
        /// </summary>
        /// <param name="geometry">The geometry.</param>
        /// <param name="writer">The <see cref="JsonWriter"/> to write to.</param>
        public void Write(Geometry geometry, JsonWriter writer)
        {
            if (geometry is null)
            {
                throw new ArgumentNullException(nameof(geometry));
            }

            if (writer is null)
            {
                throw new ArgumentNullException(nameof(writer));
            }

            var g = GeoJsonSerializer.Create(SerializerSettings, geometry.Factory);

            g.Serialize(writer, geometry);
        }
Exemplo n.º 25
0
        /// <summary>
        /// Writes any specified object.
        /// </summary>
        /// <param name="value">Any object.</param>
        /// <param name="writer">The <see cref="JsonWriter"/> to write to.</param>
        public void Write(object value, JsonWriter writer)
        {
            if (value is null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            if (writer is null)
            {
                throw new ArgumentNullException(nameof(writer));
            }

            var g = GeoJsonSerializer.Create(SerializerSettings, GeoJsonSerializer.Wgs84Factory);

            g.Serialize(writer, value);
        }
Exemplo n.º 26
0
        /// <summary>
        /// Writes the specified feature collection.
        /// </summary>
        /// <param name="featureCollection">The feature collection.</param>
        /// <param name="writer">The <see cref="JsonWriter"/> to write to.</param>
        public void Write(FeatureCollection featureCollection, JsonWriter writer)
        {
            if (featureCollection is null)
            {
                throw new ArgumentNullException(nameof(featureCollection));
            }

            if (writer is null)
            {
                throw new ArgumentNullException(nameof(writer));
            }

            var factory = SearchForFactory(featureCollection) ?? GeoJsonSerializer.Wgs84Factory;
            var g       = GeoJsonSerializer.Create(SerializerSettings, factory);

            g.Serialize(writer, featureCollection);
        }
Exemplo n.º 27
0
        /// <summary>
        /// Writes the specified feature.
        /// </summary>
        /// <param name="feature">The feature.</param>
        /// <param name="writer">The <see cref="JsonWriter"/> to write to.</param>
        public void Write(Feature feature, JsonWriter writer)
        {
            if (feature is null)
            {
                throw new ArgumentNullException(nameof(feature));
            }

            if (writer is null)
            {
                throw new ArgumentNullException(nameof(writer));
            }

            var factory = feature.Geometry?.Factory ?? GeoJsonSerializer.Wgs84Factory;
            var g       = GeoJsonSerializer.Create(SerializerSettings, factory);

            g.Serialize(writer, feature);
        }