public SqlGeometry Apply(SqlGeometry geometry)
        {
            SqlGeometryBuilder builder = new SqlGeometryBuilder();

            geometry.Populate(new GeometryTransformer(builder, this));
            return(builder.ConstructedGeometry);
        }
        public static IEnumerable <SqlGeometry> Segments(this SqlGeometry lineGeom)
        {
            if (lineGeom == null || lineGeom.IsNull || lineGeom.STIsEmpty())
            {
                yield return(null);
            }
            if (lineGeom.STGeometryType().ToString() != OpenGisGeometryType.LineString.ToString())
            {
                yield return(null);
            }
            if (lineGeom.STNumPoints().Value < 2)
            {
                yield return(null);
            }

            for (int i = 1; i < lineGeom.STNumPoints().Value; i++)
            {
                SqlGeometry ptStart = lineGeom.STPointN(i);
                SqlGeometry ptNext  = lineGeom.STPointN(i + 1);

                SqlGeometryBuilder gb = new SqlGeometryBuilder();
                gb.SetSrid(lineGeom.STSrid.Value);
                gb.BeginGeometry(OpenGisGeometryType.LineString);
                gb.BeginFigure(ptStart.STX.Value, ptStart.STY.Value);
                gb.AddLine(ptNext.STX.Value, ptNext.STY.Value);
                gb.EndFigure();
                gb.EndGeometry();

                yield return(gb.ConstructedGeometry);
            }
        }
        public static System.Data.Entity.Spatial.DbGeometry Reverse(
            this System.Data.Entity.Spatial.DbGeometry linestring)
        {
            var fromWkb = SqlGeometry.STGeomFromWKB(new SqlBytes(linestring.AsBinary()), 0);

            // Create a new Geometry Builder
            var gb = new SqlGeometryBuilder();
            // Set the Spatial Reference ID equal to the supplied linestring
            gb.SetSrid((int) (fromWkb.STSrid));
            // Start the linestring
            gb.BeginGeometry(OpenGisGeometryType.LineString);
            // Add the first point using BeginFigure()
            gb.BeginFigure((double) fromWkb.STEndPoint().STX, (double) fromWkb.STEndPoint().STY);
            // Loop through remaining points in reverse order
            for (var x = (int) fromWkb.STNumPoints() - 1; x > 0; --x)
            {
                gb.AddLine((double) fromWkb.STPointN(x).STX, (double) fromWkb.STPointN(x).STY);
            }
            // End the figure
            gb.EndFigure();
            // End the geometry
            gb.EndGeometry();
            // Return that as a SqlGeometry instance
            return System.Data.Entity.Spatial.DbGeometry.FromBinary(gb.ConstructedGeometry.STAsBinary().Buffer);
        }
        public static SqlGeometry ToSqlGeometry(SMGeometry smGeometry)
        {
            SqlGeometryBuilder builder = new SqlGeometryBuilder();

            builder.SetSrid(smGeometry.SRID);

            SharpMapGeometryToSqlGeometry(builder, smGeometry);

            SqlGeometry g = builder.ConstructedGeometry;

            if (!g.STIsValid())
            {
                try
                {
                    g = g.Reduce(ReduceTolerance);
                    g = g.MakeValid();
                }
                catch (Exception ex)
                {
                    throw new SqlGeometryConverterException(ex, smGeometry);
                }
            }

            if (!g.STIsValid())
            {
                throw new SqlGeometryConverterException(smGeometry);
            }

            return(g);
        }
        public static SqlGeometry MakeGeometry(List <Point> points, bool isClosed, int srid = 32639)
        {
            SqlGeometryBuilder builder = new SqlGeometryBuilder();

            builder.SetSrid(srid);

            builder.BeginGeometry(isClosed ? OpenGisGeometryType.Polygon : OpenGisGeometryType.LineString);

            builder.BeginFigure(points[0].X, points[0].Y);

            for (int i = 1; i < points.Count; i++)
            {
                builder.AddLine(points[i].X, points[i].Y);
            }

            if (isClosed)
            {
                builder.AddLine(points[0].X, points[0].Y);
            }

            builder.EndFigure();

            builder.EndGeometry();

            return(builder.ConstructedGeometry.MakeValid());
        }
        // This implements a completely trivial conversion from geography to geometry, simply taking each
        // point (lat,long) --> (y, x).  The result is assigned the given SRID.
        public static SqlGeometry VacuousGeographyToGeometry(SqlGeography toConvert, int targetSrid)
        {
            SqlGeometryBuilder b = new SqlGeometryBuilder();

            toConvert.Populate(new VacuousGeographyToGeometrySink(targetSrid, b));
            return(b.ConstructedGeometry);
        }
Example #7
0
        private static void BuildPolygonSimplified(SqlGeometryBuilder builder, SqlGeometry shape,
                                                   SqlDouble shortestDistance)
        {
            try
            {
                builder.BeginGeometry(OpenGisGeometryType.Polygon);
                var internalRingsNum = (int)shape.STNumInteriorRing();
                var exteriorRing     = shape.STExteriorRing();

                // Proceed shortening external ring
                AddShortenedRingByBuilder(builder, exteriorRing, shortestDistance);
                // Proceed interior rings
                if (internalRingsNum > 0)
                {
                    for (int i = 1; i <= internalRingsNum; i++)
                    {
                        AddShortenedRingByBuilder(builder, shape.STInteriorRingN(i), shortestDistance);
                    }
                }
                builder.EndGeometry();
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format(" BuildPolygonSimplified: {0}", ex.Message));
            }
        }
Example #8
0
        /// <summary>
        /// Problem here : self intersecting lines are not supported. Not ideal for GPS tracks...
        /// </summary>
        /// <param name="points"></param>
        /// <returns></returns>
        public static SqlGeometry ParseGeoPointAsGeometryLine(IEnumerable <GeoPoint> points)
        {
            SqlGeometryBuilder gb = new SqlGeometryBuilder();

            gb.SetSrid(4326);
            gb.BeginGeometry(OpenGisGeometryType.LineString);
            bool first = true;

            foreach (var pt in points)
            {
                if (first)
                {
                    gb.BeginFigure(pt.Longitude, pt.Latitude);
                    first = false;
                }
                else
                {
                    gb.AddLine(pt.Longitude, pt.Latitude);
                }
            }
            gb.EndFigure();
            gb.EndGeometry();

            SqlGeometry geom = gb.ConstructedGeometry.MakeValidIfInvalid(1);

            return(geom);
        }
Example #9
0
        public static SqlGeometry ToSqlServerGeometry(SMGeometry smGeometry)
        {
            SqlGeometryBuilder builder = new SqlGeometryBuilder();

            SharpMapGeometryToSqlGeometry(builder, smGeometry);
            return(builder.ConstructedGeometry);
        }
Example #10
0
        /// <summary>
        /// Method builds the SqlGeometry form of the MULTILINESTRING through reference GeometryBuilder.
        /// </summary>
        /// <param name="geomBuilder">Reference SqlGeometryBuilder to be used for building Geometry.</param>
        internal bool BuildSqlGeometry(ref SqlGeometryBuilder geomBuilder)
        {
            var isBuildDone = false;

            if (IsEmpty)
            {
                return(false);
            }

            if (geomBuilder == null)
            {
                geomBuilder = new SqlGeometryBuilder();
            }

            if (IsMultiLine)
            {
                geomBuilder.SetSrid(SRID);
                geomBuilder.BeginGeometry(OpenGisGeometryType.MultiLineString);
                isBuildDone = true;
            }

            // ignore points
            foreach (var line in _lines.Where(line => line.IsLine).ToList())
            {
                line.BuildSqlGeometry(ref geomBuilder, IsMultiLine);
                isBuildDone = true;
            }

            if (IsMultiLine)
            {
                geomBuilder.EndGeometry();
            }

            return(isBuildDone);
        }
 private static void GeometryToSqlGeometry(IGeometry geom, SqlGeometryBuilder bldr)
 {
     if (geom is IPoint)
     {
         GeometryToSqlGeometry(geom as IPoint, bldr);
     }
     else if (geom is IMultiPoint)
     {
         GeometryToSqlGeometry(geom as IMultiPoint, bldr);
     }
     else if (geom is ILineString)
     {
         GeometryToSqlGeometry(geom as ILineString, bldr);
     }
     else if (geom is IMultiLineString)
     {
         GeometryToSqlGeometry(geom as IMultiLineString, bldr);
     }
     else if (geom is IPolygon)
     {
         GeometryToSqlGeometry(geom as IPolygon, bldr);
     }
     else if (geom is IMultiPolygon)
     {
         GeometryToSqlGeometry(geom as IMultiPolygon, bldr);
     }
     else if (geom is IGeometryCollection)
     {
         GeometryToSqlGeometry(geom as IGeometryCollection, bldr);
     }
     else
     {
         throw new Exception(String.Format("Unable to convert geometry of type '{0}'", geom.GetType().Name));
     }
 }
Example #12
0
        private static void AddFigureRingPoints(SqlGeometryBuilder builder, Point[] points)
        {
            try
            {
              int pointqty = points.Length;
                int unicqty = points.Distinct().Count();
                if (unicqty  < 3) return;
             if (points[0] != points[pointqty - 1]) return;
             if (points[0] != points[pointqty - 1]) return;

            Point point = points[0];
                Point prevPoint = point;
            builder.BeginFigure(point.X, point.Y);
            for (int i = 1; i < pointqty; i++)
            {
                point = points[i];
                if (point != prevPoint) builder.AddLine(point.X, point.Y);
                prevPoint = point;
            }
            builder.EndFigure();
            }
            catch (Exception)
            {

                throw;
            }
        }
Example #13
0
        private static void AddFigureRingPoints(SqlGeometryBuilder builder, Point[] points)
        {
            try
            {
              int pointqty = points.Length;
                int unicqty = points.Distinct().Count();
                if (unicqty  < 3) return;
             if (points[0] != points[pointqty - 1]) return;
             if (points[0] != points[pointqty - 1]) return;

            Point point = points[0];
                Point prevPoint = point;
            builder.BeginFigure(point.X, point.Y);
            for (int i = 1; i < pointqty; i++)
            {
                point = points[i];
                if (point != prevPoint) builder.AddLine(point.X, point.Y);
                prevPoint = point;
            }
            builder.EndFigure();
            }
            catch (Exception)
            {

                throw;
            }
        }
Example #14
0
        private static void AddRings(SqlGeometryBuilder builder, List<GeometryPixelCoords> ring)
        {
            var circnum = ring.Count;

            for (int c = 0; c < circnum; c++)
                AddFigureRingPoints(builder, ring[c].PixelCoordList);
        }
        public static SqlGeometry Extend(
            SqlGeometry @geom1,
            SqlGeometry @geom2,
            SqlInt32 @Offset)
        {
            // Start the LineString
            SqlGeometryBuilder gb = new SqlGeometryBuilder();

            gb.SetSrid((int)(@geom1.STSrid));
            gb.BeginGeometry(OpenGisGeometryType.LineString);
            gb.BeginFigure(
                (double)@geom1.STStartPoint().STX,
                (double)@geom1.STStartPoint().STY
                );
            // Add the points from the first geometry
            for (int x = 2; x <= (int)@geom1.STNumPoints(); x++)
            {
                gb.AddLine(
                    (double)@geom1.STPointN(x).STX,
                    (double)@geom1.STPointN(x).STY
                    );
            }
            // Add the points from the second geometry
            for (int x = 1 + (int)@Offset; x <= (int)@geom2.STNumPoints(); x++)
            {
                gb.AddLine(
                    (double)@geom2.STPointN(x).STX,
                    (double)@geom2.STPointN(x).STY
                    );
            }
            gb.EndFigure();
            gb.EndGeometry();
            return(gb.ConstructedGeometry);
        }
Example #16
0
        public static SqlGeometry ToSqlGeometry(SMGeometry smGeometry)
        {
            SqlGeometryBuilder builder = new SqlGeometryBuilder();
#if !DotSpatialProjections
            if (smGeometry.SpatialReference != null)
                builder.SetSrid((int) smGeometry.SpatialReference.AuthorityCode);
#else
            if (smGeometry.SpatialReference != null)
                builder.SetSrid((int) smGeometry.SpatialReference.EpsgCode);
#endif
            else
                builder.SetSrid(0);

            SharpMapGeometryToSqlGeometry(builder, smGeometry);

            SqlGeometry g = builder.ConstructedGeometry;
            if (!g.STIsValid())
            {
                try
                {
                    g.Reduce(ReduceTolerance);
                    g.MakeValid();
                }
                catch (Exception ex)
                {
                    throw new SqlGeometryConverterException(ex, smGeometry);
                }
            }

            if (!g.STIsValid())
                throw new SqlGeometryConverterException(smGeometry);

            return g;
        }
Example #17
0
        public static System.Data.Entity.Spatial.DbGeometry Reverse(
            this System.Data.Entity.Spatial.DbGeometry linestring)
        {
            var fromWkb = SqlGeometry.STGeomFromWKB(new SqlBytes(linestring.AsBinary()), 0);

            // Create a new Geometry Builder
            var gb = new SqlGeometryBuilder();

            // Set the Spatial Reference ID equal to the supplied linestring
            gb.SetSrid((int)(fromWkb.STSrid));
            // Start the linestring
            gb.BeginGeometry(OpenGisGeometryType.LineString);
            // Add the first point using BeginFigure()
            gb.BeginFigure((double)fromWkb.STEndPoint().STX, (double)fromWkb.STEndPoint().STY);
            // Loop through remaining points in reverse order
            for (var x = (int)fromWkb.STNumPoints() - 1; x > 0; --x)
            {
                gb.AddLine((double)fromWkb.STPointN(x).STX, (double)fromWkb.STPointN(x).STY);
            }
            // End the figure
            gb.EndFigure();
            // End the geometry
            gb.EndGeometry();
            // Return that as a SqlGeometry instance
            return(System.Data.Entity.Spatial.DbGeometry.FromBinary(gb.ConstructedGeometry.STAsBinary().Buffer));
        }
Example #18
0
 private void AddGeometry(SqlGeometryBuilder builder, IGeometry geometry)
 {
     if (geometry is IPoint)
     {
         AddPoint(builder, (IPoint)geometry);
     }
     else if (geometry is ILineString)
     {
         AddLineString(builder, (ILineString)geometry);
     }
     else if (geometry is IPolygon)
     {
         AddPolygon(builder, (IPolygon)geometry);
     }
     else if (geometry is IMultiPoint)
     {
         AddGeometryCollection(builder, (IMultiPoint)geometry, OpenGisGeometryType.MultiPoint);
     }
     else if (geometry is IMultiLineString)
     {
         AddGeometryCollection(builder, (IMultiLineString)geometry, OpenGisGeometryType.MultiLineString);
     }
     else if (geometry is IMultiPolygon)
     {
         AddGeometryCollection(builder, (IMultiPolygon)geometry, OpenGisGeometryType.MultiPolygon);
     }
     else if (geometry is IGeometryCollection)
     {
         AddGeometryCollection(builder, (IGeometryCollection)geometry, OpenGisGeometryType.GeometryCollection);
     }
 }
 private static void SharpMapPointToSqlGeometry(SqlGeometryBuilder geomBuilder, SMPoint point)
 {
     geomBuilder.BeginGeometry(OpenGisGeometryType.Point);
     geomBuilder.BeginFigure(point.X, point.Y);
     geomBuilder.EndFigure();
     geomBuilder.EndGeometry();
 }
 private static void SharpMapMultiPointToSqlGeometry(SqlGeometryBuilder geomBuilder, SMMultiPoint multiPoint)
 {
     geomBuilder.BeginGeometry(OpenGisGeometryType.MultiPoint);
     for(int i = 0; i <= multiPoint.NumGeometries; i++)
         SharpMapPointToSqlGeometry(geomBuilder, multiPoint.Geometry(i));
     geomBuilder.EndGeometry();
 }
 private static void SharpMapGeometryToSqlGeometry(SqlGeometryBuilder geomBuilder, SMGeometry smGeometry)
 {
     
     switch (smGeometry.GeometryType)
     {
         case SMGeometryType.Point:
             SharpMapPointToSqlGeometry(geomBuilder, smGeometry as SMPoint);
             break;
         case SMGeometryType.LineString:
             SharpMapLineStringToSqlGeometry(geomBuilder, smGeometry as SMLineString);
             break;
         case SMGeometryType.Polygon:
             SharpMapPolygonToSqlGeometry(geomBuilder, smGeometry as SMPolygon);
             break;
         case SMGeometryType.MultiPoint:
             SharpMapMultiPointToSqlGeometry(geomBuilder, smGeometry as SMMultiPoint);
             break;
         case SMGeometryType.MultiLineString:
             SharpMapMultiLineStringToSqlGeometry(geomBuilder, smGeometry as SMMultiLineString);
             break;
         case SMGeometryType.MultiPolygon:
             SharpMapMultiPolygonToSqlGeometry(geomBuilder, smGeometry as SMMultiPolygon);
             break;
         case SMGeometryType.GeometryCollection:
             SharpMapGeometryCollectionToSqlGeometry(geomBuilder, smGeometry as SMGeometryCollection);
             break;
         default:
             throw new ArgumentException(
                 String.Format("Cannot convert '{0}' geometry type", smGeometry.GeometryType), "smGeometry");
     }
 }
Example #22
0
        private static void WriteEsriPolygon(SqlGeometryBuilder builder, ISimplePoints points)
        {
            if (points.NumberOfParts == 1)
            {
                builder.BeginGeometry(OpenGisGeometryType.Polygon);

                WriteLineOrRing(builder, points.Points);

                builder.EndGeometry();
            }
            else if (points.NumberOfParts > 1)
            {
                builder.BeginGeometry(OpenGisGeometryType.MultiPolygon);

                for (int i = 0; i < points.NumberOfParts; i++)
                {
                    builder.BeginGeometry(OpenGisGeometryType.Polygon);

                    WriteLineOrRing(builder, points.GetPart(i));

                    builder.EndGeometry();
                }

                builder.EndGeometry();
            }
            else
            {
                throw new NotImplementedException();
            }
        }
        private static void AddMultiPolygon <T>(SqlGeometryBuilder builder, Geometry <T> geometry) where T : IPoint, new()
        {
            //return CreatePolygon(points, srid);
            //SqlGeometryBuilder builder = new SqlGeometryBuilder();

            //builder.SetSrid(geometry.Srid);

            builder.BeginGeometry(OpenGisGeometryType.MultiPolygon);

            foreach (var item in geometry.Geometries)
            {
                builder.BeginGeometry(OpenGisGeometryType.Polygon);

                foreach (var lines in item.Geometries)
                {
                    if (lines.NumberOfPoints < minimumPolygonPoints)
                    {
                        Trace.WriteLine($"CreateMultiPolygon escape!");
                        continue;
                    }

                    AddLineStringOrRing(builder, lines, true);
                }

                builder.EndGeometry();
            }

            builder.EndGeometry();

            //return builder.ConstructedGeometry.MakeValid();
        }
Example #24
0
        private static void AddRingByBuilder(SqlGeometryBuilder builder, SqlGeometry ring)
        {
            try
            {
                var firstPoint = ring.STPointN(1);
                var startPoint = firstPoint;
                var pointCount = ring.STNumPoints();
                // Begin the polygon with the first point
                builder.BeginFigure((double)firstPoint.STX, (double)firstPoint.STY);
                // While there are still unchecked points in polygon
                for (int i = 2; i <= pointCount; i++)
                {
                    var secondPoint = ring.STPointN(i);

                    builder.AddLine((double)secondPoint.STX, (double)secondPoint.STY);
                }
                // Add last point - the same as first
                builder.AddLine((double)startPoint.STX, (double)startPoint.STY);
                builder.EndFigure();
            }
            catch (System.Exception ex)
            {
                throw new Exception(string.Format("  AddRingByBuilder: {0}", ex.Message));
            }
        }
 /// <summary>
 /// Loop through each geometry types LINESTRING and MULTILINESTRING and reverse and translate measure it accordingly.
 /// </summary>
 /// <param name="target"></param>
 /// <param name="translateMeasure"></param>
 public ReverseAndTranslateGeometrySink(SqlGeometryBuilder target, double translateMeasure)
 {
     _target           = target;
     _translateMeasure = translateMeasure;
     _isMultiLine      = false;
     _lineCounter      = 0;
 }
 private static void SharpMapMultiLineStringToSqlGeometry(SqlGeometryBuilder geomBuilder, SMMultiLineString multiLineString)
 {
     geomBuilder.BeginGeometry(OpenGisGeometryType.MultiLineString);
     for (int i = 0; i < multiLineString.NumGeometries; i++)
         SharpMapLineStringToSqlGeometry(geomBuilder, multiLineString.Geometry(i) as SMLineString);
     geomBuilder.EndGeometry();
 }
		/// <summary>
		/// Converts a GeoJSON MultiPoint to an SqlGeometry
		/// </summary>
		/// <param name="multiPoint"></param>
		/// <param name="srid"></param>
		/// <returns></returns>
		public static SqlGeometry ToSqlGeometry(this MultiPoint multiPoint, int srid = 4326)
		{
			SqlGeometryBuilder gb = new SqlGeometryBuilder();
			gb.SetSrid(srid);
			Internal_FillGeometryBuilder(gb, multiPoint);
			return gb.ConstructedGeometry;
		}
Example #28
0
        public static SqlGeometry GridCellsCenterPointsToPolygon(List <WebGridCellSpeciesObservationCount> gridCells)
        {
            if (gridCells == null || !gridCells.Any())
            {
                return(null);
            }

            var gb = new SqlGeometryBuilder();

            // Set the Spatial Reference ID
            gb.SetSrid(gridCells.First().GridCoordinateSystem.Srid());

            // Start the collection
            gb.BeginGeometry(OpenGisGeometryType.MultiPoint);

            foreach (var gridCell in gridCells)
            {
                gb.BeginGeometry(OpenGisGeometryType.Point);
                gb.BeginFigure(gridCell.OrginalCentreCoordinate.X, gridCell.OrginalCentreCoordinate.Y);
                gb.EndFigure();
                gb.EndGeometry();
            }

            // End the multipolygon
            gb.EndGeometry();

            // Return as a valid SqlGeometry instance
            return(gb.ConstructedGeometry.MakeValid());
        }
Example #29
0
        public static SqlGeometry CreateMultiPointGeometry(List <ArtDatabanken.GIS.GeoJSON.Net.Geometry.Point> geoJsonPoints, int srid)
        {
            if (geoJsonPoints == null)
            {
                return(null);
            }
            SqlGeometryBuilder geomBuilder;

            geomBuilder = new SqlGeometryBuilder();
            geomBuilder.SetSrid(srid);
            geomBuilder.BeginGeometry(OpenGisGeometryType.MultiPoint);

            foreach (ArtDatabanken.GIS.GeoJSON.Net.Geometry.Point point in geoJsonPoints)
            {
                // Setting the srid to the same as for the geoJsonPoints
                //geomBuilder.SetSrid(srid);
                geomBuilder.BeginGeometry(OpenGisGeometryType.Point);
                geomBuilder.BeginFigure(point.Coordinates.Longitude, point.Coordinates.Latitude);
                geomBuilder.EndFigure();
                geomBuilder.EndGeometry();
                //pointList.Add(geomBuilder.ConstructedGeometry.MakeValid());
            }
            geomBuilder.EndGeometry();
            SqlGeometry geometry = geomBuilder.ConstructedGeometry.MakeValid();

            return(geometry);
        }
Example #30
0
        /// <summary>
        /// Constructs a multi point geometry object from a list of points
        /// </summary>
        /// <param name="points"></param>
        /// <returns></returns>
        public static SqlGeometry BuildMultiPoint(List <LocationTimeValue> points)
        {
            // Create a new instance of the SqlGeographyBuilder
            SqlGeometryBuilder gb2 = new SqlGeometryBuilder();

            // Set the spatial reference identifier
            gb2.SetSrid(27700);

            try
            {
                // Declare the type of collection to be created
                gb2.BeginGeometry(OpenGisGeometryType.MultiPoint);

                for (int i = 0; i < points.Count; i++)
                {
                    // Create the first point in the collection
                    gb2.BeginGeometry(OpenGisGeometryType.Point);
                    gb2.BeginFigure(points[i].X, points[i].Y, points[i].Z, points[i].Value[0]);
                    gb2.EndFigure();
                    gb2.EndGeometry();
                }
            }
            catch
            {
            }
            finally
            {
                // End the geometry and retrieve the constructed instance
                gb2.EndGeometry();
            }

            return(gb2.ConstructedGeometry);
        }
Example #31
0
        private static void SpatialTraceLine(List <GeoPoint> lineElevationData, string message)
        {
            const int DEFAULT_HEIGHT = 300;
            // Say that 1 sample is one pixel and a graph is usually 300px tall
            // So 300px = 300 samples = max height (H)
            // So for numSamples, (H * numSamples / 300) = height of 1px


            double minH      = lineElevationData.Min(p => p.Elevation.GetValueOrDefault(0));
            double maxH      = lineElevationData.Max(p => p.Elevation.GetValueOrDefault(0));
            double H         = maxH - minH;
            double ratio_11  = lineElevationData.Last().DistanceFromOriginMeters / H;
            double ratio     = ratio_11 / 4;
            double tolerance = H / DEFAULT_HEIGHT;

            // Make 4:1 geom
            SqlGeometryBuilder gb = new SqlGeometryBuilder();

            gb.SetSrid(0);             // custom SRID
            gb.BeginGeometry(OpenGisGeometryType.LineString);

            gb.BeginFigure(lineElevationData[0].DistanceFromOriginMeters / ratio, lineElevationData[0].Elevation.GetValueOrDefault(0));
            for (int i = 1; i < lineElevationData.Count; i++)
            {
                gb.AddLine(lineElevationData[i].DistanceFromOriginMeters / ratio, lineElevationData[i].Elevation.GetValueOrDefault(0));
            }
            gb.EndFigure();
            gb.EndGeometry();
            SqlGeometry geom = gb.ConstructedGeometry;

            SpatialTrace.Enable();
            SpatialTrace.TraceGeometry(geom, message);
            SpatialTrace.Disable();
        }
Example #32
0
 private void AddPolygon(SqlGeometryBuilder builder, IPolygon geometry)
 {
     builder.BeginGeometry(OpenGisGeometryType.Polygon);
     AddCoordinates(builder, geometry.ExteriorRing.CoordinateSequence);
     Array.ForEach(geometry.InteriorRings, ring => AddCoordinates(builder, ring.CoordinateSequence));
     builder.EndGeometry();
 }
 private static void Internal_FillGeometryBuilder(SqlGeometryBuilder gb, MultiLineString multiLineString)
 {
     gb.BeginGeometry(OpenGisGeometryType.MultiLineString);
     foreach (var lineString in multiLineString.Coordinates)
     {
         gb.BeginGeometry(OpenGisGeometryType.LineString);
         bool beginFigureCalled = false;
         foreach (var ipos in lineString.Coordinates)
         {
             Position pos = ipos as Position;
             if (!beginFigureCalled)
             {
                 gb.BeginFigure(pos.Longitude, pos.Latitude);
                 beginFigureCalled = true;
             }
             else
             {
                 gb.AddLine(pos.Longitude, pos.Latitude);
             }
         }
         gb.EndFigure();
         gb.EndGeometry();
     }
     gb.EndGeometry();
 }
Example #34
0
 private void AddCoordinates(SqlGeometryBuilder builder, ICoordinateSequence coordinates)
 {
     for (var i = 0; i < coordinates.Count; i++)
     {
         AddCoordinate(builder, coordinates, i);
     }
 }
 private static void SharpMapGeometryCollectionToSqlGeometry(SqlGeometryBuilder geomBuilder, SMGeometryCollection geometryCollection)
 {
     geomBuilder.BeginGeometry(OpenGisGeometryType.GeometryCollection);
     for (int i = 0; i < geometryCollection.NumGeometries; i++ )
         SharpMapGeometryToSqlGeometry(geomBuilder, geometryCollection.Geometry(i));
     geomBuilder.EndGeometry();
 }
Example #36
0
        private void ReprojectFigure(SqlGeometry geometry, SqlGeometryBuilder bldr)
        {
            SqlGeometry point;
            int         numPoints = geometry.STNumPoints().Value;

            double[] pts = new double[numPoints * 2];

            for (int i = 0, idx = 0; i < numPoints; i++)
            {
                point = geometry.STPointN(i + 1);

                pts[idx++] = point.STX.Value;
                pts[idx++] = point.STY.Value;
            }

            if (numPoints > 0)
            {
                DotSpatial.Projections.Reproject.ReprojectPoints(pts, null, _proj_source, _proj_target, 0, numPoints);

                bldr.BeginFigure(pts[0], pts[1]);

                for (int i = 2; i < pts.Length; i += 2)
                {
                    bldr.AddLine(pts[i], pts[i + 1]);
                }

                bldr.EndFigure();
            }
        }
 private static void SharpMapMultiPolygonToSqlGeometry(SqlGeometryBuilder geomBuilder, SMMultiPolygon multiPolygon)
 {
     geomBuilder.BeginGeometry(OpenGisGeometryType.MultiPolygon);
     for (int i = 0; i < multiPolygon.NumGeometries; i++)
         SharpMapPolygonToSqlGeometry(geomBuilder, multiPolygon.Geometry(i) as SMPolygon);
     geomBuilder.EndGeometry();
 }
 // We target another builder, to which we will send a point representing the point we find.
 // We also take a measure, which is the point along the input linestring we will travel to.
 // Note that we only operate on LineString instances: anything else will throw an exception.
 public LocateMAlongGeometrySink(double measure, SqlGeometryBuilder target, double tolerance = Constants.Tolerance)
 {
     _target        = target;
     _measure       = measure;
     _tolerance     = tolerance;
     IsPointDerived = false;
 }
        private static void GeometryToSqlGeometry(ILineString geom, SqlGeometryBuilder bldr)
        {
            bldr.BeginGeometry(OpenGisGeometryType.LineString);

            AddFigure(geom, bldr);

            bldr.EndGeometry();
        }
		private static void Internal_FillGeometryBuilder(SqlGeometryBuilder gb, Point point)
		{
			gb.BeginGeometry(OpenGisGeometryType.Point);
			GeographicPosition pos = point.Coordinates as GeographicPosition;
			gb.BeginFigure(pos.Longitude, pos.Latitude);
			gb.EndFigure();
			gb.EndGeometry();
		}
        private static void GeometryToSqlGeometry(ILineString geom, SqlGeometryBuilder bldr)
        {
            bldr.BeginGeometry(OpenGisGeometryType.LineString);

            AddFigure(geom, bldr);

            bldr.EndGeometry();
        }
        private static void GeometryToSqlGeometry(IMultiLineString geom, SqlGeometryBuilder bldr)
        {
            bldr.BeginGeometry(OpenGisGeometryType.MultiLineString);

            for (int i = 0, c = geom.NumGeometries; i < c; i++)
                GeometryToSqlGeometry(geom.Geometries[i] as ILineString, bldr);

            bldr.EndGeometry();
        }
        public static SqlGeometry GeometryToSqlGeometry(IGeometry geom, int srid)
        {
            SqlGeometryBuilder bldr = new SqlGeometryBuilder();
            bldr.SetSrid(srid);

            GeometryToSqlGeometry(geom, bldr);

            return bldr.ConstructedGeometry;
        }
        private static void AddFigure(ILineString line, SqlGeometryBuilder bldr)
        {
            IList<Coordinate> coords = line.Coordinates;

            bldr.BeginFigure(coords[0].X, coords[0].Y);
            for (int i = 0; i < coords.Count; i++)
                bldr.AddLine(coords[i].X, coords[i].Y);
            bldr.EndFigure();
        }
        private static void GeometryToSqlGeometry(IPoint geom, SqlGeometryBuilder bldr)
        {
            bldr.BeginGeometry(OpenGisGeometryType.Point);

            bldr.BeginFigure(geom.X, geom.Y);
            bldr.EndFigure();

            bldr.EndGeometry();
        }
 private static void AddRing(SqlGeometryBuilder builder, SMLinearRing linearRing)
 {
     SMPoint pt = linearRing.StartPoint;
     builder.BeginFigure(pt.X, pt.Y);
     for (int i = 1; i < linearRing.NumPoints; i++)
     {
         pt = linearRing.Point(i);
         builder.AddLine(pt.X, pt.Y);
     }
     builder.EndFigure();
 }
		public static SqlGeometry ReprojectGeometry(SqlGeometry geom, int srid, Func<double, double, double[]> coordTransform)
		{
			if (geom != null)
			{
				SqlGeometryBuilder builder = new SqlGeometryBuilder();
				SqlGeometryProjectionSink sink = new SqlGeometryProjectionSink(builder, srid, coordTransform);
				geom.Populate(sink);

				return builder.ConstructedGeometry;
			}
			return null;
		}
Example #48
0
 public static byte[] GeoProcessingNode(Node node)
 {
     SqlGeometry geo = new SqlGeometry();
     SqlGeometryBuilder GeometryBuilder = new SqlGeometryBuilder();
     GeometryBuilder.SetSrid(4326);
     GeometryBuilder.BeginGeometry(OpenGisGeometryType.Point);
     GeometryBuilder.BeginFigure(node.Latitude, node.Longtitude);
     GeometryBuilder.EndFigure();
     GeometryBuilder.EndGeometry();
     geo = GeometryBuilder.ConstructedGeometry;
     return geo.STAsBinary().Buffer;
 }
 /// <summary>
 /// Builds multipoligon by each polygon inside geometry collection
 /// </summary>
 public static void BuildMultipolygonSimplified(SqlGeometryBuilder builder, SqlGeometry shape,
                                               SqlDouble shortestDistance)
 {
     builder.BeginGeometry(OpenGisGeometryType.MultiPolygon);
     var polygonCount = shape.STNumGeometries();
     for (int i = 1; i <= polygonCount; i++)
     {
         BuildPolygonSimplified(builder, shape.STGeometryN(i), shortestDistance);
     }
     // End the geometry
     builder.EndGeometry();
 }
Example #50
0
        private static void AddFigurePoints(SqlGeometryBuilder builder, Point[] points)
        {
            int geomlen = points.Length;

            Point point = points[0];
            builder.BeginFigure(point.X, point.Y);
            for (int i = 1; i < geomlen; i++)
            {
                point = points[i];
                builder.AddLine(point.X, point.Y);
            }
            builder.EndFigure();
        }
		private static void Internal_FillGeometryBuilder(SqlGeometryBuilder gb, MultiPoint multiPoint)
		{
			gb.BeginGeometry(OpenGisGeometryType.MultiPoint);
			List<Point> coords = multiPoint.Coordinates as List<Point>;
			foreach (var coord in coords)
			{
				GeographicPosition pos = coord.Coordinates as GeographicPosition;
				gb.BeginGeometry(OpenGisGeometryType.Point);
				gb.BeginFigure(pos.Longitude, pos.Latitude);
				gb.EndFigure();
				gb.EndGeometry();
			}
			gb.EndGeometry();
		}
 public static SqlGeometry ToLineString(this SqlGeometry poly)
 {
     var geomBuilder = new SqlGeometryBuilder();
     geomBuilder.SetSrid((0));
     geomBuilder.BeginGeometry(OpenGisGeometryType.LineString);
     var startpoint = poly.STStartPoint();
     geomBuilder.BeginFigure((double)startpoint.STX, (double)startpoint.STY);
     for (int i = 1; i <= poly.STNumPoints(); i++)
     {
         geomBuilder.AddLine((double)poly.STPointN(i).STX, (double)poly.STPointN(i).STY);
     }
     geomBuilder.EndFigure();
     geomBuilder.EndGeometry();
     return geomBuilder.ConstructedGeometry;
 }
 private static void GeometryToSqlGeometry(IGeometry geom, SqlGeometryBuilder bldr)
 {
     if (geom is IPoint)
         GeometryToSqlGeometry(geom as IPoint, bldr);
     else if (geom is IMultiPoint)
         GeometryToSqlGeometry(geom as IMultiPoint, bldr);
     else if (geom is ILineString)
         GeometryToSqlGeometry(geom as ILineString, bldr);
     else if (geom is IMultiLineString)
         GeometryToSqlGeometry(geom as IMultiLineString, bldr);
     else if (geom is IPolygon)
         GeometryToSqlGeometry(geom as IPolygon, bldr);
     else if (geom is IMultiPolygon)
         GeometryToSqlGeometry(geom as IMultiPolygon, bldr);
     else if (geom is IGeometryCollection)
         GeometryToSqlGeometry(geom as IGeometryCollection, bldr);
     else
         throw new Exception(String.Format("Unable to convert geometry of type '{0}'", geom.GetType().Name));
 }
Example #54
0
        private static byte[] ConstructLineString(List<Node> nodes, OsmPrimitive osmPrimitive)
        {
            if (nodes != null)
            {
                try
                {
                    SqlGeometry line;
                    SqlGeometryBuilder lineBuilder = new SqlGeometryBuilder();
                    lineBuilder.SetSrid(4326);
                    lineBuilder.BeginGeometry(OpenGisGeometryType.LineString);
                    lineBuilder.BeginFigure(nodes.First().Latitude, nodes.First().Longtitude);
                    for (int i = 1; i < nodes.Count; i++)
                    {
                        lineBuilder.AddLine(nodes[i].Latitude, nodes[i].Longtitude);
                    }

                    lineBuilder.EndFigure();
                    lineBuilder.EndGeometry();
                    line = lineBuilder.ConstructedGeometry;

                    if (line.STIsValid())
                    {
                        return line.STAsBinary().Buffer;
                    }
                    else
                    {
                        Log.Log.Write(osmPrimitive.GetType() + " " + osmPrimitive.Id + " not valid");
                        return null;
                    }
                }
                catch (Exception e)
                {
                    Log.Log.Write(osmPrimitive.GetType() + " " + osmPrimitive.Id + " Error constructed Geometry LINE STRING: " + e.Message);
                    return null;
                }
            }
            else
            {
                throw new NullReferenceException("Construct polygon failed. Nodes not found.");
            }
        }
        public static SqlGeometry ExcludeShortLine(SqlGeometry shape, SqlDouble shortestDistance)
        {
            // Begin the geometry
            SqlGeometryBuilder _builder = new SqlGeometryBuilder();
            _builder.SetSrid(0);
            if (shape == null) return null;

            switch (GetOpenGisGeometryType(shape))
            {
                case OpenGisGeometryType.GeometryCollection:
                    return CreateMultiPolygonFromGeometryCollectionWithExcludingLineString(shape);
                case OpenGisGeometryType.LineString:
                case OpenGisGeometryType.MultiLineString:
                    // _builder.Dispose(); //EndGeometry();
                    return shape;

                case OpenGisGeometryType.MultiPolygon:

                    BuildMultipolygonSimplified(_builder, shape, shortestDistance);
                    break;
                case OpenGisGeometryType.Polygon:

                    BuildPolygonSimplified(_builder, shape, shortestDistance);
                    break;
                default:
                    return shape;

            }

            // Return the constructed geometry
            var resultGeometry = _builder.ConstructedGeometry;
            if (resultGeometry.STIsValid()) return resultGeometry;
            else
            {

                return CreateMultiPolygonFromGeometryCollectionWithExcludingLineString(resultGeometry.MakeValid());
            }
        }
Example #56
0
 private static void SharpMapPolygonToSqlGeometry(SqlGeometryBuilder geomBuilder, SMPolygon polygon)
 {
     geomBuilder.BeginGeometry(OpenGisGeometryType.Polygon);
     AddRing(geomBuilder, polygon.ExteriorRing);
     for (int i = 0; i < polygon.NumInteriorRing; i++)
         AddRing(geomBuilder, polygon.InteriorRing(i));
     geomBuilder.EndGeometry();
 }
Example #57
0
 private static void SharpMapPointToSqlGeometry(SqlGeometryBuilder geomBuilder, SMPoint point)
 {
     try
     {
         geomBuilder.BeginGeometry(OpenGisGeometryType.Point);
         geomBuilder.BeginFigure(point.X, point.Y, point.Z, null);
         geomBuilder.EndFigure();
         geomBuilder.EndGeometry();
     }
     catch (Exception ex)
     {
         string exMessage = string.Format("Failed Point X [{0}] Y [{1}] Z[{2}]", point.X, point.Y, point.Z);
         Exception wrappingException = new Exception(exMessage, ex);
         throw wrappingException;
     }
 }
Example #58
0
 private static void SharpMapLineStringToSqlGeometry(SqlGeometryBuilder geomBuilder, SMLineString lineString)
 {
     geomBuilder.BeginGeometry(OpenGisGeometryType.LineString);
     SMPoint point = lineString.StartPoint;
     geomBuilder.BeginFigure(point.X, point.Y);
     for (int i = 1; i < lineString.NumPoints; i++)
     {
         point = lineString.Point(i);
         geomBuilder.AddLine(point.X, point.Y);
     }
     geomBuilder.EndFigure();
     geomBuilder.EndGeometry();
 }
Example #59
0
 internal static SqlGeometry ToSqlServerGeometry(SMGeometry smGeometry)
 {
     SqlGeometryBuilder builder = new SqlGeometryBuilder();
     builder.SetSrid(smGeometry.SRID);
     SharpMapGeometryToSqlGeometry(builder, smGeometry);
     SqlGeometry geometry = builder.ConstructedGeometry;
     geometry = geometry.MakeValid();
     return geometry;
 }
 private static void SharpMapPointToSqlGeometry(SqlGeometryBuilder geomBuilder, SMPoint point)
 {
     geomBuilder.BeginGeometry(OpenGisGeometryType.Point);
     geomBuilder.BeginFigure(point.X, point.Y);
     geomBuilder.EndFigure();
     geomBuilder.EndGeometry();
 }