/// <summary>
        /// Returns the county closes to the given municipality
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public HttpResponseMessage GetCounty(int id)
        {
            var session = SessionManager.Session;
            var county  =
                (from c in session.Query <County>()
                 from m in session.Query <Municipality>()
                 where m.Id == id
                 orderby m.Geom.Centroid.Distance(c.Geom) ascending
                 select c)
                .Take(1)
                .ToList()
                .FirstOrDefault();

            var feature = new Feature
            {
                Geometry   = county.Geom,
                Attributes = new AttributesTable()
            };

            feature.Attributes.AddAttribute("id", county.Id);
            feature.Attributes.AddAttribute("name", county.Name);
            feature.Attributes.AddAttribute("no", county.CountyNo);
            feature.Attributes.AddAttribute("type", "county");

            var jsonSerializer = new GeoJsonSerializer();
            var sw             = new StringWriter();

            jsonSerializer.Serialize(sw, feature);
            return(Response(sw.ToString()));
        }
예제 #2
0
        public void TestIssue83()
        {
            var geoJson = @"{ ""type"": ""Feature"", 
                              ""geometry"": { ""type"": ""Point"", ""coordinates"": [10.0, 60.0] }, 
                              ""id"": 1, 
                             ""properties"": { ""Name"": ""test"" } }";

            var     s = new GeoJsonSerializer();
            Feature f = null;

            Assert.DoesNotThrow(() =>
                                f = s.Deserialize <Feature>(new JsonTextReader(new StringReader(geoJson)))
                                );

            Assert.IsNotNull(f, "f != null");
            Assert.IsTrue(FeatureExtensions.HasID(f), "f.HasID()");
            Assert.AreEqual(1, FeatureExtensions.ID(f), "f.ID != 1");

            var sb = new StringBuilder();
            var tw = new JsonTextWriter(new StringWriter(sb));

            s.Serialize(tw, f);
            var geoJsonRes = sb.ToString();

            CompareJson(geoJson, geoJsonRes);
        }
예제 #3
0
        public void TestIssue83()
        {
            var geoJson = "{ \"type\": \"Feature\", " +
                          "\"geometry\": { \"type\": \"Point\", \"coordinates\": [10.0, 60.0] }, " +
                          "\"id\": 1, " +
                          "\"properties\": { \"Name\": \"test\" } }";

            var     s = new GeoJsonSerializer();
            Feature f = null;

            Assert.DoesNotThrow(() =>
                                f = s.Deserialize <Feature>(new JsonTextReader(new StringReader(geoJson)))
                                );

            Assert.IsNotNull(f, "f != null");
            Assert.IsTrue(f.HasID(), "f.HasID()");
            Assert.AreEqual(1, f.ID(), "f.ID != 1");

            var sb = new StringBuilder();
            var tw = new JsonTextWriter(new StringWriter(sb));

            s.Serialize(tw, f);
            var geoJsonRes = sb.ToString();

            CompareJson(geoJson, geoJsonRes);
        }
        /// <summary>
        /// Returns a list of municipalities in GeoJSON format
        /// </summary>
        /// <returns></returns>
        public HttpResponseMessage Get()
        {
            var session = SessionManager.Session;

            var municipalities = session.Query <Municipality>().ToList();

            FeatureCollection collection = new FeatureCollection();

            foreach (var municipality in municipalities)
            {
                var feature = new Feature
                {
                    Geometry   = municipality.Geom,
                    Attributes = new AttributesTable(),
                };

                feature.Attributes.AddAttribute("id", municipality.Id);
                feature.Attributes.AddAttribute("name", municipality.Name);
                feature.Attributes.AddAttribute("no", municipality.MunicipalityNo);
                feature.Attributes.AddAttribute("type", "municipality");

                collection.Add(feature);
            }

            var jsonSerializer = new GeoJsonSerializer();
            var sw             = new StringWriter();

            jsonSerializer.Serialize(sw, collection);
            return(Response(sw.ToString()));
        }
        public void GeoJsonWriterWriteAnyObjectTest()
        {
            AttributesTable attributes = new AttributesTable();
            DateTime        Date       = new DateTime(2012, 8, 8).Date;

            JsonSerializer g = new GeoJsonSerializer {
                NullValueHandling = NullValueHandling.Ignore
            };
            StringBuilder sb = new StringBuilder();

            using (StringWriter sw = new StringWriter(sb))
                g.Serialize(sw, Date);
            string expectedDateString = sb.ToString();

            string expectedResult = "{\"featureCollection\":{\"type\":\"FeatureCollection\",\"features\":[{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[23.0,56.0]},\"properties\":{\"test1\":\"value1\"}}],\"crs\":{\"type\":\"name\",\"properties\":{\"name\":\"name1\"}}},\"Date\":" + expectedDateString + "}";

            attributes.AddAttribute("test1", "value1");
            IFeature feature = new Feature(new Point(23, 56), attributes);

            FeatureCollection featureCollection = new FeatureCollection(new Collection <IFeature> {
                feature
            })
            {
                CRS = new NamedCRS("name1")
            };
            var gjw = new GeoJsonWriter();

            gjw.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
            string actual = gjw.Write(new { featureCollection, Date = Date });

            Assert.AreEqual(expectedResult, actual);
        }
        public static string ToGeoJson(this IGeometry geometry, int srId, KeyValuePair <string, object>[] attributes = null)
        {
            var attributesTable = new AttributesTable();

            if (attributes != null)
            {
                foreach (var attribute in attributes)
                {
                    attributesTable.AddAttribute(attribute.Key, attribute.Value);
                }
            }

            var feature = new Feature(geometry, attributesTable);

            var featureCollection = new FeatureCollection()
            {
                CRS      = new NamedCRS(string.Format("EPSG:{0}", srId)),
                Features = { feature }
            };

            var geoJsonSerializer = new GeoJsonSerializer(geometry.Factory);
            var stringBuilder     = new StringBuilder();

            using (var sw = new StringWriter(stringBuilder))
                geoJsonSerializer.Serialize(sw, featureCollection);
            return(stringBuilder.ToString());
        }
 public void GeoJsonSerializerGeometryTest()
 {
     StringBuilder sb = new StringBuilder();
     StringWriter writer = new StringWriter(sb);
     GeoJsonSerializer serializer = new GeoJsonSerializer();
     serializer.Serialize(writer, new Point(23, 56));
     writer.Flush();
     Assert.AreEqual("{\"type\":\"Point\",\"coordinates\":[23.0,56.0]}", sb.ToString());
 }
예제 #8
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());
        }
예제 #9
0
        private static IFeature SandD(IFeature input)
        {
            var s = new GeoJsonSerializer {
                NullValueHandling = NullValueHandling.Ignore, FloatFormatHandling = FloatFormatHandling.DefaultValue, FloatParseHandling = FloatParseHandling.Double
            };
            var sb = new StringBuilder();

            s.Serialize(new JsonTextWriter(new StringWriter(sb)), input, typeof(IFeature));
            return((IFeature)s.Deserialize <IFeature>(new JsonTextReader(new StringReader(sb.ToString()))));
        }
예제 #10
0
        public void TestCoordinateSerialize()
        {
            Coordinate        coordinate = new Coordinate(1, 1);
            GeoJsonSerializer g          = new GeoJsonSerializer();
            StringBuilder     sb         = new StringBuilder();

            g.Serialize(new JsonTextWriter(new StringWriter(sb)), coordinate);

            Console.WriteLine(sb.ToString());
        }
예제 #11
0
 public void serialize_an_array_of_geometries_should_return_a_json_fragment()
 {
     StringBuilder sb = new StringBuilder();
     JsonTextWriter writer = new JsonTextWriter(new StringWriter(sb));
     JsonSerializer serializer = new GeoJsonSerializer(factory);
     serializer.Serialize(writer, geometries);
     string actual = sb.ToString();
     Console.WriteLine(actual);
     Assert.That(actual, Is.EqualTo(serializedGeometries));            
 }
        public void GeoJsonSerializerGeometryTest()
        {
            StringBuilder  sb         = new StringBuilder();
            StringWriter   writer     = new StringWriter(sb);
            JsonSerializer serializer = new GeoJsonSerializer();

            serializer.Serialize(writer, new Point(23, 56));
            writer.Flush();
            Assert.AreEqual("{\"type\":\"Point\",\"coordinates\":[23.0,56.0]}", sb.ToString());
        }
예제 #13
0
        public void PerformGeometryTest(IGeometry geom)
        {
            GeoJsonSerializer s = new GeoJsonSerializer();
            StringBuilder sb = new StringBuilder();
            s.Serialize(new JsonTextWriter(new StringWriter(sb)), geom);
            string result = sb.ToString();
            Console.WriteLine(result);

            Deserialize(result, geom);
        }
예제 #14
0
 public void serialize_a_geometrycollection_should_return_a_valid_json()
 {
     StringBuilder sb = new StringBuilder();
     JsonTextWriter writer = new JsonTextWriter(new StringWriter(sb));
     JsonSerializer serializer = new GeoJsonSerializer(factory);
     serializer.Serialize(writer, collection);
     string actual = sb.ToString();
     Console.WriteLine(actual);
     Assert.That(actual, Is.EqualTo(serializedCollection));
 }
 /// <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);
     }
 }
 public void GeoJsonSerializerAttributesTest()
 {
     StringBuilder sb = new StringBuilder();
     StringWriter writer = new StringWriter(sb);
     AttributesTable attributes = new AttributesTable();
     attributes.AddAttribute("test1", "value1");
     GeoJsonSerializer serializer = new GeoJsonSerializer();
     serializer.Serialize(writer, attributes);
     writer.Flush();
     Assert.AreEqual("\"properties\":{\"test1\":\"value1\"}", sb.ToString());
 }
 public void GeoJsonSerializerFeatureTest()
 {
     StringBuilder sb = new StringBuilder();
     StringWriter writer = new StringWriter(sb);
     AttributesTable attributes = new AttributesTable();
     attributes.AddAttribute("test1", "value1");
     IFeature feature = new Feature(new Point(23, 56), attributes);
     GeoJsonSerializer serializer = new GeoJsonSerializer();
     serializer.Serialize(writer, feature);
     writer.Flush();
     Assert.AreEqual("{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[23.0,56.0]},\"properties\":{\"test1\":\"value1\"}}", sb.ToString());
 }
예제 #18
0
        public void PerformGeometryTest(IGeometry geom)
        {
            GeoJsonSerializer s  = new GeoJsonSerializer();
            StringBuilder     sb = new StringBuilder();

            s.Serialize(new JsonTextWriter(new StringWriter(sb)), geom);
            string result = sb.ToString();

            Console.WriteLine(result);

            Deserialize(result, geom);
        }
예제 #19
0
        public void serialize_a_geometrycollection_should_return_a_valid_json()
        {
            StringBuilder  sb         = new StringBuilder();
            JsonTextWriter writer     = new JsonTextWriter(new StringWriter(sb));
            JsonSerializer serializer = new GeoJsonSerializer(factory);

            serializer.Serialize(writer, collection);
            string actual = sb.ToString();

            Console.WriteLine(actual);
            Assert.That(actual, Is.EqualTo(serializedCollection));
        }
예제 #20
0
        public void serialize_an_array_of_geometries_should_return_a_json_fragment()
        {
            StringBuilder  sb         = new StringBuilder();
            JsonTextWriter writer     = new JsonTextWriter(new StringWriter(sb));
            JsonSerializer serializer = new GeoJsonSerializer(factory);

            serializer.Serialize(writer, geometries);
            string actual = sb.ToString();

            Console.WriteLine(actual);
            Assert.That(actual, Is.EqualTo(serializedGeometries));
        }
        public void GeoJsonSerializerAttributesTest()
        {
            StringBuilder   sb         = new StringBuilder();
            StringWriter    writer     = new StringWriter(sb);
            AttributesTable attributes = new AttributesTable();

            attributes.AddAttribute("test1", "value1");
            JsonSerializer serializer = new GeoJsonSerializer();

            serializer.Serialize(writer, attributes);
            writer.Flush();
            Assert.AreEqual("\"properties\":{\"test1\":\"value1\"}", sb.ToString());
        }
예제 #22
0
        public void TestCoordinatesSerialize()
        {
            Coordinate[] coordinates = new Coordinate[4];
            for (int i = 0; i < coordinates.Length; i++)
            {
                coordinates[i] = new Coordinate(i, i, i);
            }
            StringBuilder     sb = new StringBuilder();
            GeoJsonSerializer g  = new GeoJsonSerializer();

            g.Serialize(new JsonTextWriter(new StringWriter(sb)), coordinates);

            Console.WriteLine(sb.ToString());
        }
        public void GeoJsonSerializerFeatureTest()
        {
            StringBuilder   sb         = new StringBuilder();
            StringWriter    writer     = new StringWriter(sb);
            AttributesTable attributes = new AttributesTable();

            attributes.AddAttribute("test1", "value1");
            IFeature       feature    = new Feature(new Point(23, 56), attributes);
            JsonSerializer serializer = new GeoJsonSerializer();

            serializer.Serialize(writer, feature);
            writer.Flush();
            Assert.AreEqual("{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[23.0,56.0]},\"properties\":{\"test1\":\"value1\"}}", sb.ToString());
        }
 public void GeoJsonSerializerFeatureCollectionTest()
 {
     StringBuilder sb = new StringBuilder();
     StringWriter writer = new StringWriter(sb);
     AttributesTable attributes = new AttributesTable();
     attributes.AddAttribute("test1", "value1");
     IFeature feature = new Feature(new Point(23, 56), attributes);
     FeatureCollection featureCollection = new FeatureCollection(new Collection<IFeature> {feature})
                                 {CRS = new NamedCRS("name1")};
     GeoJsonSerializer serializer = new GeoJsonSerializer();
     serializer.Serialize(writer, featureCollection);
     writer.Flush();
     Assert.AreEqual("{\"features\":[{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[23.0,56.0]},\"properties\":{\"test1\":\"value1\"}}],\"type\":\"FeatureCollection\",\"crs\":{\"type\":\"name\",\"properties\":{\"name\":\"name1\"}}}", sb.ToString());
 }
예제 #25
0
        public void GeoJsonWriterWriteAnyObjectTest()
        {
            AttributesTable attributes = new AttributesTable();
            DateTime Date = new DateTime(2012, 8, 8).Date;

            GeoJsonSerializer g = new GeoJsonSerializer();
            StringBuilder sb = new StringBuilder();
            using (StringWriter sw = new StringWriter(sb))
                g.Serialize(sw, Date);
            string expectedDateString = sb.ToString();

            string expectedResult = "{\"featureCollection\":{\"features\":[{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[23.0,56.0]},\"properties\":{\"test1\":\"value1\"}}],\"type\":\"FeatureCollection\",\"crs\":{\"type\":\"name\",\"properties\":{\"name\":\"name1\"}}},\"Date\":" + expectedDateString + "}";
            attributes.AddAttribute("test1", "value1");
            IFeature feature = new Feature(new Point(23, 56), attributes);

            FeatureCollection featureCollection = new FeatureCollection(new Collection<IFeature> { feature }) { CRS = new NamedCRS("name1") };
            string actual = new GeoJsonWriter().Write(new { featureCollection, Date = Date });
            Assert.AreEqual(expectedResult, actual);
        }
        public void GeoJsonSerializerFeatureCollectionTest()
        {
            StringBuilder   sb         = new StringBuilder();
            StringWriter    writer     = new StringWriter(sb);
            AttributesTable attributes = new AttributesTable();

            attributes.AddAttribute("test1", "value1");
            IFeature          feature           = new Feature(new Point(23, 56), attributes);
            FeatureCollection featureCollection = new FeatureCollection(new Collection <IFeature> {
                feature
            })
            {
                CRS = new NamedCRS("name1")
            };
            JsonSerializer serializer = new GeoJsonSerializer();

            serializer.Serialize(writer, featureCollection);
            writer.Flush();
            Assert.AreEqual("{\"features\":[{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[23.0,56.0]},\"properties\":{\"test1\":\"value1\"}}],\"type\":\"FeatureCollection\",\"crs\":{\"type\":\"name\",\"properties\":{\"name\":\"name1\"}}}", sb.ToString());
        }
        // GET api/<controller>/5
        public HttpResponseMessage Get(int id)
        {
            var session = SessionManager.Session;
            var county  = session.Get <County>(id);

            var feature = new Feature
            {
                Geometry   = county.Geom,
                Attributes = new AttributesTable()
            };

            feature.Attributes.AddAttribute("Id", county.Id);
            feature.Attributes.AddAttribute("Name", county.Name);
            feature.Attributes.AddAttribute("No", county.CountyNo);

            var jsonSerializer = new GeoJsonSerializer();
            var sw             = new StringWriter();

            jsonSerializer.Serialize(sw, feature);
            return(Response(sw.ToString()));
        }
        public static string ToGeoJson(this SqlGeometry geometry, KeyValuePair <string, object>[] attributes = null)
        {
            var wkt       = new string(geometry.STAsText().Value);
            var wktReader = new WKTReader();
            var geom      = wktReader.Read(wkt);

            var attributesTable = new AttributesTable();

            if (attributes != null)
            {
                foreach (var attribute in attributes)
                {
                    attributesTable.AddAttribute(attribute.Key, attribute.Value);
                }
            }

            var feature           = new Feature(geom, attributesTable);
            var featureCollection = new FeatureCollection()
            {
                CRS      = new NamedCRS(string.Format("EPSG:{0}", geometry.STSrid)),
                Features = { feature }
            };

            var sb = new StringBuilder();

            using (var sw = new StringWriter(sb))
            {
                var serializer = new GeoJsonSerializer()
                {
                    Formatting = Newtonsoft.Json.Formatting.None
                };

                serializer.Serialize(sw, featureCollection);
                sw.Close();
            }

            return(sb.ToString());
        }
예제 #29
0
        public void TestIssue83()
        {
            var geoJson = "{ \"type\": \"Feature\", " +
                            "\"geometry\": { \"type\": \"Point\", \"coordinates\": [10.0, 60.0] }, " +
                            "\"id\": 1, " +
                            "\"properties\": { \"Name\": \"test\" } }";

            var s = new GeoJsonSerializer();
            Feature f = null;
            Assert.DoesNotThrow(() =>
                f = s.Deserialize<Feature>(new JsonTextReader(new StringReader(geoJson)))
                );

            Assert.IsNotNull(f, "f != null");
            Assert.IsTrue(FeatureExtensions.HasID(f), "f.HasID()");
            Assert.AreEqual(1, FeatureExtensions.ID(f), "f.ID != 1");

            var sb = new StringBuilder();
            var tw = new JsonTextWriter(new StringWriter(sb));
            s.Serialize(tw, f);
            var geoJsonRes = sb.ToString();

            CompareJson(geoJson, geoJsonRes);
        }
예제 #30
0
        public void TestCoordinateSerialize()
        {
            Coordinate coordinate = new Coordinate(1, 1);
            GeoJsonSerializer g = new GeoJsonSerializer();
            StringBuilder sb = new StringBuilder();
            g.Serialize(new JsonTextWriter(new StringWriter(sb)), coordinate);

            Console.WriteLine(sb.ToString());
        }
        /// <summary>
        /// Convert shape-file to geojson-file stream
        /// </summary>
        /// <param name="shapeFilePath"></param>
        public static Stream ConvertToGeoJsonStream(string shapeFilePath)
        {
            var factory = new GeometryFactory();
            //Create a feature collection
            var featureCollection = new FeatureCollection();

            //Try to get crs from .prj file
            var crs = TryToGetCrs(shapeFilePath);

            //Set crs if we found it
            if (crs != CoordinateSystemId.None)
            {
                featureCollection.CRS = new NamedCRS(new CoordinateSystem(crs).Id.EpsgCode());
            }

            using (var shapeFileDataReader = new ShapefileDataReader(shapeFilePath, factory))
            {
                //Get shape file dbase header
                var header = shapeFileDataReader.DbaseHeader;

                //Loop throw all geometries
                while (shapeFileDataReader.Read())
                {
                    var attributesTable = new AttributesTable();
                    var geometry        = (Geometry)shapeFileDataReader.Geometry;

                    //Get header fields
                    for (var i = 0; i < header.NumFields; i++)
                    {
                        var fldDescriptor = header.Fields[i];
                        attributesTable.AddAttribute(fldDescriptor.Name, shapeFileDataReader.GetValue(i));
                    }

                    //Create feature using geometry and attributes
                    var feature = new Feature()
                    {
                        Geometry   = geometry,
                        Attributes = attributesTable
                    };

                    //Add feature to collection
                    featureCollection.Features.Add(feature);
                }

                //Close and free up any resources
                shapeFileDataReader.Close();
            }

            // Create a stream to write to.
            var outputStream   = new MemoryStream();
            var sw             = new StreamWriter(outputStream);
            var jsonSerializer = new GeoJsonSerializer(factory);

            //Serialize feature collection to json
            jsonSerializer.Serialize(sw, featureCollection);

            //Flush stream writer and reset stream position
            sw.Flush();
            outputStream.Position = 0;
            return(outputStream);
        }
예제 #32
0
        public void TestCoordinatesSerialize()
        {
            Coordinate[] coordinates = new Coordinate[4];
            for (int i = 0; i < coordinates.Length; i++)
            {
                coordinates[i] = new Coordinate(i, i, i);
            }
            StringBuilder sb = new StringBuilder();
            GeoJsonSerializer g = new GeoJsonSerializer();
            g.Serialize(new JsonTextWriter(new StringWriter(sb)), coordinates);

            Console.WriteLine(sb.ToString());
        }