public void howto_serialize_geometries() { GeoJsonWriter writer = new GeoJsonWriter(); string actual = writer.Write(collection); Assert.That(actual, Is.Not.Null); Assert.That(actual, Is.EqualTo(serializedCollection)); }
public void GeoJsonWriterWriteAttributesTest() { AttributesTable attributes = new AttributesTable(); attributes.AddAttribute("test1", "value1"); string actual = new GeoJsonWriter().Write(attributes); Assert.AreEqual("\"properties\":{\"test1\":\"value1\"}", actual); }
public void GeoJsonWriterWriteFeatureTest() { AttributesTable attributes = new AttributesTable(); attributes.AddAttribute("test1", "value1"); IFeature feature = new Feature(new Point(23, 56), attributes); string actual = new GeoJsonWriter().Write(feature); Assert.AreEqual("{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[23.0,56.0]},\"properties\":{\"test1\":\"value1\"}}", actual); }
public void GeoJsonWriterWriteFeatureCollectionTest() { 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") }; string actual = new GeoJsonWriter().Write(featureCollection); Assert.AreEqual("{\"features\":[{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[23.0,56.0]},\"properties\":{\"test1\":\"value1\"}}],\"type\":\"FeatureCollection\",\"crs\":{\"type\":\"name\",\"properties\":{\"name\":\"name1\"}}}", actual); }
protected override byte[] Write(IGeometry gIn) { var gjw = new GeoJsonWriter(); var res = gjw.Write(gIn); using (var ms = new MemoryStream(res.Length)) { using (var s = new StreamWriter(ms)) s.Write(res); return ms.ToArray(); } }
public void If_multipolygon_is_valid_then_should_return_geojson() { var multiPolygon = new MultiPolygon(new[] { new Polygon(new[] { new LineString(new[] { new Point(30, 20), new Point(45, 40), new Point(10, 40), new Point(30, 20) }) }), new Polygon(new[] { new LineString(new[] { new Point(15, 5), new Point(40, 10), new Point(10, 20), new Point(5, 10), new Point(15, 5) }) }) }); const string expectGeojson = "{\"type\": \"MultiPolygon\",\"coordinates\": [[[[30, 20], [45, 40], [10, 40], [30, 20]]], [[[15, 5], [40, 10], [10, 20], [5, 10], [15, 5]]]]}"; GeoJsonWriter.Write(multiPolygon).Should().BeEquivalentTo(expectGeojson); }
protected override byte[] Write(IGeometry gIn) { var gjw = new GeoJsonWriter(); var res = gjw.Write(gIn); using (var ms = new MemoryStream(res.Length)) { using (var s = new StreamWriter(ms)) s.Write(res); return(ms.ToArray()); } }
public void GeoJsonWriterWrittenContentTest() { GeometryFactory factory = new GeometryFactory(); IMultiPolygon mp = factory.CreateMultiPolygon ( new List<IPolygon> { factory.CreatePolygon(factory.CreatePoint(10,10), factory.CreatePoint(20,10), factory.CreatePoint(25,17), factory.CreatePoint(10,10)), factory.CreatePolygon(factory.CreatePoint(50,30), factory.CreatePoint(40,20), factory.CreatePoint(20,10), factory.CreatePoint(25,17), factory.CreatePoint(30,30), factory.CreatePoint(50,30)) } ); Assert.AreEqual(2, mp.Count); IMultiPoint p = factory.CreateMultiPoint( new IPoint[2] { factory.CreatePoint(10,10), factory.CreatePoint(23,23) }); ILineString lstr = factory.CreateLineString( factory.CreatePoint(50,60), factory.CreatePoint(55,60), factory.CreatePoint(71,71) ); List<IGeometry> geo = new List<IGeometry>() { p, lstr }; string outFileName = _outputPath + ".geojson"; GeoJsonWriter writer = new GeoJsonWriter(outFileName); writer.Write(mp as IGeometry); writer.Write(geo); writer.Close(); GeoJsonReader reader = new GeoJsonReader(outFileName); IList<IGeometry> geometries = reader.ReadToEnd(); reader.Close(); GeometryComparer comp = new GeometryComparer(); Assert.AreEqual(0, comp.Compare(geometries[0], mp)); Assert.AreEqual(0, comp.Compare(geometries[1], p)); Assert.AreEqual(0, comp.Compare(geometries[2], lstr)); }
public void MultiPolygonToGeoJsonTest() { var multipolygon = new MultiPolygon( new List <Polygon> { new Polygon( new List <LineString> { new LineString( new List <GeographicPosition> { new GeographicPosition(40, 40), new GeographicPosition(20, 45), new GeographicPosition(45, 30), new GeographicPosition(40, 40) } ), new LineString( new List <GeographicPosition> { new GeographicPosition(20, 35), new GeographicPosition(10, 30), new GeographicPosition(10, 10), new GeographicPosition(30, 5), new GeographicPosition(45, 20), new GeographicPosition(20, 35) } ) } ), new Polygon( new List <LineString> { new LineString( new List <GeographicPosition> { new GeographicPosition(30, 20), new GeographicPosition(20, 15), new GeographicPosition(20, 25), new GeographicPosition(30, 20) } ) } ) } ); var featureCollection = new FeatureCollection(new List <Feature> { new Feature(multipolygon) }); var featureCollectionJson = GeoJsonWriter.ToGeoJson(featureCollection); Assert.True(featureCollectionJson.Contains("\"type\":\"MultiPolygon\"")); }
string MakeFeatureCollection(string[] wkts) { var fc = new FeatureCollection(); foreach (var wkt in wkts) { fc.Add(MakeFeature(wkt)); } var writer = new GeoJsonWriter(); var geojson = writer.Write(fc); return(geojson); }
string MakeFeatureCollection(IDictionary <string, object>[] attributess) { var fc = new FeatureCollection(); foreach (var attributes in attributess) { fc.Add(MakeFeature(attributes)); } var writer = new GeoJsonWriter(); var geojson = writer.Write(fc); return(geojson); }
public static T ToGeoJson <T>(this Geometry geometry) where T : GeoJSONObject { if (geometry == null) { return(default(T)); } GeoJsonWriter writer = new GeoJsonWriter(); var json = writer.Write(geometry); T result = JsonConvert.DeserializeObject <T>(json, new SafeGeoJsonConverter()); return(result); }
public void geojson_should_serialize_an_array_with_a_nested_array() { const string json = @" { ""type"": ""FeatureCollection"", ""features"": [ { ""type"": ""Feature"", ""geometry"": { ""type"": ""Point"", ""coordinates"": [ 1.0, 2.0 ] }, ""properties"": { ""foo"": [ { ""zee1"": ""xyz1"" }, { ""zee2"": ""xyz2"", ""zee3"": ""xyz3"" }, [ { ""zee11"": ""xyz11"" }, { ""zee22"": ""xyz22"", ""zee33"": ""xyz33"" } ] ] } } ] } "; GeoJsonReader reader = new GeoJsonReader(); FeatureCollection coll = reader.Read <FeatureCollection>(json); Assert.IsNotNull(coll); GeoJsonWriter writer = new GeoJsonWriter(); string s = writer.Write(coll); Assert.IsNotNull(s); }
public void LineStringToGeoJsonTest() { var linestring = new LineString(new List <GeographicPosition> { new GeographicPosition(30, 10), new GeographicPosition(31, 11), new GeographicPosition(32, 12) }); var featureCollection = new FeatureCollection(new List <Feature> { new Feature(linestring) }); var featureCollectionJson = GeoJsonWriter.ToGeoJson(featureCollection); Assert.True(featureCollectionJson.Contains("\"type\":\"LineString\"")); }
public static string ToGeoJson(IEnumerable <System.Data.Entity.Spatial.DbGeometry> dbGeometrys, ProjectionInfo pStart) { var pEnd = Definitions.WorldProjection; var enumerable = dbGeometrys as IList <System.Data.Entity.Spatial.DbGeometry> ?? dbGeometrys.ToList(); var reader = new WKTReader(); var geometryCollection = new GeometryCollection( enumerable.Select(x => GeometryHelper.Project(x.MakeValid(), pStart, pEnd)) .Select(dbGeometry => reader.Read(dbGeometry.WellKnownValue.WellKnownText)) .ToArray()); var geoJsonWriter = new GeoJsonWriter(); return(geoJsonWriter.Write(geometryCollection)); }
public void MultiPointToGeoJsonTest() { var multipoint = new MultiPoint(new List <Point> { new Point(new GeographicPosition(10, 40)), new Point(new GeographicPosition(40, 30)), new Point(new GeographicPosition(20, 20)), new Point(new GeographicPosition(30, 10)) }); var featureCollection = new FeatureCollection(new List <Feature> { new Feature(multipoint) }); var featureCollectionJson = GeoJsonWriter.ToGeoJson(featureCollection); Assert.True(featureCollectionJson.Contains("\"type\":\"MultiPoint\"")); }
public void GeoJsonWriterWriteFeatureCollectionTest() { var attributes = new AttributesTable(); attributes.Add("test1", "value1"); var feature = new Feature(new Point(23, 56), attributes); var featureCollection = new FeatureCollection { feature }; var gjw = new GeoJsonWriter(); gjw.SerializerSettings.NullValueHandling = NullValueHandling.Ignore; string actual = gjw.Write(featureCollection); Assert.AreEqual("{\"type\":\"FeatureCollection\",\"features\":[{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[23.0,56.0]},\"properties\":{\"test1\":\"value1\"}}]}", actual); }
public void GeoJsonWriterWriteFeatureCollectionTest() { 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") }; string actual = new GeoJsonWriter().Write(featureCollection); Assert.AreEqual("{\"features\":[{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[23.0,56.0]},\"properties\":{\"test1\":\"value1\"}}],\"type\":\"FeatureCollection\",\"crs\":{\"type\":\"name\",\"properties\":{\"name\":\"name1\"}}}", actual); }
public void TestRegressionDecode3() { string data = "CwM60CQayAs6XuaACUwOCA=="; // string geoJsonActual = "{\"type\":\"LineString\",\"coordinates\":[[4.3359875679016113,51.060848236083984],[4.335850715637207,51.060760498046875],[4.334843635559082,51.060855865478516],[4.3344945907592773,51.060886383056641],[4.33406925201416,51.0609130859375],[4.3313779830932617,51.061012268066406],[4.3303995132446289,51.061042785644531],[4.3293352127075195,51.061134338378906],[4.3285455703735352,51.061248779296875],[4.3269405364990234,51.061508178710938],[4.3250093460083008,51.061790466308594],[4.3239278793334961,51.061965942382812],[4.3231124877929688,51.062137603759766],[4.3220844268798828,51.062397003173828],[4.320711612701416,51.062847137451172],[4.3192791938781738,51.0634880065918],[4.31713342666626,51.064666748046875],[4.3153839111328125,51.065628051757812],[4.3107118606567383,51.068279266357422],[4.3084430694580078,51.069671630859375],[4.3065905570983887,51.071128845214844],[4.3063244819641113,51.071361541748047],[4.3056955337524414,51.071971893310547],[4.3053798675537109,51.072280883789062],[4.3051509857177734,51.072505950927734],[4.3041725158691406,51.073539733886719],[4.303675651550293,51.074043273925781],[4.3029775619506836,51.074745178222656],[4.3026752471923828,51.075027465820312],[4.3023591041564941,51.075298309326172],[4.3021035194396973,51.075511932373047],[4.3017449378967285,51.075771331787109],[4.3013901710510254,51.076007843017578],[4.3010897636413574,51.076210021972656],[4.3007535934448242,51.076408386230469],[4.3003439903259277,51.076633453369141],[4.3000245094299316,51.076801300048828],[4.298856258392334,51.0773811340332],[4.2980813980102539,51.077774047851562],[4.2970776557922363,51.078281402587891],[4.2965779304504395,51.078525543212891],[4.2960972785949707,51.078773498535156],[4.29279899597168,51.080398559570312],[4.2900562286376953,51.081764221191406],[4.2892160415649414,51.082183837890625],[4.2884082794189453,51.082588195800781],[4.2840476036071777,51.084754943847656],[4.2799005508422852,51.086818695068359],[4.2771029472351074,51.088191986083984],[4.2763633728027344,51.088531494140625],[4.2758951187133789,51.0887336730957],[4.2754759788513184,51.088924407958984],[4.2742376327514648,51.089462280273438],[4.27300500869751,51.089988708496094],[4.2701148986816406,51.091232299804688],[4.26772928237915,51.092247009277344],[4.2594938278198242,51.095787048339844],[4.2562212944030762,51.097202301025391],[4.252922534942627,51.098606109619141],[4.2521233558654785,51.098960876464844],[4.2499408721923828,51.099910736083984],[4.2495770454406738,51.100074768066406],[4.2486934661865234,51.1004524230957],[4.2477850914001465,51.1008415222168],[4.2434616088867188,51.102691650390625],[4.2430300712585449,51.102874755859375],[4.2373924255371094,51.105289459228516],[4.2359986305236816,51.105884552001953],[4.220517635345459,51.112506866455078],[4.2201657295227051,51.1126708984375],[4.2197279930114746,51.112892150878906],[4.2194104194641113,51.113067626953125],[4.2191100120544434,51.113273620605469],[4.2186551094055176,51.113624572753906],[4.2183289527893066,51.113929748535156],[4.2177948951721191,51.114604949951172],[4.2175607681274414,51.115093231201172],[4.2174534797668457,51.115676879882812],[4.2174396514892578,51.115940093994141],[4.2174715995788574,51.116191864013672],[4.2175202369689941,51.116523742675781],[4.217595100402832,51.116874694824219],[4.2177186012268066,51.117149353027344],[4.217808723449707,51.1172981262207],[4.2179837226867676,51.117595672607422],[4.2181568145751953,51.11785888671875]]}"; var geoJsonWriter = new GeoJsonWriter(); var lineLocationUnreferenced = (new BinaryDecoder()).CreateLineLocationDecoder().Decode(data).ToFeatures(); var geoJsonLineLocation = geoJsonWriter.Write(lineLocationUnreferenced); // create a referenced decoder. var referencedDecoder = new ReferencedOsmDecoder(RealGraphOsm.GetRoutingGraph("mons"), new BinaryDecoder()); // decodes a location. var location = referencedDecoder.Decode(data); Assert.IsNull(location); }
public void GetAllPages() { var wikiGateway = new INatureGateway(new TraceLogger()); wikiGateway.Initialize().Wait(); var results = wikiGateway.GetAll().Result; Assert.IsTrue(results.Count > 0); var writer = new GeoJsonWriter { SerializerSettings = new JsonSerializerSettings { Formatting = Formatting.Indented } }; var collection = new FeatureCollection(new Collection <IFeature>(results.Cast <IFeature>().ToArray())); File.WriteAllText(@"D:\iNature.geojson", writer.Write(collection)); }
private static void BoundaryPolygonWriter(string fileName, string extra, Geometry boundaryGeom, Stopwatch timer) { var gjws = new JsonSerializerSettings() { Formatting = Formatting.Indented }; var gjw = new GeoJsonWriter() { SerializerSettings = gjws }; string polystr = gjw.Write(boundaryGeom); File.WriteAllText(UnitTestHelper.TestDataDir + "test_" + fileName + "-gj" + extra + ".txt", polystr); timer.ReportAndRestart("Write "); MultiPolygon boundaryMPoly = boundaryGeom as MultiPolygon; // Write to GnuPlot format if (boundaryMPoly != null) { StreamWriter writer = new StreamWriter(UnitTestHelper.TestDataDir + "test_" + fileName + "-gp" + extra + ".txt"); // First write all shells foreach (Geometry geometry in boundaryMPoly.Geometries) { Polygon boundaryPoly = geometry as Polygon; GnuPlotWrite(writer, boundaryPoly.Shell); } writer.WriteLine(); // Then write all holes foreach (Geometry geometry in boundaryMPoly.Geometries) { Polygon boundaryPoly = geometry as Polygon; for (int i = 0; i < boundaryPoly.Holes.Length; i++) { writer.WriteLine(); writer.WriteLine("# " + (i + 1)); GnuPlotWrite(writer, boundaryPoly.Holes[i]); } } writer.Close(); } }
public void Run(string outputFileName) { Counter counter = new(); var layers = new List <ILayer> { new RouteNodeLayer(_postgresConnectionString, counter), new RouteSegmentLayer(_postgresConnectionString, counter) }; var writer = new GeoJsonWriter(outputFileName); foreach (var layer in layers) { Log.Information($"Start processing: {layer.Name}..."); var layerObjects = layer.GetObjects(); writer.Write(layerObjects); Log.Information($"{layerObjects.Count} {layer.Name} objects written to geojson file."); } }
public void GeoJsonWriterWriteTest() { IList<IGeometry> geometries = null; byte i = 1; foreach (string path in _inputFilePaths) { GeoJsonReader reader = new GeoJsonReader(path); geometries = reader.ReadToEnd(); reader.Close(); string outFileName = _outputPath + i + ".geojson"; GeoJsonWriter writer = new GeoJsonWriter(outFileName); writer.Write(geometries); writer.Close(); i++; } }
static void Main(string[] args) { //var optionsBuilder = new DbContextOptionsBuilder<CountryDbContext>(); //optionsBuilder.UseSqlite(@"Data Source=C:\Users\omakhin\RiderProjects\BorderCrossing\BorderCrossing.Console\BorderCrossing.db", b => { // b.UseNetTopologySuite(); //}); string connectionString = "Server=localhost;Database=BorderCrossing;Trusted_Connection=True;"; var optionsBuilder2 = new DbContextOptionsBuilder <CountryDbContext>(); optionsBuilder2.UseSqlServer(connectionString, b => { b.UseNetTopologySuite(); }); //var dbl = new CountryDbContext(optionsBuilder.Options); var dbs = new CountryDbContext(optionsBuilder2.Options); List <Country> countries = new List <Country>(); GeoJsonWriter writer = new GeoJsonWriter(); foreach (var country in dbs.Countries) { var c = new Country() { Name = country.Name, Region = country.Region, Geom = writer.Write(country.Geom) }; countries.Add(c); //dbl.Countries.Add(c); //dbl.SaveChanges(); } var json = JSON.Serialize(countries); File.WriteAllText(@"c:\temp\c3.json", json); }
public void TestDecode3() { // this is an encoded line-location, but assume we don't know this. string data = "CwM8tSQZgQs7EvvvAr4LCQ=="; string geoJsonActual = "{\"type\":\"LineString\",\"coordinates\":[[4.5523185729980469,50.7650146484375],[4.5522303581237793,50.765064239501953],[4.55216121673584,50.765102386474609],[4.5513834953308105,50.765544891357422],[4.5506992340087891,50.765937805175781],[4.549346923828125,50.7667121887207],[4.5487337112426758,50.767177581787109],[4.54760217666626,50.767967224121094],[4.5467185974121094,50.768653869628906],[4.5454902648925781,50.769634246826172],[4.5442113876342773,50.770816802978516],[4.543121337890625,50.771892547607422],[4.5426936149597168,50.772018432617188],[4.5425186157226562,50.772056579589844],[4.5423049926757812,50.772087097167969],[4.5419602394104,50.772125244140625],[4.5418844223022461,50.772132873535156]]}"; // create a referenced decoder. var referencedDecoder = new ReferencedOsmDecoder(RealGraphOsm.GetRoutingGraph("overijse"), new BinaryDecoder()); // decodes a location. var location = referencedDecoder.Decode(data); var lineLocation = location as ReferencedLine; var lineLocationGeometry = lineLocation.ToGeometry(); // write GeoJSON. var geoJsonWriter = new GeoJsonWriter(); var geoJson = geoJsonWriter.Write(lineLocationGeometry); Assert.IsNotNull(geoJson); AssertGeo.AreEqual(geoJsonActual, geoJson, 1); }
public void TestDecode1() { // this is an encoded line-location, but assume we don't know this. string data = "CwNIUCQL5gs8Ef/FA6sLDw=="; string geoJsonActual = "{\"type\":\"LineString\",\"coordinates\":[[4.6161060333251953,50.690467834472656],[4.615666389465332,50.690864562988281],[4.6154966354370117,50.696247100830078],[4.615412712097168,50.697616577148438],[4.6153979301452637,50.698280334472656],[4.6153759956359863,50.698684692382812],[4.6153225898742676,50.699268341064453],[4.6153054237365723,50.699459075927734],[4.6152987480163574,50.699722290039062]]}"; // create a referenced decoder. var referencedDecoder = new ReferencedOsmDecoder(RealGraphOsm.GetRoutingGraph("wavre"), new BinaryDecoder()); // decodes a location. var location = referencedDecoder.Decode(data); var lineLocation = location as ReferencedLine; var lineLocationGeometry = lineLocation.ToGeometry(); // write GeoJSON. var geoJsonWriter = new GeoJsonWriter(); var geoJson = geoJsonWriter.Write(lineLocationGeometry); Assert.IsNotNull(geoJson); AssertGeo.AreEqual(geoJsonActual, geoJson, 1); }
public static void WriteFeaturesToGeoJsonFile(string fileOutput, FeatureCollection featureCollection, int numberOfSignificantDigits) { var gjw = new GeoJsonWriter() { SerializerSettings = { NullValueHandling = NullValueHandling.Ignore, FloatParseHandling = FloatParseHandling.Decimal, Formatting = Formatting.Indented, Converters = new List <JsonConverter> { new DoubleFormatConverter(numberOfSignificantDigits) } } }; using (var jsonTextWriter = new JsonTextWriter(new StreamWriter(fileOutput))) { gjw.Write(featureCollection, jsonTextWriter); } }
public void TestDecode2() { // this is an encoded line-location, but assume we don't know this. string data = "CwM+IiQYtws7DPzyAbILCg=="; string geoJsonActual = "{\"type\":\"LineString\",\"coordinates\":[[4.5598363876342773,50.760837554931641],[4.5597963333129883,50.760860443115234],[4.5595493316650391,50.760982513427734],[4.55853796005249,50.761508941650391],[4.5582680702209473,50.761661529541016],[4.5523185729980469,50.7650146484375]]}"; // create a referenced decoder. var referencedDecoder = new ReferencedOsmDecoder(RealGraphOsm.GetRoutingGraph("overijse"), new BinaryDecoder()); // decodes a location. var location = referencedDecoder.Decode(data); var lineLocation = location as ReferencedLine; var lineLocationGeometry = lineLocation.ToGeometry(); // write GeoJSON. var geoJsonWriter = new GeoJsonWriter(); var geoJson = geoJsonWriter.Write(lineLocationGeometry); Assert.IsNotNull(geoJson); AssertGeo.AreEqual(geoJsonActual, geoJson, 1); }
public void TestGeoJsonWriterWritesEmptyFeatureCollection() { var w = new GeoJsonWriter(); var r = new GeoJsonReader(); var fc = new FeatureCollection(); string geoJson = null; Assert.That(() => geoJson = w.Write(fc), Throws.Nothing); Assert.That(geoJson, Is.Not.Null.Or.Empty); fc = null; Assert.That(() => fc = r.Read <FeatureCollection>(geoJson), Throws.Nothing); Assert.That(fc, Is.Not.Null); var f = new Feature(); Assert.That(() => geoJson = w.Write(f), Throws.Nothing); Assert.That(geoJson, Is.Not.Null.Or.Empty); f = null; Assert.That(() => f = r.Read <Feature>(geoJson), Throws.Nothing); Assert.That(f, Is.Not.Null); }
/// <summary> /// Applys the example of a <see cref="Feature"/> to the schema /// </summary> /// <param name="model"></param> /// <param name="context"></param> public void Apply(Schema model, SchemaFilterContext context) { if (context == null) { return; } if (context.SystemType != typeof(Feature)) { return; } var writer = new GeoJsonWriter(); var exampleFeatureString = writer.Write( new Feature(new LineString(new[] { new Coordinate(1, 2), new Coordinate(3, 4), }), new AttributesTable()) ); model.Example = JsonConvert.DeserializeObject(exampleFeatureString); model.Default = JsonConvert.DeserializeObject(exampleFeatureString); }
public void TestWriteFeatureCollectionWithFirstFeatureGeometryNull() { // Setup var geoJsonWriter = new GeoJsonWriter(); string featureJson = "{\"type\": \"Feature\",\"geometry\": {\"type\": \"LineString\",\"coordinates\": [[0,0],[2,2],[3,2]]},\"properties\": {\"key\": \"value\"}}"; var notNullGeometryFeature = new GeoJsonReader().Read <Feature>(featureJson); var attributesTable = new AttributesTable { { "key", "value" } }; IGeometry geometry = null; var nullGeometryFeature = new Feature(geometry, attributesTable); var features_notNullFirst = new Collection <IFeature> { notNullGeometryFeature, nullGeometryFeature }; var features_nullFirst = new Collection <IFeature> { nullGeometryFeature, notNullGeometryFeature }; var featureCollection_notNullFirst = new FeatureCollection(features_notNullFirst); var featureCollection_nullFirst = new FeatureCollection(features_nullFirst); // Act TestDelegate write_notNullFirst = () => geoJsonWriter.Write(featureCollection_notNullFirst); TestDelegate write_nullFirst = () => geoJsonWriter.Write(featureCollection_nullFirst); Assert.That(write_notNullFirst, Throws.Nothing); Assert.That(write_nullFirst, Throws.Nothing); }
public void geojson_should_serialize_an_array_witn_a_single_item() { const string json = @" { ""type"": ""FeatureCollection"", ""features"": [ { ""type"": ""Feature"", ""geometry"": { ""type"": ""Point"", ""coordinates"": [ 1.0, 2.0 ] }, ""properties"": { ""foo"": [ { ""zee1"": ""xyz1"" } ] } } ] } "; var reader = new GeoJsonReader(); var coll = reader.Read <FeatureCollection>(json); Assert.IsNotNull(coll); var writer = new GeoJsonWriter(); string s = writer.Write(coll); Assert.IsNotNull(s); }
public static string WriteFeaturesToGeoJsonString(FeatureCollection featureCollection, int numberOfSignificantDigits) { var sb = new StringBuilder(); var gjw = new GeoJsonWriter() { SerializerSettings = { NullValueHandling = NullValueHandling.Ignore, FloatParseHandling = FloatParseHandling.Decimal, Formatting = Formatting.Indented, Converters = new List <JsonConverter> { new DoubleFormatConverter(numberOfSignificantDigits) } } }; using (var jsonTextWriter = new JsonTextWriter(new StringWriter(sb))) { gjw.Write(featureCollection, jsonTextWriter); } return(sb.ToString()); }
public override IValueNode ParseValue(object value) { if (value == null) { return(new NullValueNode(null)); } if (value is Geometry) { var writer = new GeoJsonWriter(); var objStr = writer.Write(value); // turn json back into object var obj = JsonSerializer.Deserialize <object>(objStr); // convert it to graphql node tree var nodeTree = _anyType.ParseValue(obj); return(nodeTree); } throw new ArgumentException("The specified value has to be a Geometry Type"); }
public static string ToGeoJson(IEnumerable <System.Data.Entity.Spatial.DbGeometry> dbGeometrys, ProjectionInfo pStart, DataTable dataTable) { var pEnd = Definitions.WorldProjection; var dbGeometrys1 = dbGeometrys as IList <System.Data.Entity.Spatial.DbGeometry> ?? dbGeometrys.ToList(); var reader = new WKTReader(); var featureCollection = new FeatureCollection(); var columns = (from DataColumn column in dataTable.Columns select column.ColumnName).ToList(); for (var i = 0; i < dbGeometrys1.Count(); i++) { var geometry = GeometryHelper.Project(dbGeometrys1[i].MakeValid(), pStart, pEnd); var read = reader.Read(geometry.WellKnownValue.WellKnownText); var table = new AttributesTable(); foreach (var column in columns) { table.AddAttribute(column, dataTable.Rows[i][column]); } featureCollection.Add(new Feature(read, table)); } var geoJsonWriter = new GeoJsonWriter(); return(geoJsonWriter.Write(featureCollection)); }
public GeoJsonConverter() { var maker = new ShapeConverter(); _reader = new GeoJsonReader(maker); _writer = new GeoJsonWriter(maker); }
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 GeoJsonWriterWriteGeometryTest() { string actual = new GeoJsonWriter().Write(new Point(23, 56)); Assert.AreEqual("{\"type\":\"Point\",\"coordinates\":[23.0,56.0]}", actual); }
public GeoJsonConverter(IGeometryFactory geometryFactory) { var maker = new ShapeConverter(geometryFactory); _reader = new GeoJsonReader(maker); _writer = new GeoJsonWriter(maker); }
public void GivenAGeoJsonReaderAndWriter() { _reader = new GeoJsonReader(); _writer = new GeoJsonWriter(); var geometry = new Point(1, 2); _feature = new Feature(geometry, new AttributesTable()); }