我以前从未使用过 geojson 或任何其他空间文件类型,但我有一个包含经度、纬度和温度列的数据框以及一个单独的 geojson 文件,该文件表示适合我的 df 中所有坐标的区域(想象一下 geojson 是df广场...)
如何过滤温度 df 以便只记录适合 geojson 文件的区域?
我不知道从哪里开始,所以还没有尝试过任何事情!
例如,在温度 df 下:
纬度 | 长 | 温度 |
---|---|---|
52.00 | -1.89 | 13.67 |
53.23 | -2.45 | 10.38 |
51.37 | -2.10 | -2.45 |
我知道最后一行中表示的坐标不会出现在 geojson 文件中。
我想返回过滤后的 df:
纬度 | 长 | 温度 |
---|---|---|
52.00 | -1.89 | 13.67 |
53.23 | -2.45 | 10.38 |
谢谢
sf::st_points()
和sf::st_intersects()
是你的朋友。
由于您不提供数据。
.geoJSON
取自这里。
library(sf)
download.file("https://raw.githubusercontent.com/gregoiredavid/france-geojson/master/communes.geojson",
tempfile(fileext = ".geojson"))
gj <- read_sf(tmp_geojson)
# just the first one
gj = gj[1L, ]
# some points I created randomly
points = structure(list(lat = c(3.30454190848165, 3.29946780571954, 3.29612845330439, 3.27991321713169, 3.30485103998978), long = c(46.1807449149569, 46.1254499991144, 46.137126928633, 46.1617752630819, 46.1662531731711), temp = c(-6.46641247389143, -0.910962931755058, 32.8972983969805, 24.8380473564282, 11.9387508544478)), row.names = c(NA, -5L), class = "data.frame")