Ejemplo n.º 1
0
 public static SqlGeometry ToSqlGeometry(this DbGeometry dbGeometry)
 {
     if (dbGeometry == null)
     {
         return(null);
     }
     return(SqlGeometry.STGeomFromWKB(new SqlBytes(dbGeometry.AsBinary()), dbGeometry.CoordinateSystemId));
 }
        public static IGeometry ToGeometry(this DbGeometry self)
        {
            if (self == null)
            {
                return(null);
            }

            var reader = new WKBReader(NetTopologySuite.NtsGeometryServices.Instance);

            return(reader.Read(self.AsBinary()));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Перепроецирует геометрию в WGS84 и возвращает набор точек
        /// </summary>
        /// <param name="input">Простые полигоны и полилинии из БД в системе координать UtmZone31N</param>
        /// <returns>Набор точек в системе координат WGS84 (SRID 4326)</returns>
        /// <remarks>Внимание! Многоконтурная геометрия не поддерживается</remarks>
        public LocationCollection UtmZone31nToWgs84Transform(DbGeometry input)
        {
            if (input == null)
            {
                return(null);
            }

            //Перевод геометрии в формат понятный для библиотеки DotSpatial
            byte[]    wkbBynary = input.AsBinary();
            WkbReader wkbReader = new WkbReader();

            DotSpatial.Topology.IGeometry geom = wkbReader.Read(wkbBynary);

            //Определение используемых проекций
            ProjectionInfo beninPrj = KnownCoordinateSystems.Projected.UtmWgs1984.WGS1984UTMZone31N;
            ProjectionInfo worldPrj = KnownCoordinateSystems.Geographic.World.WGS1984;

            //Создание массива точек Х1,У1,Х2,У2...Хn,Уn
            double[] pointArray = new double[geom.Coordinates.Count() * 2];
            double[] zArray     = new double[1];
            zArray[0] = 0;

            int counterX = 0; int counterY = 1;

            foreach (var coordinate in geom.Coordinates)
            {
                pointArray[counterX] = coordinate.X;
                pointArray[counterY] = coordinate.Y;

                counterX += 2;
                counterY += 2;
            }

            //Операция перепроецирования над массивом точек
            Reproject.ReprojectPoints(pointArray, zArray, beninPrj, worldPrj, 0, (pointArray.Length / 2));


            LocationCollection loc = new LocationCollection();

            counterX = 0; counterY = 1;

            foreach (var coordinate in geom.Coordinates)
            {
                loc.Add(new Location(pointArray[counterY], pointArray[counterX]));

                counterX += 2;
                counterY += 2;
            }

            return(loc);
        }
Ejemplo n.º 4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="dbGeometry"></param>
        /// <returns></returns>
        public static IGeometry dbGeometryToGeometry(DbGeometry dbGeometry)
        {
            if (dbGeometry == null)
            {
                return(null);
            }
            byte[]           wkb     = dbGeometry.AsBinary();
            IGeometry        geomOut = new GeometryBag();
            IGeometryFactory gf      = new GeometryEnvironment() as IGeometryFactory;
            int readCount            = 0;

            gf.CreateGeometryFromWkbVariant(wkb, out geomOut, out readCount);
            return(geomOut);
        }
Ejemplo n.º 5
0
        public static DbGeometry ProjectCaliforniaStatePlaneVIToWebMercator(DbGeometry inputGeometry)
        {
            var wkb = inputGeometry.AsBinary();

            NetTopologySuite.IO.WKBReader reader = new NetTopologySuite.IO.WKBReader();
            Geometry internalGeometry            = (Geometry)reader.Read(wkb);

            double[] pointArray = new double[internalGeometry.Coordinates.Count() * 2];
            double[] zArray     = new double[1];
            zArray[0] = 1;

            int counterX = 0;
            int counterY = 1;

            foreach (var coordinate in internalGeometry.Coordinates)
            {
                pointArray[counterX] = coordinate.X;
                pointArray[counterY] = coordinate.Y;

                counterX = counterX + 2;
                counterY = counterY + 2;
            }

            Reproject.ReprojectPoints(pointArray, zArray, CaStatePlane, WebMercator, 0, (pointArray.Length / 2));

            counterX = 0;
            counterY = 1;
            foreach (var coordinate in internalGeometry.Coordinates)
            {
                coordinate.X = pointArray[counterX];
                coordinate.Y = pointArray[counterY];

                counterX = counterX + 2;
                counterY = counterY + 2;
            }

            var outputWkb = internalGeometry.AsBinary();

            var dbGeometry = DbGeometry.FromBinary(outputWkb, WGS_1984_SRID);

            if (!dbGeometry.IsValid)
            {
                dbGeometry = dbGeometry.ToSqlGeometry().MakeValid().ToDbGeometry().FixSrid(WGS_1984_SRID);
            }

            return(dbGeometry);
        }
Ejemplo n.º 6
0
        public static object NormalizeValue(TypeUsage type, object value)
        {
            PrimitiveType pt = (PrimitiveType)type.EdmType;

#if NET_45_OR_GREATER
            if (value != null &&
                value != DBNull.Value &&
                Type.GetTypeCode(value.GetType()) == TypeCode.Object)
            {
                DbGeometry geometryValue = value as DbGeometry;
                if (geometryValue != null)
                {
                    Byte[] geometryValBinary = new Byte[25];
                    var    buffer            = geometryValue.AsBinary();
                    var    srid = geometryValue.WellKnownValue.CoordinateSystemId;
                    for (int i = 0; i < buffer.Length; i++)
                    {
                        if (i < 4)
                        {
                            geometryValBinary[i] = (byte)(srid & 0xff);
                            srid >>= 8;
                        }
                        geometryValBinary[i + 4] = buffer[i];
                    }
                    return(geometryValBinary);
                }
            }
#endif
            if (pt.PrimitiveTypeKind != PrimitiveTypeKind.DateTimeOffset)
            {
                return(value);
            }
            DateTimeOffset dto = (DateTimeOffset)value;
            DateTime       dt  = dto.DateTime;
            if (dt.Year < 1970)
            {
                return(new DateTime(1970, 1, 1, 0, 0, 1));
            }
            return(dt);
        }
Ejemplo n.º 7
0
        public static DbGeometry ProjectWebMercatorToCaliforniaStatePlaneVI(DbGeometry inputGeometry)
        {
            var wkb = inputGeometry.AsBinary();

            NetTopologySuite.IO.WKBReader reader = new NetTopologySuite.IO.WKBReader();
            Geometry internalGeometry            = (Geometry)reader.Read(wkb);

            double[] pointArray = new double[internalGeometry.Coordinates.Count() * 2];
            double[] zArray     = new double[1];
            zArray[0] = 1;

            int counterX = 0;
            int counterY = 1;

            foreach (var coordinate in internalGeometry.Coordinates)
            {
                pointArray[counterX] = coordinate.X;
                pointArray[counterY] = coordinate.Y;

                counterX = counterX + 2;
                counterY = counterY + 2;
            }

            Reproject.ReprojectPoints(pointArray, zArray, WebMercator, CaStatePlane, 0, (pointArray.Length / 2));

            counterX = 0;
            counterY = 1;
            foreach (var coordinate in internalGeometry.Coordinates)
            {
                coordinate.X = pointArray[counterX];
                coordinate.Y = pointArray[counterY];

                counterX = counterX + 2;
                counterY = counterY + 2;
            }

            var outputWkb = internalGeometry.AsBinary();

            return(DbGeometry.FromBinary(outputWkb, NAD_83_HARN_CA_ZONE_VI_SRID));
        }
Ejemplo n.º 8
0
 public static DbGeography GeographyFromGeometry(DbGeometry ogrGeometry)
 {
     // Not enforcing 4326 here, but am explicitly passing through relevant starting CoordinateSystemId.
     return(DbGeography.FromBinary(ogrGeometry.AsBinary(), ogrGeometry.CoordinateSystemId));
 }
Ejemplo n.º 9
0
 public static SqlGeometry ToSqlGeometry(this DbGeometry dbGeometry)
 {
     // Not enforcing 4326 here, but am explicitly passing through relevant starting CoordinateSystemId.
     return(SqlGeometry.STGeomFromWKB(new SqlBytes(dbGeometry.AsBinary()), dbGeometry.CoordinateSystemId));
 }
 public static IGeometryObject ToGeoJSONGeometry(this DbGeometry dbGeometry)
 {
     return(WkbDecode.Decode(dbGeometry.AsBinary()));
 }
Ejemplo n.º 11
0
 public static DbGeography GeographyFromGeometry(DbGeometry ogrGeometry)
 {
     return(DbGeography.FromBinary(ogrGeometry.AsBinary()));
 }