/// <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(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);
        }
Example #3
0
        //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);
        }
        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;
        }
Example #5
0
        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 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);
        }
Example #7
0
        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);
            }
        }
Example #8
0
        ////-----------------------------------------------------------------------------------------------------------

        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);
        }
        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);
        }
Example #10
0
        private static SqlGeographyBuilder GetSearchArea(BoundaryRequest data)
        {
            var searchArea = new SqlGeographyBuilder();

            searchArea.SetSrid(4326);
            searchArea.BeginGeography(OpenGisGeographyType.Polygon);
            searchArea.BeginFigure(data.NorthEast.Lat, data.NorthEast.Lng);
            searchArea.AddLine(data.NorthWest.Lat, data.NorthWest.Lng);
            searchArea.AddLine(data.SouthWest.Lat, data.SouthWest.Lng);
            searchArea.AddLine(data.SouthEast.Lat, data.SouthEast.Lng);
            searchArea.AddLine(data.NorthEast.Lat, data.NorthEast.Lng);
            searchArea.EndFigure();
            searchArea.EndGeography();
            return(searchArea);
        }
        private static void AddLineStringOrRing <T>(SqlGeographyBuilder builder, Geometry <T> geometry, bool isRing) where T : IPoint, new()
        {
            builder.BeginFigure(longitude: geometry.Points[0].X, latitude: geometry.Points[0].Y);

            for (int i = 1; i < geometry.Points.Count; i++)
            {
                builder.AddLine(longitude: geometry.Points[i].X, latitude: geometry.Points[i].Y);
            }

            if (isRing)
            {
                builder.AddLine(longitude: geometry.Points[0].X, latitude: geometry.Points[0].Y);
            }

            builder.EndFigure();
        }
        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();
        }
Example #13
0
 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();
 }
        private void AddCoordinates(Coordinate[] coordinates)
        {
            int points = 0;

            Array.ForEach <Coordinate>(coordinates, delegate(Coordinate coordinate)
            {
                double?z = null;
                if (!double.IsNaN(coordinate.Z) && !double.IsInfinity(coordinate.Z))
                {
                    z = coordinate.Z;
                }
                if (points == 0)
                {
                    builder.BeginFigure(coordinate.Y, coordinate.X, z, null);
                }
                else
                {
                    builder.AddLine(coordinate.Y, coordinate.X, z, null);
                }
                points++;
            });
            if (points != 0)
            {
                builder.EndFigure();
            }
        }
Example #15
0
 private void AddLongitudeLine(SqlGeographyBuilder sb, double lon, double latmin, double latmax)
 {
     sb.BeginGeography(OpenGisGeographyType.LineString);
     sb.BeginFigure(latmin, lon);
     sb.AddLine(latmax, lon);
     sb.EndFigure();
     sb.EndGeography();
 }
Example #16
0
        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>
        /// Build an SqlGeography Polygon from a bounding box.
        /// </summary>
        /// <param name="box">Bounding Box</param>
        /// <returns>SqlGeography Polygon</returns>
        public static SqlGeography GeographyFromBoundingBox(Models.BoundingBox box, int srid = 4326)
        {
            var geob = new SqlGeographyBuilder();

            geob.SetSrid(srid);
            geob.BeginGeography(OpenGisGeographyType.Polygon);
            geob.BeginFigure(box.maxY, box.minX);
            geob.AddLine(box.minY, box.minX);
            geob.AddLine(box.minY, box.maxX);
            geob.AddLine(box.maxY, box.maxX);
            geob.AddLine(box.maxY, box.minX);
            geob.EndFigure();
            geob.EndGeography();
            var geog = geob.ConstructedGeography;

            //Debug.WriteLine(geog.AsGml().Value);
            return(geog);
        }
Example #18
0
        /// <summary>
        /// Build an SqlGeography Polygon from a bounding box.
        /// </summary>
        /// <param name="box">Bounding Box</param>
        /// <returns>SqlGeography Polygon</returns>
        public static SqlGeography GeographyFromBoundingBox(Models.BoundingBox box)
        {
            var geob = new SqlGeographyBuilder();

            geob.SetSrid(SRID);
            geob.BeginGeography(OpenGisGeographyType.Polygon);
            geob.BeginFigure(box.South, box.West);
            geob.AddLine(box.South, box.East);
            geob.AddLine(box.North, box.East);
            geob.AddLine(box.North, box.West);
            geob.AddLine(box.South, box.West);
            geob.EndFigure();
            geob.EndGeography();
            var geog = geob.ConstructedGeography;

            //Trace.WriteLine("GeographyFromBoundingBox : " + geog.AsGml().Value);
            return(geog);
        }
Example #19
0
        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.");
        }
Example #20
0
        //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;
        }
Example #21
0
        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;
        }
        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;
        }
Example #23
0
        /// <summary>
        /// Przykład stworzenia obiektu zawierającego linię.
        /// </summary>
        /// <param name="latitude1">Szerokość geograficzna pierwszego punktu.</param>
        /// <param name="longitude1">Długość geograficzna pierwszego punktu.</param>
        /// <param name="latitude2">Szerokość geograficzna drugiego punktu.</param>
        /// <param name="longitude2">Długość geograficzna drugiego punktu.</param>
        /// <returns></returns>
        private static SqlGeography BuildLine(double latitude1, double longitude1, double latitude2, double longitude2)
        {
            SqlGeographyBuilder builder = new SqlGeographyBuilder();

            builder.SetSrid(4326);
            builder.BeginGeography(OpenGisGeographyType.LineString);
            builder.BeginFigure(latitude1, longitude1);
            builder.AddLine(latitude2, longitude2);
            builder.EndFigure();
            builder.EndGeography();

            return(builder.ConstructedGeography);
        }
Example #24
0
        private static void SharpMapLineStringToSqlGeography(SqlGeographyBuilder geomBuilder, SMLineString lineString)
        {
            geomBuilder.BeginGeography(OpenGisGeographyType.LineString);
            var coords = lineString.Coordinates;

            geomBuilder.BeginFigure(coords[0].Y, coords[0].X);
            for (int i = 1; i < lineString.NumPoints; i++)
            {
                var point = coords[i];
                geomBuilder.AddLine(point.Y, point.X);
            }
            geomBuilder.EndFigure();
            geomBuilder.EndGeography();
        }
Example #25
0
        private void AddLatitudeLine(SqlGeographyBuilder sb, double lonmin, double lonmax, double lat)
        {
            // TODO: calculate number of steps from tolerance

            sb.BeginGeography(OpenGisGeographyType.LineString);
            sb.BeginFigure(lat, lonmin);

            for (int i = 0; i < 100; i++)
            {
                sb.AddLine(lat, lonmin + (i + 1) * (lonmax - lonmin) / 100);
            }

            sb.EndFigure();
            sb.EndGeography();
        }
Example #26
0
        private static void AddTrackSegment(SqlGeographyBuilder builder, GpxTrackSegment segment)
        {
            builder.BeginGeography(OpenGisGeographyType.LineString);

            builder.BeginFigure(segment.TrackPoints[0].Latitude, segment.TrackPoints[0].Longitude);

            for (int i = 1; i < segment.TrackPoints.Count; i++)
            {
                builder.AddLine(segment.TrackPoints[i].Latitude, segment.TrackPoints[i].Longitude);
            }

            builder.EndFigure();

            builder.EndGeography();
        }
Example #27
0
        private static SqlGeography TryCreatePolygon(List <WayCreationPoint> points)
        {
            var builder = new SqlGeographyBuilder();

            builder.SetSrid(4326);
            builder.BeginGeography(OpenGisGeographyType.Polygon);
            builder.BeginFigure(points[0].Latitude, points[0].Longitude);
            for (var i = 1; i != points.Count; ++i)
            {
                builder.AddLine(points[i].Latitude, points[i].Longitude);
            }
            builder.EndFigure();
            builder.EndGeography();
            return(builder.ConstructedGeography);
        }
Example #28
0
        public void BuildLineString()
        {
            SqlGeographyBuilder b = new SqlGeographyBuilder();

            b.SetSrid(4326);
            b.BeginGeography(OpenGisGeographyType.LineString);
            b.BeginFigure(1, 2);
            b.AddLine(3, 4, 10, null);
            b.AddLine(6, 7, 11, 12);
            b.EndFigure();
            b.EndGeography();
            var g = b.ConstructedGeography;

            Assert.AreEqual("LineString", g.STGeometryType());
            Assert.AreEqual(3, g.STNumPoints());
            Assert.AreEqual(1, g.STStartPoint().Lat);
            Assert.AreEqual(2, g.STStartPoint().Long);
            Assert.AreEqual(6, g.STEndPoint().Lat);
            Assert.AreEqual(7, g.STEndPoint().Long);
            Assert.AreEqual(3, g.STPointN(2).Lat);
            Assert.AreEqual(4, g.STPointN(2).Long);
            Assert.IsTrue(g.STPointN(2).HasZ);
            Assert.IsFalse(g.STPointN(2).HasM);
        }
        /// <summary>
        /// Build an SqlGeography LineString from a GeoJson LineString.
        /// </summary>
        /// <param name="line">GeoJson LineString</param>
        /// <returns>SqlGeography LineString</returns>
        public static SqlGeography GeographyFromGeoJsonLineString(Models.GeoJson.LineString line, int SRID)
        {
            var geob = new SqlGeographyBuilder();

            geob.SetSrid(SRID);
            geob.BeginGeography(OpenGisGeographyType.LineString);
            geob.BeginFigure(line.coordinates[0][0], line.coordinates[0][1]);
            foreach (var pair in line.coordinates.Skip(1))
            {
                geob.AddLine(pair[0], pair[1]);
            }
            geob.EndFigure();
            geob.EndGeography();
            var geog = geob.ConstructedGeography;

            return(geog);
        }
Example #30
0
        private SqlGeography CreateGeographyLine()
        {
            var builder = new SqlGeographyBuilder();

            builder.SetSrid(4326);
            builder.BeginGeography(OpenGisGeographyType.LineString);
            builder.BeginFigure(_points[0].Latitude, _points[0].Longitude);
            for (var i = 1; i != _points.Count; ++i)
            {
                builder.AddLine(_points[i].Latitude, _points[i].Longitude);
            }
            builder.EndFigure();
            builder.EndGeography();

            var res = builder.ConstructedGeography;

            return(res);
        }
Example #31
0
        public override SqlGeography AsSqlGeography()
        {
            SqlGeographyBuilder b = new SqlGeographyBuilder();

            b.SetSrid(4326);
            b.BeginGeography(OpenGisGeographyType.LineString);
            if (this.Points.Count > 0)
            {
                b.BeginFigure(this.Points[0].Lat, this.Points[0].Long, this.Points[0].Z, this.Points[0].Time.HasValue ? (double?)null : this.Points[0].Time.Value.ToOADate());
            }
            for (int i = 1; i < this.Points.Count; i++)
            {
                b.AddLine(this.Points[i].Lat, this.Points[i].Long, this.Points[i].Z, this.Points[i].Time.HasValue ? (double?)null : this.Points[i].Time.Value.ToOADate());
            }
            b.EndFigure();
            b.EndGeography();
            return(b.ConstructedGeography);
        }
        /// <summary>
        /// Build an SqlGeography Polygon from a GeoJson Polygon.
        /// </summary>
        /// <param name="poly">GeoJson Polygon</param>
        /// <returns>SqlGeography Polygon</returns>
        public static SqlGeography GeographyFromGeoJsonPolygon(Models.GeoJson.Polygon poly, int SRID)
        {
            var geob = new SqlGeographyBuilder();

            geob.SetSrid(SRID);
            geob.BeginGeography(OpenGisGeographyType.Polygon);
            geob.BeginFigure(poly.coordinates[0][0][0], poly.coordinates[0][0][1]);
            foreach (var pair in poly.coordinates[0].Skip(1))
            {
                geob.AddLine(pair[0], pair[1]);
            }
            geob.EndFigure();
            geob.EndGeography();
            var geog = geob.ConstructedGeography;

            Debug.WriteLine(geog.AsGml().Value);
            return(geog);
        }
Example #33
0
        /// <summary>
        /// Build an SqlGeography LineString from a GeoJson LineString.
        /// </summary>
        /// <param name="line">GeoJson LineString</param>
        /// <returns>SqlGeography LineString</returns>
        public static SqlGeography GeographyFromGeoJsonLineString(DurMap.Site.Models.GeoJson.LineString line)
        {
            var geob = new SqlGeographyBuilder();

            geob.SetSrid(SRID);
            geob.BeginGeography(OpenGisGeographyType.LineString);
            geob.BeginFigure(line.coordinates[0][1], line.coordinates[0][0]);
            foreach (var pair in line.coordinates.Skip(1))
            {
                geob.AddLine(pair[1], pair[0]);
            }
            geob.EndFigure();
            geob.EndGeography();
            var geog = geob.ConstructedGeography;

            //Trace.WriteLine(geog.AsGml().Value);
            return(geog);
        }
		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();
		}
Example #35
0
        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 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;
		}
Example #37
0
        /// <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;
        }
		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();
		}
Example #39
0
        /// <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 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;
        }
        /// <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;
        }
Example #42
0
 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;
 }