public void BuildLineString() { GeographyLineString ls = GeographyFactory.LineString(NonDefaultGeographicCoords, 10, 20, 30, 40).LineTo(20, 30, 40, 50); Assert.AreEqual(NonDefaultGeographicCoords, ls.CoordinateSystem); ls.VerifyAsLineString(new PositionData(10, 20, 30, 40), new PositionData(20, 30, 40, 50)); ls = GeographyFactory.LineString(NonDefaultGeographicCoords, 10, 20).LineTo(20, 30); Assert.AreEqual(NonDefaultGeographicCoords, ls.CoordinateSystem); ls.VerifyAsLineString(new PositionData(10, 20), new PositionData(20, 30)); ls = GeographyFactory.LineString(10, 20, 30, 40).LineTo(20, 30, 40, 50); Assert.AreEqual(CoordinateSystem.DefaultGeography, ls.CoordinateSystem); ls.VerifyAsLineString(new PositionData(10, 20, 30, 40), new PositionData(20, 30, 40, 50)); ls = GeographyFactory.LineString(10, 20).LineTo(20, 30); Assert.AreEqual(CoordinateSystem.DefaultGeography, ls.CoordinateSystem); ls.VerifyAsLineString(new PositionData(10, 20), new PositionData(20, 30)); ls = GeographyFactory.LineString(); Assert.AreEqual(CoordinateSystem.DefaultGeography, ls.CoordinateSystem); ls.VerifyAsLineString(null); ls = GeographyFactory.LineString(NonDefaultGeographicCoords); Assert.AreEqual(NonDefaultGeographicCoords, ls.CoordinateSystem); ls.VerifyAsLineString(null); ls = GeographyFactory.LineString().LineTo(10, 20).LineTo(20, 30); ls.VerifyAsLineString(new PositionData(10, 20), new PositionData(20, 30)); }
public void CreateGeographyPolygon() { GeographyPolygon polygon = GeographyFactory .Polygon() .Ring(0.0, 0.0) .LineTo(1.0, 0.0) .LineTo(1.0, 1.0) .LineTo(0.0, 1.0) .LineTo(0.0, 0.0); GeographyPolygonProxy proxy = new GeographyPolygonProxy(polygon); Assert.AreSame(polygon, proxy.Value); Assert.AreEqual(1, proxy.Rings.Count); GeographyLineString line0 = polygon.Rings[0]; GeographyLineStringProxy proxyLine0 = proxy.Rings[0]; Assert.AreSame(line0, proxyLine0.Value); Assert.AreEqual(5, line0.Points.Count); Assert.AreEqual(line0.Points.Count, proxyLine0.Points.Count); for (int i = 0; i < line0.Points.Count; i++) { Assert.AreEqual(line0.Points[i].Latitude, proxyLine0.Points[i].Latitude); Assert.AreEqual(line0.Points[i].Longitude, proxyLine0.Points[i].Longitude); } }
public static double GetLength(GeographyLineString line) { if (line == null) { throw new ArgumentNullException("line"); } return(line.Length().Value); }
private static void WriteLineStringCoordinates(JsonWriter writer, GeographyLineString lineString) { writer.WriteStartArray(); foreach (var point in lineString.Points) { WritePointCoordinates(writer, point); } writer.WriteEndArray(); }
private static void WriteGeography( this Utf8JsonWriter writer, GeographyLineString geography) { writer.WriteStartObject(); writer.WriteString(s_TypePropertyNameBytes, GeoJsonConstants.LineStringTypeName); WritePointArray(writer, geography.Points, s_CoordinatesPropertyNameBytes); writer.WriteEndObject(); }
public static GeographyLineString Create(this GeographyFactory <GeographyLineString> factory, List <GeographyPoint> points) { if (points.Count < 2) { throw new JsonSerializationException($"Deserialization failed: GeoJson type '{GeoJsonConstants.LineStringTypeName}' must contain at least two points."); } foreach (GeographyPoint point in points) { factory = factory.LineTo(point.Latitude, point.Longitude); } GeographyLineString result = factory.Build(); return(result); }
/// <summary> /// Converts a Microsoft.Spatial GeographyLineString to an NTS LineString. /// </summary> /// <param name="lineString">The Microsoft.Spatial GeographyLineString.</param> /// <returns></returns> public static LineString ToNtsLineString(this GeographyLineString lineString) { if (lineString == null) { return(null); } var coords = new List <Coordinate>(); foreach (var coord in lineString.Points) { coords.Add(new Coordinate(coord.Longitude, coord.Latitude)); } var ntsLineString = GeographyFactory.CreateLineString(coords.ToArray()); return((LineString)ntsLineString); }
/// <summary> /// Convert a DbGeography to Edm GeographyPoint /// </summary> /// <param name="geography"> The DbGeography to be converted</param> /// <returns>A Edm GeographyLineString</returns> public static GeographyLineString ToGeographyLineString(this DbGeography geography) { if (geography == null) { return(null); } if (geography.SpatialTypeName != GeographyTypeNameLineString) { throw new InvalidOperationException(string.Format( CultureInfo.InvariantCulture, Resources.InvalidLineStringGeographyType, geography.SpatialTypeName)); } SpatialBuilder builder = SpatialBuilder.Create(); GeographyPipeline pipleLine = builder.GeographyPipeline; pipleLine.SetCoordinateSystem(CoordinateSystem.DefaultGeography); pipleLine.BeginGeography(SpatialType.LineString); int numPoints = geography.PointCount ?? 0; if (numPoints > 0) { DbGeography point = geography.PointAt(1); pipleLine.BeginFigure(new GeographyPosition( point.Latitude ?? 0, point.Latitude ?? 0, point.Elevation, point.Measure)); for (int n = 2; n <= numPoints; n++) { point = geography.PointAt(n); pipleLine.LineTo(new GeographyPosition( point.Latitude ?? 0, point.Latitude ?? 0, point.Elevation, point.Measure)); } pipleLine.EndFigure(); } pipleLine.EndGeography(); GeographyLineString lineString = (GeographyLineString)builder.ConstructedGeography; return(lineString); }
private static IEnumerable GetMicrosoftSpatialPolygonsData() { GeographyLineString line = GeographyFactory .LineString(0, 0) .LineTo(1, 0) .LineTo(1, 1) .LineTo(0, 0); yield return new TestCaseData(line).Returns("geo.intersects(Foo, geography'POLYGON((0 0,0 1,1 1,0 0))')"); GeographyPolygon polygon = GeographyFactory .Polygon() .Ring(0, 0) .LineTo(1, 0) .LineTo(1, 1) .LineTo(0, 0); yield return new TestCaseData(polygon).Returns("geo.intersects(Foo, geography'POLYGON((0 0,0 1,1 1,0 0))')"); }
internal static void SendFigure(this GeographyLineString lineString, GeographyPipeline pipeline) { ReadOnlyCollection <GeographyPoint> points = lineString.Points; for (int i = 0; i < points.Count; i++) { if (i == 0) { pipeline.BeginFigure(new GeographyPosition(points[i].Latitude, points[i].Longitude, points[i].Z, points[i].M)); } else { pipeline.LineTo(new GeographyPosition(points[i].Latitude, points[i].Longitude, points[i].Z, points[i].M)); } } if (points.Count > 0) { pipeline.EndFigure(); } }
public void CreateGeographyLineString() { GeographyLineString line = GeographyFactory .LineString(0.0, 0.0) .LineTo(1.0, 0.0) .LineTo(1.0, 1.0) .LineTo(0.0, 1.0) .LineTo(0.0, 0.0); GeographyLineStringProxy proxy = new GeographyLineStringProxy(line); Assert.AreSame(line, proxy.Value); Assert.AreEqual(5, line.Points.Count); Assert.AreEqual(line.Points.Count, proxy.Points.Count); for (int i = 0; i < line.Points.Count; i++) { Assert.AreEqual(line.Points[i].Latitude, proxy.Points[i].Latitude); Assert.AreEqual(line.Points[i].Longitude, proxy.Points[i].Longitude); } }
/// <summary> /// Convert a Edm GeographyLineString to DbGeography /// </summary> /// <param name="lineString">The Edm GeographyLineString to be converted</param> /// <returns>A DbGeography</returns> public static DbGeography ToDbGeography(this GeographyLineString lineString) { if (lineString == null) { return(null); } StringBuilder sb = new StringBuilder("LINESTRING("); int n = 0; foreach (var pt in lineString.Points) { double lat = pt.Latitude; double lon = pt.Longitude; double?alt = pt.Z; double?m = pt.M; string pointStr = lat.ToString(DefaultCulture) + " " + lon.ToString(DefaultCulture); if (alt != null) { pointStr += " " + alt.Value; } if (m != null) { pointStr += " " + m.Value; } sb.Append(pointStr); n++; if (n != lineString.Points.Count) { sb.Append(","); } } sb.Append(")"); return(DbGeography.FromText(sb.ToString())); }
static ODataSpatialTypeUtil() { // Geometry type values. GeometryValue = GeometryFactory.Point(32.0, -10.0).Build(); GeometryPointValue = GeometryFactory.Point(33.1, -11.0).Build(); GeometryLineStringValue = GeometryFactory.LineString(33.1, -11.5).LineTo(35.97, -11).Build(); GeometryPolygonValue = GeometryFactory.Polygon().Ring(33.1, -13.6).LineTo(35.97, -11.15).LineTo(11.45, 87.75).Ring(35.97, -11).LineTo(36.97, -11.15).LineTo(45.23, 23.18).Build(); GeometryCollectionValue = GeometryFactory.Collection().Point(-19.99, -12.0).Build(); GeometryMultiPointValue = GeometryFactory.MultiPoint().Point(10.2, 11.2).Point(11.9, 11.6).Build(); GeometryMultiLineStringValue = GeometryFactory.MultiLineString().LineString(10.2, 11.2).LineTo(11.9, 11.6).LineString(16.2, 17.2).LineTo(18.9, 19.6).Build(); GeometryMultiPolygonValue = GeometryFactory.MultiPolygon().Polygon().Ring(10.2, 11.2).LineTo(11.9, 11.6).LineTo(11.45, 87.75).Ring(16.2, 17.2).LineTo(18.9, 19.6).LineTo(11.45, 87.75).Build(); // Geography type values. GeographyValue = GeographyFactory.Point(32.0, -100.0).Build(); GeographyPointValue = GeographyFactory.Point(33.1, -110.0).Build(); GeographyLineStringValue = GeographyFactory.LineString(33.1, -110.0).LineTo(35.97, -110).Build(); GeographyPolygonValue = GeographyFactory.Polygon().Ring(33.1, -110.0).LineTo(35.97, -110.15).LineTo(11.45, 87.75).Ring(35.97, -110).LineTo(36.97, -110.15).LineTo(45.23, 23.18).Build(); GeographyCollectionValue = GeographyFactory.Collection().Point(-19.99, -12.0).Build(); GeographyMultiPointValue = GeographyFactory.MultiPoint().Point(10.2, 11.2).Point(11.9, 11.6).Build(); GeographyMultiLineStringValue = GeographyFactory.MultiLineString().LineString(10.2, 11.2).LineTo(11.9, 11.6).LineString(16.2, 17.2).LineTo(18.9, 19.6).Build(); GeographyMultiPolygonValue = GeographyFactory.MultiPolygon().Polygon().Ring(10.2, 11.2).LineTo(11.9, 11.6).LineTo(11.45, 87.75).Ring(16.2, 17.2).LineTo(18.9, 19.6).LineTo(11.45, 87.75).Build(); }
private static IEnumerable GetMicrosoftSpatialPolygonsThrowsData() { // Require >= 4 points. GeographyLineString line = GeographyFactory .LineString(0, 0) .LineTo(1, 1); yield return new TestCaseData( line, "A GeographyLineString must have at least four Points to form a searchable polygon."); // Requires that first and last points are the same. line = GeographyFactory .LineString(0, 0) .LineTo(0, 0) .LineTo(0, 0) .LineTo(1, 1); yield return new TestCaseData( line, "A GeographyLineString must have matching first and last Points to form a searchable polygon."); // Require that polygons define exactly 1 ring. GeographyPolygon polygon = GeographyFactory .Polygon() .Ring(0, 0) .LineTo(0, 1) .LineTo(1, 1) .LineTo(0, 0) .Ring(2, 2) .LineTo(2, 3) .LineTo(3, 3) .LineTo(2, 2); yield return new TestCaseData( polygon, "A GeographyPolygon must have exactly one Rings to form a searchable polygon."); }
private static GeographyFactory <GeographyCollection> Add(this GeographyFactory <GeographyCollection> factory, GeographyLineString lineString) { factory = factory.LineString(); foreach (GeographyPoint point in lineString.Points) { factory = factory.LineTo(point.Latitude, point.Longitude); } return(factory); }