gpt4 book ai didi

r - 在 R 中选择/子集空间数据

转载 作者:行者123 更新时间:2023-12-04 09:26:38 25 4
gpt4 key购买 nike

我正在处理包含空间数据(纬度/经度)的大型数据集。我的数据集包含一些我在分析中不想要的位置(它使文件在 ArcMap 中处理起来很繁重 - 许多数据)。

这就是为什么我要为我的工作对相关数据进行子集化。这是我的数据示例:

lat        long
59.979687 29.706236
60.136177 28.148186
59.331383 22.376234
57.699154 11.667305
54.90566 17.768538
57.122417 8.59762
55.8108 10.820534
57.86072 22.616697
59.912144 30.24504

我拥有的数据来自以下 map 中涵盖的所有区域。我只想在波罗的海工作,所以(几乎)只使用这条黑线左侧的点。
map

有人听说过一个包或对如何处理这个有一些想法吗?

另一种解决方案可能是围绕波罗的海绘制一个多边形,然后仅选择该多边形内的点。

最佳答案

我假设你的意思是“向右”,因为你说“另一个解决方案可能是在波罗的海周围绘制一个多边形,然后只选择这个多边形内的点”

# your sample data

pts <- read.table(text="lat long
59.979687 29.706236
60.136177 28.148186
59.331383 22.376234
57.699154 11.667305
54.90566 17.768538
57.122417 8.59762
55.8108 10.820534
57.86072 22.616697
59.912144 30.24504", header=TRUE)

library(ggplot2)
library(sp)
library(rgdal)

# a rough baltic polygon that I deliberately made to exclude one point

baltic <- data.frame(lat=c(69.91, 57.98, 51.0, 60, 69.91),
long=c(23.07, 7.05, 12.0, 30, 23.07))

# only doing this to show the points, you can filter out in similar fashion

pts$is_in <- factor(point.in.polygon(pts$long, pts$lat, baltic$long, baltic$lat))

# I made an _extremely_ oversimplified map of europe for speedy plotting
europe <- readOGR("http://rud.is/dl/simple_europe.geojson", "OGRGeoJSON")
europe_map <- fortify(europe)


# plot to show it works
gg <- ggplot()
gg <- gg + geom_map(map=europe_map, data=europe_map,
aes(x=long, y=lat, map_id=id),
fill="white", color="black", size=0.25)
gg <- gg + geom_point(data=pts, aes(x=long, y=lat, color=is_in))
gg <- gg + geom_path(data=baltic, (aes(x=long, y=lat)), linetype=2)

# lambert isn't a bad projection for the region
gg <- gg + coord_map("lambert", lat0=50, lat1=70, xlim=c(5,31), ylim=c(50, 70))
gg

enter image description here

您可以根据需要使多边形变大以获得想要包含的点。请记住,我把它做得足够小,以故意排除那个“红色”点。

关于r - 在 R 中选择/子集空间数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30975032/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com