/// <summary> /// Makes Geography with default ellipsoid WGS84: 4326 /// </summary> /// <param name="points"></param> /// <param name="isClosed"></param> /// <returns></returns> public static SqlGeography MakeGeography <T>(List <T> points, bool isClosed, int srid = SridHelper.GeodeticWGS84) where T : IPoint, new() { if (points == null || points.Count < 1) { return(null); } SqlGeographyBuilder builder = new SqlGeographyBuilder(); builder.SetSrid(srid); builder.BeginGeography(isClosed ? OpenGisGeographyType.Polygon : OpenGisGeographyType.LineString); builder.BeginFigure(points[0].Y, points[0].X); for (int i = 1; i < points.Count; i++) { builder.AddLine(points[i].Y, points[i].X); } if (isClosed) { builder.AddLine(points[0].Y, points[0].X); } builder.EndFigure(); builder.EndGeography(); var resultGeography = builder.ConstructedGeography.STIsValid().Value ? builder.ConstructedGeography : builder.ConstructedGeography.MakeValid(); return(resultGeography.EnvelopeAngle().Value == 180 ? resultGeography.ReorientObject() : resultGeography); }
public static SqlGeography CreatePolygon(Coordinates[] coordinates, int srid) { var list = coordinates.Distinct().ToList(); var b = new SqlGeographyBuilder(); b.SetSrid(srid); b.BeginGeography(OpenGisGeographyType.Polygon); b.BeginFigure(list[0].Latitude, list[0].Longitude); for (var i = 1; i < list.Count; i++) { b.AddLine(list[i].Latitude, list[i].Longitude); } b.AddLine(list[0].Latitude, list[0].Longitude); b.EndFigure(); b.EndGeography(); //Fix left-hand rule. We always want left-hand. var g = b.ConstructedGeography; if (!g.STIsValid()) { throw new SisoDbException(ExceptionMessages.NotAValidPolygon.Inject(g.IsValidDetailed())); } if (g.EnvelopeAngle() > 90) { g = g.ReorientObject(); } return(g); }
//TOP LEFT LONGITUDE=item1, LATITUDE=item2, //BOTTOM RIGHT LONGITUDE=item3, LATITUDE=item4 public static SqlGeography CreateMultiRectangle(this List <Tuple <double, double, double, double> > coordinates, int srid = 4326) { var list = coordinates.Distinct().ToList(); var b = new SqlGeographyBuilder(); b.SetSrid(srid); b.BeginGeography(OpenGisGeographyType.MultiPolygon); for (var i = 0; i < list.Count; i++) { b.BeginGeography(OpenGisGeographyType.Polygon); b.BeginFigure(list[i].Item1, list[i].Item2); b.AddLine(list[i].Item1, list[i].Item4); b.AddLine(list[i].Item3, list[i].Item4); b.AddLine(list[i].Item3, list[i].Item2); b.AddLine(list[i].Item1, list[i].Item2); b.EndFigure(); b.EndGeography(); } b.EndGeography(); return(b.ConstructedGeography.EnvelopeAngle() > 90 ? b.ConstructedGeography.ReorientObject() : b.ConstructedGeography); }
private void btnCheckIntersection_Click(object sender, EventArgs e) { lat = Convert.ToDouble(txtLat.Text); lon = Convert.ToDouble(txtLon.Text); SqlGeographyBuilder builder = new SqlGeographyBuilder(); builder.SetSrid(4326); builder.BeginGeography(OpenGisGeographyType.Point); builder.BeginFigure(lat, lon); builder.EndFigure(); builder.EndGeography(); DrawData(cboBeatSegs.Text, lat, lon); //bool tf = GeoClass.checkSegIntersection(cboBeatSegs.Text, builder.ConstructedGeography); bool tf = GeoClass.checkEsriIntersect(lat, lon, cboBeatSegs.Text); //bool tf = GeoClass.checkIntersects(builder.ConstructedGeography); if (tf == true) { lblInside.Text = "Point is inside figure"; } else { lblInside.Text = "Point is outside figure"; } }
private static List <Section> FindContainedSections(DbConnection connection, SqlGeographyBuilder searchArea) { IEnumerable <Township> townships = FindContainedTownships(connection, searchArea); List <Section> results = new List <Section>(); foreach (var township in townships) { var sectionsParameters = new BoundingBoxParam(); sectionsParameters.AddGeography("@geometry", searchArea.ConstructedGeography); sectionsParameters.AddString("@township", township.TWP); sectionsParameters.AddString("@range", township.RGE); sectionsParameters.AddString("@meridian", township.MER); var sections = connection.Query <Section>(@"select *, geom as Coordinates from sections s where ptwp=@township and prge = @range and pmer = @meridian and @geometry.STIntersects(s.geom) > 0", sectionsParameters); results.AddRange(sections); } return(results); }
public void Read(BinaryReader r) { if (r.ReadBoolean()) { Init(); } else { SqlGeography g = new SqlGeography(); g.Read(r); if (g.IsNull) { m_srid = -1; m_error = true; m_builder = null; m_sink = null; } else { m_srid = g.STSrid.Value; m_builder = new SqlGeographyBuilder(); m_sink = new StripSRID(m_builder); m_builder.SetSrid(m_srid); m_builder.BeginGeography(OpenGisGeographyType.GeometryCollection); g.Populate(new StripCollection(m_builder)); } } }
public void Init() { m_srid = -1; m_builder = null; m_sink = null; m_error = false; }
private static void AddMultiPolygon <T>(SqlGeographyBuilder builder, Geometry <T> geometry) where T : IPoint, new() { //return CreatePolygon(points, srid); //SqlGeometryBuilder builder = new SqlGeometryBuilder(); //builder.SetSrid(geometry.Srid); builder.BeginGeography(OpenGisGeographyType.MultiPolygon); foreach (var item in geometry.Geometries) { builder.BeginGeography(OpenGisGeographyType.Polygon); foreach (var lines in item.Geometries) { if (lines.NumberOfPoints < minimumPolygonPoints) { Trace.WriteLine($"CreateMultiPolygon escape!"); continue; } AddLineStringOrRing(builder, lines, true); } builder.EndGeography(); } builder.EndGeography(); //return builder.ConstructedGeometry.MakeValid(); }
private static void Internal_FillGeographyBuilder(SqlGeographyBuilder gb, MultiLineString multiLineString) { gb.BeginGeography(OpenGisGeographyType.MultiLineString); foreach (var lineString in multiLineString.Coordinates) { gb.BeginGeography(OpenGisGeographyType.LineString); bool beginFigureCalled = false; foreach (var ipos in lineString.Coordinates) { Position pos = ipos as Position; if (!beginFigureCalled) { gb.BeginFigure(pos.Latitude, pos.Longitude); beginFigureCalled = true; } else { gb.AddLine(pos.Latitude, pos.Longitude); } } gb.EndFigure(); gb.EndGeography(); } gb.EndGeography(); }
public static SqlGeography GetBounds(SqlDouble xOrLon, SqlDouble yOrLat, SqlDouble xOrLon2, SqlDouble yOrLat2) { if (xOrLon.IsNull || yOrLat.IsNull) { return(SqlGeography.Null); } var builder = new SqlGeographyBuilder(); builder.SetSrid(4326); builder.BeginGeography(OpenGisGeographyType.Polygon); double x = xOrLon.Value; double x2 = xOrLon2.Value; double y = yOrLat.Value; double y2 = yOrLat2.Value; if (Math.Abs(x) > 180 && Math.Abs(y) > 180) { x = GetLon(xOrLon).Value; x2 = GetLon(xOrLon2).Value; y = GetLat(yOrLat).Value; y2 = GetLat(yOrLat2).Value; } builder.BeginFigure(y, x); builder.AddLine(y, x2); builder.AddLine(y2, x2); builder.AddLine(y2, x); builder.AddLine(y, x); builder.EndFigure(); builder.EndGeography(); return(builder.ConstructedGeography); }
public static SqlGeography ToSqlGeography(SMGeometry smGeometry) { SqlGeographyBuilder builder = new SqlGeographyBuilder(); builder.SetSrid(smGeometry.SRID); SharpMapGeometryToSqlGeography(builder, smGeometry); SqlGeography g = builder.ConstructedGeography; if (!g.STIsValid()) { try { g = g.Reduce(ReduceTolerance); g = g.MakeValid(); } catch (Exception ex) { throw new SqlGeographyConverterException(ex, smGeometry); } } if (!g.STIsValid()) { throw new SqlGeographyConverterException(smGeometry); } return(g); }
private void AddGeometry(SqlGeographyBuilder builder, IGeometry geometry) { if (geometry is IPoint) { AddPoint(builder, geometry); } else if (geometry is ILineString) { AddLineString(builder, geometry); } else if (geometry is IPolygon) { AddPolygon(builder, geometry); } else if (geometry is IMultiPoint) { AddGeometryCollection(builder, geometry, OpenGisGeographyType.MultiPoint); } else if (geometry is IMultiLineString) { AddGeometryCollection(builder, geometry, OpenGisGeographyType.MultiLineString); } else if (geometry is IMultiPolygon) { AddGeometryCollection(builder, geometry, OpenGisGeographyType.MultiPolygon); } else if (geometry is IGeometryCollection) { AddGeometryCollection(builder, geometry, OpenGisGeographyType.GeometryCollection); } }
public override void OnBeginRender(RenderingContext context) { base.OnBeginRender(context); var sb = new SqlGeographyBuilder(); sb.SetSrid(4326); sb.BeginGeography(OpenGisGeographyType.MultiLineString); double lat = context.Projection.GeoMin.Lat; lat = Math.Floor(lat / majorStep) * majorStep; for (; lat <= context.Projection.GeoMax.Lat; lat += majorStep) { AddLatitudeLine(sb, context.Projection.GeoMin.Lon, context.Projection.GeoMax.Lon, lat); } // Longitudes are great circles double lon = context.Projection.GeoMin.Lon; lon = Math.Floor(lon / majorStep) * majorStep; for (; lon <= context.Projection.GeoMax.Lon; lon += majorStep) { AddLongitudeLine(sb, lon, context.Projection.GeoMin.Lat, context.Projection.GeoMax.Lat); } sb.EndGeography(); RenderGeography(context, sb.ConstructedGeography); }
/// <summary> /// Check if an input geometry can represent a valid geography without throwing an exception. /// This function requires that the geometry be in longitude/latitude coordinates and that /// those coordinates are in correct order in the geometry instance (i.e. latitude/longitude /// not longitude/latitude). This function will return false (0) if the input geometry is not /// in the correct latitude/longitude format, including a valid geography SRID. /// </summary> /// <param name="geometry">Input Sql Geometry</param> /// <returns></returns> public static bool IsValidGeographyFromGeometry(SqlGeometry geometry) { if (geometry.IsNull) { return(false); } try { var geogBuilder = new SqlGeographyBuilder(); geometry.Populate(new VacuousGeometryToGeographySink(geometry.STSrid.Value, geogBuilder)); // ReSharper disable once UnusedVariable var geography = geogBuilder.ConstructedGeography; return(true); } catch (FormatException) { // Syntax error return(false); } catch (ArgumentException) { // Semantic (Geometrical) error return(false); } }
private void AddCoordinates(SqlGeographyBuilder builder, ICoordinateSequence coordinates) { for (var i = 0; i < coordinates.Count; i++) { AddCoordinate(builder, coordinates, i, i); } }
private static void SharpMapPointToSqlGeography(SqlGeographyBuilder geogBuilder, SMPoint point) { geogBuilder.BeginGeography(OpenGisGeographyType.Point); geogBuilder.BeginFigure(point.Y, point.X); geogBuilder.EndFigure(); geogBuilder.EndGeography(); }
/// <summary> /// Converts a GeoJSON MultiPoint to an SqlGeography /// </summary> /// <param name="multiPoint"></param> /// <param name="srid"></param> /// <returns></returns> public static SqlGeography ToSqlGeography(this MultiPoint multiPoint, int srid = 4326) { SqlGeographyBuilder gb = new SqlGeographyBuilder(); gb.SetSrid(srid); Internal_FillGeographyBuilder(gb, multiPoint); return gb.ConstructedGeography; }
public void GetConstructedBeforeCompletion() { SqlGeographyBuilder b = new SqlGeographyBuilder(); b.SetSrid(4326); AssertEx.ThrowsException(() => { var g = b.ConstructedGeography; }, typeof(FormatException), "24300: Expected a call to BeginGeography, but Finish was called."); }
/// <summary> /// Converts the given value from original representation to alternative one. /// </summary> /// <param name="value">Value in its original representation.</param> /// <returns>Value in its alternative representation.</returns> public override object Convert(object value) { object ret = null; Models.Location l = null; SqlGeographyBuilder builder = null; if (value != null && value is Models.Location) { l = value as Models.Location; builder = new SqlGeographyBuilder(); builder.SetSrid(4326); builder.BeginGeography(OpenGisGeographyType.Point); builder.BeginFigure(l.Latitude, l.Longitude); builder.EndFigure(); builder.EndGeography(); ret = builder.ConstructedGeography; } return ret; }
public static SqlGeography AsGeography(this GpxTrack track) { SqlGeographyBuilder builder = new SqlGeographyBuilder(); builder.SetSrid(SridHelper.GeodeticWGS84); builder.BeginGeography(OpenGisGeographyType.MultiLineString); foreach (var item in track.Segments) { if (item.TrackPoints.Count < 2) { continue; } AddTrackSegment(builder, item); //builder.BeginFigure(item.TrackPoints[0].Latitude, item.TrackPoints[0].Longitude); //for (int i = 1; i < item.TrackPoints.Count; i++) //{ // builder.AddLine(item.TrackPoints[i].Latitude, item.TrackPoints[i].Longitude); //} //builder.EndFigure(); } builder.EndGeography(); return(builder.ConstructedGeography.STIsValid().Value ? builder.ConstructedGeography : builder.ConstructedGeography.MakeValid()); }
// This function is used for generating a new geography object where additional points are inserted // along every line in such a way that the angle between two consecutive points does not // exceed a prescribed angle. The points are generated between the unit vectors that correspond // to the line's start and end along the great-circle arc on the unit sphere. This follows the // definition of geodetic lines in SQL Server. public static SqlGeography DensifyGeography(SqlGeography g, double maxAngle) { SqlGeographyBuilder b = new SqlGeographyBuilder(); g.Populate(new DensifyGeographySink(b, maxAngle)); return(b.ConstructedGeography); }
// This implements a completely trivial conversion from geometry to geography, simply taking each // point (x,y) --> (long, lat). The result is assigned the given SRID. public static SqlGeography VacuousGeometryToGeography(SqlGeometry toConvert, int targetSrid) { SqlGeographyBuilder b = new SqlGeographyBuilder(); toConvert.Populate(new VacuousGeometryToGeographySink(targetSrid, b)); return(b.ConstructedGeography); }
// Check if an input geometry can represent a valid geography without throwing an exception. // This function requires that the geometry be in longitude/latitude coordinates and that // those coordinates are in correct order in the geometry instance (i.e. latitude/longitude // not longitude/latitude). This function will return false (0) if the input geometry is not // in the correct latitude/longitude format, including a valid geography SRID. // public static bool IsValidGeographyFromGeometry(SqlGeometry geometry) { if (geometry.IsNull) { return(false); } try { SqlGeographyBuilder builder = new SqlGeographyBuilder(); geometry.Populate(new VacuousGeometryToGeographySink(geometry.STSrid.Value, builder)); SqlGeography geography = builder.ConstructedGeography; return(true); } catch (FormatException) { // Syntax error return(false); } catch (ArgumentException) { // Semantical (Geometrical) error return(false); } }
public static SqlGeography CreatePolygon(Coordinates[] coordinates, int srid) { var list = coordinates.Distinct().ToList(); var b = new SqlGeographyBuilder(); b.SetSrid(srid); b.BeginGeography(OpenGisGeographyType.Polygon); b.BeginFigure(list[0].Latitude, list[0].Longitude); for (var i = 1; i < list.Count; i++) b.AddLine(list[i].Latitude, list[i].Longitude); b.AddLine(list[0].Latitude, list[0].Longitude); b.EndFigure(); b.EndGeography(); //Fix left-hand rule. We always want left-hand. var g = b.ConstructedGeography; if (!g.STIsValid()) throw new SisoDbException(ExceptionMessages.NotAValidPolygon.Inject(g.IsValidDetailed())); if (g.EnvelopeAngle() > 90) g = g.ReorientObject(); return g; }
public static SqlGeography CreatePolygon(IEnumerable <Coordinates> coordinates, int srid = 4326) { var list = coordinates.Distinct().ToList(); var b = new SqlGeographyBuilder(); b.SetSrid(srid); b.BeginGeography(OpenGisGeographyType.Polygon); b.BeginFigure(list[0].Latitude, list[0].Longitude); for (var i = 1; i < list.Count; i++) { b.AddLine(list[i].Latitude, list[i].Longitude); } b.AddLine(list[0].Latitude, list[0].Longitude); b.EndFigure(); b.EndGeography(); if (b.ConstructedGeography.EnvelopeAngle() > 90) { return(b.ConstructedGeography.ReorientObject()); } return(b.ConstructedGeography); }
private static void Internal_FillGeographyBuilder(SqlGeographyBuilder gb, Polygon polygon) { gb.BeginGeography(OpenGisGeographyType.Polygon); bool isExteriorRing = true; foreach (var lineString in polygon.Coordinates) { List <Position> listGeoCoords = lineString.Coordinates.Select(p => p as Position).ToList(); IEnumerable <Position> orderedPositions = EnsureCorrectWinding(listGeoCoords, isExteriorRing); // exterior ring must be anti clockwise for SqlGeography isExteriorRing = false; bool beginFigureCalled = false; foreach (var pos in orderedPositions) { if (!beginFigureCalled) { gb.BeginFigure(pos.Latitude, pos.Longitude); beginFigureCalled = true; } else { gb.AddLine(pos.Latitude, pos.Longitude); } } gb.EndFigure(); } gb.EndGeography(); }
private IEnumerable <dynamic> GetLSDs(DbConnection connection, SqlGeographyBuilder searchArea, int zoomLevel) { List <LSD> results = new List <LSD>(); foreach (var section in FindContainedSections(connection, searchArea)) { var lsdsParameters = new BoundingBoxParam(); lsdsParameters.AddGeography("@geometry", searchArea.ConstructedGeography); lsdsParameters.AddString("@township", section.PTWP); lsdsParameters.AddString("@range", section.PRGE); lsdsParameters.AddString("@meridian", section.PMER); lsdsParameters.AddString("@section", section.SECT); var lsds = connection.Query <LSD>(@"select *, LSD as LSDName, geom as Coordinates from lsds l where ptwp=@township and prge = @range and pmer = @meridian and psect = @section and @geometry.STIntersects(l.geom) > 0", lsdsParameters); results.AddRange(lsds); } return(results.Select(x => new { Name = x.Name, Coordinates = GetCoordinates(x.Coordinates, zoomLevel), CenterCoordinates = GetLabelCoordinates(x.Coordinates) })); }
public void BeginFigureBeforeBeginGeography() { SqlGeographyBuilder b = new SqlGeographyBuilder(); b.SetSrid(4326); AssertEx.ThrowsException(() => { b.BeginFigure(1, 2); }, typeof(FormatException), "24300: Expected a call to BeginGeography, but BeginFigure was called."); }
/// <summary> /// This function is used for generating a new geography object where additional points are inserted /// along every line in such a way that the angle between two consecutive points does not /// exceed a prescribed angle. The points are generated between the unit vectors that correspond /// to the line's start and end along the great-circle arc on the unit sphere. This follows the /// definition of geodetic lines in SQL Server. /// </summary> /// <param name="geography">Input Sql geography</param> /// <param name="maxAngle">Max Angle</param> /// <returns></returns> public static SqlGeography DensifyGeography(SqlGeography geography, double maxAngle) { var geogBuilder = new SqlGeographyBuilder(); geography.Populate(new DensifyGeographySink(geogBuilder, maxAngle)); return(geogBuilder.ConstructedGeography); }
/// <summary> /// Converts a GeoJSON LineString to an SqlGeography /// </summary> /// <param name="lineString"></param> /// <param name="srid"></param> /// <returns></returns> public static SqlGeography ToSqlGeography(this LineString lineString, int srid = 4326) { SqlGeographyBuilder gb = new SqlGeographyBuilder(); gb.SetSrid(srid); Internal_FillGeographyBuilder(gb, lineString); return(gb.ConstructedGeography); }
public static SqlGeography RoundGeography(SqlGeography g, Int32 precision) { SqlGeographyBuilder constructed = new SqlGeographyBuilder(); RoundGeography rounded = new RoundGeography(precision, constructed); g.Populate(rounded); return(constructed.ConstructedGeography); }
private static void Internal_FillGeographyBuilder(SqlGeographyBuilder gb, Point point) { gb.BeginGeography(OpenGisGeographyType.Point); GeographicPosition pos = point.Coordinates as GeographicPosition; gb.BeginFigure(pos.Latitude, pos.Longitude); gb.EndFigure(); gb.EndGeography(); }
/// <summary> /// Converts a GeoJSON MultiPoint to an SqlGeography /// </summary> /// <param name="multiPoint"></param> /// <param name="srid"></param> /// <returns></returns> public static SqlGeography ToSqlGeography(this MultiPoint multiPoint, int srid = 4326) { SqlGeographyBuilder gb = new SqlGeographyBuilder(); gb.SetSrid(srid); Internal_FillGeographyBuilder(gb, multiPoint); return(gb.ConstructedGeography); }
/// <summary> /// Converts a GeoJSON Polygon to an SqlGeography /// </summary> /// <param name="polygon"></param> /// <param name="srid"></param> /// <returns></returns> public static SqlGeography ToSqlGeography(this Polygon polygon, int srid = 4326) { SqlGeographyBuilder gb = new SqlGeographyBuilder(); gb.SetSrid(srid); Internal_FillGeographyBuilder(gb, polygon); return(gb.ConstructedGeography); }
//LONGITUDE=item1, LATITUDE=item2 public static SqlGeography CreatePoint(this Tuple<double, double> coordinates, int srid = 4326) { var b = new SqlGeographyBuilder(); b.SetSrid(srid); b.BeginGeography(OpenGisGeographyType.Point); b.BeginFigure(coordinates.Item2, coordinates.Item1); b.EndFigure(); b.EndGeography(); return b.ConstructedGeography; }
public SqlGeography asSqlGeography() { SqlGeographyBuilder builder = new SqlGeographyBuilder(); builder.SetSrid(4326); builder.BeginGeography(OpenGisGeographyType.Point); builder.BeginFigure(latitude, longitude); builder.EndFigure(); builder.EndGeography(); return builder.ConstructedGeography; }
private static void Internal_FillGeographyBuilder(SqlGeographyBuilder gb, MultiPoint multiPoint) { gb.BeginGeography(OpenGisGeographyType.MultiPoint); List<Point> coords = multiPoint.Coordinates as List<Point>; foreach (var coord in coords) { GeographicPosition pos = coord.Coordinates as GeographicPosition; gb.BeginGeography(OpenGisGeographyType.Point); gb.BeginFigure(pos.Latitude, pos.Longitude); gb.EndFigure(); gb.EndGeography(); } gb.EndGeography(); }
public static SqlGeography CreateGeography(this string points, out OpenGisGeographyType geographyType, int srid = 4326) { geographyType = OpenGisGeographyType.Point; SqlGeography firstPoint = null; var pts = points.Trim().Split(new string[] { "\n" }, StringSplitOptions.None).ToList(); pts.Add(null); var b = new SqlGeographyBuilder(); b.SetSrid(srid); b.BeginGeography(OpenGisGeographyType.GeometryCollection); var coordinates = new List<Tuple<double, double>>(); for (int i = 0; i < pts.Count; i++) { if (string.IsNullOrWhiteSpace(pts[i])) { if (coordinates.Count == 1) { if (firstPoint == null) firstPoint = coordinates[0].CreatePoint(); b.BeginGeography(OpenGisGeographyType.Point); b.BeginFigure(coordinates[0].Item2, coordinates[0].Item1); b.EndFigure(); b.EndGeography(); } else if (coordinates.Count > 1) { geographyType = OpenGisGeographyType.GeometryCollection; var list = coordinates.Distinct().ToList(); b.BeginGeography(OpenGisGeographyType.Polygon); b.BeginFigure(list[0].Item2, list[0].Item1); for (var j = 1; j < list.Count; j++) b.AddLine(list[j].Item2, list[j].Item1); b.AddLine(list[0].Item2, list[0].Item1); b.EndFigure(); b.EndGeography(); } coordinates = new List<Tuple<double, double>>(); continue; } var pt = pts[i].Trim().Split(new string[] { " ", ",", ";", "\t", ":", "\r" }, StringSplitOptions.RemoveEmptyEntries); coordinates.Add(new Tuple<double, double>(double.Parse(pt[0]), double.Parse(pt[1]))); } b.EndGeography(); if (geographyType == OpenGisGeographyType.GeometryCollection) return b.ConstructedGeography.EnvelopeAngle() > 90 ? b.ConstructedGeography.ReorientObject() : b.ConstructedGeography; else return firstPoint; }
//LONGITUDE=item1, LATITUDE=item2 public static SqlGeography CreatePolygon(this List<Tuple<double, double>> coordinates, int srid = 4326) { var list = coordinates.Distinct().ToList(); var b = new SqlGeographyBuilder(); b.SetSrid(srid); b.BeginGeography(OpenGisGeographyType.Polygon); b.BeginFigure(list[0].Item2, list[0].Item1); for (var i = 1; i < list.Count; i++) b.AddLine(list[i].Item2, list[i].Item1); b.AddLine(list[0].Item2, list[0].Item1); b.EndFigure(); b.EndGeography(); return b.ConstructedGeography.EnvelopeAngle() > 90 ? b.ConstructedGeography.ReorientObject() : b.ConstructedGeography; }
public static SqlGeography CreatePolygon(IEnumerable<Coordinates> coordinates, int srid = 4326) { var list = coordinates.Distinct().ToList(); var b = new SqlGeographyBuilder(); b.SetSrid(srid); b.BeginGeography(OpenGisGeographyType.Polygon); b.BeginFigure(list[0].Latitude, list[0].Longitude); for (var i = 1; i < list.Count; i++) b.AddLine(list[i].Latitude, list[i].Longitude); b.AddLine(list[0].Latitude, list[0].Longitude); b.EndFigure(); b.EndGeography(); if(b.ConstructedGeography.EnvelopeAngle() > 90) return b.ConstructedGeography.ReorientObject(); return b.ConstructedGeography; }
public SqlGeography generateLineString(IList<RoutePointModel> points) { SqlGeographyBuilder builder = new SqlGeographyBuilder(); builder.SetSrid(4326); builder.BeginGeography(OpenGisGeographyType.LineString); for (int i = 0; i < points.Count; i++) { if (i == 0) { // First element, create figure builder.BeginFigure(points.ElementAt(0).latitude, points.ElementAt(0).longitude); } else { builder.AddLine(points.ElementAt(i).latitude, points.ElementAt(i).longitude); } } builder.EndFigure(); builder.EndGeography(); return builder.ConstructedGeography; }
private static void Internal_FillGeographyBuilder(SqlGeographyBuilder gb, Polygon polygon) { gb.BeginGeography(OpenGisGeographyType.Polygon); bool isExteriorRing = true; foreach (var lineString in polygon.Coordinates) { List<GeographicPosition> listGeoCoords = lineString.Coordinates.Select(p => p as GeographicPosition).ToList(); IEnumerable<GeographicPosition> orderedPositions = EnsureCorrectWinding(listGeoCoords, isExteriorRing); // exterior ring must be anti clockwise for SqlGeography isExteriorRing = false; bool beginFigureCalled = false; foreach (var pos in orderedPositions) { if (!beginFigureCalled) { gb.BeginFigure(pos.Latitude, pos.Longitude); beginFigureCalled = true; } else { gb.AddLine(pos.Latitude, pos.Longitude); } } gb.EndFigure(); } gb.EndGeography(); }
/// <summary> /// Converts a GeoJSON MultiPolygon to an SqlGeography /// </summary> /// <param name="multiPolygon"></param> /// <param name="srid"></param> /// <returns></returns> public static SqlGeography ToSqlGeography(this MultiPolygon multiPolygon, int srid = 4326) { SqlGeographyBuilder gb = new SqlGeographyBuilder(); gb.SetSrid(srid); gb.BeginGeography(OpenGisGeographyType.MultiPolygon); foreach (var polygon in multiPolygon.Coordinates) { Internal_FillGeographyBuilder(gb, polygon); } gb.EndGeography(); return gb.ConstructedGeography; }
private static void Internal_FillGeographyBuilder(SqlGeographyBuilder gb, MultiPolygon multiPolygon) { gb.BeginGeography(OpenGisGeographyType.MultiPolygon); foreach (var polygon in multiPolygon.Coordinates) { Internal_FillGeographyBuilder(gb, polygon); } gb.EndGeography(); }
/// <summary> /// Converts a GeoJSON GeometryCollection to an SqlGeography /// </summary> /// <param name="geometryCollection"></param> /// <param name="srid"></param> /// <returns></returns> public static SqlGeography ToSqlGeography(this GeometryCollection geometryCollection, int srid = 4326) { SqlGeographyBuilder gb = new SqlGeographyBuilder(); gb.SetSrid(srid); gb.BeginGeography(OpenGisGeographyType.GeometryCollection); foreach (var geom in geometryCollection.Geometries) { switch (geom.Type) { case GeoJSONObjectType.LineString: Internal_FillGeographyBuilder(gb, geom as LineString); break; case GeoJSONObjectType.MultiLineString: Internal_FillGeographyBuilder(gb, geom as MultiLineString); break; case GeoJSONObjectType.Point: Internal_FillGeographyBuilder(gb, geom as Point); break; case GeoJSONObjectType.MultiPoint: Internal_FillGeographyBuilder(gb, geom as MultiPoint); break; case GeoJSONObjectType.Polygon: Internal_FillGeographyBuilder(gb, geom as Polygon); break; case GeoJSONObjectType.MultiPolygon: Internal_FillGeographyBuilder(gb, geom as MultiPolygon); break; default: throw new NotSupportedException("Geometry conversion is not supported for " + geom.Type.ToString()); } } gb.EndGeography(); return gb.ConstructedGeography; }
private SqlGeography Sample_PolygonGeography() { SqlGeographyBuilder geoBuilder = new SqlGeographyBuilder(); geoBuilder.SetSrid(4326); geoBuilder.BeginGeography(OpenGisGeographyType.Polygon); geoBuilder.BeginFigure(40, -10); geoBuilder.AddLine(40, 10); geoBuilder.AddLine(50, 0); geoBuilder.AddLine(40, -10); geoBuilder.EndFigure(); geoBuilder.EndGeography(); return geoBuilder.ConstructedGeography; }
/// <summary> /// Converts a GeoJSON LineString to an SqlGeography /// </summary> /// <param name="lineString"></param> /// <param name="srid"></param> /// <returns></returns> public static SqlGeography ToSqlGeography(this LineString lineString, int srid = 4326) { SqlGeographyBuilder gb = new SqlGeographyBuilder(); gb.SetSrid(srid); Internal_FillGeographyBuilder(gb, lineString); return gb.ConstructedGeography; }
private static void Internal_FillGeographyBuilder(SqlGeographyBuilder gb, LineString lineString) { gb.BeginGeography(OpenGisGeographyType.LineString); bool beginFigureCalled = false; foreach (var ipos in lineString.Coordinates) { GeographicPosition pos = ipos as GeographicPosition; if (!beginFigureCalled) { gb.BeginFigure(pos.Latitude, pos.Longitude); beginFigureCalled = true; } else { gb.AddLine(pos.Latitude, pos.Longitude); } } gb.EndFigure(); gb.EndGeography(); }
/// <summary> /// Builds the route geography. /// </summary> /// <param name="startIndex">The start index.</param> /// <param name="endIndex">The end index.</param> /// <param name="nearestStop">The nearest stop.</param> /// <param name="furtherStop">The further stop.</param> /// <param name="route">The route.</param> /// <returns>Geography of appropriate route</returns> private static Route BuildRouteGeography(int startIndex,int endIndex,Stop nearestStop, Stop furtherStop,Route route) { var geographyBuilder = new SqlGeographyBuilder(); geographyBuilder.SetSrid(4326); geographyBuilder.BeginGeography(OpenGisGeographyType.LineString); SqlGeography beginGb = nearestStop.StopGeography; geographyBuilder.BeginFigure((double) beginGb.Lat, (double) beginGb.Long); for (var j = startIndex; j <= endIndex; j++) { var point = route.RouteGeography.STPointN(j); geographyBuilder.AddLine((double) point.Lat, (double) point.Long); } SqlGeography endFigure = furtherStop.StopGeography; geographyBuilder.AddLine((double) endFigure.Lat, (double) endFigure.Long); geographyBuilder.EndFigure(); geographyBuilder.EndGeography(); route.RouteGeography = geographyBuilder.ConstructedGeography; return route; }
/// <summary> /// Converts a GeoJSON Polygon to an SqlGeography /// </summary> /// <param name="polygon"></param> /// <param name="srid"></param> /// <returns></returns> public static SqlGeography ToSqlGeography(this Polygon polygon, int srid = 4326) { SqlGeographyBuilder gb = new SqlGeographyBuilder(); gb.SetSrid(srid); Internal_FillGeographyBuilder(gb, polygon); return gb.ConstructedGeography; }
/// <summary> /// Create a SqlGeography instance for the point specified by the LatLong object /// </summary> /// <param name="point">LatLong object representing the point</param> /// <returns>SqlGeography point</returns> private SqlGeography CreatePoint(LatLong point) { SqlGeographyBuilder geoPoint = new SqlGeographyBuilder(); //set the SRID and build the sql geography point for the centroid geoPoint.SetSrid(4326); geoPoint.BeginGeography(OpenGisGeographyType.Point); geoPoint.BeginFigure(point.Latitude, point.Longitude); geoPoint.EndFigure(); geoPoint.EndGeography(); return geoPoint.ConstructedGeography; }
/// <summary> /// Builds the route geography. /// </summary> /// <param name="route">The route.</param> /// <returns> /// New route geography /// </returns> private Route BuildRouteGeography(Route route) { var routeBuilder = new SqlGeographyBuilder(); routeBuilder.SetSrid(4326); routeBuilder.BeginGeography(OpenGisGeographyType.LineString); SqlGeography beginPoint = route.StartStop.StopGeography; routeBuilder.BeginFigure((double)beginPoint.Lat, (double)beginPoint.Long); for (var j = route.StartRouteIndex; j <= route.EndRouteIndex; j++) { var point = route.RouteGeography.STPointN(j); routeBuilder.AddLine((double)point.Lat, (double)point.Long); } SqlGeography endPoint = route.EndStop.StopGeography; routeBuilder.AddLine((double)endPoint.Lat, (double)endPoint.Long); routeBuilder.EndFigure(); routeBuilder.EndGeography(); route.CurrentPath = routeBuilder.ConstructedGeography; return route; }
/// <summary> /// Create the SqlGeography line string from the points given /// </summary> /// <param name="linePoints">List of LatLong that represents the start and end of the line</param> /// <returns>SqlGeography line string</returns> private SqlGeography CreateLineFromPoints(List<LatLong> linePoints) { SqlGeographyBuilder geoLine = new SqlGeographyBuilder(); //build the sql geography representation of the linestring geoLine.SetSrid(4326); geoLine.BeginGeography(OpenGisGeographyType.LineString); geoLine.BeginFigure(linePoints.First().Latitude, linePoints.First().Longitude); geoLine.AddLine(linePoints.Last().Latitude, linePoints.Last().Longitude); geoLine.EndFigure(); geoLine.EndGeography(); return geoLine.ConstructedGeography; }
/// <summary> /// Create a SqlGeography builder for the polygon specified by the points /// </summary> /// <param name="points">Latitude longitude points for the polygon</param> /// <returns>SqlGeographyBuilder for the polygon</returns> private SqlGeography CreatePolygonFromPoints(List<LatLong> points) { SqlGeographyBuilder polygon = new SqlGeographyBuilder(); polygon.SetSrid(4326); polygon.BeginGeography(OpenGisGeographyType.Polygon); //set the initial point to skip, and use that to begin the figure LatLong initialPoint = points.First(); polygon.BeginFigure(initialPoint.Latitude, initialPoint.Longitude); foreach (var point in points) { if (point != initialPoint) { //add each point to the geography poly polygon.AddLine(point.Latitude, point.Longitude); } } //end the configuration of the geography polygon.EndFigure(); polygon.EndGeography(); return polygon.ConstructedGeography; }