Example #1
0
 public static Dataset AutoCreateWarpedVRT(Dataset src_ds, string src_wkt, string dst_wkt, ResampleAlg eResampleAlg, double maxerror)
 {
     IntPtr cPtr = GdalPINVOKE.AutoCreateWarpedVRT(Dataset.getCPtr(src_ds), src_wkt, dst_wkt, (int)eResampleAlg, maxerror);
     Dataset ret = (cPtr == IntPtr.Zero) ? null : new Dataset(cPtr, true, ThisOwn_true());
     if (GdalPINVOKE.SWIGPendingException.Pending) throw GdalPINVOKE.SWIGPendingException.Retrieve();
     return ret;
 }
Example #2
0
        /// <summary>
        /// Initilizes default fields.
        /// </summary>
        /// <param name="inputFile">FullName of input GeoTIFF.</param>
        /// <param name="outputDirectory">FullName of output directory.</param>
        /// <param name="minZ">Minimum zoom.</param>
        /// <param name="maxZ">Maxmimum zoom.</param>
        /// <param name="resampling">Resampling method.</param>
        private static void Initialize(string inputFile,
                                       string outputDirectory,
                                       int minZ,
                                       int maxZ,
                                       ResampleAlg resampling)
        {
            InputFile       = inputFile;
            OutputDirectory = outputDirectory;
            MinZ            = minZ;
            MaxZ            = maxZ;
            Resampling      = resampling;
            QuerySize       = 4 * TileSize;

            // Open the input file
            Dataset inputDataset = Gdal.Open(InputFile, Access.GA_ReadOnly);

            DataBandsCount = inputDataset.RasterCount;

            // Read the georeference
            double[] outGeoTransform = new double[6];
            inputDataset.GetGeoTransform(outGeoTransform);
            OutGeoTransform = outGeoTransform;

            // Set x/y sizes of dataset
            RasterXSize = inputDataset.RasterXSize;
            RasterYSize = inputDataset.RasterYSize;

            // Dispose InputDataset
            inputDataset.Dispose();

            // Generate dictionary with min max tile coordinates for all zoomlevels
            foreach (int zoom in Enumerable.Range(MinZ, MaxZ - MinZ + 1))
            {
                double xMin = OutGeoTransform[0];
                double yMin = OutGeoTransform[3] - RasterYSize * OutGeoTransform[1];
                double xMax = OutGeoTransform[0] + RasterXSize * OutGeoTransform[1];
                double yMax = OutGeoTransform[3];

                int[] lonLatToTile = GetTileNumbersFromCoords(xMin, yMin, xMax, yMax, TileSize, zoom);
                int   tileMinX     = lonLatToTile[0];
                int   tileMinY     = lonLatToTile[1];
                int   tileMaxX     = lonLatToTile[2];
                int   tileMaxY     = lonLatToTile[3];

                // crop tiles extending world limits (+-180,+-90)
                tileMinX = Math.Max(0, tileMinX);
                tileMinY = Math.Max(0, tileMinY);
                tileMaxX = Math.Min(Convert.ToInt32(Math.Pow(2, zoom + 1)) - 1, tileMaxX);
                tileMaxY = Math.Min(Convert.ToInt32(Math.Pow(2, zoom)) - 1, tileMaxY);
                MinMax.Add(zoom, new[] { tileMinX, tileMinY, tileMaxX, tileMaxY });
            }
        }
Example #3
0
 /// <summary>
 /// Crops input GeoTIFF to tiles.
 /// Important: You should call your <see cref="GdalConfiguration.ConfigureGdal"/> method from Gdal.NET package BEFORE using this method.
 /// </summary>
 /// <param name="inputFile">FullName of input GeoTIFF.</param>
 /// <param name="outputDirectory">FullName of output directory.</param>
 /// <param name="minZ">Minimum zoom.</param>
 /// <param name="maxZ">Maxmimum zoom.</param>
 /// <param name="resampling">Resampling method.</param>
 public static void CropTifToTiles(string inputFile,
                                   string outputDirectory,
                                   int minZ,
                                   int maxZ,
                                   ResampleAlg resampling)
 {
     Initialize(inputFile, outputDirectory, minZ, maxZ, resampling);
     GenerateBaseTiles();
     CreateBaseTile();
     CreateOverviewTiles();
     MinMax.Clear();
     foreach (Dictionary <string, int> d in Metadata)
     {
         d.Clear();
     }
     Metadata.Clear();
 }
Example #4
0
 /// <summary>
 /// Scales down query dataset to the tile dataset.
 /// </summary>
 /// <param name="queryDataset">Source dataset.</param>
 /// <param name="tileDataset">Destination dataset.</param>
 /// <param name="resampling">Resampling method.</param>
 private static void ScaleQueryToTile(Dataset queryDataset, Dataset tileDataset, ResampleAlg resampling)
 {
     queryDataset.SetGeoTransform(new[]
     {
         0.0,
         Convert.ToSingle(tileDataset.RasterXSize) / queryDataset.RasterXSize,
         0.0, 0.0, 0.0,
         Convert.ToSingle(tileDataset.RasterXSize) / queryDataset.RasterXSize
     });
     tileDataset.SetGeoTransform(new[] { 0.0, 1.0, 0.0, 0.0, 0.0, 1.0 });
     Gdal.ReprojectImage(queryDataset, tileDataset, null, null, resampling, 0.0, 0.0, null, null, null);
 }
Example #5
0
        private void 栅格数据重投影ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //选择投影变换图层
            SelectLayer selFrm = new SelectLayer(mapControl);

            selFrm.Text              = "选择变换图层";
            selFrm.label2.Text       = "选择栅格图层";
            selFrm.label1.Visible    = false;
            selFrm.comboBox1.Visible = false;
            selFrm.label3.Visible    = true;
            selFrm.comboBox3.Visible = true;
            selFrm.comboBox3.Items.Add("NearestNeighbour");
            selFrm.comboBox3.Items.Add("Bilinear");
            selFrm.comboBox3.Items.Add("Cubic");
            selFrm.comboBox3.Items.Add("CubicSpline");
            if (selFrm.ShowDialog(this) == DialogResult.OK)
            {
                int index = 0;
                for (int i = 0; i < mapControl._MapLayers.Count(); i++)
                {
                    if (mapControl._MapLayers[i].Name == selFrm.cb2)
                    {
                        index = i;
                        break;
                    }
                }
                //选择输出文件路径
                SaveFileDialog sfd = new SaveFileDialog();
                sfd.Title  = "选择重投影文件存储路径";
                sfd.Filter = @"Tiff(*.tif)|*.tif";
                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    string   path  = sfd.FileName; //文件路径
                    string[] path2 = path.Split(new char[1] {
                        '\\'
                    });                                               //文件名
                    string name = path2[path2.Count() - 1];
                    //投影转换
                    TransformProject transform = new TransformProject();
                    float[]          MBR       = new float[4];
                    mapControl._MapLayers[index].GetExtent(MBR);
                    double[] dbx = { (double)MBR[0], (double)MBR[0], (double)MBR[2], (double)MBR[2] };
                    double[] dby = { (double)MBR[1], (double)MBR[3], (double)MBR[1], (double)MBR[3] };
                    Dataset  ds  = Gdal.Open(mapControl._MapLayers[index].FilePath, Access.GA_ReadOnly);
                    //重采样方式
                    ResampleAlg re = ResampleAlg.GRA_NearestNeighbour;
                    if (selFrm.cb3 == "Bilinear")
                    {
                        re = ResampleAlg.GRA_Bilinear;
                    }
                    else if (selFrm.cb3 == "Cubic")
                    {
                        re = ResampleAlg.GRA_Cubic;
                    }
                    else if (selFrm.cb3 == "CubicSpline")
                    {
                        re = ResampleAlg.GRA_CubicSpline;
                    }
                    transform.TransformTiff(ds, dbx, dby, path, re);
                    //转换结果添加至地图
                    mapControl.AddTiffLayer(path, name);
                    if (tVLayers.Nodes.Count == 0)
                    {
                        tVLayers.Nodes.Add("图层");
                    }
                    tVLayers.Nodes[0].Nodes.Insert(0, name);
                    tVLayers.ExpandAll();
                    tVLayers.SelectedNode = tVLayers.Nodes[0].Nodes[0];
                    MessageBox.Show("成功转换至Web墨卡托投影!", "投影转换结果");
                }
            }
        }
Example #6
0
        public static Dataset AutoCreateWarpedVRT(Dataset src_ds, string src_wkt, string dst_wkt, ResampleAlg eResampleAlg, double maxerror)
        {
            IntPtr  cPtr = GdalPINVOKE.AutoCreateWarpedVRT(Dataset.getCPtr(src_ds), src_wkt, dst_wkt, (int)eResampleAlg, maxerror);
            Dataset ret  = (cPtr == IntPtr.Zero) ? null : new Dataset(cPtr, true, ThisOwn_true());

            if (GdalPINVOKE.SWIGPendingException.Pending)
            {
                throw GdalPINVOKE.SWIGPendingException.Retrieve();
            }
            return(ret);
        }
Example #7
0
        public static CPLErr ReprojectImage(Dataset src_ds, Dataset dst_ds, string src_wkt, string dst_wkt, ResampleAlg eResampleAlg, double WarpMemoryLimit, double maxerror, Gdal.GDALProgressFuncDelegate callback, string callback_data)
        {
            CPLErr ret = (CPLErr)GdalPINVOKE.ReprojectImage(Dataset.getCPtr(src_ds), Dataset.getCPtr(dst_ds), src_wkt, dst_wkt, (int)eResampleAlg, WarpMemoryLimit, maxerror, callback, callback_data);

            if (GdalPINVOKE.SWIGPendingException.Pending)
            {
                throw GdalPINVOKE.SWIGPendingException.Retrieve();
            }
            return(ret);
        }
Example #8
0
 public static CPLErr ReprojectImage(Dataset src_ds, Dataset dst_ds, string src_wkt, string dst_wkt, ResampleAlg eResampleAlg, double WarpMemoryLimit, double maxerror, Gdal.GDALProgressFuncDelegate callback, string callback_data)
 {
     CPLErr ret = (CPLErr)GdalPINVOKE.ReprojectImage(Dataset.getCPtr(src_ds), Dataset.getCPtr(dst_ds), src_wkt, dst_wkt, (int)eResampleAlg, WarpMemoryLimit, maxerror, callback, callback_data);
     if (GdalPINVOKE.SWIGPendingException.Pending) throw GdalPINVOKE.SWIGPendingException.Retrieve();
     return ret;
 }
Example #9
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();
        }