有没有办法解释(使用
sp
、sf
等)使用 R 中的美国国家网格表示的网格位置?
这是我想要转换的向量的示例:
locations <- c("17T PH 8482 7766", "18T XL 6257 1639", "18T XL 6244 1636", "18T XL 6267 1673", "18T XL 6272 1671", "18T XL 6272 1674")
理想情况下,我想要一种获取网格质心的纬度/经度的方法,但即使只是能够将它们作为 sf 对象读取也只是工作的一半。谢谢!
我尝试使用软件包
mgrs
(https://github.com/hrbrmstr/mgrs),但无法安装它。
我可以确认
hrbrmstr/mgrs
构建和安装得很好,至少在安装了 R 4.3.2 和 RTools43 的 Windows 10 计算机上是这样。但是,如果您仍然需要替代方案,您可以通过 V8
使用 JavaScript 库,例如https://github.com/proj4js/mgrs/。这有点矫枉过正,但如果您出于某种原因无法从源代码构建软件包,这也许是一个选择。
library(V8)
#> Using V8 engine 11.8.172.13
library(sf)
#> Linking to GEOS 3.11.2, GDAL 3.6.2, PROJ 9.2.0; sf_use_s2() is TRUE
locations <- c("17T PH 8482 7766", "18T XL 6257 1639", "18T XL 6244 1636", "18T XL 6267 1673", "18T XL 6272 1671", "18T XL 6272 1674")
ct <- v8()
ct$source("https://cdn.jsdelivr.net/npm/[email protected]/dist/mgrs.min.js")
#> [1] "true"
ct$assign("locations", locations)
(lon_lat <- ct$get("locations.map(mgrs.toPoint)"))
#> [,1] [,2]
#> [1,] -78.72772 43.12917
#> [2,] -73.07329 40.78247
#> [3,] -73.07484 40.78222
#> [4,] -73.07202 40.78551
#> [5,] -73.07143 40.78532
#> [6,] -73.07143 40.78559
(points_sf <- sfheaders::sf_point(lon_lat) |> st_set_crs("WGS84"))
#> Simple feature collection with 6 features and 0 fields
#> Geometry type: POINT
#> Dimension: XY
#> Bounding box: xmin: -78.72772 ymin: 40.78222 xmax: -73.07143 ymax: 43.12917
#> Geodetic CRS: WGS 84
#> geometry
#> 1 POINT (-78.72772 43.12917)
#> 2 POINT (-73.07329 40.78247)
#> 3 POINT (-73.07484 40.78222)
#> 4 POINT (-73.07202 40.78551)
#> 5 POINT (-73.07143 40.78532)
#> 6 POINT (-73.07143 40.78559)
mapview::mapview(points_sf)
创建于 2023-11-22,使用 reprex v2.0.2