簡體   English   中英

GDAL中像素值的緯度和經度

[英]Latitude and Longitude to pixel-value in GDAL

我正在嘗試讀取一個帶有一個存儲在 0 和 3 之間的值的帶的 GeoTIFF( 255 不是 mak )。 我的目標是編寫一個小程序,它接受緯度/經度並返回 geotiff 中地理坐標處的擬合像素值。

我從這里下載了它: https ://dataverse.harvard.edu/dataset.xhtml?persistentId=doi:10.7910/DVN/QQHCIK 如果你想看看它......這里是下載鏈接。 https://drive.google.com/file/d/104De_YQN1V8tSbj2uhIPO0FfowjnInnX/view?usp=sharing

但是,我的代碼不起作用。 我不能告訴你出了什么問題,它只是每次都輸出錯誤的像素值。 我想要么我的地理坐標到像素的轉換是錯誤的,要么是一個巨大的邏輯錯誤。

這是我的第一次嘗試,它打印了錯誤的地理坐標值。 此外,它會因某些地理坐標而崩潰,負經度會使其崩潰,因為這將使pixelY負數,從而導致 raster 方法出現異常。

            GdalConfiguration.ConfigureGdal();
            var tif = "C:\\Users\\Lars\\Downloads\\pnv_biome.type_biome00k_c_1km_s0..0cm_2000..2017_v0.1.tif";
            var lat = 51.0;
            var lon = 8.3;
            using (var image = Gdal.Open(tif, Access.GA_ReadOnly)) {

                var bOneBand = image.GetRasterBand(1);
                var width = bOneBand.XSize;
                var height = bOneBand.YSize;
                
                // Geocoordinate ( lat, lon ) to pixel in the tiff ? 
                var geoTransform = new double[6];
                image.GetGeoTransform(geoTransform);
                Gdal.InvGeoTransform(geoTransform, geoTransform);
                
                var bOne = new int[1];
                Gdal.ApplyGeoTransform(geoTransform, lat, lon, out var pixelXF, out var pixelYF);

                var pixelX = (int)Math.Floor(pixelXF);
                var pixelY = (int)Math.Floor(pixelYF);
                
                // Read pixel 
                bOneBand.ReadRaster(pixelX, pixelY, 1, 1, bOne, 1, 1,0,0);
                Console.WriteLine(bOne[0]); // bOne[0] contains wrong data
            }

我的第二次嘗試如下所示......但也為給定坐標輸出錯誤的像素值。 它還與一些地理坐標崩潰。

GdalConfiguration.ConfigureGdal();
            var tif = "C:\\Users\\Lars\\Downloads\\pnv_biome.type_biome00k_c_1km_s0..0cm_2000..2017_v0.1.tif";
            var lat = 24.7377; // 131.5847
            var lon = 131.5847;
            
            using (var image = Gdal.Open(tif, Access.GA_ReadOnly)) {

                var bOneBand = image.GetRasterBand(1);
                var bOne = new int[1];
                
                // Spatial reference to transform latlng to map coordinates... from python code
                var point_srs = new SpatialReference("");
                var file_srs = new SpatialReference(image.GetProjection());
                point_srs.ImportFromEPSG(4326);
                point_srs.SetAxisMappingStrategy(AxisMappingStrategy.OAMS_TRADITIONAL_GIS_ORDER);
           
                var mapCoordinates = new double[2]{ lat, lon};
                var transform = new CoordinateTransformation(point_srs, file_srs);
                transform.TransformPoint(mapCoordinates);
                
                // Map coordinates to pixel coordinates ? 
                var geoTransform = new double[6];
                image.GetGeoTransform(geoTransform);
                Gdal.InvGeoTransform(geoTransform, geoTransform);
                
                Gdal.ApplyGeoTransform(geoTransform, mapCoordinates[0], mapCoordinates[1], out var pixelXF, out var pixelYF);
                var pixelX = (int)pixelXF;
                var pixelY = (int)pixelYF;
                
                bOneBand.ReadRaster(pixelX, pixelY, 1, 1, bOne, 1, 1, 0,0);
                Console.WriteLine(bOne[0]);  // bOne[0] contains wrong value
            }

我的代碼有什么問題,為什么輸出錯誤的像素值? 任何幫助表示贊賞!

也許您在應該是 lon/lat 的地方使用 lat/lon? 如果您打印一些值(例如mapCoordinates )會很有幫助。

以下是使用 R 執行此操作的方法,以進行比較:

library(terra)
r = rast("pnv_biome.type_biome00k_c_1km_s0..0cm_2000..2017_v0.1.tif")
xy = cbind(c(8.3, 131.5847), c(51.0, 24.7377))
extract(r, xy)
#  pnv_biome.type_biome00k_c_1km_s0..0cm_2000..2017_v0.1
#1                                                     9
#2                                                    NA

以及從零開始的行/列數

rowColFromCell(r, cellFromXY(r, xy)) - 1
#     [,1]  [,2]
#[1,] 4364 22596
#[2,] 7515 37390

您可以使用describe (又名 GDALinfo)來獲取一些元數據。 例如

d = describe("pnv_biome.type_biome00k_c_1km_s0..0cm_2000..2017_v0.1.tif")
d[50]
# [1] "Band 1 Block=43200x1 Type=Byte, ColorInterp=Gray"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM