protected override byte[] Write(IGeometry gIn)
        {
            var geoWriter = new MsSql2008GeometryWriter();
            var b = geoWriter.WriteGeometry(gIn);
            var b2 = geoWriter.Write(gIn);
            using( var conn = new SqlConnection(ConnectionString))
            {
                conn.Open();
                using (var cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "INSERT INTO [nts_io_geometry] VALUES(@P1, @P2, @P3);";
                    var p1 = new SqlParameter("P1", SqlDbType.Int) { SqlValue = Counter };
                    var p2 = new SqlParameter("P2", SqlDbType.Text) { SqlValue = gIn.AsText() };
                    var p3 = new SqlParameter("P3", SqlDbType.Udt) { UdtTypeName = "geometry", SqlValue = b };
                    cmd.Parameters.AddRange(new[] { p1, p2, p3 });
                    cmd.ExecuteNonQuery();

                    /*
                    p1.SqlValue = 100000 + Counter;
                    cmd.Parameters.Remove(p3);
                    p3 = new SqlParameter("P3", SqlDbType.Image) { SqlValue = b };
                    cmd.Parameters.Add(p3);
                    p3.SqlValue = b2;
                    cmd.ExecuteNonQuery();
                     */
                }

            }
            return b2;
        }
        protected override byte[] Write(IGeometry gIn)
        {
            var geoWriter = new MsSql2008GeometryWriter();
            var b         = geoWriter.WriteGeometry(gIn);
            var b2        = geoWriter.Write(gIn);

            using (var conn = new SqlConnection(ConnectionString))
            {
                conn.Open();
                using (var cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "INSERT INTO [nts_io_geometry] VALUES(@P1, @P2, @P3);";
                    var p1 = new SqlParameter("P1", SqlDbType.Int)
                    {
                        SqlValue = Counter
                    };
                    var p2 = new SqlParameter("P2", SqlDbType.Text)
                    {
                        SqlValue = gIn.AsText()
                    };
                    var p3 = new SqlParameter("P3", SqlDbType.Udt)
                    {
                        UdtTypeName = "geometry", SqlValue = b
                    };
                    cmd.Parameters.AddRange(new[] { p1, p2, p3 });
                    cmd.ExecuteNonQuery();

                    /*
                     * p1.SqlValue = 100000 + Counter;
                     * cmd.Parameters.Remove(p3);
                     * p3 = new SqlParameter("P3", SqlDbType.Image) { SqlValue = b };
                     * cmd.Parameters.Add(p3);
                     * p3.SqlValue = b2;
                     * cmd.ExecuteNonQuery();
                     */
                }
            }
            return(b2);
        }
Пример #3
0
        internal static object ConvertToSqlType(IGeometry geom, int SRID, bool useGeography, int curRowIndex)
        {
            object v_ret = null;

            try
            {
                // Set geom SRID
                geom.SRID = SRID;

                if (useGeography)
                {
                    try
                    {
                        // SQL server needs anticlockwise rings
                        if (!REVERSE_GEOMETRIES.HasValue)
                        {
                            enRingOrientation ringOrientation = SqlServerHelper.GetRingOrientation(geom.Coordinates);
                            if (ringOrientation == enRingOrientation.Clockwise)
                            {
                                REVERSE_GEOMETRIES = true;
                            }
                        }

                        MsSql2008GeographyWriter geoWriter = new MsSql2008GeographyWriter();
                        SqlGeography             v_geog    = null;
                        if (REVERSE_GEOMETRIES.GetValueOrDefault(false))
                        {
                            v_geog = geoWriter.WriteGeography(geom.Reverse());
                        }
                        else
                        {
                            v_geog = geoWriter.WriteGeography(geom);
                        }
                        if (!v_geog.STIsValid().Value)
                        {
                            Trace.TraceWarning(string.Format("Invalid geometry. Must call make valid : {0}", v_geog.IsValidDetailed()));
                            v_geog = v_geog.MakeValid();
                        }
                        v_ret = v_geog;
                    }
                    catch (OutOfMemoryException exMemory)
                    {
                        Trace.WriteLine(string.Format("OutOfMemory on geom #{0} ({1}: {2})", curRowIndex, exMemory.GetType().Name, exMemory.Message));
                        throw;
                    }
                    catch (Exception exWriteGeom)
                    {
                        try
                        {
                            enRingOrientation ringOrientation = SqlServerHelper.GetRingOrientation(geom.Coordinates);

                            Trace.WriteLine(string.Format("Invalid geom #{0} ({1}: {2}). Try to reverse", curRowIndex, exWriteGeom.GetType().Name, exWriteGeom.Message));
                            // Maybe bad orientation
                            MsSql2008GeographyWriter geogWriter = new MsSql2008GeographyWriter();
                            v_ret = geogWriter.WriteGeography(geom.Reverse());

                            REVERSE_GEOMETRIES = true;
                        }
                        catch (OutOfMemoryException exMemory)
                        {
                            Trace.WriteLine(string.Format("OutOfMemory on geom #{0} ({1}: {2})", curRowIndex, exMemory.GetType().Name, exMemory.Message));
                            throw;
                        }
                        catch (Exception exReverse)
                        {
                            try
                            {
                                Trace.Write(string.Format("Bad reverse ({0}: {1})/ Converting to geometry", exReverse.GetType().Name, exReverse.Message));
                                // Maybe a self intersecting polygon. Use the buffer trick with the geometry
                                MsSql2008GeometryWriter geoWriter = new MsSql2008GeometryWriter();
                                SqlGeometry             sqlGeom   = geoWriter.WriteGeometry(geom);
                                if (!sqlGeom.STIsValid().Value)
                                {
                                    Trace.Write(" / Make valid");
                                    v_ret = SqlGeography.STGeomFromText(new SqlChars(new SqlString(sqlGeom.MakeValid().ToString())), SRID);
                                    Trace.WriteLine(" / OK");
                                }
                                else
                                {
                                    Trace.Write(" / Buffer");
                                    v_ret = SqlGeography.STGeomFromText(new SqlChars(new SqlString(sqlGeom.STBuffer(INVALIDGEOM_BUFFER).STBuffer(-INVALIDGEOM_BUFFER).Reduce(INVALIDGEOM_REDUCE).ToString())), SRID);
                                    Trace.WriteLine(" / OK");
                                }
                            }
                            catch (OutOfMemoryException exMemory)
                            {
                                Trace.WriteLine(string.Format("OutOfMemory on geom #{0} ({1}: {2})", curRowIndex, exMemory.GetType().Name, exMemory.Message));
                                throw;
                            }
                            catch (Exception exBuffer)
                            {
                                Trace.WriteLine(string.Format(" / KO ({0}: {1})", exBuffer.GetType().Name, exBuffer.Message));
                                throw;
                            }
                        }
                    }
                }
                else
                {
                    MsSql2008GeometryWriter geoWriter = new MsSql2008GeometryWriter();
                    SqlGeometry             v_retGeom = geoWriter.WriteGeometry(geom);
                    if (!v_retGeom.STIsValid().Value)
                    {
                        Trace.TraceWarning(string.Format("Invalid geometry. Must call make valid : {0}", v_retGeom.IsValidDetailed()));
                        v_retGeom = v_retGeom.MakeValid();
                    }
                    v_ret = v_retGeom;
                    //feature.geomWKT = geoWriter.WriteGeometry(geomOut).ToString();
                }
            }
            catch (Exception)
            {
                throw;
            }

            return(v_ret);
        }