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" } }; Geometry geometry = null; var nullGeometryFeature = new Feature(geometry, attributesTable); var featureCollection_notNullFirst = new FeatureCollection { notNullGeometryFeature, nullGeometryFeature }; var featureCollection_nullFirst = new FeatureCollection { nullGeometryFeature, notNullGeometryFeature }; // 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 WhenArrayOfJsonObjectArraysPropertyInGeoJsonThenWriteable() { const string geojsonString = @" { ""type"":""FeatureCollection"", ""features"":[ { ""type"":""Feature"", ""geometry"":{ ""type"":""Point"", ""coordinates"":[1.0,2.0] }, ""properties"":{ ""foo"":[ { ""xyz"":[ {""zee"":""xyz""}, {""hay"":""zus""} ] } ] } } ] } "; var featureCollection = _reader.Read <FeatureCollection>(geojsonString); var written = _writer.Write(featureCollection); Assert.IsTrue(written.Contains("FeatureCollection")); }
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 override object Serialize(object value) { if (value == null) { return(null); } if (value is Geometry geo) { // We have to serialize and deserialize the object to remove circular references // from the original Geometry object var writer = new GeoJsonWriter(); var geoStr = writer.Write(geo); // var reader = new GeoJsonReader(); // var deObj = reader.Read<object>(geoStr); // TODO: Wait for better alternative // Erstmal einfach als str zurückgeben return(geoStr); } throw new ArgumentException( "The specified value cannot be serialized by the StringType."); }
public void If_linestring_is_valid_then_should_return_geojson_with_non_integer_numbers() { var lineString = new LineString(new[] { new Point(10.5, 20.5), new Point(10.5, 30.5), new Point(0.5, 0.5) }); const string expectGeojson = "{\"type\": \"LineString\",\"coordinates\": [[10.5, 20.5], [10.5, 30.5], [0.5, 0.5]]}"; GeoJsonWriter.Write(lineString).ShouldBe(expectGeojson); }
public void TestRegressionDecode1() { string data = "CwMVUCRPVxJYt9IEFkYScQYP"; 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]]}"; // create a referenced decoder. var referencedDecoder = new ReferencedOsmDecoder(RealGraphOsm.GetRoutingGraph("puurs"), new BinaryDecoder()); // decodes a location. var maxSettlesOriginal = BasicRouter.MaxSettles; BasicRouter.MaxSettles = 65536; var location = referencedDecoder.Decode(data); Assert.IsNotNull(location); var lineLocation = location as ReferencedLine; var lineLocationGeometry = lineLocation.ToGeometry(); BasicRouter.MaxSettles = maxSettlesOriginal; // write GeoJSON. var geoJsonWriter = new GeoJsonWriter(); var geoJson = geoJsonWriter.Write(lineLocationGeometry); Assert.IsNotNull(geoJson); AssertGeo.AreEqual(geoJsonActual, geoJson, 1); }
private static void Log(Dictionary <string, IGeometry> shapes, string step, Stopwatch watch) { watch.Stop(); Console.WriteLine(step + $": {watch.ElapsedMilliseconds / 1000m} seconds"); if (OutputFolder == null) { return; } var geoJson = new GeoJsonWriter(); var features = new FeatureCollection(); foreach (var result in shapes) { var feature = new Feature(); feature.Geometry = result.Value; var attributesTable = new AttributesTable(); if (result.Key != null) { attributesTable.Add("Id", result.Key); } feature.Attributes = attributesTable; features.Add(feature); } var json = geoJson.Write(features); File.WriteAllText(OutputFolder + $@"{step}.json", json); }
private static object ConvertToJson(byte[] geometry) { var geom = Reader.Read(geometry); if (!geom.IsValid) { throw new Exception(); } //return JsonConvert.DeserializeObject(Writer.Write(geom)); for (var i = 0; i < geom.NumGeometries; i++) { TransformRing(((Polygon)geom.GetGeometryN(i)).Boundary); } if (geom.IsValid) { return(JsonConvert.DeserializeObject(Writer.Write(geom))); } throw new Exception("Something went wrong when reprojecting"); }
public void If_geometry_is_invalid_then_should_throw_exception() { var point = new Point(); Assert.Throws <ArgumentException>(() => GeoJsonWriter.Write(point)).Message.Should() .BeEquivalentTo("Invalid geometry"); }
public static string ToGeoJson(byte[] bytes) { var fc = FeatureCollectionConversions.FromFlatGeobuf(bytes); var writer = new GeoJsonWriter(); var geojson = writer.Write(fc); return geojson; }
public void If_polygon_with_one_ring_is_valid_then_should_return_geojson() { var polygon = new Polygon(new[] { new LineString(new[] { new Point(-120, 30), new Point(0, 0), new Point(120, 30), new Point(-120, 30) }) }); const string expectGeojson = "{\"type\": \"Polygon\",\"coordinates\": [[[-120, 30], [0, 0], [120, 30], [-120, 30]]]}"; GeoJsonWriter.Write(polygon).Should().BeEquivalentTo(expectGeojson); }
/// <summary> /// Applys the example of a <see cref="FeatureCollection"/> 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(FeatureCollection)) { return; } var writer = new GeoJsonWriter(); var exampleFeatureCollectionString = writer.Write( new FeatureCollection(new Collection <IFeature> { new Feature(new LineString(new[] { new Coordinate(1, 2), new Coordinate(3, 4), }), new AttributesTable()) })); var jsonObject = JsonConvert.DeserializeObject <JObject>(exampleFeatureCollectionString); model.Example = jsonObject; model.Default = jsonObject; }
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 If_multipoint_is_valid_then_should_return_geojson() { var multiPoint = new MultiPoint(new[] { new Point(10, 20), new Point(20, 30), new Point(30, 60) }); const string expectGeojson = "{\"type\": \"MultiPoint\",\"coordinates\": [[10, 20], [20, 30], [30, 60]]}"; GeoJsonWriter.Write(multiPoint).Should().BeEquivalentTo(expectGeojson); }
public void If_linestring_is_valid_then_should_return_geojson() { var lineString = new LineString(new[] { new Point(10, 20), new Point(10, 30), new Point(0, 0) }); const string expectGeojson = "{\"type\": \"LineString\",\"coordinates\": [[10, 20], [10, 30], [0, 0]]}"; GeoJsonWriter.Write(lineString).Should().BeEquivalentTo(expectGeojson); }
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[] ConvertGeoJsonLineStringsToWgs84(string[] geoJsonLineStrings) { if (geoJsonLineStrings == null) { return(null); } var geoJsonReader = new GeoJsonReader(); var geoJsonWriter = new GeoJsonWriter(); List <string> result = new(); foreach (var inputGeoJson in geoJsonLineStrings) { var line = geoJsonReader.Read <LineString>("{ \"type\": \"LineString\",\"coordinates\":" + inputGeoJson + " }"); foreach (var coord in line.Coordinates) { var conversionResult = UTM32WGS84Converter.ConvertFromUTM32NToWGS84(coord.X, coord.Y); coord.X = conversionResult[0]; coord.Y = conversionResult[1]; } var newGeoJson = geoJsonWriter.Write(line); newGeoJson = newGeoJson.Replace("{\"type\":\"LineString\",\"coordinates\":", ""); newGeoJson = newGeoJson.Replace("}", ""); result.Add(newGeoJson); } return(result.ToArray()); }
public void geojson_should_serialize_nested_objects() { const string json = @" { ""type"": ""FeatureCollection"", ""features"": [ { ""type"": ""Feature"", ""geometry"": { ""type"": ""Point"", ""coordinates"": [ 1.0, 2.0 ] }, ""properties"": { ""foo"": { ""bar"": ""xyz"" } } } ] } "; 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); }
/// <summary> /// Applys the example of a <see cref="Feature"/> to the schema /// </summary> /// <param name="context"></param> /// <param name="schema"></param> public void Apply(OpenApiSchema schema, SchemaFilterContext context) { if (context == null) { return; } if (context.ApiModel.Type != typeof(Feature)) { return; } var writer = new GeoJsonWriter { SerializerSettings = new JsonSerializerSettings { Formatting = Formatting.Indented } }; var exampleFeatureString = writer.Write( new Feature(new LineString(new[] { new Coordinate(1, 2), new Coordinate(3, 4), }), new AttributesTable { { "key", "value" } }) ); schema.Example = new OpenApiString(exampleFeatureString); schema.Default = new OpenApiString(exampleFeatureString); }
public void TestOutputPrecision() { var coords = new[] { new Coordinate(0.001, 0.001), new Coordinate(10.1, 0.002), new Coordinate(10, 10.1), new Coordinate(0.05, 9.999), new Coordinate(0.001, 0.001) }; // Create a factory with scale = 10 var factory = new GeometryFactory(new PrecisionModel(10), 4326); // Creating the polygon with the above factory var polygon = factory.CreatePolygon(coords); var serializer = GeoJsonSerializer.Create(factory); var writer = new StringWriter(); serializer.Serialize(writer, polygon); string json = writer.ToString(); Assert.That(json, Is.EqualTo("{\"type\":\"Polygon\",\"coordinates\":[[[0.0,0.0],[10.1,0.0],[10.0,10.1],[0.1,10.0],[0.0,0.0]]]}")); var gjWriter = new GeoJsonWriter(); string json2 = gjWriter.Write(polygon); Assert.That(json2, Is.EqualTo("{\"type\":\"Polygon\",\"coordinates\":[[[0.0,0.0],[10.1,0.0],[10.0,10.1],[0.1,10.0],[0.0,0.0]]]}")); }
public static string Deserialize(byte[] bytes) { var fc = FeatureCollectionConversions.Deserialize(bytes); var writer = new GeoJsonWriter(); var geojson = writer.Write(fc); return(geojson); }
public static string ToGeoJson(System.Data.Entity.Spatial.DbGeometry location) { var wktReader = new WKTReader(); var geometry = wktReader.Read(location.WellKnownValue.WellKnownText); var geoJsonWriter = new GeoJsonWriter(); return(geoJsonWriter.Write(geometry)); }
public static string Geom2GeoJSON(IGeometry geometry) { var geoWriter = new GeoJsonWriter(); var geoJSON = geoWriter.Write(geometry); return(geoJSON); }
public void If_point_is_valid_then_should_return_geojson() { var point = new Point(10, 20); const string expectGeojson = "{\"type\": \"Point\",\"coordinates\": [10, 20]}"; var resultWkt = GeoJsonWriter.Write(point); resultWkt.Should().BeEquivalentTo(expectGeojson); point.ToGeoJson().Should().BeEquivalentTo(expectGeojson); }
public string GenerateMap(List <object> hexagons, HexagonDefinition hexagonDefinition, int hexagonReferenceZoom) { GeoJsonWriter writer = new GeoJsonWriter(); FeatureCollection hexagonCollection = new FeatureCollection(); foreach (IDictionary <string, object> hexagon in hexagons) { HexagonLocationUV locationUV = new HexagonLocationUV(Convert.ToInt32(hexagon["U"]), Convert.ToInt32(hexagon["V"])); IList <PointXY> points = HexagonUtils.GetPointsXYOfHexagon(locationUV, hexagonDefinition); LinearRing ring = new LinearRing(points.Select(pixelCoordinate => { var(latitude, longitude) = TileSystem.PixelXYToLatLong((int)pixelCoordinate.X, (int)pixelCoordinate.Y, hexagonReferenceZoom); return(new Coordinate(longitude, latitude)); }).ToArray()); Polygon hexagonPolygon = new Polygon(ring); AttributesTable attributes = new AttributesTable( new List <KeyValuePair <string, object> > { new KeyValuePair <string, object>("U", locationUV.U), new KeyValuePair <string, object>("V", locationUV.V), }); foreach (var key in hexagon.Keys) { switch (key) { case "U": case "V": break; default: object value = hexagon[key]; attributes.Add(key, value); break; } } Feature hexagonFeature = new Feature(hexagonPolygon, attributes); hexagonCollection.Add(hexagonFeature); } //4. Export Geojson just for the hexagons in this particular tile string result = writer.Write(hexagonCollection); return(result); }
public static string ToGeoJson(System.Data.Entity.Spatial.DbGeometry location, ProjectionInfo pStart, ProjectionInfo pEnd) { var wktReader = new WKTReader(); var geometry = wktReader.Read(location.WellKnownValue.WellKnownText); geometry = Project(geometry, pStart, pEnd); var geoJsonWriter = new GeoJsonWriter(); return(geoJsonWriter.Write(geometry)); }
public void TestNullValueHandlingEnvelope() { var factory = new GeometryFactory(new PrecisionModel(100), 4326); IFeature value = new Feature(factory.CreatePoint(new Coordinate(23, 56)), new AttributesTable()); var writer = new GeoJsonWriter(); writer.SerializerSettings.NullValueHandling = NullValueHandling.Include; Console.WriteLine(writer.Write(value)); }
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()); } }
//returns geoJSON of boundaries from DB filters by type public string GetGeoJsonCollectionFromBoundaryCollection(ICollection <Boundary> collectionOfBoundaries, BoundaryType boundaryType) { //create GeoJSON writer GeoJsonWriter geoJsonWriter = new GeoJsonWriter(); //create WKT Binary reader WKBReader wKBReader = new WKBReader(); //create empty feature collection FeatureCollection features = new FeatureCollection(); //setup return boundary flag bool returnBoundary = false; //iterate through collection foreach (Boundary boundary in collectionOfBoundaries) { returnBoundary = false; switch (boundaryType) { case BoundaryType.ALL: returnBoundary = true; break; case BoundaryType.COVERAGE: if (boundary.isCoverageArea) { returnBoundary = true; } break; case BoundaryType.DISPLAY: if (boundary.displayOnMap) { returnBoundary = true; } break; } if (returnBoundary) { //set up feature attributes table AttributesTable attributesTable = new AttributesTable(); //add attributes to table from boundary attributesTable.AddAttribute("id", boundary.BoundaryId); attributesTable.AddAttribute("Name", boundary.Name); //convert DbGeometry to GeoApi.IGeometry IGeometry iGeom = wKBReader.Read(boundary.Area.AsBinary()); //create feature from geom and attributes Feature feature = new Feature(iGeom, attributesTable); //add feature to feature collection features.Add(feature); } } return(geoJsonWriter.Write(features)); }