/// <summary>
 /// Get the Well Known Text format that DbGeography supports. This is a little different from the Well Known Text that is output
 /// by Microsoft.Data.Spatial.Geography types, so we can't just directly use that representation here.
 /// </summary>
 /// <param name="geography">Geography instance to convert to Well Known Text.</param>
 /// <returns>Well Known Text for the specified geography instance.</returns>
 private static string GetWellKnownText(Geography geography)
 {
     string extendedWKT = geography.ToString();
     int semicolon = extendedWKT.IndexOf(';');
     Assert.IsTrue(semicolon > 0, "Expected to find a semicolon in the extended WellKnownText output (using ToString) for the type {0}", geography.GetType());
     return extendedWKT.Substring(semicolon + 1);
 }
示例#2
0
        public static DbGeography ConvertFrom(Geography geo)
        {
            string geographyEwkt = geo.ToString();
            int    semicolon     = geographyEwkt.IndexOf(';');

            string geographyWkt = geographyEwkt.Substring(semicolon + 1);

            return(DbGeography.FromText(geographyWkt, int.Parse(geo.CoordinateSystem.Id)));
        }
        /// <summary>
        /// Converts a DbGeography to a Geography.
        /// </summary>
        /// <param name="geography">Geography instance to convert.</param>
        /// <returns>New DbGeography instance representing the same value as <paramref name="geography"/>.</returns>
        internal static DbGeography ConvertGeography(Geography geography)
        {
            // DbGeography supports a different WKT format than Geography, so need to strip off the SRID first
            string geographyEWKT = geography.ToString();
            int semicolon = geographyEWKT.IndexOf(';');

            Debug.Assert(semicolon >= 0, "Expected to find a semicolon in the WKT format of Geography instance.");

            string geographyWKT = geographyEWKT.Substring(semicolon + 1);
            return DbGeography.FromText(geographyWKT, geography.CoordinateSystem.Id);
        }
示例#4
0
        /// <summary>
        /// Converts a DbGeography to a Geography.
        /// </summary>
        /// <param name="geography">Geography instance to convert.</param>
        /// <returns>New DbGeography instance representing the same value as <paramref name="geography"/>.</returns>
        internal static DbGeography ConvertGeography(Geography geography)
        {
            // DbGeography supports a different WKT format than Geography, so need to strip off the SRID first
            string geographyEWKT = geography.ToString();
            int    semicolon     = geographyEWKT.IndexOf(';');

            Debug.Assert(semicolon >= 0, "Expected to find a semicolon in the WKT format of Geography instance.");

            string geographyWKT = geographyEWKT.Substring(semicolon + 1);

            return(DbGeography.FromText(geographyWKT, geography.CoordinateSystem.Id));
        }
    /// <summary>
    /// This method stores the given box into the database
    /// </summary>
    /// <param name="box">Box to be saved</param>
    /// <param name="insertedKmlId">The KmlId to connect this object to</param>
    /// <param name="con">Sql connection</param>
    /// <param name="tran">Sql transaction</param>
    /// <returns>The database id which is assigned to the given box</returns>
    protected static int?SaveLatLonBox(Geography box, int insertedKmlId, SqlConnection con, SqlTransaction tran)
    {
        #region Insert Geography

        int?insertedGeographyId = null;

        if (box != null)
        {
            SqlCommand saveGeographyCmd =
                new SqlCommand(
                    " insert into Geographies(Geography, KmlId) " +
                    " values(geography::Parse('" + box.ToString() + "'), @KmlId);select scope_identity()", con, tran);

            AddParameter(saveGeographyCmd, "KmlId", insertedKmlId, DbType.Int32);

            insertedGeographyId = (int)((decimal)saveGeographyCmd.ExecuteScalar());
            box.DbId            = insertedGeographyId;
        }

        #endregion

        #region Insert Lat Lon Box

        SqlCommand saveLatLonBox =
            new SqlCommand(
                " insert into LatLonBoxes(BoxType, GeographyId, rotation, MinAlt, MaxAlt, AltitudeMode) " +
                " values(@BoxType, @GeographyId, @rotation, @MinAlt, @MaxAlt, @AltitudeMode);select scope_identity()",
                con, tran);

        AddParameter(saveLatLonBox, "GeographyId", insertedGeographyId, DbType.Int32);

        LatLonBox    latLonBox    = box as LatLonBox;
        LatLonAltBox latLonAltBox = box as LatLonAltBox;
        LatLonQuad   latLonQuad   = box as LatLonQuad;

        string BoxType = "";

        if (latLonBox != null)
        {
            BoxType = "LatLonBox";
            AddParameter(saveLatLonBox, "rotation", latLonBox.Rotation, DbType.Double);
        }
        else
        {
            AddParameter(saveLatLonBox, "rotation", null, DbType.Double);
        }

        if (latLonAltBox != null)
        {
            BoxType = "LatLonAltBox";

            AddParameter(saveLatLonBox, "MinAlt", latLonAltBox.MinAltitude, DbType.Double);
            AddParameter(saveLatLonBox, "MaxAlt", latLonAltBox.MaxAltitude, DbType.Double);
            AddParameter(saveLatLonBox, "AltitudeMode", latLonAltBox.AltitudeMode.ToString(), DbType.String);
        }
        else
        {
            AddParameter(saveLatLonBox, "MinAlt", null, DbType.Double);
            AddParameter(saveLatLonBox, "MaxAlt", null, DbType.Double);
            AddParameter(saveLatLonBox, "AltitudeMode", null, DbType.Double);
        }

        if (latLonQuad != null)
        {
            BoxType = "LatLonQuad";
        }

        AddParameter(saveLatLonBox, "BoxType", BoxType, DbType.String);

        int?insertedLatLonBox = (int)((decimal)saveLatLonBox.ExecuteScalar());

        #endregion

        return(insertedLatLonBox);
    }