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 GeoJsonToSqlGeographyObjectWalker() { _bld.SetSrid(GeoToSql.ReferenceId); _constructedGeography = new Lazy <SqlGeography>(() => { if (_pendingCircles.Count == 0) { return(_bld.ConstructedGeography); } // Decompose the geo collection, add the pending circles, wrap as geometry // collection and reparse as geo collection. var geos = new List <SqlGeography>(); var geoRoot = _bld.ConstructedGeography; for (var i = 1; i <= geoRoot.STNumGeometries(); i++) { geos.Add(geoRoot.STGeometryN(i)); } var sb = new StringBuilder(); sb.Append("GEOMETRYCOLLECTION ("); sb.Append(string.Join(",", geos.Concat(_pendingCircles))); sb.Append(")"); Debug.WriteLine(sb.ToString()); return(SqlGeography.STGeomCollFromText(new SqlChars(new SqlString(sb.ToString())), GeoToSql.ReferenceId)); }); }
//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"; } }
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()); }
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."); }
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); }
/// <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 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); }
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); }
/// <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 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 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 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> /// 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; }
//private readonly SqlGeographyBuilder _builder = new SqlGeographyBuilder(); /// <summary> /// Function to convert a <see cref="IGeometry"/> into a <see cref="SqlGeography"/> /// </summary> /// <param name="geometry">The geometry</param> /// <returns>A <see cref="SqlGeography"/></returns> public SqlGeography WriteGeography(IGeometry geometry) { var builder = new SqlGeographyBuilder(); builder.SetSrid(geometry.SRID); AddGeometry(builder, geometry); return(builder.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); }
public void AddLineBeforeBeginFigure() { SqlGeographyBuilder b = new SqlGeographyBuilder(); b.SetSrid(4326); b.BeginGeography(OpenGisGeographyType.LineString); AssertEx.ThrowsException(() => { b.AddLine(1, 2); }, typeof(FormatException), "24301: Expected a call to BeginFigure or EndGeography, but AddLine was called."); }
/// <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); }
/// <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 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); } }
public void EndGeographyBeforeBeginGeography() { SqlGeographyBuilder b = new SqlGeographyBuilder(); b.SetSrid(4326); AssertEx.ThrowsException(() => { b.EndGeography(); }, typeof(FormatException), "24300: Expected a call to BeginGeography, but EndGeography was called."); //var g = b.ConstructedGeography; }
private static SqlGeography CreateEmptyGeography(int srid) { SqlGeographyBuilder b = new SqlGeographyBuilder(); b.SetSrid(srid); b.BeginGeography(OpenGisGeographyType.GeometryCollection); b.EndGeography(); return(b.ConstructedGeography); }
public void AddLineOnPoint() { SqlGeographyBuilder b = new SqlGeographyBuilder(); b.SetSrid(4326); b.BeginGeography(OpenGisGeographyType.Point); b.BeginFigure(1, 2); AssertEx.ThrowsException(() => { b.AddLine(1, 2); }, typeof(FormatException), "24300: Expected a call to EndFigure, but AddLine was called."); }
//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 BuildGeography(List <LatLong> points, OpenGisGeographyType type) { SqlGeography geog = null; SqlGeographyBuilder geogBuilder = new SqlGeographyBuilder(); geogBuilder.SetSrid(4326); geogBuilder.BeginGeography(type); LatLong firstLatLong = null; bool firstLatLongStored = false; foreach (LatLong latLong in points) { if (!firstLatLongStored) { firstLatLong = latLong; geogBuilder.BeginFigure(firstLatLong.Latitude, firstLatLong.Longitude); firstLatLongStored = true; } else { geogBuilder.AddLine(latLong.Latitude, latLong.Longitude); } } if (type == OpenGisGeographyType.Polygon) { if (firstLatLong.Latitude != points[points.Count - 1].Latitude && firstLatLong.Longitude != points[points.Count - 1].Longitude) { geogBuilder.AddLine(firstLatLong.Latitude, firstLatLong.Longitude); } } geogBuilder.EndFigure(); geogBuilder.EndGeography(); try { geog = geogBuilder.ConstructedGeography; } catch (Exception ex) { throw; } var invertedGeogrpahy = geog.ReorientObject(); if (geog.STArea() > invertedGeogrpahy.STArea()) { geog = invertedGeogrpahy; } return(geog); }
public override SqlGeography AsSqlGeography() { SqlGeographyBuilder b = new SqlGeographyBuilder(); b.SetSrid(4326); b.BeginGeography(OpenGisGeographyType.Point); b.BeginFigure(this.Lat, this.Long, this.Z, this.Time.HasValue ? this.Time.Value.ToOADate() : (double?)null); b.EndFigure(); b.EndGeography(); return(b.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 void CreateEmpty() { SqlGeographyBuilder b = new SqlGeographyBuilder(); b.SetSrid(4326); b.BeginGeography(OpenGisGeographyType.Point); b.EndGeography(); var g = b.ConstructedGeography; Assert.IsTrue(g.STIsEmpty().Value); }
private SqlGeography CreateSinglePoint() { var builder = new SqlGeographyBuilder(); builder.SetSrid(4326); builder.BeginGeography(OpenGisGeographyType.Point); builder.BeginFigure(_points[0].Latitude, _points[0].Longitude); builder.EndFigure(); builder.EndGeography(); return(builder.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; }
public static SqlGeography Project(this SqlGeometry geometry, Func <IPoint, IPoint> mapFunction, int srid) { var builder = new SqlGeographyBuilder(); builder.SetSrid(srid); geometry = geometry.MakeValid(); OpenGisGeographyType geographyType = (OpenGisGeographyType)Enum.Parse(typeof(OpenGisGeographyType), geometry.STGeometryType().Value, true); builder.BeginGeography(geographyType); switch (geographyType) { case OpenGisGeographyType.Point: ProjectPoint(builder, geometry, mapFunction); break; case OpenGisGeographyType.LineString: ProjectLineString(builder, geometry, mapFunction); break; case OpenGisGeographyType.Polygon: ProjectPolygon(builder, geometry, mapFunction); break; case OpenGisGeographyType.MultiPoint: ProjectMultiPoint(builder, geometry, mapFunction); break; case OpenGisGeographyType.MultiLineString: ProjectMultiLineSring(builder, geometry, mapFunction); break; case OpenGisGeographyType.MultiPolygon: ProjectMultiPolygon(builder, geometry, mapFunction); break; case OpenGisGeographyType.GeometryCollection: case OpenGisGeographyType.CircularString: case OpenGisGeographyType.CompoundCurve: case OpenGisGeographyType.CurvePolygon: case OpenGisGeographyType.FullGlobe: default: throw new NotImplementedException(); } builder.EndGeography(); var result = builder.ConstructedGeography.STIsValid().IsTrue ? builder.ConstructedGeography : builder.ConstructedGeography.MakeValid(); return(result.EnvelopeAngle().Value == 180 ? result.ReorientObject() : result); }
public void MakeWay(MongoCollection <Node> nodes) { var builder = new SqlGeographyBuilder(); builder.SetSrid(base.Srid); if (this.NodeIds.Count == 1) { this.Shape = OpenGisGeographyType.Point; builder.BeginGeography(OpenGisGeographyType.Point); } else if (this.NodeIds.First().Equals(this.NodeIds.Last())) { if (this.NodeIds.Count == 2) { builder.BeginGeography(OpenGisGeographyType.Point); this.Shape = OpenGisGeographyType.Point; } if (this.NodeIds.Count == 3) { builder.BeginGeography(OpenGisGeographyType.LineString); this.Shape = OpenGisGeographyType.LineString; var index = this.NodeIds.Count - 1; this.NodeIds.RemoveAt(index); } else { this.Shape = OpenGisGeographyType.Polygon; builder.BeginGeography(OpenGisGeographyType.Polygon); } } else { this.Shape = OpenGisGeographyType.LineString; builder.BeginGeography(OpenGisGeographyType.LineString); } var one = nodes.FindOneById(this.NodeIds[0]); builder.BeginFigure(one.Latitude, one.Longtitude); for (var index = 1; index < this.NodeIds.Count; index++) { var nodeId = this.NodeIds[index]; var node = nodes.FindOneById(nodeId); builder.AddLine(node.Latitude, node.Longtitude); } builder.EndFigure(); builder.EndGeography(); this.Lines = builder.ConstructedGeography; }
/// <summary> /// Przykład stworzenia obiektu zawierającego punkt. /// </summary> /// <param name="latitude">Szerokość geograficzna punktu.</param> /// <param name="longitude">Długość geograficzna punktu.</param> /// <returns></returns> private static SqlGeography BuildPoint(double latitude, double longitude) { SqlGeographyBuilder builder = new SqlGeographyBuilder(); builder.SetSrid(4326); builder.BeginGeography(OpenGisGeographyType.Point); builder.BeginFigure(latitude, longitude); builder.EndFigure(); builder.EndGeography(); return(builder.ConstructedGeography); }
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; }
/// <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> /// 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; }
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; }
/// <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> /// 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> /// 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> /// 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; }
/// <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> /// 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 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; }