public void FeatureSerialization() { var coordinates = new[] { new List<IPosition> { new GeographicPosition(52.370725881211314, 4.889259338378906), new GeographicPosition(52.3711451105601, 4.895267486572266), new GeographicPosition(52.36931095278263, 4.892091751098633), new GeographicPosition(52.370725881211314, 4.889259338378906) }, new List<IPosition> { new GeographicPosition(52.370725881211314, 4.989259338378906), new GeographicPosition(52.3711451105601, 4.995267486572266), new GeographicPosition(52.36931095278263, 4.992091751098633), new GeographicPosition(52.370725881211314, 4.989259338378906) }, }; var settings = new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }; IGeometryObject geometry; geometry = new LineString(coordinates[0]); JsonAssert.AssertCoordinates(JsonConvert.SerializeObject(new Feature.Feature(geometry), DefaultJsonSerializerSettings), 1, coordinates[0]); geometry = new Point(coordinates[0][0]); JsonAssert.AssertCoordinates(JsonConvert.SerializeObject(new Feature.Feature(geometry), DefaultJsonSerializerSettings), 0, coordinates[0].Take(1).ToArray()); geometry = new MultiLineString(coordinates.Select(ca => new LineString(ca)).ToList()); JsonAssert.AssertCoordinates(JsonConvert.SerializeObject(new Feature.Feature(geometry), DefaultJsonSerializerSettings), 2, coordinates); geometry = new Polygon(coordinates.Select(ca => new LineString(ca)).ToList()); JsonAssert.AssertCoordinates(JsonConvert.SerializeObject(new Feature.Feature(geometry), DefaultJsonSerializerSettings), 2, coordinates); }
public static GeoJSON.Net.Geometry.Point Point2JG(PointD point) { JGeometry.GeographicPosition position = new JGeometry.GeographicPosition(point.Y, point.X); JGeometry.IPosition ip = position; JGeometry.Point jPoint = new JGeometry.Point(position); return(jPoint); }
public IActionResult GetPoint(int citiId) { var city = _dbContext.Cities.FirstOrDefault(c => c.CityID == citiId); if (city == null) { return(Json($"查询不到id为{citiId}的城市")); } //理解:Geometry/geojson/WKT 三者可以互转 //Geometry to GeoJSON //GeoJSON:http://geojson.org/ //GeoJSON.Net: //https://github.com/GeoJSON-Net/GeoJSON.Net //https://github.com/GeoJSON-Net/GeoJSON.Net/tree/master/src/GeoJSON.Net.Tests/Geometry //原理是先把Geometry对象转成geojson对象,然后再序列化 Position position = new Position(city.Location.X, city.Location.Y); GeoJSON.Net.Geometry.Point point = new GeoJSON.Net.Geometry.Point(position); var s = JsonConvert.SerializeObject(point); //Geometry to wkt //http://nettopologysuite.github.io/html/class_net_topology_suite_1_1_i_o_1_1_w_k_t_reader.html var s2 = NetTopologySuite.IO.WKTWriter.ToPoint(city.Location.Coordinate); return(Json($"GeoJSON结果:{s},WKT结果:{s2}")); }
/// <summary> /// Prints GEOJson dump of Sites /// </summary> /// <param name="requiredProperties">list of properties to include (if not found in siteproperties an empty string will be inserted)</param> public void Execute(string[] requiredProperties, string siteType) { Console.Write("Content-Type: application/json\n\n"); var features = new List<Feature>(); FeatureCollection fc = new FeatureCollection(features); var filter = ""; if( siteType != "") filter = "type = '"+siteType+"'"; var sites = db.GetSiteCatalog(filter:filter); var siteProp = new TimeSeriesDatabaseDataSet.sitepropertiesDataTable(db); int id = 0; foreach (var s in sites) { try { var pos = new GeographicPosition(s.latitude, s.longitude); var pt = new GeoJSON.Net.Geometry.Point(pos); var props = siteProp.GetDictionary(s.siteid); for (int i = 0; i < requiredProperties.Length; i++) { if (requiredProperties[i].Trim() == "") continue; if (!props.ContainsKey(requiredProperties[i])) props.Add(requiredProperties[i], ""); } props.Add("siteid", s.siteid); props.Add("title", s.description); props.Add("state", s.state); props.Add("type", s.type); props.Add("region", s.responsibility.Trim()); props.Add("install", s.install); id++; var feature = new Feature(pt, props, id.ToString()); fc.Features.Add(feature); } catch (Exception error) { Console.WriteLine("Error at site:"+s); Console.WriteLine(error.Message); } } var settings = new JsonSerializerSettings(); settings.NullValueHandling = NullValueHandling.Ignore; var json = Newtonsoft.Json.JsonConvert.SerializeObject(fc, Newtonsoft.Json.Formatting.Indented,settings); Console.WriteLine(json); //File.WriteAllText(@"c:\temp\test.json", json); }
/// <summary> /// Prints GEOJson dump of Sites /// </summary> /// <param name="requiredProperties">list of properties to include (if not found in siteproperties an empty string will be inserted)</param> /// <param name="propertyFilter">two part filter in property table i.e. 'program:agrimet'</param> public void Execute(string[] requiredProperties, string propertyFilter = "") { Console.Write("Content-Type: application/json\n\n"); var features = new List <Feature>(); FeatureCollection fc = new FeatureCollection(features); var sites = db.GetSiteCatalog(propertyFilter: propertyFilter); var siteProp = new TimeSeriesDatabaseDataSet.sitepropertiesDataTable(db); int id = 0; foreach (var s in sites) { var pos = new GeographicPosition(s.latitude, s.longitude); var pt = new GeoJSON.Net.Geometry.Point(pos); var props = siteProp.GetDictionary(s.siteid); for (int i = 0; i < requiredProperties.Length; i++) { if (requiredProperties[i].Trim() == "") { continue; } if (!props.ContainsKey(requiredProperties[i])) { props.Add(requiredProperties[i], ""); } } props.Add("siteid", s.siteid); props.Add("title", s.description); props.Add("state", s.state); props.Add("type", s.type); if (!props.ContainsKey("region")) { props.Add("region", s.agency_region); } props.Add("install", s.install); id++; var feature = new Feature(pt, props, id.ToString()); fc.Features.Add(feature); } var settings = new JsonSerializerSettings(); settings.NullValueHandling = NullValueHandling.Ignore; var json = Newtonsoft.Json.JsonConvert.SerializeObject(fc, Newtonsoft.Json.Formatting.Indented, settings); Console.WriteLine(json); //File.WriteAllText(@"c:\temp\test.json", json); }
private static void AddPointCoordinates(Point point, StringBuilder sb) { var coord = point?.Coordinates as GeographicPosition; if (coord != null) { sb.Append("(" + coord.Longitude + " " + coord.Latitude + ")"); } }
public void PointFeatureSerialization() { var point = new Point(new GeographicPosition(45.79012, 15.94107)); var featureProperties = new Dictionary<string, object> { { "Name", "Foo" } }; var model = new Feature.Feature(point, featureProperties); var serializedData = JsonConvert.SerializeObject(model); Assert.IsFalse(serializedData.Contains("longitude")); }
public void PointFeatureSerialization() { var point = new GeoJSON.Net.Geometry.Point(new GeoJSON.Net.Geometry.GeographicPosition(45.79012, 15.94107)); var featureProperties = new Dictionary<string, object> { {"Name", "Foo"} }; var model = new GeoJSON.Net.Feature.Feature(point, featureProperties); var serializedData = JsonConvert.SerializeObject(model, Formatting.Indented, new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver(), NullValueHandling = NullValueHandling.Ignore }); Assert.IsFalse(serializedData.Contains("longitude")); }
public void Can_Serialize_With_Lat_Lon_Alt() { var point = new Point(new GeographicPosition(53.2455662, 90.65464646, 200.4567)); var expectedJson = "{\"coordinates\":[90.65464646,53.2455662,200.4567],\"type\":\"Point\"}"; var actualJson = JsonConvert.SerializeObject(point); JsonAssert.AreEqual(expectedJson, actualJson); }
public void Can_Deserialize_With_Lat_Lon() { var json = "{\"coordinates\":[90.65464646,53.2455662],\"type\":\"Point\"}"; var expectedPoint = new Point(new GeographicPosition(53.2455662, 90.65464646)); var actualPoint = JsonConvert.DeserializeObject<Point>(json); Assert.AreEqual(expectedPoint, actualPoint); }
/* * Set origin point of the buliding to geojson */ public static Feature setOriginPointToGeoJSON(double latitude, double longitude, double altitude) { var properties = new Dictionary <String, object>(); properties.Add("Origin Point", "OriginPoint"); Point OriginCoordinate = new Point(new GeographicPosition(latitude, longitude, altitude)); var feature = new Feature(OriginCoordinate, properties); return(feature); }
public void PointFeatureSerialization() { var point = new GeoJSON.Net.Geometry.Point(new GeoJSON.Net.Geometry.GeographicPosition(45.79012, 15.94107)); var featureProperties = new Dictionary <string, object> { { "Name", "Foo" } }; var model = new GeoJSON.Net.Feature.Feature(point, featureProperties); var serializedData = GetSerialized(model); Assert.IsFalse(serializedData.Contains("longitude")); }
public void PointFeatureSerialization() { var point = new GeoJSON.Net.Geometry.Point(new GeoJSON.Net.Geometry.GeographicPosition(45.79012, 15.94107)); var featureProperties = new Dictionary <string, object> { { "Name", "Foo" } }; var model = new GeoJSON.Net.Feature.Feature(point, featureProperties); var serializedData = JsonConvert.SerializeObject(model, Formatting.Indented, new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver(), NullValueHandling = NullValueHandling.Ignore }); Assert.IsFalse(serializedData.Contains("longitude")); }
private static void MapboxAPIUsage() { GeoJSON.Net.Geometry.Position mlawa = new GeoJSON.Net.Geometry.Position(53.112128, 20.383661); var polygon = MapboxAPIHelper.GetIsochroneAsPolygon(mlawa, 10); GeoJSON.Net.Geometry.Position positionInside = new GeoJSON.Net.Geometry.Position(53.125982, 20.358108); GeoJSON.Net.Geometry.Point pointInside = new GeoJSON.Net.Geometry.Point(positionInside); GeoJSON.Net.Geometry.Position positionOutside = new GeoJSON.Net.Geometry.Position(53.155378, 20.363038); GeoJSON.Net.Geometry.Point pointOutside = new GeoJSON.Net.Geometry.Point(positionOutside); bool inside = GeometryUtils.CheckIfPointIsInsidePolygon(polygon, pointInside); bool outside = GeometryUtils.CheckIfPointIsInsidePolygon(polygon, pointOutside); }
private async Task GetPointData() { var city = await _dbContext.Cities.FirstOrDefaultAsync(); //Geometry to geoJSON Position position = new Position(city.Location.X, city.Location.Y); GeoJSON.Net.Geometry.Point point = new GeoJSON.Net.Geometry.Point(position); var s = JsonConvert.SerializeObject(point); //Geometry to wkt var s2 = NetTopologySuite.IO.WKTWriter.ToPoint(city.Location.Coordinate); Console.WriteLine($"GeoJSON结果:{s},WKT结果:{s2}"); }
public void Should_serialize_Enumeration_to_simplified_json() { var point = new Point(new GeographicPosition(1, 2)) { CRS = new NamedCRS(26912.ToString(CultureInfo.InvariantCulture)) }; Assert.That(point.Type, Is.EqualTo(GeoJSONObjectType.Point)); var json = JsonConvert.SerializeObject(point); Assert.AreEqual( @"{""coordinates"":[2.0,1.0],""bbox"":null,""type"":""Point"",""crs"":{""type"":""Name"",""properties"":{""name"":""26912""}}}", json); }
public void Equals_GetHashCode_Contract() { var json = "{\"coordinates\":[90.65464646,53.2455662],\"type\":\"Point\"}"; var expectedPoint = new Point(new GeographicPosition(53.2455662, 90.65464646)); var actualPoint = JsonConvert.DeserializeObject<Point>(json); Assert.AreEqual(expectedPoint, actualPoint); Assert.IsTrue(expectedPoint.Equals(actualPoint)); Assert.IsTrue(actualPoint.Equals(expectedPoint)); Assert.AreEqual(expectedPoint.GetHashCode(), actualPoint.GetHashCode()); }
public static Feature Parse(Tile.Feature feature, List<string> keys, List<Tile.Value> values, int x, int y, int z, uint extent, bool convertToGeographicPosition) { Feature result = null; var geom = GeometryParser.ParseGeometry(feature.Geometry, feature.Type); var id = feature.Id; var positions= convertToGeographicPosition? geom.Select(p => p.ToGeographicPosition(x,y,z,extent)).ToList(): geom.Select(p => new GeographicPosition(p.Latitude, p.Longitude)).ToList(); var coordinates = positions.ToList<IPosition>(); if (feature.Type == Tile.GeomType.Polygon) { if (coordinates.Count > 3) { var polygon = new Polygon(new List<LineString> {new LineString(coordinates)}); result = new Feature(polygon); } } else if (feature.Type == Tile.GeomType.LineString) { var ls = new LineString(coordinates); result = new Feature(ls); } else if (feature.Type == Tile.GeomType.Point) { var pt = new Point(coordinates[0]); result = new Feature(pt); } if (result != null) { result.Id = id.ToString(CultureInfo.InvariantCulture); var attributes = AttributesParser.Parse(keys, values, feature.Tags); foreach (var item in attributes) { result.Properties.Add(item.Key, item.Value); } } return result; }
protected override ApiResponseContainer <Feature> Handle(Command request) { IGeometryObject geometry = null; var attributes = new Dictionary <string, object>(); var message = request.Container.Message; var status = request.Container.Status; var result = request.Container.Result; if (result?.Location != null) { geometry = new Point(new Position(result.Location.Y, result.Location.X)); attributes = JObject.FromObject(request.Container.Result) .ToObject <Dictionary <string, object> >(); } if (geometry == null && attributes.Count < 1) { return(new ApiResponseContainer <Feature> { Status = status, Message = message }); } var feature = new Feature(geometry, attributes.Where(x => x.Value != null).ToDictionary(x => x.Key, y => y.Value)); var responseContainer = new ApiResponseContainer <Feature> { Result = feature, Status = status, Message = message }; return(responseContainer); }
public IGeometryObject GetGeometry(IGeometry esriGeometry) { IGeometryObject geoJsonGeom = null; string error = string.Format("A Geometry of type {0} cannot be created", Enum.GetName(typeof(esriGeometryType), esriGeometry.GeometryType)); switch (esriGeometry.GeometryType) { case esriGeometryType.esriGeometryAny: throw new ArgumentOutOfRangeException(error); case esriGeometryType.esriGeometryBag: throw new ArgumentOutOfRangeException(error); case esriGeometryType.esriGeometryBezier3Curve: throw new ArgumentOutOfRangeException(error); case esriGeometryType.esriGeometryCircularArc: throw new ArgumentOutOfRangeException(error); case esriGeometryType.esriGeometryEllipticArc: throw new ArgumentOutOfRangeException(error); case esriGeometryType.esriGeometryEnvelope: throw new ArgumentOutOfRangeException(error); case esriGeometryType.esriGeometryLine: throw new ArgumentOutOfRangeException(error); case esriGeometryType.esriGeometryMultiPatch: throw new ArgumentOutOfRangeException(error); case esriGeometryType.esriGeometryMultipoint: throw new ArgumentOutOfRangeException(error); case esriGeometryType.esriGeometryNull: throw new ArgumentOutOfRangeException(error); case esriGeometryType.esriGeometryPath: throw new ArgumentOutOfRangeException(error); case esriGeometryType.esriGeometryPoint: //cast to esri obj IPoint point = esriGeometry as IPoint; List<double> coords = new List<double> { point.X, point.Y }; //create geojson obj GeoJSON.Net.Geometry.Point pnt = new GeoJSON.Net.Geometry.Point(coords); geoJsonGeom = pnt; break; case esriGeometryType.esriGeometryPolygon: IGeometryCollection polygons = esriGeometry as IGeometryCollection; List<LineString> linearRings = new List<LineString>(); var rings = new List<List<List<double>>>(); var ring = new List<List<double>>(); //todo - how does esri handle polygons with holes for (int i = 0; i < polygons.GeometryCount; i++) { //for each geometry in the polygon create a point collection var pGeom = polygons.Geometry[i]; var pntCollection = (IPointCollection)pGeom; //GeoJSON.Net.Geometry.LineString line = new LineString(); for (var a = 0; a < pntCollection.PointCount; a++) { //create a list of coordinates for each line List<double> linePointCoords = new List<double> { pntCollection.Point[a].X, pntCollection.Point[a].Y }; //..and add them to the poly ring.Add(linePointCoords); } rings.Add(ring); } //initialise a poly GeoJSON.Net.Geometry.Polygon poly = new GeoJSON.Net.Geometry.Polygon(rings); geoJsonGeom = poly; break; case esriGeometryType.esriGeometryPolyline: //cast to esri obj IGeometryCollection polylines = esriGeometry as IGeometryCollection; //create geojson obj GeoJSON.Net.Geometry.LineString polyLine = new LineString(); for (int i = 0; i < polylines.GeometryCount; i++) { //for each geometry in the polyline create a point collection var pGeom = polylines.Geometry[i]; var pntCollection = (IPointCollection)pGeom; for (var a = 0; a < pntCollection.PointCount; a++) { //create a list of coordinates for each line List<double> linePointCoords = new List<double> { pntCollection.Point[a].X, pntCollection.Point[a].Y }; //..and add them to the line polyLine.Coordinates.Add(linePointCoords); } } geoJsonGeom = polyLine; break; case esriGeometryType.esriGeometryRay: throw new ArgumentOutOfRangeException(error); case esriGeometryType.esriGeometryRing: throw new ArgumentOutOfRangeException(error); case esriGeometryType.esriGeometrySphere: throw new ArgumentOutOfRangeException(error); case esriGeometryType.esriGeometryTriangleFan: throw new ArgumentOutOfRangeException(error); case esriGeometryType.esriGeometryTriangleStrip: throw new ArgumentOutOfRangeException(error); case esriGeometryType.esriGeometryTriangles: throw new ArgumentOutOfRangeException(error); default: throw new ArgumentOutOfRangeException(error); } return geoJsonGeom; }
private static IGeometryObject GetPointGeometry(List<GeographicPosition> pointList) { IGeometryObject geom; if (pointList.Count == 1) { geom = new Point(pointList[0]); } else { var pnts = pointList.Select(p => new Point(p)).ToList(); geom = new MultiPoint(pnts); } return geom; }