protected override IGeometry Read(byte[] b) { string json; using (var ms = new MemoryStream(b)) { using (var r = new StreamReader(ms)) json = r.ReadToEnd(); } var gjs = new GeoJsonSerializer(); var j = (JObject) gjs.Deserialize(new JsonTextReader(new StringReader(json))); switch (j.Value<string>("type")) { case "Point": return gjs.Deserialize<Point>(new JsonTextReader(new StringReader(json))); case "LineString": return gjs.Deserialize<LineString>(new JsonTextReader(new StringReader(json))); case "Polygon": return gjs.Deserialize<Polygon>(new JsonTextReader(new StringReader(json))); case "MultiPoint": return gjs.Deserialize<MultiPoint>(new JsonTextReader(new StringReader(json))); case "MultiLineString": return gjs.Deserialize<MultiLineString>(new JsonTextReader(new StringReader(json))); case "MultiPolygon": return gjs.Deserialize<MultiPolygon>(new JsonTextReader(new StringReader(json))); case "GeometryCollection": return gjs.Deserialize<GeometryCollection>(new JsonTextReader(new StringReader(json))); default: throw new InvalidOperationException(); } }
public void TestIssue88WithFlatProperties() { string expectedJson = @" { ""id"" : ""00000000-0000-0000-0000-000000000000"", ""type"" : ""Feature"", ""geometry"" : null, ""properties"" : { ""yesNo 1"" : false, ""date 1"" : ""2016-02-16T00:00:00"" }, ""collection_userinfo"" : ""()"", ""collection_timestamp"" : ""2016-02-25T14:38:01.9087672"", ""collection_todoid"" : """", ""collection_templateid"" : ""nj7Glv-AqV0"", ""collection_layerid"" : ""nj7Glv-AqV0"" } "; GeoJsonSerializer serializer = new GeoJsonSerializer(); Feature feat = null; Assert.DoesNotThrow(() => { using (JsonReader reader = new JsonTextReader(new StringReader(expectedJson))) feat = serializer.Deserialize <Feature>(reader); }); Assert.IsNotNull(feat); Assert.IsNull(feat.Geometry); }
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); }
public void TestIssue88WithoutAdditionalProperties() { string expectedJson = @" { ""id"" : ""00000000-0000-0000-0000-000000000000"", ""type"" : ""Feature"", ""geometry"" : null, ""properties"" : { ""yesNo 1"" : false, ""date 1"" : ""2016-02-16T00:00:00"" } } "; GeoJsonSerializer serializer = new GeoJsonSerializer(); Feature feat = null; Assert.DoesNotThrow(() => { using (JsonReader reader = new JsonTextReader(new StringReader(expectedJson))) feat = serializer.Deserialize<Feature>(reader); }); Assert.IsNotNull(feat); Assert.IsTrue(FeatureExtensions.HasID(feat)); Assert.AreEqual("00000000-0000-0000-0000-000000000000", FeatureExtensions.ID(feat)); IGeometry geometry = feat.Geometry; Assert.IsNull(geometry); IAttributesTable attributes = feat.Attributes; Assert.IsNotNull(attributes); Assert.AreEqual(3, attributes.Count); Assert.AreEqual(FeatureExtensions.ID(feat), attributes["id"]); Assert.AreEqual(false, attributes["yesNo 1"]); Assert.AreEqual(DateTime.Parse("2016-02-16T00:00:00", CultureInfo.InvariantCulture), attributes["date 1"]); }
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); }
public void deserialize_a_json_fragment_should_throws_an_error() { JsonTextReader reader = new JsonTextReader(new StringReader(serializedGeometries)); JsonSerializer serializer = new GeoJsonSerializer(factory); IGeometry[] actual = serializer.Deserialize<IGeometry[]>(reader); Assert.That(actual, Is.Not.Null); Assert.That(actual, Is.EqualTo(geometries)); }
public void GeoJsonDeserializePolygonTest() { StringBuilder sb = new StringBuilder(); JsonSerializer serializer = new GeoJsonSerializer(); var s = "{\"type\": \"Polygon\",\"coordinates\": [[[-180,-67.8710140098964],[-180,87.270282879440586],[180,87.270282879440586],[180,-67.8710140098964],[-180,-67.8710140098964]]],\"rect\": {\"min\": [-180,-67.8710140098964],\"max\": [180,87.270282879440586]}}"; var poly = serializer.Deserialize <Polygon>(JObject.Parse(s).CreateReader()); }
public void deserialize_a_valid_json_should_return_a_geometrycollection() { JsonTextReader reader = new JsonTextReader(new StringReader(serializedCollection)); JsonSerializer serializer = new GeoJsonSerializer(factory); IGeometryCollection actual = serializer.Deserialize<GeometryCollection>(reader); Assert.That(actual, Is.Not.Null); Assert.That(actual.EqualsExact(collection), Is.True); }
public void TestCoordinateDeserialize() { string json = "{coordinates:[1.0, 1.0]}"; GeoJsonSerializer s = new GeoJsonSerializer(); Coordinate c = s.Deserialize <Coordinate>(new JsonTextReader(new StringReader(json))); Console.WriteLine(c.ToString()); }
public void deserialize_a_json_fragment_should_throws_an_error() { JsonTextReader reader = new JsonTextReader(new StringReader(serializedGeometries)); JsonSerializer serializer = new GeoJsonSerializer(factory); IGeometry[] actual = serializer.Deserialize <IGeometry[]>(reader); Assert.That(actual, Is.Not.Null); Assert.That(actual, Is.EqualTo(geometries)); }
public void deserialize_a_valid_json_should_return_a_geometrycollection() { JsonTextReader reader = new JsonTextReader(new StringReader(serializedCollection)); JsonSerializer serializer = new GeoJsonSerializer(factory); IGeometryCollection actual = serializer.Deserialize <GeometryCollection>(reader); Assert.That(actual, Is.Not.Null); Assert.That(actual.EqualsExact(collection), Is.True); }
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())))); }
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)); }
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)); }
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)); }
protected override IGeometry Read(byte[] b) { string json; using (var ms = new MemoryStream(b)) { using (var r = new StreamReader(ms)) json = r.ReadToEnd(); } var gjs = new GeoJsonSerializer(); var j = (JObject)gjs.Deserialize(new JsonTextReader(new StringReader(json))); switch (j.Value <string>("type")) { case "Point": return(gjs.Deserialize <Point>(new JsonTextReader(new StringReader(json)))); case "LineString": return(gjs.Deserialize <LineString>(new JsonTextReader(new StringReader(json)))); case "Polygon": return(gjs.Deserialize <Polygon>(new JsonTextReader(new StringReader(json)))); case "MultiPoint": return(gjs.Deserialize <MultiPoint>(new JsonTextReader(new StringReader(json)))); case "MultiLineString": return(gjs.Deserialize <MultiLineString>(new JsonTextReader(new StringReader(json)))); case "MultiPolygon": return(gjs.Deserialize <MultiPolygon>(new JsonTextReader(new StringReader(json)))); case "GeometryCollection": return(gjs.Deserialize <GeometryCollection>(new JsonTextReader(new StringReader(json)))); default: throw new InvalidOperationException(); } }
private static void Deserialize(string result, IGeometry geom) { GeoJsonSerializer s = new GeoJsonSerializer(); JsonTextReader r = new JsonTextReader(new StringReader(result)); IGeometry des; if (geom is IPoint) { des = s.Deserialize <Point>(r); } else if (geom is ILineString) { des = s.Deserialize <LineString>(r); } else if (geom is IPolygon) { des = s.Deserialize <Polygon>(r); } else if (geom is IMultiPoint) { des = s.Deserialize <MultiPoint>(r); } else if (geom is IMultiLineString) { des = s.Deserialize <MultiLineString>(r); } else if (geom is IMultiPolygon) { des = s.Deserialize <MultiPolygon>(r); } else if (geom is IGeometryCollection) { des = s.Deserialize <GeometryCollection>(r); } else { throw new Exception(); } Console.WriteLine(des.AsText()); Assert.IsTrue(des.EqualsExact(geom)); }
public void TestIssue88WithoutAdditionalProperties() { string expectedJson = @" { ""id"" : ""00000000-0000-0000-0000-000000000000"", ""type"" : ""Feature"", ""geometry"" : null, ""properties"" : { ""yesNo 1"" : false, ""date 1"" : ""2016-02-16T00:00:00"" } } "; GeoJsonSerializer serializer = new GeoJsonSerializer(); Feature feat = null; Assert.DoesNotThrow(() => { using (JsonReader reader = new JsonTextReader(new StringReader(expectedJson))) feat = serializer.Deserialize <Feature>(reader); }); Assert.IsNotNull(feat); Assert.IsTrue(FeatureExtensions.HasID(feat)); Assert.AreEqual("00000000-0000-0000-0000-000000000000", FeatureExtensions.ID(feat)); IGeometry geometry = feat.Geometry; Assert.IsNull(geometry); IAttributesTable attributes = feat.Attributes; Assert.IsNotNull(attributes); Assert.AreEqual(3, attributes.Count); Assert.AreEqual(FeatureExtensions.ID(feat), attributes["id"]); Assert.AreEqual(false, attributes["yesNo 1"]); Assert.AreEqual(DateTime.Parse("2016-02-16T00:00:00", CultureInfo.InvariantCulture), attributes["date 1"]); }
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); }
private static void Deserialize(string result, IGeometry geom) { GeoJsonSerializer s = new GeoJsonSerializer(); JsonTextReader r = new JsonTextReader(new StringReader(result)); IGeometry des; if (geom is IPoint) des = s.Deserialize<Point>(r); else if (geom is ILineString) des = s.Deserialize<LineString>(r); else if (geom is IPolygon) des = s.Deserialize<Polygon>(r); else if (geom is IMultiPoint) des = s.Deserialize<MultiPoint>(r); else if (geom is IMultiLineString) des = s.Deserialize<MultiLineString>(r); else if (geom is IMultiPolygon) des = s.Deserialize<MultiPolygon>(r); else if (geom is IGeometryCollection) des = s.Deserialize<GeometryCollection>(r); else throw new Exception(); Console.WriteLine(des.AsText()); Assert.IsTrue(des.EqualsExact(geom)); }
public void TestCoordinateDeserialize() { string json = "{coordinates:[1.0, 1.0]}"; GeoJsonSerializer s = new GeoJsonSerializer(); Coordinate c = s.Deserialize<Coordinate>(new JsonTextReader(new StringReader(json))); Console.WriteLine(c.ToString()); }
public void TestIssue88WithFlatProperties() { string expectedJson = @" { ""id"" : ""00000000-0000-0000-0000-000000000000"", ""type"" : ""Feature"", ""geometry"" : null, ""properties"" : { ""yesNo 1"" : false, ""date 1"" : ""2016-02-16T00:00:00"" }, ""collection_userinfo"" : ""()"", ""collection_timestamp"" : ""2016-02-25T14:38:01.9087672"", ""collection_todoid"" : """", ""collection_templateid"" : ""nj7Glv-AqV0"", ""collection_layerid"" : ""nj7Glv-AqV0"" } "; GeoJsonSerializer serializer = new GeoJsonSerializer(); Feature feat = null; Assert.DoesNotThrow(() => { using (JsonReader reader = new JsonTextReader(new StringReader(expectedJson))) feat = serializer.Deserialize<Feature>(reader); }); Assert.IsNotNull(feat); Assert.IsNull(feat.Geometry); }
public void GeoJsonDeserializePolygonTest() { StringBuilder sb = new StringBuilder(); JsonSerializer serializer = new GeoJsonSerializer(); var s = "{\"type\": \"Polygon\",\"coordinates\": [[[-180,-67.8710140098964],[-180,87.270282879440586],[180,87.270282879440586],[180,-67.8710140098964],[-180,-67.8710140098964]]],\"rect\": {\"min\": [-180,-67.8710140098964],\"max\": [180,87.270282879440586]}}"; var poly = serializer.Deserialize<Polygon>(JObject.Parse(s).CreateReader()); }