2016-12-14 43 views
0

目前我在學校項目半徑座標和我很新的R和論壇和腳本一般返回與初始緯度和長

我有Lat_XLong_Y

我也有一個表,其中包含一個列表Lat_nLong_n

如何從Lat_XLong_Y座標100公里範圍內的表格中返回所有值?

+1

查看'geosphere'庫中的距離函數。 – aichao

回答

0

您可以使用包中的gBuffer函數來創建圓圈。請注意,您必須爲此使用一些平面投影。

然後你可以用「子集」點over函數。

實施例:

library(sp) 
library(rgeos) 

# Initial data 
lat_X = 10 
long_Y = 10 
Radius = 500000 # in meters 
Points_dat <- data.frame(lon_n = c(8:15), lat_n = c(8:15)) 

### Solution with SP and rgeos packages 

# First, prepare projected spatial objects 
Origin <- SpatialPoints(data.frame(long_Y, lat_X), 
        proj4string = CRS("+proj=longlat +datum=WGS84")) 

Points <- SpatialPoints(Points_dat, proj4string = CRS("+proj=longlat +datum=WGS84")) 

# Reproject them into planar 
Origin_t <- spTransform(Origin, CRS("+init=epsg:2393")) 
Points_t <- spTransform(Points, CRS("+init=epsg:2393")) 

# Construct buffer 
Circle <- gBuffer(Origin_t, width=Radius, byid=TRUE) 

# Plot_results 
plot(Circle) 
plot(Points_t, add=TRUE) 
plot(Origin_t, add=TRUE, pch=16) 
plot(Points_t[!is.na(over(Points_t, Circle)), ], pch=5, col=2, add=TRUE) 


# Make subset 
Points_subset <- Points_dat[!is.na(over(Points_t, Circle)), ] 
Points_subset 

結果:

lon_n lat_n 
1  8  8 
2  9  9 
3 10 10 
4 11 11 
5 12 12 
6 13 13 

UPD:與地圈 初始數據解是相同的前面的例子英寸

library(geosphere) 
# Distances calculation 
distances <- distm(Points_dat, c(long_Y, lat_X), fun=distHaversine) 
Points_subset_1 <- Points_dat[distances <= Radius,] 
# Attach column with distances in meters 
Points_subset_1$dist <- distances[distances <= Radius] 
Points_subset_1 

    lon_n lat_n  dist 
1  8  8 312918.2 
2  9  9 156352.6 
3 10 10  0.0 
4 11 11 156115.9 
5 12 12 311971.1 
6 13 13 467541.5 

如果您需要實際距離,使用geosphere的解決方案更合適。但是,有時候最好使用緩衝區,例如,如果您使用柵格。

+0

感謝您的建議。我得到以下錯誤:.local(obj,...)中的錯誤: 無法從非數字矩陣派生座標。難道我的價值觀不是整數? – victorj

+0

你能用座標'str(Your_table)'顯示你的對象的結構嗎? – Istrel

+0

Lat |長 ------ | ------ 48.8851 | 12.519600 – victorj