R 奇怪的问题将范围投影从UTM更改为latlon(WGS84),然后再次更改为UTM

R 奇怪的问题将范围投影从UTM更改为latlon(WGS84),然后再次更改为UTM,r,gis,r-raster,rgdal,map-projections,R,Gis,R Raster,Rgdal,Map Projections,我正在尝试以下步骤(我为错误道歉,但这是第一次)。我将光栅dtm.temp表示为: > dtm.temp class : RasterLayer dimensions : 668, 965, 644620 (nrow, ncol, ncell) resolution : 64.9, 92.6 (x, y) extent : 437230.1, 499858.6, 5138842, 5200699 (xmin, xmax, ymin, ymax) coord

我正在尝试以下步骤(我为错误道歉,但这是第一次)。我将光栅dtm.temp表示为:

> dtm.temp
class       : RasterLayer 
dimensions  : 668, 965, 644620  (nrow, ncol, ncell)
resolution  : 64.9, 92.6  (x, y)
extent      : 437230.1, 499858.6, 5138842, 5200699  (xmin, xmax, ymin, ymax)
coord. ref. : +init=epsg:32632 +proj=utm +zone=32 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0 
data source : in memory
names       : elev 
values      : 362.8404, 3584.865  (min, max)
使用这个区段,我用以下方式将其转换为longlat

# I build a spatial dataframe with extent data from dtm.temp
df <- data.frame(ID = 1:2, X = c(dtm.temp@extent@xmin, dtm.temp@extent@xmax),
                           Y = c(dtm.temp@extent@ymin, dtm.temp@extent@ymax))
coordinates(df) <- c("X", "Y")
crs_text <- crs(dtm.temp, asText=TRUE) # extracting crs from dtm.temp 
proj4string(df) <- CRS(crs_text) 
ext.lonlat <- spTransform(df, CRS("+proj=longlat +datum=WGS84"))
ext.lonlat

> ext.lonlat
class       : SpatialPointsDataFrame 
features    : 2 
extent      : 8.183449, 8.998142, 46.40024, 46.95982  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
variables   : 1
names       : ID 
min values  :  1 
max values  :  2 
然后

误差(以米为单位)为:

我肯定我做错了什么,但我要发疯了,试图理解

有人这么有耐心来帮助我?提前谢谢

我在Mike的评论的基础上添加了更多的信息,试图澄清。 使用不同的方法(更简单但计算耗时),我遵循以下步骤:

    > dtm.temp.extent <- extent(dtm.temp) # is the extent fixed
    > dtm.temp.extent
    class       : Extent 
    xmin        : 437230.1 
    xmax        : 499858.6 
    ymin        : 5138842 
    ymax        : 5200699 
# then I project directly the second raster dtm.temp1 using the crs 
# with projectRaster (very slow)
    > dtm.temp1.proj <- projectRaster(dtm.temp1,crs=crs_text)
# then I crop to the fixed extent
    > dtm.temp1.proj.crop <- crop(dtm.temp1.proj, dtm.temp.extent)
# this is what I obtain        
    > extent(dtm.temp)
    class       : Extent 
    xmin        : 437230.1 
    xmax        : 499858.6 
    ymin        : 5138842 
    ymax        : 5200699 
    > extent(dtm.temp1.proj.crop)
    class       : Extent 
    xmin        : 437233.6 
    xmax        : 499852 
    ymin        : 5138857 
    ymax        : 5200688 
第一种方法的原因只是因为我试图加速代码(第二种方法非常耗时)

编辑)我添加了第一个工作流中问题的图像(范围的两个点-LL和UR-远离光栅,即使在与问题中的一个不同的光栅上),以及Mkennedy建议的解决方案,这可能对某些人有用(使用四个点取消投影光栅的所有四个角,LL、UL、UR、LR,然后取8个值中的最小值/最大值)。我没有捕捉到仅使用LL和UR坐标取消投影到lat/lon时发生的旋转。取真正的最小值/最大值,结果更接近(如第二个工作流中所示)


我添加了一堆
dtm.temp1.crop
,以便光栅化值可见。谢谢你,迈克。不幸的是,我预计误差的大小与光栅的分辨率相同。为了回答你的问题,范围在两个不同的光栅化中定义了一个地理参考的空间区域(使用不同的单元分辨率)。光栅化(然后是单元大小、别名分辨率)会发生变化,但范围应与分辨率的功能保持几乎相同(例如,此处所示)。它与更精细(秒)位图的顺序相同,但要低得多(因子约为15)比原始位图大。这有什么奇怪的吗?这些都是非线性变换(我认为)。你的论点是合理的。这让我有些怀疑,我不是GIS专家,但如果我改变方法,首先用
>投影光栅直接投影dtm.temp1(dtm.temp1,crs=crs\u text)
,在UTM中,我检查了错误变得更合理的程度…我不明白为什么…那么错误有多大?我添加了一堆
dtm.temp1.crop
,以便光栅化值可见。谢谢你,迈克。不幸的是,我预期的错误大小与光栅的分辨率相同。尝试回答您的问题范围在两种不同的光栅化(具有不同的单元分辨率)中定义了一个地理参考的空间区域。光栅化(然后是单元大小、别名分辨率)会发生变化,但范围应与分辨率的函数保持几乎相同(如图所示。这是相同的精细度(秒)顺序)位图,但它比原始位图低得多(大约15倍)。这真的令人惊讶吗?这些都是非线性变换(我认为)。你的论点是合理的。这让我有些怀疑,我不是一个GIS专家,但如果我改变方法,先用
>直接投影dtm.temp1,这是事实吗(dtm.temp1,crs=crs_text)
,在UTM中,我检查了误差变得更合理的程度…我不明白为什么…那么误差有多大?
dtm.temp1.crop <- crop(dtm.temp1, ext.lonlat)

> dtm.temp1.crop
class       : RasterLayer 
dimensions  : 2015, 2933, 5909995  (nrow, ncol, ncell)
resolution  : 0.0002777779, 0.0002777772  (x, y)
extent      : 8.183473, 8.998195, 46.40014, 46.95986  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
 # crs_text as defined above 
  dtm.temp1.crop.proj <- projectRaster(dtm.temp1.crop, crs=crs_text)

>dtm.temp1.crop.proj
class       : RasterLayer 
dimensions  : 2033, 2968, 6033944  (nrow, ncol, ncell)
resolution  : 21.2, 30.9  (x, y)
extent      : 437083.4, 500005, 5138362, 5201182  (xmin, xmax, ymin, ymax)
coord. ref. : +init=epsg:32632 +proj=utm +zone=32 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0 
data source : in memory
names       : elev 
values      : 360, 3603.655  (min, max)
extent dtm.temp           : 437230.1, 499858.6, 5138842, 5200699
extent dtm.temp1.crop.proj: 437083.4, 500005,   5138362, 5201182 
> 437230.1 - 437083.4
[1] 146.7
> 499858.6-500005
[1] -146.4
> 5138842 - 5138362
[1] 480
> 5200699-5201182
[1] -483
    > dtm.temp.extent <- extent(dtm.temp) # is the extent fixed
    > dtm.temp.extent
    class       : Extent 
    xmin        : 437230.1 
    xmax        : 499858.6 
    ymin        : 5138842 
    ymax        : 5200699 
# then I project directly the second raster dtm.temp1 using the crs 
# with projectRaster (very slow)
    > dtm.temp1.proj <- projectRaster(dtm.temp1,crs=crs_text)
# then I crop to the fixed extent
    > dtm.temp1.proj.crop <- crop(dtm.temp1.proj, dtm.temp.extent)
# this is what I obtain        
    > extent(dtm.temp)
    class       : Extent 
    xmin        : 437230.1 
    xmax        : 499858.6 
    ymin        : 5138842 
    ymax        : 5200699 
    > extent(dtm.temp1.proj.crop)
    class       : Extent 
    xmin        : 437233.6 
    xmax        : 499852 
    ymin        : 5138857 
    ymax        : 5200688 
> 437230.1 - 437233.6 
[1] -3.5
> 499858.6 - 499852
[1] 6.6
> 5138842 - 5138857 
[1] -15
> 5200699 - 5200688 
[1] 11