如何在R中将UTM坐标转换为经纬度 [英] How to convert UTM coordinates to lat and long in R

查看:228
本文介绍了如何在R中将UTM坐标转换为经纬度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试运行物种分布模型,并且需要创建背景点来运行我的逻辑回归模型.我刚刚创建了500个randomPoint,但是它们位于UTM坐标中,因此我需要纬度和经度.有没有办法将它们转换为lat并在R中加长?如果可以,您可以和我分享代码吗?我对R很陌生.谢谢!

解决方案

如果您需要长/纬度,则可能应该使用该坐标参考系生成随机点.但是,否则,您可以执行以下操作.

我首先生成一组示例坐标()

 库(terra)set.seed(1)x<-runif(10,-10000,10000)y<-runif(10,-10000,10000)点<-cbind(x,y) 

现在,假设您知道坐标参考系统(CRS),您的点,您可以创建一个空间对象并分配该已知的CRS.在我的示例中,这些点位于 UTM,区域10,datum = WGS84 投影中.

  library(terra)v<-vect(点,crs ="+ proj = utm + zone = 10 +基准= WGS84 + units = m")v#class:SpatVector#几何:点#尺寸:10,0(几何,属性)#范围:-8764.275,8893.505,-6468.865,9838.122(xmin,xmax,ymin,ymax)#协调.参考:+ proj = utm + zone = 10 + datum = WGS84 + units = m + no_defs 

现在,我们可以将其转换为另一个CRS,例如,转换为经度/纬度

  y<-project(v,"+ proj = longlat + datum = WGS84")ÿ#class:SpatVector#几何:点#尺寸:10,0(几何,属性)#范围:-127.5673,-127.4091,-0.05834327、0.08873723(xmin,xmax,ymin,ymax)#协调.参考:+ proj = longlat + datum = WGS84 + no_defs 

您可以像这样提取坐标

  lonlat<-geom(y)[,c("x","y"))]头(lonlat,3)#x y#[1,] -127.5308 -0.0530354276#[2,] -127.5117 -0.0583432750#[3,] -127.4757 0.0337371933 

您当然也可以做相反的操作

  back<-项目(y,``+ proj = utm + zone = 10 + datum = WGS84 + units = m'') 

使用 sf 包或旧的 sp 包可以完成相同的操作.使用 sp 创建一个 SpatialPoints 对象,然后使用 spTransform .

 库(rgdal)痰<-SpatialPoints(点,proj4string = CRS("+ proj = utm + zone = 10 + datum = WGS84"))spgeo <- spTransform(sputm, CRS("+proj=longlat +datum=WGS84"))lnlt<-坐标(spgeo) 

在示例中,我使用了UTM区域10.但是请注意,有60个UTM区域,您必须选择一个.每个覆盖(360/60 =)6度的条带.如果数据跨很多经度或跨越UTM区域,则不应使用UTM.对于[-180,180)之间的经度,您可以像这样计算所需的区域

  utm_zone<-函数(经度){trunc((180 +经度/6)+1}长<-c(-122,-119,-118)utm_zone(min(longs))#[1] 10utm_zone(max(longs))#[1] 11utm_zone(max(longs)) 

或者看看类似此地图

的地图在使用虚假北向"时,与UTM的混淆点在于:对于南半球的位置,请避免使用负坐标.这是通过在y坐标上添加10,000,000来完成的,如下所示,使用 + south 元素.

  s<-vect(cbind(174,-44),crs ="+ proj = longlat + datum = WGS84")geom(project(s,"+ proj = utm + zone = 59"))[,c("x","y")]#x y#740526.3 -4876249.1geom(project(s,"+ proj = utm + south + zone = 59"))[,c("x","y")]#x y#740526.3 5123750.9 

还要注意,我使用了"PROJ4"定义CRS的符号.如果使用的基准(基准是地球表面形状的模型)为WGS84或NAD83,则效果很好.如果不是这种情况,则需要使用"EPSG"标记.代码或"WKT2"您的CRS的说明.

I am trying to run a species distribution model and need to create background points to run my logistic regression model. I have just created 500 randomPoints but they are in UTM coordinates and I need lat and long. Is there a way to convert them to lat and long in R? If so, can you share the code with me? I am fairly new to R. Thanks!

解决方案

If you need long/lat you should probably generate the random points using that coordinate reference system. But otherwise you can do something like the below.

I first generate an example set of coordinates (points)

 library(terra)
 set.seed(1)
 x <- runif(10, -10000, 10000)   
 y <- runif(10, -10000, 10000)   
 points <- cbind(x, y)

Now, assuming you know the coordinate reference system (CRS) of your points, you can create a spatial object and assign that known CRS. In my example, the points are in the UTM, zone 10, datum=WGS84 projection.

library(terra)
v <- vect(points, crs="+proj=utm +zone=10 +datum=WGS84  +units=m")
v
# class       : SpatVector 
# geometry    : points 
# dimensions  : 10, 0  (geometries, attributes)
# extent      : -8764.275, 8893.505, -6468.865, 9838.122  (xmin, xmax, ymin, ymax)
# coord. ref. : +proj=utm +zone=10 +datum=WGS84 +units=m +no_defs 

Now we can transform these to another CRS, for example to longitude/latitude

y <- project(v, "+proj=longlat +datum=WGS84")
y
# class       : SpatVector 
# geometry    : points 
# dimensions  : 10, 0  (geometries, attributes)
# extent      : -127.5673, -127.4091, -0.05834327, 0.08873723  (xmin, xmax, ymin, ymax)
# coord. ref. : +proj=longlat +datum=WGS84 +no_defs 
 

And you can extract the coordinates like this

lonlat <- geom(y)[, c("x", "y")]
head(lonlat, 3)
#             x             y
#[1,] -127.5308 -0.0530354276
#[2,] -127.5117 -0.0583432750
#[3,] -127.4757  0.0337371933

You can of course also do the inverse

back <- project(y, "+proj=utm +zone=10 +datum=WGS84 +units=m")

The same thing can be done with the sf package, or with the old sp package. With sp, create a SpatialPoints object and use spTransform.

library(rgdal)
sputm <- SpatialPoints(points, proj4string=CRS("+proj=utm +zone=10 +datum=WGS84")) 
spgeo <- spTransform(sputm, CRS("+proj=longlat +datum=WGS84"))
lnlt <- coordinates(spgeo)
 

I used UTM zone 10 in the example. But note that there are 60 UTM zones and you have to pick one. Each covers a strip of (360/60=) 6 degrees. You should not use UTM if your data spans a lot of longitude or crosses UTM zones. For a longitude between [-180, 180) You can compute the zone you need like this

utm_zone <- function(longitude) { trunc((180 + longitude) / 6) + 1 }

longs <- c(-122,-119, -118)
utm_zone(min(longs))
# [1] 10
utm_zone(max(longs))
# [1] 11

utm_zone(max(longs))

Or have a look at a map like this one

A point of confusion with UTM in the use of "false northings" for locations in the Southern hemisphere, to avoid negative coordinates. This is done by adding 10,000,000 to the y coordinates, as illustrated below using the +south element.

s <- vect(cbind(174, -44), crs="+proj=longlat +datum=WGS84")
geom(project(s, "+proj=utm +zone=59"))[, c("x", "y")]
#       x          y 
#740526.3 -4876249.1 

geom(project(s, "+proj=utm +south +zone=59"))[, c("x", "y")]
#       x         y 
#740526.3 5123750.9 

Also note that I use the "PROJ4" notation to define the CRS. That works fine if the datum used (a datum is a model of the shape of the earth's surface) is WGS84 or NAD83. If that is not the case you would need to use an "EPSG" code or a "WKT2" description of your CRS.

这篇关于如何在R中将UTM坐标转换为经纬度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆