【问题标题】:convert raster (GeoTiff) to ascii with no numbers after decimal将栅格(GeoTiff)转换为小数点后没有数字的 ascii
【发布时间】:2019-08-19 19:54:55
【问题描述】:

我有一个光栅文件 (GeoTiff)。我在R中将其转换为ASCII。转换后,小数点后有很多数字。我想制作一个只有整数值的 ASCII。

writeRaster(a, sigdig = 0, filename = "K:/ouput/test.asc")

screenshot of my ascii

【问题讨论】:

    标签: r decimal ascii raster


    【解决方案1】:

    你可以在你想要四舍五入的向量上使用round() 吗?

    【讨论】:

    • 我试过这个并且它工作,除了第一个数字是 1042.000000000000000。相反,我只希望它为 1042。
    【解决方案2】:

    你可以的

    writeRaster(round(a), filename = "K:/ouput/test.asc")
    

    让我们尝试一些示例数据

    library(raster)
    a <- raster(ncol=2, nrow=2, xmn=0, xmx=1, ymn=0, ymx=1)
    values(a) <- 1:4 * pi
    

    现在读写文件

    writeRaster(a, filename = "test.asc")
    readLines("test.asc")
    #[1] "NCOLS 2 "                           "NROWS 2 "                          
    #[3] "XLLCORNER 0 "                       "YLLCORNER 0 "                      
    #[5] "CELLSIZE 0.5 "                      "NODATA_value -9999 "               
    #[7] "3.141592653589790 6.28318530717959" "9.42477796076938 12.5663706143592" 
    

    第一轮,然后读写

    ra <- round(a)
    writeRaster(ra, filename = "test2.asc")
    readLines("test2.asc")
    #[1] "NCOLS 2 "            "NROWS 2 "            "XLLCORNER 0 "       
    #[4] "YLLCORNER 0 "        "CELLSIZE 0.5 "       "NODATA_value -9999 "
    #[7] "3.000000000000000 6" "9 13"               
    

    第一个值仍然有小数(避免实际值被截断的技巧),但其他值没有。

    【讨论】:

    • 如何摆脱这个第一价值问题?
    猜你喜欢
    • 2016-11-17
    • 2016-08-14
    • 1970-01-01
    • 2018-04-04
    • 1970-01-01
    • 2021-09-19
    • 1970-01-01
    • 2013-11-04
    • 2011-09-06
    相关资源
    最近更新 更多