Raster不创建正确的输出文件(.gri和.grd文件),而不是.tif文件。

huangapple go评论104阅读模式
英文:

Raster doesn't create right output file (.gri and .grd file) instead of an .tif file

问题

I'm using Rstudio on a Mac. I use the Dismo and Raster packages to set the levels of the resolution, extent and crs all equal for different files.

整体脚本工作正常,但输出应为.tif文件,但RStudio将输出导出为.gri和.grd文件。

Did I miss installing an extra package to have the right output or has it something to do with using a Mac version of RStudio because the original script was written on a Windows version?

是否我错过了安装额外包以获得正确的输出,或者与在Windows版本上编写的原始脚本有关,因为我在Mac版本的RStudio上使用它?

Code:

  1. > library(raster)
  2. > #Put all your raster files in the working directory, including BedCrop provided
  3. > # load in your raster environmental files
  4. > RastList<-list.files(getwd(), pattern = ".tif")
  5. > r <- lapply(RastList, raster)
  6. > r # The extents, crs, resolution might be different for each raster
  7. [[1]]
  8. class : RasterLayer
  9. dimensions : 4320, 8640, 37324800 (nrow, ncol, ncell)
  10. resolution : 0.04166667, 0.04166667 (x, y)
  11. extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
  12. crs : +proj=longlat +datum=WGS84 +no_defs
  13. source : Annual precipitation_wc2.1_2.5m_bio_12.tif
  14. names : Annual.precipitation_wc2.1_2.5m_bio_12
  15. values : 0, 11246 (min, max)
  16. [[2]]
  17. class : RasterLayer
  18. dimensions : 2395, 3379, 8092705 (nrow, ncol, ncell)
  19. resolution : 0.0306, 0.018 (x, y)
  20. extent : -41.28155, 62.11585, 31.29335, 74.40335 (xmin, xmax, ymin, ymax)
  21. crs : +proj=longlat +datum=WGS84 +no_defs
  22. source : BedCrop.tif
  23. names : BedCrop
  24. values : 0, 8200 (min, max)
  25. [[3]]
  26. class : RasterLayer
  27. dimensions : 4320, 8640, 37324800 (nrow, ncol, ncell)
  28. resolution : 0.04166667, 0.04166667 (x, y)
  29. extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
  30. crs : +proj=longlat +datum=WGS84 +no_defs
  31. source : Elevation_wc2.1_2.5m_elev.tif
  32. names : Elevation_wc2.1_2.5m_elev
  33. values : -415, 7412 (min, max)
  34. [[4]]
  35. class : RasterLayer
  36. dimensions : 4320, 8640, 37324800 (nrow, ncol, ncell)
  37. resolution : 0.04166667, 0.04166667 (x, y)
  38. extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
  39. crs : +proj=longlat +datum=WGS84 +no_defs
  40. source : Temp_JAN_wc2.1_2.5m_tavg_01.tif
  41. names : Temp_JAN_wc2.1_2.5m_tavg_01
  42. values : -46.096, 34.1 (min, max)
  43. > # Choose a raster with the resolution, extent you want and crs=wgs84, use it as a template
  44. > template <- r[[2]] #I choose you BedCrop!
  45. > #Reproject and mask each raster to the same CRS and extent as your template
  46. > r[[1]] <- projectRaster(r[[1]], template) #repeat for each layer of the raster stack (r[[3]], r[[4]]...)
  47. Warning message:
  48. In projectRaster(r[[1]], template) : input and ouput crs are the same
  49. > r[[1]]<-mask(r[[1]], template)
  50. > r[[3]] <- projectRaster(r[[3]], template)
  51. Warning message:
  52. In projectRaster(r[[3]], template) : input and ouput crs are the same
  53. > r[[3]]<-mask(r[[3]], template)
  54. > r[[4]] <- projectRaster(r[[4]], template)
  55. Warning message:
  56. In projectRaster(r[[4]], template) : input and ouput crs are the same
  57. > r[[4]]<-mask(r[[4]], template)
  58. > r #check they have the same extent, resolution, crs
  59. [[1]]
  60. class : RasterLayer
  61. dimensions : 2395, 3379, 8092705 (nrow, ncol, ncell)
  62. resolution : 0.0306, 0.018 (x, y)
  63. extent : -41.28155, 62.11585, 31.29335, 74.40335 (xmin, xmax, ymin, ymax)
  64. crs : +proj=longlat +datum=WGS84 +no_defs
  65. source : memory
  66. names : Annual precipitation_wc2.1_2.5m_bio_12
  67. values : 107.9981, 2992.399 (min, max)
  68. [[2]]
  69. class : RasterLayer
  70. dimensions : 2395, 3379, 8092705 (nrow, ncol, ncell)
  71. resolution : 0.0306, 0.018 (x, y)
  72. extent : -41.28155, 62.11585, 31.29335, 74.40335 (xmin, xmax, ymin, ymax)
  73. crs : +proj=longlat +datum=WGS84 +no_defs
  74. source : BedCrop.tif
  75. names : BedCrop
  76. values : 0, 8200 (min, max)
  77. [[3]]
  78. class : RasterLayer
  79. dimensions : 2395, 3379, 8092705 (nrow, ncol, ncell)
  80. resolution : 0.0306, 0.018 (x, y)
  81. extent : -41.28155, 62.11585, 31.29335, 74.40335 (xmin, xmax, ymin, ymax)
  82. crs : +proj=longlat +datum=WGS84 +no_defs
  83. source : memory
  84. names : Elevation_wc2.1_2.5m_elev
  85. values : -5.991715,
  86. <details>
  87. <summary>英文:</summary>
  88. I&#39;m using Rstudio on a Mac. I use the Dismo and Raster packages to set the levels of the resolution, extent and crs all equal for different files.
  89. The whole script works however but the output should be a .tif file however RStudio exports the output as a .gri and .grd file.
  90. Did I miss installing an extra package to have the right output or has it something to do with using a Mac version of RStudio because the original script was written on a Windows version?
  91. Code:
  92. ```&gt; library(dismo)
  93. &gt; library(raster)
  94. &gt; #Put all your raster files in the working directory, including BedCrop provided
  95. &gt; # load in your raster environmental files
  96. &gt; RastList&lt;-list.files(getwd(), pattern = &quot;.tif&quot;)
  97. &gt; r &lt;- lapply(RastList, raster)
  98. &gt; r # The extents, crs, resolution might be different for each raster
  99. [[1]]
  100. class : RasterLayer
  101. dimensions : 4320, 8640, 37324800 (nrow, ncol, ncell)
  102. resolution : 0.04166667, 0.04166667 (x, y)
  103. extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
  104. crs : +proj=longlat +datum=WGS84 +no_defs
  105. source : Annual precipitation_wc2.1_2.5m_bio_12.tif
  106. names : Annual.precipitation_wc2.1_2.5m_bio_12
  107. values : 0, 11246 (min, max)
  108. [[2]]
  109. class : RasterLayer
  110. dimensions : 2395, 3379, 8092705 (nrow, ncol, ncell)
  111. resolution : 0.0306, 0.018 (x, y)
  112. extent : -41.28155, 62.11585, 31.29335, 74.40335 (xmin, xmax, ymin, ymax)
  113. crs : +proj=longlat +datum=WGS84 +no_defs
  114. source : BedCrop.tif
  115. names : BedCrop
  116. values : 0, 8200 (min, max)
  117. [[3]]
  118. class : RasterLayer
  119. dimensions : 4320, 8640, 37324800 (nrow, ncol, ncell)
  120. resolution : 0.04166667, 0.04166667 (x, y)
  121. extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
  122. crs : +proj=longlat +datum=WGS84 +no_defs
  123. source : Elevation_wc2.1_2.5m_elev.tif
  124. names : Elevation_wc2.1_2.5m_elev
  125. values : -415, 7412 (min, max)
  126. [[4]]
  127. class : RasterLayer
  128. dimensions : 4320, 8640, 37324800 (nrow, ncol, ncell)
  129. resolution : 0.04166667, 0.04166667 (x, y)
  130. extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
  131. crs : +proj=longlat +datum=WGS84 +no_defs
  132. source : Temp_JAN_wc2.1_2.5m_tavg_01.tif
  133. names : Temp_JAN_wc2.1_2.5m_tavg_01
  134. values : -46.096, 34.1 (min, max)
  135. &gt; # Choose a raster with the resolution, extent you want and crs=wgs84, use it as a template
  136. &gt; template &lt;- r[[2]] #I choose you BedCrop!
  137. &gt; #Reproject and mask each raster to the same CRS and extent as your template
  138. &gt; r[[1]] &lt;- projectRaster(r[[1]], template) #repeat for each layer of the raster stack (r[[3]], r[[4]]...)
  139. Warning message:
  140. In projectRaster(r[[1]], template) : input and ouput crs are the same
  141. &gt; r[[1]]&lt;-mask(r[[1]], template)
  142. &gt; r[[3]] &lt;- projectRaster(r[[3]], template)
  143. Warning message:
  144. In projectRaster(r[[3]], template) : input and ouput crs are the same
  145. &gt; r[[3]]&lt;-mask(r[[3]], template)
  146. &gt; r[[4]] &lt;- projectRaster(r[[4]], template)
  147. Warning message:
  148. In projectRaster(r[[4]], template) : input and ouput crs are the same
  149. &gt; r[[4]]&lt;-mask(r[[4]], template)
  150. &gt; r #check they have the same extent, resolution, crs
  151. [[1]]
  152. class : RasterLayer
  153. dimensions : 2395, 3379, 8092705 (nrow, ncol, ncell)
  154. resolution : 0.0306, 0.018 (x, y)
  155. extent : -41.28155, 62.11585, 31.29335, 74.40335 (xmin, xmax, ymin, ymax)
  156. crs : +proj=longlat +datum=WGS84 +no_defs
  157. source : memory
  158. names : Annual.precipitation_wc2.1_2.5m_bio_12
  159. values : 107.9981, 2992.399 (min, max)
  160. [[2]]
  161. class : RasterLayer
  162. dimensions : 2395, 3379, 8092705 (nrow, ncol, ncell)
  163. resolution : 0.0306, 0.018 (x, y)
  164. extent : -41.28155, 62.11585, 31.29335, 74.40335 (xmin, xmax, ymin, ymax)
  165. crs : +proj=longlat +datum=WGS84 +no_defs
  166. source : BedCrop.tif
  167. names : BedCrop
  168. values : 0, 8200 (min, max)
  169. [[3]]
  170. class : RasterLayer
  171. dimensions : 2395, 3379, 8092705 (nrow, ncol, ncell)
  172. resolution : 0.0306, 0.018 (x, y)
  173. extent : -41.28155, 62.11585, 31.29335, 74.40335 (xmin, xmax, ymin, ymax)
  174. crs : +proj=longlat +datum=WGS84 +no_defs
  175. source : memory
  176. names : Elevation_wc2.1_2.5m_elev
  177. values : -5.991715, 3844.619 (min, max)
  178. [[4]]
  179. class : RasterLayer
  180. dimensions : 2395, 3379, 8092705 (nrow, ncol, ncell)
  181. resolution : 0.0306, 0.018 (x, y)
  182. extent : -41.28155, 62.11585, 31.29335, 74.40335 (xmin, xmax, ymin, ymax)
  183. crs : +proj=longlat +datum=WGS84 +no_defs
  184. source : memory
  185. names : Temp_JAN_wc2.1_2.5m_tavg_01
  186. values : -16.45166, 16.23138 (min, max)
  187. &gt; # 1: &quot;Name file&quot; + &quot;Reprojected&quot;
  188. &gt; writeRaster(r[[1]], &quot;Annual precipitation_Reprojected_wc2.1_2.5m_bio_12 &quot;, type=&quot;tif&quot;) #repeat for each layer, with correct name ```
  189. </details>
  190. # 答案1
  191. **得分**: 1
  192. `writeRaster`函数没有`type =`参数。正确的参数是`format = 'GTiff'`。或者,您可以直接在文件名中指定类型:
  193. ```R
  194. writeRaster(r[[1]], "Annual precipitation_Reprojected_wc2.1_2.5m_bio_12.tif")
英文:

writeRaster does not have a type = argument. The correct argument to use is format = &#39;GTiff&#39;. Alternatively, just specify the type in the file name:

  1. writeRaster(r[[1]], &quot;Annual precipitation_Reprojected_wc2.1_2.5m_bio_12.tif&quot;)

huangapple
  • 本文由 发表于 2023年2月26日 21:14:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75572228.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定