Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 从x和y猜测坐标系,以米为单位,并近似对应的长度和纬度_R_Coordinates_Latitude Longitude_Sp_Rgdal - Fatal编程技术网

R 从x和y猜测坐标系,以米为单位,并近似对应的长度和纬度

R 从x和y猜测坐标系,以米为单位,并近似对应的长度和纬度,r,coordinates,latitude-longitude,sp,rgdal,R,Coordinates,Latitude Longitude,Sp,Rgdal,我有比利时位置坐标的数据框: data # # A tibble: 11 x 4 # LON LAT x y # <dbl> <dbl> <dbl> <dbl> # 1 3.618942 50.68165 96227.01 152551.2 # 2 3.473466 50.55899 86074.26 138702.0 # 3 3.442395 50.6997

我有比利时位置坐标的数据框:

data
# # A tibble: 11 x 4
#         LON      LAT        x        y
#       <dbl>    <dbl>    <dbl>    <dbl>
#  1 3.618942 50.68165 96227.01 152551.2
#  2 3.473466 50.55899 86074.26 138702.0
#  3 3.442395 50.69979 84369.88 154860.5
#  4 3.293505 50.68766 73127.74 153413.9
#  5 3.352688 50.68009 77876.00 153115.7
#  6 3.567919 50.52372 93229.16 134975.7
#  7 3.333666 50.54388 76183.82 137373.9
#  8 3.394737 50.61322 80806.11 145230.0
#  9 3.410566 50.53073 82041.22 135743.9
# 10 3.613925 50.59610 96907.40 143057.9
# 11 3.502580 50.74147 88860.56 159399.0
转换功能

#' add UTM coordinates (x and y in km) from WGS84 coordinates (long and lat)
#'
#' @param data a data frame 
#' @param out_names names of x and y columns in output data frame
#' @param in_names names of longlat columns in input, by default searches for
#'   an unambiguous set of columns with names starting with "lat" and "lon"
#'    (case insensitive)
#' @param zone UTM zone, for Belgium it's 31 (the default), see 
#'   http://www.dmap.co.uk/utmworld.htm
#'
#' @return a data frame with 2 more columns
#' @export
#'
#' @examples
#' xy <- data.frame(ID = 1:2, longitude = c(118, 119), latitude = c(10, 50))
#  add_longlat(add_utm(xy))
add_utm <- function(data, out_names = c("x","y"), in_names = NULL, zone = 31){
  nms <- names(data)
  if(length(temp <- intersect(out_names,nms)))
    stop(paste("data already contains",paste(temp,collapse =" and ")))
  if(is.null(in_names)){
    lon_col <- grep("^lon",nms, ignore.case = TRUE,value = TRUE)
    lat_col <- grep("^lat",nms, ignore.case = TRUE,value = TRUE)
    if((n <- length(lon_col)) != 1)
      stop(sprintf(
        "%s columns have names starting with 'lon' (case insensitive)", n))
    if((n <- length(lat_col)) != 1)
      stop(sprintf(
        "%s columns have names starting with 'lon' (case insensitive)", n))
    in_names <- c(lon_col, lat_col)
  }
  new_data <- data[in_names]
  sp::coordinates(new_data) <- names(new_data)
  sp::proj4string(new_data) <- sp::CRS("+proj=longlat +datum=WGS84")  ## for example
  res <- sp::spTransform(new_data, sp::CRS(sprintf("+proj=utm +zone=%s ellps=WGS84",zone)))
  res <- setNames(as.data.frame(res), out_names)
  cbind(data,res)
}

#' add WGS84 coordinates (long and lat) from UTM coordinates (x and y in km)
#'
#' @param data a data frame 
#' @param out_names names of longitude and latitude columns in output data frame
#' @param in_names names of longlat columns in input, by default searches for
#'   an unambiguous set of columns with names starting with "x" and "y"
#'    (case insensitive)
#' @param zone UTM zone, for Belgium it's 31 (the default), see 
#'   http://www.dmap.co.uk/utmworld.htm
#'
#' @return a data frame with 2 more columns
#' @export
#'
#' @examples
#' xy <- data.frame(ID = 1:2, longitude = c(118, 119), latitude = c(10, 50))
#  add_longlat(add_utm(xy))
add_longlat <- function(data, out_names = c("LON","LAT"), in_names = NULL, zone = 31){
  nms <- names(data)
  if(length(temp <- intersect(out_names,nms)))
    stop(paste("data already contains",paste(temp,collapse =" and ")))
  if(is.null(in_names)){
    lon_col <- grep("^x",nms, ignore.case = TRUE,value = TRUE)
    lat_col <- grep("^y",nms, ignore.case = TRUE,value = TRUE)
    if((n <- length(lon_col)) != 1)
      stop(sprintf(
        "%s columns have names starting with 'x' (case insensitive)", n))
    if((n <- length(lat_col)) != 1)
      stop(sprintf(
        "%s columns have names starting with 'y' (case insensitive)", n))
    in_names <- c(lon_col, lat_col)
  }
  new_data <- data[in_names]
  sp::coordinates(new_data) <- names(new_data)
  sp::proj4string(new_data) <- sp::CRS(sprintf("+proj=utm +zone=%s ellps=WGS84",zone))  ## for example
  res <- sp::spTransform(new_data, sp::CRS("+proj=longlat +datum=WGS84"))
  res <- setNames(as.data.frame(res), out_names)
  cbind(data,res)
}
从WGS84坐标(长坐标和纬度坐标)中添加UTM坐标(x和y,单位为km) #' #“@param data a数据帧 #“@param out_指定输出数据框中x和y列的名称 #“@param in_指定输入中longlat列的名称,默认情况下搜索 #'一组名称以“lat”和“lon”开头的明确列' #'(不区分大小写) #“@param zone UTM zone,比利时为31(默认值),请参见 #' http://www.dmap.co.uk/utmworld.htm #' #“@返回另外两列的数据帧 #“@出口 #' #“@示例 #“xy我认为可能是最新版本或与之相近的版本,EPSG代码31370。我基本上是通过搜索a中的前几个EPSG代码列表来实现的,假设x_y坐标位于该CRS中,然后转换为WGS84以与纬度和经度进行比较。这是我的密码;您可以看到31370的均方根误差约为490m,这比我搜索的其他算法要小。(很明显,你可以扩大搜索范围,我尝试了一些,但这是我找到的最接近的)。注意使用
可能
来处理这样一个事实,即当没有找到EPSG代码的转换时,代码将出错,因为我无法找到所有可用代码的简单列表。我也不知道这里的预期精度是多少,因为你说lat lon点与未知crs点不完全相同。根据区域的大小和合理的采样模式,470米可能是一个关键点或一个指标,表明我们仍然很遥远

库(tidyverse)
图书馆(sf)
#>链接至GEOS 3.6.1、GDAL 2.1.3、项目4.9.3
数据%
as.numeric%>%
`^`(2) %>%
平均%>%
sqrt
}
可比性%
排列(rms\U区)
}
猜测(数据,c(“LON”、“LAT”)、c(“x”、“y”)、epsg(比利时)
#>#A tibble:13 x 2
#>crs rms_区
#>        
#>  1 31370     492.
#>  2  6190     492.
#>  3 21500     533.
#>  4 31300    1101.
#>  5  3447    1695.
#>  6  3812  706664.
#>  7 25832 5466924.
#>  8 25831 5478500.
#>  9  4809 9862261.
#> 10  4215 9882639.
#>118370 Inf
#>125710 Inf
#>134313NA

由(v0.2.1)于2019-02-08创建,我不确定我是否理解你的问题,但你在寻找x/y坐标系的原点,我是对的吗?不完全正确,我的最终目标是找到一个转换系统,但作为第一步,这个问题的目的是找到这个系统是如何命名的(这样我就可以查找如何转换它)。知道来源不会让我更接近我的目标。干得好!我将在周一确认它解决了我的问题,并将其标记为已回答。非常感谢!我无法复制它,我所有的距离都是
Inf
,我没有太多时间调查它,但我很可能会找出我自己,并将及时检查已回答的问题,而不会关于出错原因的信息我将
否则=Inf
放入
可能的
中,这样它会排序到底,也许这是最直接的原因?很可能是早些时候发生了错误,使得一切
Inf
问题来自于
st_distance
需要安装
lwgeom
,您使用的事实
可能
隐藏了此显式错误。安装它(无需附加它)解决了问题,我可以复制您的答案。感谢您的出色工作!我允许自己编辑一个
requireNamespace
调用,使其更加健壮
#' add UTM coordinates (x and y in km) from WGS84 coordinates (long and lat)
#'
#' @param data a data frame 
#' @param out_names names of x and y columns in output data frame
#' @param in_names names of longlat columns in input, by default searches for
#'   an unambiguous set of columns with names starting with "lat" and "lon"
#'    (case insensitive)
#' @param zone UTM zone, for Belgium it's 31 (the default), see 
#'   http://www.dmap.co.uk/utmworld.htm
#'
#' @return a data frame with 2 more columns
#' @export
#'
#' @examples
#' xy <- data.frame(ID = 1:2, longitude = c(118, 119), latitude = c(10, 50))
#  add_longlat(add_utm(xy))
add_utm <- function(data, out_names = c("x","y"), in_names = NULL, zone = 31){
  nms <- names(data)
  if(length(temp <- intersect(out_names,nms)))
    stop(paste("data already contains",paste(temp,collapse =" and ")))
  if(is.null(in_names)){
    lon_col <- grep("^lon",nms, ignore.case = TRUE,value = TRUE)
    lat_col <- grep("^lat",nms, ignore.case = TRUE,value = TRUE)
    if((n <- length(lon_col)) != 1)
      stop(sprintf(
        "%s columns have names starting with 'lon' (case insensitive)", n))
    if((n <- length(lat_col)) != 1)
      stop(sprintf(
        "%s columns have names starting with 'lon' (case insensitive)", n))
    in_names <- c(lon_col, lat_col)
  }
  new_data <- data[in_names]
  sp::coordinates(new_data) <- names(new_data)
  sp::proj4string(new_data) <- sp::CRS("+proj=longlat +datum=WGS84")  ## for example
  res <- sp::spTransform(new_data, sp::CRS(sprintf("+proj=utm +zone=%s ellps=WGS84",zone)))
  res <- setNames(as.data.frame(res), out_names)
  cbind(data,res)
}

#' add WGS84 coordinates (long and lat) from UTM coordinates (x and y in km)
#'
#' @param data a data frame 
#' @param out_names names of longitude and latitude columns in output data frame
#' @param in_names names of longlat columns in input, by default searches for
#'   an unambiguous set of columns with names starting with "x" and "y"
#'    (case insensitive)
#' @param zone UTM zone, for Belgium it's 31 (the default), see 
#'   http://www.dmap.co.uk/utmworld.htm
#'
#' @return a data frame with 2 more columns
#' @export
#'
#' @examples
#' xy <- data.frame(ID = 1:2, longitude = c(118, 119), latitude = c(10, 50))
#  add_longlat(add_utm(xy))
add_longlat <- function(data, out_names = c("LON","LAT"), in_names = NULL, zone = 31){
  nms <- names(data)
  if(length(temp <- intersect(out_names,nms)))
    stop(paste("data already contains",paste(temp,collapse =" and ")))
  if(is.null(in_names)){
    lon_col <- grep("^x",nms, ignore.case = TRUE,value = TRUE)
    lat_col <- grep("^y",nms, ignore.case = TRUE,value = TRUE)
    if((n <- length(lon_col)) != 1)
      stop(sprintf(
        "%s columns have names starting with 'x' (case insensitive)", n))
    if((n <- length(lat_col)) != 1)
      stop(sprintf(
        "%s columns have names starting with 'y' (case insensitive)", n))
    in_names <- c(lon_col, lat_col)
  }
  new_data <- data[in_names]
  sp::coordinates(new_data) <- names(new_data)
  sp::proj4string(new_data) <- sp::CRS(sprintf("+proj=utm +zone=%s ellps=WGS84",zone))  ## for example
  res <- sp::spTransform(new_data, sp::CRS("+proj=longlat +datum=WGS84"))
  res <- setNames(as.data.frame(res), out_names)
  cbind(data,res)
}