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());
        }
Example #2
0
        public void geojson_should_deserialize_a_feature_with_geometrycollection()
        {
            const string  json     = @"
{
    ""type"": ""Feature"",
    ""geometry"": {
        ""type"": ""GeometryCollection"",
        ""geometries"": [ 
            {
                ""type"":""Polygon"",
                ""coordinates"":[[[1.0,1.0],[1.0,2.0],[2.0,2.0],[1.0,1.0]]]
            },
            {
                ""type"":""Point"",
                ""coordinates"":[100.0,100.0]
            },
            {
                ""type"":""Polygon"",
                ""coordinates"":[[[201.0,201.0],[201.0,202.0],[202.0,202.0],[201.0,201.0]]]
            }
        ]
    },
    ""properties"": {
    }
}
";
            GeoJsonReader reader   = new GeoJsonReader();
            Feature       geometry = reader.Read <Feature>(json);

            Assert.IsNotNull(geometry);
        }
        public void TestWhenFeatureCollectionHasBBox()
        {
            const string geoJsonString     = @"
{
  ""type"":""FeatureCollection"",
  ""features"":[ 
  { 
      ""geometry"":{ 
          ""type"":""Point"", 
          ""coordinates"":[ -6.09, 4.99 ]
      }, 
      ""type"":""Feature"", 
      ""properties"":{
          ""prop1"":[ ""a"", ""b"" ] 
      }, 
      ""id"":1 
  } ], 
  ""bbox"":[ -8.59, 4.35, -2.49, 10.73 ] 
}";
            var          featureCollection = new GeoJsonReader().Read <FeatureCollection>(geoJsonString);

            Assert.AreEqual(1, featureCollection.Count);
            Assert.That(FeatureExtensions.HasID(featureCollection[0]));
            var res = new GeoJsonWriter().Write(featureCollection);

            CompareJson(geoJsonString, res);
        }
        public void valid_geojson_feature_collection_should_be_serialized()
        {
            const string json = @"
{
	""type"" : ""FeatureCollection"",
	""features"" : [{
			""type"" : ""Feature"",
			""properties"" : {},
			""geometry"" : {
				""type"" : ""Polygon"",
				""coordinates"" : [[[-2.537841796875, 53.50111704294316], [-2.537841796875, 54.226707764386695], [-1.0986328125, 54.226707764386695], [-1.0986328125, 53.50111704294316], [-2.537841796875, 53.50111704294316]]]
			}
		}, {
			""type"" : ""Feature"",
			""properties"" : {},
			""geometry"" : {
				""type"" : ""Polygon"",
				""coordinates"" : [[[-2.724609375, 52.34205163638784], [-2.724609375, 53.08082737207479], [-0.9667968749999999, 53.08082737207479], [-0.9667968749999999, 52.34205163638784], [-2.724609375, 52.34205163638784]]]
			}
		}
	]
}
";
            GeoJsonReader reader = new GeoJsonReader();            
            FeatureCollection coll = reader.Read<FeatureCollection>(json);
            Assert.That(coll, Is.Not.Null);
            Assert.That(coll.Count, Is.EqualTo(2));
        }
Example #5
0
        public void GeoJsonReaderReadGeometryCollectionTest()
        {
            const string json   = "{\"type\": \"GeometryCollection\", \"geometries\": [{\"type\": \"Point\", \"coordinates\": [99.0, 89.0]}, { \"type\": \"LineString\", \"coordinates\": [ [101.0, 0.0], [102.0, 1.0]]}] }";
            Geometry     result = new GeoJsonReader().Read <Geometry>(json);

            Assert.IsNotNull(result);
            Assert.IsInstanceOf(typeof(GeometryCollection), result);
            GeometryCollection geometryCollection = (GeometryCollection)result;

            Assert.AreEqual(2, geometryCollection.Count);

            var point = (Point)geometryCollection.GetGeometryN(0);

            Assert.IsInstanceOf(typeof(Point), point);
            Assert.AreEqual(99, point.X);
            Assert.AreEqual(89, point.Y);

            var lineString = (LineString)geometryCollection.GetGeometryN(1);

            Assert.IsInstanceOf(typeof(LineString), lineString);
            Assert.AreEqual(2, lineString.Coordinates.Length);
            Assert.AreEqual(101, lineString.Coordinates[0].X);
            Assert.AreEqual(0, lineString.Coordinates[0].Y);
            Assert.AreEqual(102, lineString.Coordinates[1].X);
            Assert.AreEqual(1, lineString.Coordinates[1].Y);
        }
        public void geojson_feature_with_both_id_and_null_crs_should_be_serialized()
        {
            const string json   = @"
{
	""type"" : ""FeatureCollection"",
	""totalFeatures"" : 5,
	""features"" : [{
			""type"" : ""Feature"",
			""id"" : ""vfs_kommun.256"",
			""geometry"" : null,
			""properties"" : {
				""kommunnamn"" : ""Karlskrona""
			}
		}
	],
	""crs"" : null
}
";
            var          reader = new GeoJsonReader();
            var          coll   = reader.Read <FeatureCollection>(json);

            Assert.That(coll, Has.Count.EqualTo(1));

            var feature = coll[0];

            Assert.That(feature.Geometry, Is.Null);
            Assert.That(feature.Attributes, Is.Not.Null);
            Assert.That(feature.Attributes.TryGetId(out object id));
            Assert.That(id, Is.EqualTo("vfs_kommun.256"));
        }
Example #7
0
        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);
        }
Example #8
0
        public void GeoJsonReaderReadContentTest()
        {
            //test 1st input file
            GeoJsonReader reader = new GeoJsonReader(_inputFilePaths[0]);

            IGeometry result = reader.Read();

            Assert.IsInstanceOf(typeof(IGeometryCollection <IGeometry>), result);

            IGeometryCollection <IGeometry> collection = result as IGeometryCollection <IGeometry>;

            Assert.IsTrue(collection[0] is IPoint);
            Assert.IsTrue((collection[0] as IPoint).Coordinate == new Coordinate(1, 1, 9));

            Assert.IsTrue(collection[1] is IMultiLineString);
            Assert.AreEqual((collection[1] as IMultiLineString).Count, 2);
            ILineString lstr = (collection[1] as IMultiLineString)[1];

            Assert.AreEqual(5, lstr.Coordinates.Count);
            Assert.AreEqual(lstr.Coordinates[0], new Coordinate(401, 28));
            Assert.AreEqual(lstr.Coordinates[1], new Coordinate(36, 12));
            Assert.AreEqual(lstr.Coordinates[2], new Coordinate(100, 37));
            Assert.AreEqual(lstr.Coordinates[3], new Coordinate(49, 46));
            Assert.AreEqual(lstr.Coordinates[4], new Coordinate(28, 34));

            Assert.IsTrue(collection[2] is IMultiPolygon);
            Assert.AreEqual(2, (collection[2] as IMultiPolygon).Count);

            Assert.IsTrue(reader.EndOfStream);
            Assert.AreEqual(0, reader.ReadToEnd().Count);

            reader.Close();
        }
        public void geojson_should_deserialize_a_feature_with_geometrycollection()
        {
            const string json = @"
{
    ""type"": ""Feature"",
    ""geometry"": {
        ""type"": ""GeometryCollection"",
        ""geometries"": [ 
            {
                ""type"":""Polygon"",
                ""coordinates"":[[[1.0,1.0],[1.0,2.0],[2.0,2.0],[1.0,1.0]]]
            },
            {
                ""type"":""Point"",
                ""coordinates"":[100.0,100.0]
            },
            {
                ""type"":""Polygon"",
                ""coordinates"":[[[201.0,201.0],[201.0,202.0],[202.0,202.0],[201.0,201.0]]]
            }
        ]
    },
    ""properties"": {
    }
}
";
            GeoJsonReader reader = new GeoJsonReader();
            Feature geometry = reader.Read<Feature>(json);
            Assert.IsNotNull(geometry);
        }
Example #10
0
        public void valid_geojson_feature_should_be_serialized()
        {
            const string      json   = @"
{
	""type"" : ""FeatureCollection"",
	""totalFeatures"" : 5,
	""features"" : [{
			""type"" : ""Feature"",
			""id"" : ""vfs_kommun.256"",
			""geometry"" : null,
			""properties"" : {
				""kommunnamn"" : ""Karlskrona""
			}
		}
	],
	""crs"" : null
}
";
            GeoJsonReader     reader = new GeoJsonReader();
            FeatureCollection coll   = reader.Read <FeatureCollection>(json);

            Assert.That(coll, Is.Not.Null);
            Assert.That(coll.CRS, Is.Null);
            Assert.That(coll.Count, Is.EqualTo(1));

            IFeature feature = coll[0];

            Assert.That(feature.Geometry, Is.Null);
            Assert.That(feature.Attributes, Is.Not.Null);
            Assert.That(feature.Attributes.Exists("id"), Is.True);
            Assert.That(feature.Attributes["id"], Is.EqualTo("vfs_kommun.256"));
        }
        public void geojson_feature_with_both_id_and_null_crs_should_be_serialized()
        {
            const string json = @"
{
	""type"" : ""FeatureCollection"",
	""totalFeatures"" : 5,
	""features"" : [{
			""type"" : ""Feature"",
			""id"" : ""vfs_kommun.256"",
			""geometry"" : null,
			""properties"" : {
				""kommunnamn"" : ""Karlskrona""
			}
		}
	],
	""crs"" : null
}
";
            GeoJsonReader reader = new GeoJsonReader();
            FeatureCollection coll = reader.Read<FeatureCollection>(json);
            Assert.That(coll, Is.Not.Null);
            Assert.That(coll.CRS, Is.Null);
            Assert.That(coll.Count, Is.EqualTo(1));

            IFeature feature = coll[0];
            Assert.That(feature.Geometry, Is.Null);
            Assert.That(feature.Attributes, Is.Not.Null);
            Assert.That(feature.Attributes.Exists("id"), Is.True);
            Assert.That(feature.Attributes["id"], Is.EqualTo("vfs_kommun.256"));
        }
        public static FeatureCollection LoadFeatureCollectionFromGeoJsonString(string geoJson)
        {
            var reader            = new GeoJsonReader();
            var featureCollection = reader.Read <FeatureCollection>(geoJson);

            return(featureCollection);
        }
        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 GeoJsonConverter(IGeometryFactory geometryFactory)
        {
            var maker = new ShapeConverter(geometryFactory);

            _reader = new GeoJsonReader(maker);
            _writer = new GeoJsonWriter(maker);
        }
Example #15
0
        public void GeoJsonReaderReadGeometryPolygonWithHoleTest()
        {
            const string json   = "{\"type\": \"Polygon\",\"coordinates\": [[[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]], [[100.2, 0.2], [100.8, 0.2], [100.8, 0.8], [100.2, 0.8], [100.2, 0.2]]]}";
            Geometry     result = new GeoJsonReader().Read <Geometry>(json);

            Assert.IsNotNull(result);
            Assert.IsInstanceOf(typeof(Polygon), result);
            Polygon poly = (Polygon)result;

            Assert.AreEqual(1, poly.NumInteriorRings);
            Assert.AreEqual(100, poly.ExteriorRing.Coordinates[0].X);
            Assert.AreEqual(0, poly.ExteriorRing.Coordinates[0].Y);
            Assert.AreEqual(101, poly.ExteriorRing.Coordinates[1].X);
            Assert.AreEqual(0, poly.ExteriorRing.Coordinates[1].Y);
            Assert.AreEqual(101, poly.ExteriorRing.Coordinates[2].X);
            Assert.AreEqual(1, poly.ExteriorRing.Coordinates[2].Y);
            Assert.AreEqual(100, poly.ExteriorRing.Coordinates[3].X);
            Assert.AreEqual(1, poly.ExteriorRing.Coordinates[3].Y);
            Assert.AreEqual(100, poly.ExteriorRing.Coordinates[4].X);
            Assert.AreEqual(0, poly.ExteriorRing.Coordinates[4].Y);

            Assert.AreEqual(100.2, poly.GetInteriorRingN(0).Coordinates[0].X);
            Assert.AreEqual(0.2, poly.GetInteriorRingN(0).Coordinates[0].Y);
            Assert.AreEqual(100.8, poly.GetInteriorRingN(0).Coordinates[1].X);
            Assert.AreEqual(0.2, poly.GetInteriorRingN(0).Coordinates[1].Y);
            Assert.AreEqual(100.8, poly.GetInteriorRingN(0).Coordinates[2].X);
            Assert.AreEqual(0.8, poly.GetInteriorRingN(0).Coordinates[2].Y);
            Assert.AreEqual(100.2, poly.GetInteriorRingN(0).Coordinates[3].X);
            Assert.AreEqual(0.8, poly.GetInteriorRingN(0).Coordinates[3].Y);
            Assert.AreEqual(100.2, poly.GetInteriorRingN(0).Coordinates[4].X);
            Assert.AreEqual(0.2, poly.GetInteriorRingN(0).Coordinates[4].Y);
        }
        public void valid_geojson_feature_collection_should_be_serialized_also_with_properties()
        {
            const string      json   = @"
{
	""type"" : ""FeatureCollection"",
	""features"" : [{
			""type"" : ""Feature"",
			""properties"" : {},
			""geometry"" : {
				""type"" : ""Polygon"",
				""coordinates"" : [[[-2.537841796875, 53.50111704294316], [-2.537841796875, 54.226707764386695], [-1.0986328125, 54.226707764386695], [-1.0986328125, 53.50111704294316], [-2.537841796875, 53.50111704294316]]]
			}
		}, {
			""type"" : ""Feature"",
			""properties"" : {},
			""geometry"" : {
				""type"" : ""Polygon"",
				""coordinates"" : [[[-2.724609375, 52.34205163638784], [-2.724609375, 53.08082737207479], [-0.9667968749999999, 53.08082737207479], [-0.9667968749999999, 52.34205163638784], [-2.724609375, 52.34205163638784]]]
			}
		}
	],
    ""properties"" : {
        ""ignored1"" : ""ignoredval1"",
        ""ignored2"" : ""ignoredval2""
    }
}
";
            GeoJsonReader     reader = new GeoJsonReader();
            FeatureCollection coll   = reader.Read <FeatureCollection>(json);

            Assert.That(coll, Is.Not.Null);
            Assert.That(coll.Count, Is.EqualTo(2));
        }
Example #17
0
        public void Test()
        {
            const string geoJsonString     = "{ \"type\":\"FeatureCollection\", \"features\":[ { \"geometry\":{ \"type\":\"Point\", \"coordinates\":[ -6.091861724853516, 4.991835117340088 ] }, \"type\":\"Feature\", \"properties\":{ \"prop1\":[ \"a\", \"b\" ] }, \"id\":1 } ], \"bbox\":[ -8.599302291870117, 4.357067108154297, -2.494896650314331, 10.736641883850098 ] }";
            var          featureCollection = new GeoJsonReader().Read <FeatureCollection>(geoJsonString);

            Assert.AreEqual(1, featureCollection.Count);
        }
        public void TestGeoJsonWithComments()
        {
            const string geojson =
                @"{
  // here we go
  ""id"": ""featureID"",
  // A feature
    ""type"": ""Feature"",
    // Its geometry
    ""geometry"": {
      // look, its a point
      ""type"": ""Point"",
      // where is it
      ""coordinates"": [50.77, 6.11]
      // ah in Aix la Chapelle
    },
    // here come the properties?
    ""properties"": {
      // now what
      ""plz"": ""52xxx""
      // boring
    } 
    // booooring
  }
  // the end
}";

            Feature f = null;

            Assert.That(() => f = new GeoJsonReader().Read <Feature>(geojson), Throws.Nothing);
            Assert.That(f, Is.Not.Null);
        }
Example #19
0
        private Dictionary <long, (IFeature feature, long parent)> LoadBoundaries()
        {
            var boundaries = new Dictionary <long, (IFeature feature, long parent)>();

            // read all boundaries from geojson.
            var geoJsonReader = new GeoJsonReader();

            foreach (var file in Directory.EnumerateFiles(_configuration.DataPath, "*.geojson"))
            {
                var features = geoJsonReader.Read <FeatureCollection>(
                    File.ReadAllText(file));

                foreach (var feature in features)
                {
                    if (feature.Attributes == null)
                    {
                        continue;
                    }
                    if (!feature.Attributes.Exists("id"))
                    {
                        continue;
                    }

                    var idValue = feature.Attributes["id"];
                    if (!(idValue is long))
                    {
                        continue;
                    }
                    var id = (long)idValue;

                    var parentId = -1L;
                    if (feature.Attributes.Exists("parents"))
                    {
                        var parents = feature.Attributes["parents"];
                        if (parents is string parentString &&
                            !string.IsNullOrWhiteSpace(parentString))
                        {
                            var parentSplit = parentString.Split(',');
                            if (parentSplit.Length > 0)
                            {
                                foreach (var parentIdString in parentSplit)
                                {
                                    if (!long.TryParse(parentIdString, NumberStyles.Any,
                                                       System.Globalization.CultureInfo.InvariantCulture, out parentId))
                                    {
                                        parentId = -1;
                                    }
                                    break;
                                }
                            }
                        }
                    }

                    boundaries[id] = (feature, parentId);
                }
            }

            return(boundaries);
        }
Example #20
0
        public static byte[] Serialize(string geojson)
        {
            var reader = new GeoJsonReader();
            var fc     = reader.Read <FeatureCollection>(geojson);
            var bytes  = FeatureCollectionConversions.Serialize(fc, GeometryType.Unknown);

            return(bytes);
        }
Example #21
0
        public void GeoJsonReaderHandleUnsupportedCrsTest()
        {
            GeoJsonReader reader = new GeoJsonReader(_invalidInputJsonFilePath);

            Assert.Throws <InvalidDataException>(() => reader.Read());

            reader.Close();
        }
Example #22
0
        public static Geometry FromGeoJson(string geoJson)
        {
            Geometry result = new GeoJsonReader(_Factory, new JsonSerializerSettings()).Read <Geometry>(geoJson);

            //var featureCollection = new GeoJsonReader(_Factory, new JsonSerializerSettings()).Read<FeatureCollection>(geoJson);
            //result.SRID = featureCollection[0].Attributes[0]
            return(result);
        }
Example #23
0
        public static async Task <byte[]> SerializeAsync(string geojson)
        {
            var reader = new GeoJsonReader();
            var fc     = reader.Read <FeatureCollection>(geojson);
            var bytes  = await FeatureCollectionConversions.SerializeAsync(fc, GeometryType.Unknown);

            return(bytes);
        }
        public void GivenAGeoJsonReaderAndWriter()
        {
            _reader = new GeoJsonReader();
            _writer = new GeoJsonWriter();
            var geometry = new Point(1, 2);

            _feature = new Feature(geometry, new AttributesTable());
        }
 private void ConvertGeoJSONToGeometry_Click(object sender, RoutedEventArgs e)
 {
     //引用NetTopologySuite.IO.GeoJSON
     //https://github.com/NetTopologySuite/NetTopologySuite.IO.GeoJSON
     String        geoJSONText = GetTxtFromFile();
     GeoJsonReader reader      = new GeoJsonReader();
     var           geometry    = reader.Read <NetTopologySuite.Geometries.Geometry>(geoJSONText);
 }
Example #26
0
        /// <summary>
        /// Compares two linestrings.
        /// </summary>
        /// <param name="geoJsonActual"></param>
        /// <param name="geoJson"></param>
        public static void AreEqual(string geoJsonActual, string geoJson, double delta)
        {
            var geoJsonReader = new GeoJsonReader();
            var actual        = geoJsonReader.Read <IGeometry>(geoJsonActual);
            var expected      = geoJsonReader.Read <IGeometry>(geoJson);

            AssertGeo.AreEqual(actual, expected, delta);
        }
Example #27
0
        private FeatureCollection GetFeaturesFromCollection(Dataset dataset)
        {
            string json       = System.IO.File.ReadAllText(dataset.Provider.Data);
            var    reader     = new GeoJsonReader();
            var    collection = reader.Read <FeatureCollection>(json);

            return(collection);
        }
Example #28
0
        public void howto_deserialize_geometries()
        {
            GeoJsonReader reader = new GeoJsonReader();
            IGeometry     actual = reader.Read <GeometryCollection>(serializedCollection);

            Assert.That(actual, Is.Not.Null);
            Assert.That(actual.EqualsExact(collection), Is.True);
        }
Example #29
0
        public Geometry Convert(string sourceMember, ResolutionContext context)
        {
            var      reader  = new GeoJsonReader();
            var      feature = reader.Read <Feature>(sourceMember);
            Geometry geo     = feature.Geometry.Normalized().Reverse();

            return(geo);
        }
Example #30
0
        public void Point()
        {
            var reader = new GeoJsonReader();
            var geo    = new Point(0, 0);

            Assert.AreEqual(@"{""type"":""Point"",""coordinates"":[0,0]}", geo.ToGeoJson());
            Assert.AreEqual(geo, reader.Read(geo.ToGeoJson()));
        }
Example #31
0
        public void If_json_is_valid_point_then_should_return_correct_point()
        {
            const string geojson   = "{\"type\": \"Point\",\"coordinates\": [10, 20]}";
            var          expectGeo = new Point(10, 20);

            var resultGeo = GeoJsonReader.Read(geojson);

            resultGeo.Equals(expectGeo).Should().BeTrue();
        }
Example #32
0
        public void MultiLineString()
        {
            var reader = new GeoJsonReader();
            var geo    = new MultiLineString(new LineString(new Coordinate(0, 0), new Coordinate(1, 1)));

            Assert.AreEqual(@"{""type"":""MultiLineString"",""coordinates"":[[[0,0],[1,1]]]}",
                            geo.ToGeoJson());
            Assert.AreEqual(geo, reader.Read(geo.ToGeoJson()));
        }
        public void If_json_is_valid_point_and_in_different_order_then_should_return_correct_point()
        {
            const string geojson   = "{ \"coordinates\" : [10, 20], \"type\" : \"Point\" }";
            var          expectGeo = new Point(10, 20);

            var resultGeo = GeoJsonReader.Read(geojson);

            resultGeo.ShouldBe(expectGeo);
        }
        public void GeoJsonReaderReadCRSTest()
        {
            const string json = "{\"type\":\"name\",\"properties\":{\"name\":\"name1\"}}";
            CRSBase result = new GeoJsonReader().Read<CRSBase>(json);

            Assert.IsNotNull(result);
            Assert.AreEqual(CRSTypes.Name, result.Type);
            Assert.IsNotNull(result.Properties);
            Assert.AreEqual("name1", result.Properties["name"]);
        }
        public void GeoJsonReaderReadGeometryPointTest()
        {
            const string json = "{\"type\":\"Point\",\"coordinates\":[23.0,56.0]}";
            Geometry result = new GeoJsonReader().Read<Geometry>(json);

            Assert.IsNotNull(result);
            Assert.IsInstanceOf(typeof(Point), result);
            Point p = (Point)result;
            Assert.AreEqual(23, p.X);
            Assert.AreEqual(56, p.Y);
        }
        public void GeoJsonReaderReadFeatureTest()
        {
            const string json = "{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[23.0,56.0]},\"properties\":{\"test1\":\"value1\"}}";
            IFeature result = new GeoJsonReader().Read<Feature>(json);

            Assert.IsNotNull(result);
            Assert.IsInstanceOf(typeof(Point), result.Geometry);
            Point p = (Point)result.Geometry;
            Assert.AreEqual(23, p.X);
            Assert.AreEqual(56, p.Y);
            Assert.IsNotNull(result.Attributes);
            Assert.AreEqual(1, result.Attributes.Count);
            Assert.AreEqual("value1", result.Attributes["test1"]);
        }
        public void GeoJsonReaderReadFeatureCollectionTest()
        {
            const string json = "{\"features\":[{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[23.0,56.0]},\"properties\":{\"test1\":\"value1\"}}],\"type\":\"FeatureCollection\",\"crs\":{\"type\":\"name\",\"properties\":{\"name\":\"name1\"}}}";
            GeoJsonReader reader = new GeoJsonReader();
            FeatureCollection result = reader.Read<FeatureCollection>(json);

            Assert.IsNotNull(result);
            Assert.AreEqual(1, result.Count);
            Assert.IsNotNull(result.CRS);
            Assert.IsInstanceOf(typeof(NamedCRS), result.CRS);
            Assert.AreEqual(CRSTypes.Name, result.CRS.Type);
            NamedCRS crs = (NamedCRS)result.CRS;
            Assert.AreEqual("name1", crs.Properties["name"]);
        }
        /// <summary>
        /// Creates the processor that belongs to this data.
        /// </summary>
        /// <returns></returns>
        public override ProcessorBase CreateProcessor()
        {
            // parse geojson file.
            using(var geoJSONFile = new FileInfo(this.GeoJSONFile).OpenText())
            {
                string geoJson = geoJSONFile.ReadToEnd();
                var geoJsonReader = new GeoJsonReader();

                var featureCollection = geoJsonReader.Read<FeatureCollection>(geoJson) as FeatureCollection;

                foreach (var feature in featureCollection.Features)
                {
                    return new ProcessorFeedFilter()
                    { // create a bounding box filter.
                        Filter = new GTFS.Filters.GTFSFeedStopsFilter((s) =>
                        {
                            return feature.Geometry.Covers(new Point(new Coordinate(s.Longitude, s.Latitude)));
                        })
                    };
                }
            }
            throw new Exception("No geometries found in GeoJSON");
        }
        public void geojson_feature_with_null_crs_should_be_serialized()
        {
            const string json = @"
{
	""type"" : ""FeatureCollection"",
	""totalFeatures"" : 5,
	""features"" : [{
			""type"" : ""Feature"",
			""geometry"" : null,
			""properties"" : {
				""kommunnamn"" : ""Karlskrona""
			}
		}
	],
	""crs"" : null
}
";
            GeoJsonReader reader = new GeoJsonReader();
            FeatureCollection coll = reader.Read<FeatureCollection>(json);
            Assert.That(coll, Is.Not.Null);
            Assert.That(coll.CRS, Is.Null);
            Assert.That(coll.Count, Is.EqualTo(1));
        }
        public void GeoJsonDeserializeFeatureCollectionTest()
        {
            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);
            Assert.AreEqual(1, coll.Count);
            IFeature feature = coll[0];

            Assert.IsNotNull(feature);
            Assert.AreEqual(1, feature.Attributes.Count);

            var gjs = new GeoJsonWriter();
            gjs.Write(coll);
        }
Example #41
0
        public void WhenEmptyArrayPropertyInGeoJsonThenReadable()
        {
            const string geojsonString = @"
{
  ""type"":""FeatureCollection"",
  ""features"":[
    {
      ""type"":""Feature"",
      ""geometry"":{
        ""type"":""Point"",
        ""coordinates"":[1.0,2.0]
      },
      ""properties"":{
        ""foo"":[]
      }
    }
  ]
}
";
            var featureCollection = new GeoJsonReader().Read<FeatureCollection>(geojsonString);
            Assert.AreEqual(1, featureCollection.Count);
        }
Example #42
0
 public void GivenAGeoJsonReaderAndWriter()
 {
     _reader = new GeoJsonReader();
     _writer = new GeoJsonWriter();
     var geometry = new Point(1, 2);
     _feature = new Feature(geometry, new AttributesTable());
 }
 public async Task<List<Feature>> GetHighways(LatLng northEast, LatLng southWest)
 {
     var response = await _elasticClient.SearchAsync<object>(
         s => s.Index(OSM_HIGHWAYS_INDEX).Size(5000).Query(
             q => q.GeoShapeEnvelope(
                 e => e.Coordinates(new List<GeoCoordinate>
                     {
                         new GeoCoordinate(southWest.lat, northEast.lng),
                         new GeoCoordinate(northEast.lat, southWest.lng)
                     }).Field("geometry")
                     .Relation(GeoShapeRelation.Intersects)
             )
         )
     );
     var reader = new GeoJsonReader();
     return response.Documents.Select(d => reader.Read<Feature>(d.ToString())).ToList();
 }
        public void GeoJsonReaderReadGeometryCollectionTest()
        {
            const string json ="{\"type\": \"GeometryCollection\", \"geometries\": [{\"type\": \"Point\", \"coordinates\": [99.0, 89.0]}, { \"type\": \"LineString\", \"coordinates\": [ [101.0, 0.0], [102.0, 1.0]]}]";
            Geometry result = new GeoJsonReader().Read<Geometry>(json);

            Assert.IsNotNull(result);
            Assert.IsInstanceOf(typeof(GeometryCollection), result);
            GeometryCollection geometryCollection = (GeometryCollection)result;
            Assert.AreEqual(2, geometryCollection.Count);

            Point point = (Point)geometryCollection.GetGeometryN(0);
            Assert.IsInstanceOf(typeof(Point), point);
            Assert.AreEqual(99, point.X);
            Assert.AreEqual(89, point.Y);

            LineString lineString = (LineString)geometryCollection.GetGeometryN(1);
            Assert.IsInstanceOf(typeof(LineString), lineString);
            Assert.AreEqual(2, lineString.Coordinates.Length);
            Assert.AreEqual(101, lineString.Coordinates[0].X);
            Assert.AreEqual(0, lineString.Coordinates[0].Y);
            Assert.AreEqual(102, lineString.Coordinates[1].X);
            Assert.AreEqual(1, lineString.Coordinates[1].Y);
        }
        public void GeoJsonReaderReadGeometryLineStringTest()
        {
            const string json = "{\"type\": \"LineString\",\"coordinates\": [ [100.0, 0.0], [101.0, 1.0] ]}";
            Geometry result = new GeoJsonReader().Read<Geometry>(json);

            Assert.IsNotNull(result);
            Assert.IsInstanceOf(typeof(LineString), result);
            LineString ls = (LineString)result;
            Assert.AreEqual(2, ls.Coordinates.Length);
            Assert.AreEqual(100, ls.Coordinates[0].X);
            Assert.AreEqual(0, ls.Coordinates[0].Y);
            Assert.AreEqual(101, ls.Coordinates[1].X);
            Assert.AreEqual(1, ls.Coordinates[1].Y);
        }
        public void GeoJsonReaderReadGeometryMultiPointTest()
        {
            const string json = "{\"type\": \"MultiPoint\",\"coordinates\": [[100.0, 0.0], [101.0, 1.0]]}";
            Geometry result = new GeoJsonReader().Read<Geometry>(json);

            Assert.IsNotNull(result);
            Assert.IsInstanceOf(typeof(MultiPoint), result);
            MultiPoint multiPoint = (MultiPoint)result;

            Assert.AreEqual(2, multiPoint.Coordinates.Length);
            Assert.AreEqual(2, multiPoint.NumGeometries);
            Assert.AreEqual(100, multiPoint.Coordinates[0].X);
            Assert.AreEqual(0, multiPoint.Coordinates[0].Y);
            Assert.AreEqual(101, multiPoint.Coordinates[1].X);
            Assert.AreEqual(1, multiPoint.Coordinates[1].Y);
            
        }
        public void geojson_feature_without_additional_fields_should_be_serialized()
        {
            const string json = @"
{
	""type"" : ""FeatureCollection"",
	""totalFeatures"" : 1,
	""features"" : [{
			""type"" : ""Feature"",
			""id"" : ""Road.1249"",
			""geometry"" : {
				""type"" : ""MultiLineString"",
				""coordinates"" : [[[1610822.0864005657, 7249990.524654245], [1610819.2585326964, 7250021.630828278], [1611733.0938508697, 7250797.447679726]]]
			},
			""properties"" : {
				""road_id"" : 173922
			}
		}
	],
	""crs"" : {
		""type"" : ""name"",
		""properties"" : {
			""name"" : ""urn:ogc:def:crs:EPSG::2400""
		}
	}
}
";
            GeoJsonReader reader = new GeoJsonReader();
            FeatureCollection coll = reader.Read<FeatureCollection>(json);
            Assert.That(coll, Is.Not.Null);
            Assert.That(coll.CRS, Is.Not.Null);
            Assert.That(coll.Count, Is.EqualTo(1));

            IFeature feature = coll[0];
            Assert.That(feature.Geometry, Is.Not.Null);
            Assert.That(feature.Attributes, Is.Not.Null);
            Assert.That(feature.Attributes.Exists("id"), Is.True);
            Assert.That(feature.Attributes["id"], Is.EqualTo("Road.1249"));
            Assert.That(feature.Attributes.Exists("road_id"), Is.True);
            Assert.That(feature.Attributes["road_id"], Is.EqualTo(173922));
        }
        public void GeoJsonReaderReadGeometryPolygonTest()
        {
            const string json = "{\"type\": \"Polygon\",\"coordinates\": [[ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ]]}";
            Geometry result = new GeoJsonReader().Read<Geometry>(json);

            Assert.IsNotNull(result);
            Assert.IsInstanceOf(typeof(Polygon), result);
            Polygon poly = (Polygon)result;
            Assert.AreEqual(5, poly.Coordinates.Length);
            Assert.AreEqual(100, poly.Coordinates[0].X);
            Assert.AreEqual(0, poly.Coordinates[0].Y);
            Assert.AreEqual(101, poly.Coordinates[1].X);
            Assert.AreEqual(0, poly.Coordinates[1].Y);
            Assert.AreEqual(101, poly.Coordinates[2].X);
            Assert.AreEqual(1, poly.Coordinates[2].Y);
            Assert.AreEqual(100, poly.Coordinates[3].X);
            Assert.AreEqual(1, poly.Coordinates[3].Y);
            Assert.AreEqual(100, poly.Coordinates[4].X);
            Assert.AreEqual(0, poly.Coordinates[4].Y);
        }
        public void GeoJsonReaderReadGeometryMultiLineStringTest()
        {
            const string json = "{\"type\": \"MultiLineString\",\"coordinates\": [[[100.0, 0.0], [101.0, 1.0]], [[102.0, 2.0], [103.0, 3.0]]]}";
            Geometry result = new GeoJsonReader().Read<Geometry>(json);

            Assert.IsNotNull(result);
            Assert.IsInstanceOf(typeof(MultiLineString), result);
            MultiLineString multiLineString = (MultiLineString)result;

            Assert.AreEqual(2, multiLineString.NumGeometries);
            Assert.AreEqual(100, multiLineString.Coordinates[0].X);
            Assert.AreEqual(0, multiLineString.Coordinates[0].Y);
            Assert.AreEqual(101, multiLineString.Coordinates[1].X);
            Assert.AreEqual(1, multiLineString.Coordinates[1].Y);
            Assert.AreEqual(102, multiLineString.Coordinates[2].X);
            Assert.AreEqual(2, multiLineString.Coordinates[2].Y);
            Assert.AreEqual(103, multiLineString.Coordinates[3].X);
            Assert.AreEqual(3, multiLineString.Coordinates[3].Y);
            
        }
		public GeoJsonConverter(IGeometryFactory geometryFactory)
		{
			var maker = new ShapeConverter(geometryFactory);
			_reader = new GeoJsonReader(maker);
			_writer = new GeoJsonWriter(maker);
		}
Example #51
0
        private static FeatureCollection FeatureCollection(string geoJson)
        {
            var reader = new GeoJsonReader();

            var featureCollection = new FeatureCollection();

            try
            {
                featureCollection =
                    reader.Read<FeatureCollection>(geoJson);
                if (featureCollection.Count == 0)
                {
                    var feature =
                        reader.Read<Feature>(geoJson);
                    featureCollection.Add(feature);
                }
            }
            catch (Exception)
            {
                var feature =
                    reader.Read<Feature>(geoJson);
                featureCollection.Add(feature);
            }
            return featureCollection;
        }
		public GeoJsonConverter()
		{
			var maker = new ShapeConverter();
			_reader = new GeoJsonReader(maker);
			_writer = new GeoJsonWriter(maker);
		}
Example #53
0
 public void howto_deserialize_geometries()
 {
     GeoJsonReader reader = new GeoJsonReader();
     IGeometry actual = reader.Read<GeometryCollection>(serializedCollection);
     Assert.That(actual, Is.Not.Null);
     Assert.That(actual.EqualsExact(collection), Is.True);
 }
        public void GeoJsonReaderReadGeometryPolygonWithHoleTest()
        {
            const string json = "{\"type\": \"Polygon\",\"coordinates\": [[[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]], [[100.2, 0.2], [100.8, 0.2], [100.8, 0.8], [100.2, 0.8], [100.2, 0.2]]]}";
            Geometry result = new GeoJsonReader().Read<Geometry>(json);

            Assert.IsNotNull(result);
            Assert.IsInstanceOf(typeof(Polygon), result);
            Polygon poly = (Polygon)result;

            Assert.AreEqual(1, poly.NumInteriorRings);
            Assert.AreEqual(100, poly.ExteriorRing.Coordinates[0].X);
            Assert.AreEqual(0, poly.ExteriorRing.Coordinates[0].Y);
            Assert.AreEqual(101, poly.ExteriorRing.Coordinates[1].X);
            Assert.AreEqual(0, poly.ExteriorRing.Coordinates[1].Y);
            Assert.AreEqual(101, poly.ExteriorRing.Coordinates[2].X);
            Assert.AreEqual(1, poly.ExteriorRing.Coordinates[2].Y);
            Assert.AreEqual(100, poly.ExteriorRing.Coordinates[3].X);
            Assert.AreEqual(1, poly.ExteriorRing.Coordinates[3].Y);
            Assert.AreEqual(100, poly.ExteriorRing.Coordinates[4].X);
            Assert.AreEqual(0, poly.ExteriorRing.Coordinates[4].Y);

            Assert.AreEqual(100.2, poly.GetInteriorRingN(0).Coordinates[0].X);
            Assert.AreEqual(0.2, poly.GetInteriorRingN(0).Coordinates[0].Y);
            Assert.AreEqual(100.8, poly.GetInteriorRingN(0).Coordinates[1].X);
            Assert.AreEqual(0.2, poly.GetInteriorRingN(0).Coordinates[1].Y);
            Assert.AreEqual(100.8, poly.GetInteriorRingN(0).Coordinates[2].X);
            Assert.AreEqual(0.8, poly.GetInteriorRingN(0).Coordinates[2].Y);
            Assert.AreEqual(100.2, poly.GetInteriorRingN(0).Coordinates[3].X);
            Assert.AreEqual(0.8, poly.GetInteriorRingN(0).Coordinates[3].Y);
            Assert.AreEqual(100.2, poly.GetInteriorRingN(0).Coordinates[4].X);
            Assert.AreEqual(0.2, poly.GetInteriorRingN(0).Coordinates[4].Y);
        }
Example #55
0
 public void Test()
 {
     const string geoJsonString = "{ \"type\":\"FeatureCollection\", \"features\":[ { \"geometry\":{ \"type\":\"Point\", \"coordinates\":[ -6.091861724853516, 4.991835117340088 ] }, \"type\":\"Feature\", \"properties\":{ \"prop1\":[ \"a\", \"b\" ] }, \"id\":1 } ], \"bbox\":[ -8.599302291870117, 4.357067108154297, -2.494896650314331, 10.736641883850098 ] }";
     var featureCollection = new GeoJsonReader().Read<FeatureCollection>(geoJsonString);
     Assert.AreEqual(1, featureCollection.Count);
 }
 public async Task<List<Feature>> Search(string searchTerm, string fieldName)
 {
     if (string.IsNullOrWhiteSpace(searchTerm))
     {
         return new List<Feature>();
     }
     var field = "properties." + fieldName;
     var response = await _elasticClient.SearchAsync<object>(
         s => s.Size(NUMBER_OF_RESULTS)
             .TrackScores()
             .Sort(f => f.Descending("_score"))
             .Query(
                 q => q.FunctionScore(
                     fs => fs.Query(
                         iq => iq.DisMax(
                             dm => dm.Queries(
                                 dmq => dmq.MultiMatch(
                                     mm => mm.Query(searchTerm)
                                         .Fields(f => f.Fields(field, "properties.name*", "properties._name"))
                                         .Type(TextQueryType.BestFields)
                                         .Fuzziness(Fuzziness.Auto)
                                 ),
                                 dmq => dmq.Match(
                                     m => m.Query(searchTerm)
                                         .Boost(1.2)
                                         .Field(new Field { Name = field })
                                 )
                             )
                         )
                     ).Functions(fn => fn.FieldValueFactor(f => f.Field("properties.search_factor")))
                 )
             )
     );
     var reader = new GeoJsonReader();
     return response.Documents.Select(d => reader.Read<Feature>(d.ToString())).ToList();
 }
        public void GeoJsonReaderReadGeometryMultiPolygonTest()
        {
            const string json = "{\"type\": \"MultiPolygon\",\"coordinates\": [[[[102.0, 2.0], [103.0, 2.0], [103.0, 3.0], [102.0, 3.0], [102.0, 2.0]]],[[[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]], [[100.2, 0.2], [100.8, 0.2], [100.8, 0.8], [100.2, 0.8], [100.2, 0.2]]]]}";
            Geometry result = new GeoJsonReader().Read<Geometry>(json);

            Assert.IsNotNull(result);
            Assert.IsInstanceOf(typeof(MultiPolygon), result);
            MultiPolygon multiPolygon = (MultiPolygon)result;

            Assert.AreEqual(2, multiPolygon.NumGeometries);

            Assert.AreEqual(102, multiPolygon.Geometries[0].Coordinates[0].X);
            Assert.AreEqual(2, multiPolygon.Geometries[0].Coordinates[0].Y);
            Assert.AreEqual(103, multiPolygon.Geometries[0].Coordinates[1].X);
            Assert.AreEqual(2, multiPolygon.Geometries[0].Coordinates[1].Y);
            Assert.AreEqual(103, multiPolygon.Geometries[0].Coordinates[2].X);
            Assert.AreEqual(3, multiPolygon.Geometries[0].Coordinates[2].Y);
            Assert.AreEqual(102, multiPolygon.Geometries[0].Coordinates[3].X);
            Assert.AreEqual(3, multiPolygon.Geometries[0].Coordinates[3].Y);
            Assert.AreEqual(102, multiPolygon.Geometries[0].Coordinates[4].X);
            Assert.AreEqual(2, multiPolygon.Geometries[0].Coordinates[4].Y);
            Polygon poly1 = (Polygon) multiPolygon.Geometries[0];
            Assert.AreEqual(0, poly1.NumInteriorRings);


            Assert.AreEqual(100, multiPolygon.Geometries[1].Coordinates[0].X);
            Assert.AreEqual(0, multiPolygon.Geometries[1].Coordinates[0].Y);
            Assert.AreEqual(101, multiPolygon.Geometries[1].Coordinates[1].X);
            Assert.AreEqual(0, multiPolygon.Geometries[1].Coordinates[1].Y);
            Assert.AreEqual(101, multiPolygon.Geometries[1].Coordinates[2].X);
            Assert.AreEqual(1, multiPolygon.Geometries[1].Coordinates[2].Y);
            Assert.AreEqual(100, multiPolygon.Geometries[1].Coordinates[3].X);
            Assert.AreEqual(1, multiPolygon.Geometries[1].Coordinates[3].Y);
            Assert.AreEqual(100, multiPolygon.Geometries[1].Coordinates[4].X);
            Assert.AreEqual(0, multiPolygon.Geometries[1].Coordinates[4].Y);
            Polygon poly2 = (Polygon)multiPolygon.Geometries[1];
            Assert.AreEqual(1, poly2.NumInteriorRings);

            Assert.AreEqual(100.2, poly2.GetInteriorRingN(0).Coordinates[0].X);
            Assert.AreEqual(0.2, poly2.GetInteriorRingN(0).Coordinates[0].Y);
            Assert.AreEqual(100.8, poly2.GetInteriorRingN(0).Coordinates[1].X);
            Assert.AreEqual(0.2, poly2.GetInteriorRingN(0).Coordinates[1].Y);
            Assert.AreEqual(100.8, poly2.GetInteriorRingN(0).Coordinates[2].X);
            Assert.AreEqual(0.8, poly2.GetInteriorRingN(0).Coordinates[2].Y);
            Assert.AreEqual(100.2, poly2.GetInteriorRingN(0).Coordinates[3].X);
            Assert.AreEqual(0.8, poly2.GetInteriorRingN(0).Coordinates[3].Y);
            Assert.AreEqual(100.2, poly2.GetInteriorRingN(0).Coordinates[4].X);
            Assert.AreEqual(0.2, poly2.GetInteriorRingN(0).Coordinates[4].Y);

        }
        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""
            }
          ]
      }
    }
  ]
}
";
            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);
        }