在R中将XY转换为经纬度

时间:2018-07-29 12:57:36

标签: r sf r-leaflet r-mapedit

我正在尝试在单张地图上绘制落入特定区域的端口的位置。这30个奇数区域是使用mapedit包作为sf多边形创建的。一个这样的区域WestCoastIndiax(印度西海岸)的代码如下:

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"))

该区域在leaflet中正确绘制。

leaflet() %>% addTiles() %>% addPolygons(data=WestCoastIndiax)

然后,我尝试使用st_within()包中的sf函数来查看特定位置是否在这些区域内。确实属于该地区的孟买市的一个示例是:

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)

enter image description here

我看了一下this,并收集到我需要有正确的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)

现在bbox单位已更改,但它们不正确:

Geometry set for 1 feature 
geometry type:  POLYGON
dimension:      XY
bbox:           xmin: -0.002681113 ymin: -1.026325e-05 xmax: -0.002536085 ymax: 0.000234604
epsg (SRID):    4326
proj4string:    +proj=longlat +datum=WGS84 +no_defs
POLYGON ((-0.002678744 0.0002307422, -0.0026811...

请在这里感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

您的数据在crs 4326中,好!发生的情况很可能是在使用mapedit创建多边形时,在错误的位置绘制了要素(即,在-180/180经度之外)。您可以通过从点位置减去360°进行检查,然后将其放置在多边形内。

mapview::mapview(tst) + sf::st_point(c(72.49-360,18.54))

因此,您需要通过添加360°来更新x坐标,或者在正确的位置重新绘制多边形。您可以按以下方式更新多边形的坐标:

st_geometry(WestCoastIndiax) = st_geometry(WestCoastIndiax) + c(360, 0)
st_crs(WestCoastIndiax) = 4326

请注意,在添加偏移量时,我们会放宽crs信息,因此我们需要重新定义crs。

我在mapedit中打开了一个问题/功能请求,以确保仅在合理的经纬度范围内允许绘制图形。