【问题标题】:How to convert Sentinel-3 .nc-file into .tiff-file?如何将 Sentinel-3 .nc 文件转换为 .tiff 文件?
【发布时间】:2020-01-03 11:34:45
【问题描述】:

关于将 .nc 文件转换为 .tiff 文件时,我遇到了丢失像素地理信息的问题。我知道其他用户也遇到了同样的问题,并试图通过 kotlin 解决,但失败了。我更喜欢使用 R 的解决方案。请参阅此处了解 kotlin 方法 URL:https://gis.stackexchange.com/questions/259700/converting-sentinel-3-data-netcdf-to-geotiff

我下载了免费提供的 ESA Sentinel-3 数据 (URL:https://scihub.copernicus.eu/dhus/#/home)。不幸的是,此数据以 .nc 格式提供,因此我想将其转换为 .tiff 格式。我已经尝试了各种方法,但都失败了。到目前为止我尝试过的:

data_source <- 'D:/user_1/01_test_data/S3A_SL_1_RBT____20180708T093240_20180708T093540_20180709T141944_0179_033_150_2880_LN2_O_NT_003.SEN3/F1_BT_in.nc'
# define path to .nc-file

data_output <- 'D:/user_1/01_test_data/S3A_SL_1_RBT____20180708T093240_20180708T093540_20180709T141944_0179_033_150_2880_LN2_O_NT_003.SEN3/test.tif'
# define path of output .tiff-file


###################################################
# 1.) use gdal_translate via Windows cmd-line in R
# see here URL:https://stackoverflow.com/questions/52046282/convert-netcdf-nc-to-geotiff

system(command = paste('gdal_translate -of GTiff -sds -a_srs epsg:4326', data_source, data_output))
# hand over character string to Windows cmd-line to use gdal_translate


###################################################
# 2.) use the raster-package
# see here URL:https://www.researchgate.net/post/How_to_convert_a_NetCDF4_file_to_GeoTIFF_using_R2

epsg4326 <- "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"
# proj4-code
# URL:https://spatialreference.org/ref/epsg/wgs-84/proj4/

specific_band <- raster(data_source)
crs(specific_band) <- epsg4326
writeRaster(specific_band, filename = data_output)


# both approaches work, i can convert the files from .nc-format into .tiff-format, but i **loose the geoinformation for the pixels** and just get pixel coordinates instead of longlat-values.

我非常感谢任何保留像素地理信息的解决方案!

非常感谢,ExploreR

【问题讨论】:

  • 你能提供一个示例文件的链接吗?
  • 是的,当然,只需将此数据集的 url 作为示例 URL:scihub.copernicus.eu/dhus/odata/v1/…
  • 需要用户名和密码
  • 是的,这是正确的,我很抱歉。但是您可以注册欧洲航天局 (ESA) 的哥白尼中心。它是免费的,您可以访问比我所指的更有趣的数据集......甚至还有一个由 J. Schwalb-Willmann 提供的 R 包(getSpatialData),自然在一篇工具箱文章中提到,您可以在其中可以很方便地下载哥白尼数据。网址:github.com/16EAGLE/getSpatialData

标签: r raster tiff netcat ncdf4


【解决方案1】:

作为@j08lue points out

Sentinel 3 产品的产品格式很糟糕。是的,数据 值存储在 netCDF 中,但坐标轴是分开的 文件,它只是一堆文件和元数据。

我没有找到任何文档(我认为它必须存在),但您似乎可以像这样获取数据:

library(ncdf4)
# coordinates
nc <- nc_open("geodetic_in.nc")
lon <- ncvar_get(nc, "longitude_in")
lat <- ncvar_get(nc, "latitude_in")
# including elevation for sanity check only
elv <- ncvar_get(nc, "elevation_in")
nc_close(nc)

# the values of interest
nc <- nc_open("F1_BT_in.nc")
F1_BT <- ncvar_get(nc, "F1_BT_in")
nc_close(nc)

# combine 
d <- cbind(as.vector(lon), as.vector(lat), as.vector(elv), as.vector(F1_BT_in))

绘制位置样本。注意光栅是旋转的

plot(d[sample(nrow(d), 25000),1:2], cex=.1)

我需要进行更多调查才能了解如何编写旋转栅格。

目前,不推荐快捷方式可能是光栅化为非旋转光栅

e <- extent(as.vector(apply(d[,1:2],2, range))) + 1/120
r <- raster(ext=e, res=1/30)
#elev <- rasterize(d[,1:2], r, d[,3], mean)
F1_BT <- rasterize(d[,1:2], r, d[,4], mean, filename="")
plot(F1_BT)

【讨论】:

    【解决方案2】:

    这就是我到目前为止所做的 - 不幸的是,光栅并没有以某种方式旋转 180 度,而是以另一种方式扭曲......

    # (1.) first part of the code adapted to Robert Hijmans approach (see code of answer provided above)
    
    nc_geodetic <- nc_open(paste0(wd, "/01_test_data/sentinel_3/geodetic_in.nc"))
    nc_geodetic_lon <- ncvar_get(nc_geodetic, "longitude_in")
    nc_geodetic_lat <- ncvar_get(nc_geodetic, "latitude_in")
    nc_geodetic_elv <- ncvar_get(nc_geodetic, "elevation_in")
    nc_close(nc_geodetic)
    # to get the longitude, latitude and elevation information
    
    F1_BT_in_vars <- nc_open(paste0(wd, "/01_test_data/sentinel_3/F1_BT_in.nc"))
    F1_BT_in <- ncvar_get(F1_BT_in_vars, "F1_BT_in")
    nc_close(F1_BT_in_vars)
    # extract the band information
    
    
    
    ###############################################################################
    # (2.) following part of the code is adapted to @Matthew Lundberg rotation-code see URL:https://stackoverflow.com/questions/16496210/rotate-a-matrix-in-r
    
    rotate_fkt <- function(x) t(apply(x, 2, rev))
    # create rotation-function
    
    F1_BT_in_rot180 <- rotate_fkt(rotate_fkt(F1_BT_in))
    # rotate raster by 180degree 
    
    test_F1_BT_in <- raster(F1_BT_in_rot180)
    # convert matrix to raster
    
    
    
    ###############################################################################
    # (3.) extract corner coordinates and transform with gdal
    
    writeRaster(test_F1_BT_in, filename = paste0(wd, "/01_test_data/sentinel_3/test_flip.tif"), overwrite = TRUE)
    # write the raster layer
    
    data_source_flip <- '"D:/unknown_user/X_processing/01_test_data/sentinel_3/test_flip.tif"'
    data_tmp_flip <- '"D:/unknown_user/X_processing/01_test_data/temp/test_flip.tif"'
    data_out_flip <- '"D:/unknown_user/X_processing/01_test_data/sentinel_3/test_flip_ref.tif"'
    # define input, temporary output and output for gdal-transformation
    
    nrow_nc_mtx <- nrow(nc_geodetic_lon)
    ncol_nc_mtx <- ncol(nc_geodetic_lon)
    # investigate on matrix size of the image
    
    xy_coord_char1 <- as.character(paste("1", "1", nc_geodetic_lon[1, 1], nc_geodetic_lat[1, 1]))
    xy_coord_char2 <- as.character(paste(nrow_nc_mtx, "1", nc_geodetic_lon[nrow_nc_mtx, 1], nc_geodetic_lat[nrow_nc_mtx, 1]))
    xy_coord_char3 <- as.character(paste(nrow_nc_mtx, ncol_nc_mtx, nc_geodetic_lon[nrow_nc_mtx, ncol_nc_mtx], nc_geodetic_lat[nrow_nc_mtx, ncol_nc_mtx]))
    xy_coord_char4 <- as.character(paste("1", ncol_nc_mtx, nc_geodetic_lon[1, ncol_nc_mtx], nc_geodetic_lat[1, ncol_nc_mtx]))
    # extract the corner coordinates from the image
    
    system(command = paste('gdal_translate -of GTiff -gcp ', xy_coord_char1, ' -gcp ', xy_coord_char2, ' -gcp ', xy_coord_char3, ' -gcp ', xy_coord_char4, data_source_flip, data_tmp_flip))
    system(command = paste('gdalwarp -r near -order 1 -co COMPRESS=NONE ', data_tmp_flip,  data_out_flip))
    # run gdal-transformation
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-21
      • 2012-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多