在R中将XY转换为lat long

在R中将XY转换为lat long,r,sf,r-leaflet,r-mapedit,R,Sf,R Leaflet,R Mapedit,我试图在传单地图上标出属于某一地区的港口的位置。这30多个区域是使用mapedit包作为sf多边形创建的。其中一个地区的代码西海岸Indiax(印度西海岸)如下: structure(list(X_leaflet_id = 629L, feature_type = "polygon", geometry = structure(list(structure(list(structure(c(-298.1964, -298.4601, -298.3722, -297.6855,

我试图在传单地图上标出属于某一地区的港口的位置。这30多个区域是使用
mapedit
包作为
sf
多边形创建的。其中一个地区的代码
西海岸Indiax
(印度西海岸)如下:

structure(list(X_leaflet_id = 629L, feature_type = "polygon", 
    geometry = structure(list(structure(list(structure(c(-298.1964, 
    -298.4601, -298.3722, -297.6855, -293.9062, -293.4009, -292.8296, 
    -292.1265, -291.7529, -289.3359, -288.1934, -289.248, -289.0723, 
    -287.7539, -284.8535, -286.04, -283.0957, -282.3157, -282.4255, 
    -282.9529, -283.4253, -284.0295, -284.9634, -285.8643, -286.7651, 
    -286.7432, -286.875, -286.9519, -288.2922, -288.6328, -290.05, 
    -291.8958, -292.8955, -293.4009, -294.6863, -298.1964, 25.6861, 
    24.8565, 24.682, 24.8765, 25.1453, 24.5671, 23.8256, 23.5237, 
    22.6343, 19.6012, 20.0559, 12.5975, 7.1881, -1.1425, -0.791, 
    8.4941, 8.0484, 7.8416, 8.2441, 9.2973, 10.4338, 11.62, 14.094, 
    16.1197, 19.3215, 20.0456, 21.8411, 22.6748, 23.433, 24.4572, 
    24.7768, 25.0657, 25.3043, 25.8592, 26.116, 25.6861), .Dim = c(36L, 
    2L))), class = c("XY", "POLYGON", "sfg"))), class = c("sfc_POLYGON", 
    "sfc"), precision = 0, bbox = structure(c(xmin = -298.4601, 
    ymin = -1.1425, xmax = -282.3157, ymax = 26.116), class = "bbox"), crs = structure(list(
        epsg = 4326L, proj4string = "+proj=longlat +datum=WGS84 +no_defs"), class = "crs"), n_empty = 0L)), row.names = 1L, sf_column = "geometry", agr = structure(c(X_leaflet_id = NA_integer_, 
feature_type = NA_integer_), class = "factor", .Label = c("constant", 
"aggregate", "identity")), class = c("sf", "data.frame"))
st_geometry(WestCoastIndiax) = st_geometry(WestCoastIndiax) + c(360, 0)
st_crs(WestCoastIndiax) = 4326
该区域在
小册子中正确绘制

leaflet() %>% addTiles() %>% addPolygons(data=WestCoastIndiax)
然后,我尝试使用
sf
包中的
st_in()
函数来查看特定位置是否属于这些区域。孟买市的一个例子是,它确实属于这一地区:

st_within(st_point(c(72.49,18.54)),WestCoastIndiax)
返回:

Sparse geometry binary predicate list of length 1, where the predicate was `within'
 1: (empty)
当我尝试绘制多边形和孟买的坐标时,我得到的是:

leaflet() %>% addTiles() %>% addPolygons(data=WestCoastIndiax) %>% addMarkers(lng = 72.49, lat = 18.54)

我查看并收集了我需要的正确的
crs
,然后再次将其转换为4326。然而,我不知道这30多个几何体的坐标参考系应该是什么。我试过这个:

st_crs(WestCoastIndiax) <- "+init=epsg:3857 +proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +no_defs"
WestCoastIndiax <- st_transform(WestCoastIndiax, 4326)

非常感谢您的帮助。

您的数据在crs 4326中,好的!发生的情况是,使用mapedit创建多边形时,特征很可能绘制在错误的位置(即-180/180经度之外)。您可以通过从位于多边形内的点位置减去360°来检查这一点

mapview::mapview(tst) + sf::st_point(c(72.49-360,18.54))
因此,您需要通过添加360°来更新x坐标,或者在正确的位置重新绘制多边形。可以按如下方式更新多边形的坐标:

structure(list(X_leaflet_id = 629L, feature_type = "polygon", 
    geometry = structure(list(structure(list(structure(c(-298.1964, 
    -298.4601, -298.3722, -297.6855, -293.9062, -293.4009, -292.8296, 
    -292.1265, -291.7529, -289.3359, -288.1934, -289.248, -289.0723, 
    -287.7539, -284.8535, -286.04, -283.0957, -282.3157, -282.4255, 
    -282.9529, -283.4253, -284.0295, -284.9634, -285.8643, -286.7651, 
    -286.7432, -286.875, -286.9519, -288.2922, -288.6328, -290.05, 
    -291.8958, -292.8955, -293.4009, -294.6863, -298.1964, 25.6861, 
    24.8565, 24.682, 24.8765, 25.1453, 24.5671, 23.8256, 23.5237, 
    22.6343, 19.6012, 20.0559, 12.5975, 7.1881, -1.1425, -0.791, 
    8.4941, 8.0484, 7.8416, 8.2441, 9.2973, 10.4338, 11.62, 14.094, 
    16.1197, 19.3215, 20.0456, 21.8411, 22.6748, 23.433, 24.4572, 
    24.7768, 25.0657, 25.3043, 25.8592, 26.116, 25.6861), .Dim = c(36L, 
    2L))), class = c("XY", "POLYGON", "sfg"))), class = c("sfc_POLYGON", 
    "sfc"), precision = 0, bbox = structure(c(xmin = -298.4601, 
    ymin = -1.1425, xmax = -282.3157, ymax = 26.116), class = "bbox"), crs = structure(list(
        epsg = 4326L, proj4string = "+proj=longlat +datum=WGS84 +no_defs"), class = "crs"), n_empty = 0L)), row.names = 1L, sf_column = "geometry", agr = structure(c(X_leaflet_id = NA_integer_, 
feature_type = NA_integer_), class = "factor", .Label = c("constant", 
"aggregate", "identity")), class = c("sf", "data.frame"))
st_geometry(WestCoastIndiax) = st_geometry(WestCoastIndiax) + c(360, 0)
st_crs(WestCoastIndiax) = 4326
请注意,在添加偏移量时,我们会丢失crs信息,因此需要重新定义crs

我在mapedit中打开了一个问题/特征请求,以确保绘图只允许在sane longlat范围内