当前位置: 动力学知识库 > 问答 > 编程问答 >

r - How can I remove outlines from location data

问题描述:

I have a location data of marine species. However, some data fall on Land and I would like to remove them without finding the rows that they belong to using ( PS2<-PS1[-c(1,2,3),]) because I have a large data set. I have search for clue but can't really find something good. Thank you.

Here is my codes

 require(sp)

library(maptools)

require(raster)

data(wrld_simpl)

PS<-read.csv('Test.csv')

PS

#-----------------

lat lon

1 -32.98000 154.000000

2 36.94625 8.212916

3 -37.81430 -57.479584

4 -19.77236 -40.019028

5 -25.70459 -48.473195

6 -22.47125 -41.859027

7 -28.08153 -48.627082

8 10.56000 39.090000

9 50.50000 50.600000

10 52.50000 5.700000

PS1 <- subset(PS, !is.na(lon) & !is.na(lat))

plot(wrld_simpl, bg='azure2', col='khaki', border='#AAAAAA')

#restore the box around the map

box()

#plot points

points(PS1$lon, PS1$lat, col='orange', pch=20, cex=0.75)

# plot points again to add a border, for better visibility

points(PS1$lon, PS1$lat, col='red', cex=0.75)

The last three data fall on land.

网友答案:

This shows how you can use %over% (you can also use the over function

library(sp)
library(raster)
library(maptools)
data(wrld_simpl)

PS <- matrix( c(-32.98000, 154.000000, 36.94625, 8.212916,-37.81430, -57.479584,-19.77236, -40.019028,-25.70459, -48.473195,-22.47125, -41.859027,-28.08153, -48.627082, 10.56000,  39.090000,50.50000,  50.600000, 52.50000,   5.700000), ncol=2, byrow=TRUE)
ps <- PS[,2:1]

sp <- SpatialPoints(ps)
crs(sp) = crs(wrld_simpl)
i <- sp %over% wrld_simpl
marine <- is.na(i$ISO3)

spm <- sp[marine, ]

plot(wrld_simpl)
points(ps, pch=20, col='red')
points(spm, pch=20, col='blue')
分享给朋友:
您可能感兴趣的文章:
随机阅读: