Exemplo n.º 1
0
        public void GdalRasterTest()
        {
            var rp     = new GdalRasterProvider();
            var raster = rp.Open(@"Data\Grids\sample_geotiff.tif");
            var at     = new AffineTransform(raster.Bounds.AffineCoefficients);

            Assert.AreEqual(at.CellCenterToProj(0, 0), new Coordinate(-179.9499969, 89.9499969)); // correct location from sample_geotiff.tfw
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            // gdal等必要なdllをまとめて参照したいので
            SetDllDirectory(@"D:\DotSpatial-master\Source\bin\Debug\Windows Extensions\DotSpatial.Data.Rasters.GdalExtension\gdal\x86");

            string savefilepath = @"D:\test.tif";

            int    ncol      = 3; // 水平方向ピクセル数
            int    nrow      = 3; // 鉛直方向ピクセル数
            double nodata    = -9999;
            double cellsize  = 10;
            double xllcorner = 20000;
            double yllcorner = -40000;

            // https://epsg.io/6677 から
            string prj =
                @"PROJCS[""unnamed"",
                    GEOGCS[""GRS 1980(IUGG, 1980)"",
                    DATUM[""unknown"",
                            SPHEROID[""GRS80"", 6378137, 298.257222101]],
                        PRIMEM[""Greenwich"", 0],
                        UNIT[""degree"", 0.0174532925199433]],
                    PROJECTION[""Transverse_Mercator""],
                    PARAMETER[""latitude_of_origin"", 36],
                    PARAMETER[""central_meridian"", 139.8333333333333],
                    PARAMETER[""scale_factor"", 0.9999],
                    PARAMETER[""false_easting"", 0],
                    PARAMETER[""false_northing"", 0],
                    UNIT[""Meter"", 1],
                    AUTHORITY[""epsg"", ""6677""]]";

            // GeoTiff書き出し
            GdalRasterProvider d   = new GdalRasterProvider(); // 無意味なようで無意味ではないです。拡張子の登録しています
            IRaster            dst = Raster.CreateRaster(savefilepath, null, ncol, nrow, 1, typeof(float), new[] { string.Empty });

            dst.NoDataValue      = nodata;
            dst.ProjectionString = prj;
            dst.Bounds           = new RasterBounds(nrow, ncol, new double[] { xllcorner, cellsize, 0, yllcorner, 0, -1 * cellsize });

            dst.Value[0, 0] = 32;   // H11
            dst.Value[0, 1] = 64;   // H12
            dst.Value[0, 2] = 128;  // H13
            dst.Value[1, 0] = 16;   // H21
            dst.Value[1, 1] = 0;    // H22
            dst.Value[1, 2] = 1;    // H23
            dst.Value[2, 0] = 8;    // H31
            dst.Value[2, 1] = 4;    // H32
            dst.Value[2, 2] = 2;    // H33

            dst.Save();

            return;
        }
Exemplo n.º 3
0
        public static IRaster OpenRaster(string dataPath)
        {
            IRaster            dataSet            = null;
            string             extension          = Path.GetExtension(dataPath);
            GdalRasterProvider gdalRasterProvider = new GdalRasterProvider();

            if (GetSupportedExtensions(gdalRasterProvider.DialogReadFilter).Contains(extension))
            {
                dataSet = gdalRasterProvider.Open(dataPath);
                return(dataSet);
            }
            return(dataSet);
        }
Exemplo n.º 4
0
        public void CanCreateMultiPartPolygons(string file)
        {
            var target   = new RasterToPolygon();
            var p        = new GdalRasterProvider();
            var raster   = p.Open(file);
            var outShape = new PolygonShapefile {
                Filename = FileTools.GetTempFileName(".shp")
            };

            target.Execute(raster, outShape, new MockProgressHandler());
            FileTools.DeleteShapeFile(outShape.Filename);

            var mpCount = outShape.Features.Count(t => t.Geometry is MultiPolygon);

            Assert.That(mpCount > 0);
        }
Exemplo n.º 5
0
        public void CanOpenRasterAfterClose()
        {
            var rasterFileName = FileTools.GetTempFileName(".tif");
            var p      = new GdalRasterProvider();
            var raster = p.Create(rasterFileName, null, 20, 20, 1, typeof(float), new[] { string.Empty });

            raster.Close();

            try
            {
                using (var openTif = File.Open(rasterFileName, FileMode.Open)) Assert.IsNotNull(openTif);
            }
            finally
            {
                File.Delete(rasterFileName);
            }
        }
Exemplo n.º 6
0
        public void NoMultiPartPolygonsWithConnectionGrid(string rasterFile, string flowDirectionGridFile)
        {
            var p                 = new GdalRasterProvider();
            var raster            = p.Open(rasterFile);
            var flowDirectionGrid = p.Open(flowDirectionGridFile);

            var target   = new RasterToPolygon();
            var outShape = new PolygonShapefile {
                Filename = FileTools.GetTempFileName(".shp")
            };

            target.Execute(raster, flowDirectionGrid, outShape, new MockProgressHandler());
            FileTools.DeleteShapeFile(outShape.Filename);

            var mpCount = outShape.Features.Count(t => t.Geometry is MultiPolygon);

            Assert.That(mpCount == 0);
        }
Exemplo n.º 7
0
        static void Main(string[] args)
        {
            System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
            sw.Start();
            try
            {
                SetDllDirectory(gdal_wrap.dllが置いてあるフォルダパス。例えば@ "~\DotSpatial-master\Source\bin\x64\Release\gdal\x64" とか);

                string loadfilepath = 読み込むファイルパス;
                string savefilepath = 書き込みファイルパス;

                // GeoTiff読み込み
                GdalRasterProvider d   = new GdalRasterProvider();
                IRaster            src = d.Open(loadfilepath);
                int    ncol            = src.NumColumns; // 水平方向ピクセル数
                int    nrow            = src.NumRows;    // 鉛直方向ピクセル数
                int    band_num        = src.NumBands;   // バンド数
                string prj             = src.ProjectionString;
                double nodata          = src.NoDataValue;

                double[] pGT        = src.Bounds.AffineCoefficients;
                double   xllcenter  = pGT[0];
                double   cellsize_x = pGT[1];
                // pGT[2] # 回転 今回は使わない
                double yllcenter = pGT[3];
                // pGT[4] # 回転 今回は使わない
                double cellsize_y = pGT[5];

                IRaster dst = Raster.CreateRaster(savefilepath, src.DriverCode, ncol - 2, nrow - 2, 1, src.DataType, new[] { string.Empty });
                dst.NoDataValue      = nodata;
                dst.ProjectionString = prj;
                dst.Bounds           = new RasterBounds(nrow - 2, ncol - 2,
                                                        new double[] { xllcenter + cellsize_x / 2, cellsize_x, 0, yllcenter + cellsize_y / 2, 0, cellsize_y });

                Hoi(src.Value, nodata, xllcenter, yllcenter, cellsize_x, nrow, ncol, dst.Value);

                dst.Save();
            }
            finally
            {
                sw.Stop();
                Console.WriteLine(sw.Elapsed.ToString() + " 終了しました");
            }
        }
Exemplo n.º 8
0
        public void SaveAsTest()
        {
            const string GridDataFolder = @"Data\Grids\";
            var          p                 = new GdalRasterProvider();
            var          sourceGrid        = p.Open(GridDataFolder + @"elev_cm_ESRI\elev_cm_clip2\hdr.adf");
            var          sourceGridMaximum = sourceGrid.Maximum;

            const string SavedGridName = GridDataFolder + @"elev_cm.tif";

            sourceGrid.SaveAs(SavedGridName);

            Assert.AreEqual(sourceGrid.Maximum, sourceGridMaximum, 0.0001);

            var savedSourceGrid = Raster.Open(SavedGridName);

            Assert.AreEqual(sourceGridMaximum, savedSourceGrid.Maximum, 0.0001);

            sourceGrid.Close();
            savedSourceGrid.Close();
            File.Delete(SavedGridName);
        }
Exemplo n.º 9
0
        public void SaveAsTest()
        {
            string gridDataFolder = Common.AbsolutePath(@"Data\Grids\");
            var    p                 = new GdalRasterProvider();
            var    sourceGrid        = p.Open(Path.Combine(gridDataFolder, @"elev_cm_ESRI\elev_cm_clip2\hdr.adf"));
            var    sourceGridMaximum = sourceGrid.Maximum;

            string savedGridName = Path.Combine(gridDataFolder, @"elev_cm.tif");

            sourceGrid.SaveAs(savedGridName);

            Assert.AreEqual(sourceGrid.Maximum, sourceGridMaximum, 0.0001);

            var savedSourceGrid = Raster.Open(savedGridName);

            Assert.AreEqual(sourceGridMaximum, savedSourceGrid.Maximum, 0.0001);

            sourceGrid.Close();
            savedSourceGrid.Close();
            File.Delete(savedGridName);
        }
Exemplo n.º 10
0
        static void Main(string[] args)
        {
            SetDllDirectory(@"D:\DotSpatial-master\Source\bin\Debug\Windows Extensions\DotSpatial.Data.Rasters.GdalExtension\gdal\x86");

            string loadfilepath = @"D:\test.tif";

            // GeoTiff読み込み
            GdalRasterProvider d   = new GdalRasterProvider();
            IRaster            src = d.Open(loadfilepath);

            int    ncol     = src.NumColumns;
            int    nrow     = src.NumRows;
            int    band_num = src.NumBands;
            string prj      = src.ProjectionString;
            double nodata   = src.NoDataValue;

            double[] pGT        = src.Bounds.AffineCoefficients;
            double   xllcenter  = pGT[0]; // コーナーではないので注意
            double   cellsize_x = pGT[1];
            double   rotate1    = pGT[2];
            double   yllcenter  = pGT[3]; // コーナーではないので注意
            double   rotate2    = pGT[4];
            double   cellsize_y = pGT[5];

            Console.WriteLine("H11 = " + src.Value[0, 0]);
            Console.WriteLine("H12 = " + src.Value[0, 1]);
            Console.WriteLine("H13 = " + src.Value[0, 2]);
            Console.WriteLine("H21 = " + src.Value[1, 0]);
            Console.WriteLine("H22 = " + src.Value[1, 1]);
            Console.WriteLine("H23 = " + src.Value[1, 2]);
            Console.WriteLine("H31 = " + src.Value[2, 0]);
            Console.WriteLine("H32 = " + src.Value[2, 1]);
            Console.WriteLine("H33 = " + src.Value[2, 2]);

            Console.ReadKey();
            return;
        }