Beispiel #1
0
        public static Extent TransformExtent(this CoordinateTransformation coordinateTransformation, Extent extent)
        {
            Extent destExtent = null;

            if (coordinateTransformation != null && extent != null)
            {
                double[] xs = { extent.MinX, extent.MaxX };
                double[] ys = { extent.MinY, extent.MaxY };
                double[] zs = { 0, 0 };
                coordinateTransformation.TransformPoints(2, xs, ys, zs);
                double minX, minY, maxX, maxY;
                if (xs[0] > xs[1])
                {
                    minX = xs[1];
                    maxX = xs[0];
                }
                else
                {
                    minX = xs[0];
                    maxX = xs[1];
                }
                if (ys[0] > ys[1])
                {
                    minY = ys[1];
                    maxY = ys[0];
                }
                else
                {
                    minY = ys[0];
                    maxY = ys[1];
                }
                destExtent = new Extent(minX, minY, maxX, maxY);
            }
            return(destExtent);
        }
Beispiel #2
0
        public static void Transform(Proj4Projection src, Proj4Projection dst,
                                     double[] x, double[] y, double[] z)
        {
            lock (lockObj)
            {
                //Proj4Projection.CheckInitialized(src);
                //Proj4Projection.CheckInitialized(dst);
                if (x == null)
                {
                    throw new ArgumentException("Argument is required", "x");
                }

                if (y == null)
                {
                    throw new ArgumentException("Argument is required", "y");
                }

                if (x.Length != y.Length || (z != null && z.Length != x.Length))
                {
                    throw new ArgumentException("Coordinate arrays must have the same length");
                }

                CoordinateTransformation trans = new CoordinateTransformation(src.srs, dst.srs);

                trans.TransformPoints(x.Length, x, y, z);
            }
        }
Beispiel #3
0
        // 将给定Tif转换为Web Mercator投影
        public void TransformTiff(Dataset ds, double[] VerticeX, double[] VerticeY, string FilePath, ResampleAlg eResampleMethod)
        {
            //获取Web墨卡托坐标系
            SpatialReference Mercator = new SpatialReference("");

            Mercator.ImportFromEPSG(3857);             // Web Mercator
            Mercator.SetMercator(0d, 0d, 1d, 0d, 0d);
            string MercatorWkt;

            Mercator.ExportToWkt(out MercatorWkt);
            //原栅格坐标信息
            SpatialReference Raster_spf = new SpatialReference(ds.GetProjectionRef());
            //影像四个顶点投影转换
            CoordinateTransformation coordinateTrans = Osr.CreateCoordinateTransformation(Raster_spf, Mercator);

            coordinateTrans.TransformPoints(4, VerticeX, VerticeY, null);            //VerticeX和VerticeY存储的是影像四个顶点坐标
            coordinateTrans.Dispose();
            //计算重投影后栅格顶点坐标
            double dbMinx = 0;
            double dbMaxx = 0;
            double dbMiny = 0;
            double dbMaxy = 0;

            dbMinx = Math.Min(Math.Min(Math.Min(VerticeX[0], VerticeX[1]), VerticeX[2]), VerticeX[3]);
            dbMaxx = Math.Max(Math.Max(Math.Max(VerticeX[0], VerticeX[1]), VerticeX[2]), VerticeX[3]);
            dbMiny = Math.Min(Math.Min(Math.Min(VerticeY[0], VerticeY[1]), VerticeY[2]), VerticeY[3]);
            dbMaxy = Math.Max(Math.Max(Math.Max(VerticeY[0], VerticeY[1]), VerticeY[2]), VerticeY[3]);
            //计算新的仿射变换参数
            double[] newTransform = new double[6];
            newTransform[0] = dbMinx;         //左上角点x坐标
            newTransform[3] = dbMaxy;         //左上角点y坐标
            newTransform[1] = 100;            //像素宽度
            newTransform[5] = -100;           //像素高度
            //计算大小
            int width  = (int)Math.Ceiling(Math.Abs(dbMaxx - dbMinx) / 100.0);
            int height = (int)Math.Ceiling(Math.Abs(dbMaxy - dbMiny) / 100.0);

            //创建新的栅格影像
            OSGeo.GDAL.Driver pGDalDriver = Gdal.GetDriverByName("GTiff");
            Dataset           poDataset   = pGDalDriver.Create(FilePath, width, height, 1, DataType.GDT_Float32, null);

            poDataset.SetGeoTransform(newTransform);
            poDataset.SetProjection(MercatorWkt);
            //重投影
            Gdal.ReprojectImage(ds, poDataset, ds.GetProjectionRef(), MercatorWkt, eResampleMethod, 0, 0, new Gdal.GDALProgressFuncDelegate(ProgressFunc), null, null);
            //设置NoData值
            Band band = poDataset.GetRasterBand(1);

            band.SetNoDataValue(-10000000);
            poDataset.FlushCache();
            poDataset.Dispose();
        }
Beispiel #4
0
        public static List <ICoordinate> TransformPoints(this CoordinateTransformation coordinateTransformation, IEnumerable <Coordinate> coords)
        {
            List <ICoordinate> destCoords = new List <ICoordinate>();

            if (coordinateTransformation != null && coords != null)
            {
                var firstCoord = coords.FirstOrDefault();
                if (firstCoord != null)
                {
                    var count     = coords.Count();
                    var dimension = firstCoord.Dimension;
                    if (dimension >= 2)
                    {
                        double[] xs = new double[count];
                        double[] ys = new double[count];
                        double[] zs = new double[count];
                        int      i  = 0;
                        foreach (var coord in coords)
                        {
                            xs[i] = coord[0];
                            ys[i] = coord[1];
                            if (dimension > 2)
                            {
                                zs[i] = coord[2];
                            }
                            i++;
                        }
                        coordinateTransformation.TransformPoints(count, xs, ys, zs);
                        for (int j = 0; j < xs.Length; j++)
                        {
                            var coord = new Coordinate(xs[j], ys[j]);
                            if (dimension > 2)
                            {
                                coord.Z = zs[j];
                            }
                            destCoords.Add(coord);
                        }
                    }
                }
            }
            return(destCoords);
        }
        private static void TransformWKT(object srcProj, object dstProj, EPSGType srcType, EPSGType dstType, ref string wkt)
        {
            try
            {
                GdalConfiguration.ConfigureOgr();
                SpatialReference src = new SpatialReference("");
                switch (srcType)
                {
                case EPSGType.OGC_WKT:
                    string srcProj_str = srcProj.ToString();
                    src.ImportFromWkt(ref srcProj_str);
                    break;

                case EPSGType.PROJ4:
                    src.ImportFromProj4(srcProj.ToString());
                    break;

                case EPSGType.EPSG_NUM:
                    src.ImportFromEPSG((int)srcProj);
                    break;
                }

                SpatialReference dst = new SpatialReference("");
                switch (dstType)
                {
                case EPSGType.OGC_WKT:
                    string dstProj_str = dstProj.ToString();
                    dst.ImportFromWkt(ref dstProj_str);
                    break;

                case EPSGType.PROJ4:
                    dst.ImportFromProj4(dstProj.ToString());
                    break;

                case EPSGType.EPSG_NUM:
                    dst.ImportFromEPSG((int)dstProj);
                    break;
                }

                CoordinateTransformation coordinate = Osr.CreateCoordinateTransformation(src, dst);
                string wktType = wkt.Split('(')[0];
                wkt = wkt.Split('(')[2].Split(')')[0];
                string[] splitWKT = wkt.Split(',');
                double[] xPoints  = new double[splitWKT.Length];
                double[] yPoints  = new double[splitWKT.Length];
                for (int i = 0; i < splitWKT.Length; i++)
                {
                    double.TryParse(splitWKT[i].Split(' ')[0], out xPoints[i]);
                    double.TryParse(splitWKT[i].Split(' ')[1], out yPoints[i]);
                }
                coordinate.TransformPoints(splitWKT.Length, xPoints, yPoints, null);

                wkt = wktType + "((";
                for (int i = 0; i < xPoints.Length; i++)
                {
                    wkt += xPoints[i] + " " + yPoints[i] + ",";
                }
                wkt  = wkt.Substring(0, wkt.Length - 1);
                wkt += "))";
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.ToString());
            }
        }