2017-04-20 66 views
1

我正在使用R來過濾經過網格框的熱帶氣旋軌跡。我有一個包含曲目的csv文件並將它們轉換爲shape文件。在指定的網格框上過濾TC軌跡(lat-lon點)

我想只過濾通過指定網格框(5N到25N和115E到135 E)的標識符相同的點(下面的示例數據中的「SN」列)。以下是我正在使用的代碼和數據鏈接。

jtwc <- read.csv("1979-1993_TC.csv",header=T,sep=",") 

latmin <-5.00 
latmax <- 25.00 
lonmin <- 115.00 
lonmax <- 135.00 

jtwc.unique <- unique(jtwc[jtwc$Lat >= latmin & jtwc$Lat <= latmax & jtwc$Lon >= lonmin & jtwc$Lon <= lonmax,c(1,2)]) 
jtwc.filter <- merge(jtwc,jtwc.unique,all.x = F,all.y = T, sort = F) 
jtwc.filter$Lon <- ifelse(jtwc.filter$Lon < 0, jtwc.filter$Lon + 360, jtwc.filter$Lon) 
jtwc.filter <- jtwc.filter[with(jtwc.filter,order(Year,Month,Day,Hour,CY)),] 

write.table(jtwc.filter,file = "test2_jul_par_1979-1993.csv", sep = ",", row.names = F) 

Link to data

問題:

此代碼不能正常工作。當我運行腳本時,我仍然可以看到盒子外的曲目。

任何人都可以提出任何方式來改善這一點?

我會很感激任何幫助。

+0

取了點。感謝您的建議。我發佈了一個示例數據。 – Lyndz

+0

你在R中使用任何繪圖庫來「繪製曲目」? – SymbolixAU

+0

ah.no ..我使用gis將點轉換爲line shapefile。我會重申我的問題,使其更清晰。 – Lyndz

回答

1

下面是一個使用data.table做數據處理的方法,然後使用googleway繪製軌道上的谷歌地圖

library(googleway) 
library(data.table) ## because I like working with data.table to do data manipulation 

jtwc <- read.csv("~/Downloads/1979-1993_TC.csv") 
setDT(jtwc) ## set as data.table 

latmin <-5.00 
latmax <- 25.00 
lonmin <- 115.00 
lonmax <- 135.00 

df_bounds <- data.frame(north = latmax, south = latmin, west = lonmin, east = lonmax) 

## apply a logical column whether the point is in the box 
jtwc[, inBounds := Lat >= latmin & Lat <= latmax & Lon >= lonmin & Lon <= lonmax] 


## create a column that identifies if the SN at some point passes through the box 
jtwc[SN %in% jtwc[inBounds == TRUE, unique(SN)], passesThroughBox := T ] 
jtwc[is.na(passesThroughBox), passesThroughBox := F] 

## adding a colour for plotting 
jtwc[, colour := ifelse(passesThroughBox, "#4286F4", "#F44141") ] 

## you need a google maps API key to plot on Google Maps 
map_key <- 'your_api_key' 

google_map(key = map_key) %>% 
    add_polylines(data = jtwc, lat = "Lat", lon = "Lon", id = "SN", stroke_colour = 'colour', 
           mouse_over_group = 'passesThroughBox') %>% 
    add_rectangles(data = df_bounds, north = 'north', south = 'south', west = 'west', east = 'east', 
           fill_opacity = 0.1) 

enter image description here

然後,當懸停在線上,你可以看到那些通過

enter image description here

+0

@Lyndz - 不客氣:我應該在不久的將來在CRAN上推出下一個版本的googleway! – SymbolixAU

2

要篩選,您可以使用

library(dplyr) 

dat %>% 
    filter(Lat>=5 & Lat <=25 & Lon>=115 & Lon<135) 

反之,如果你想保持原來的數據幀,你可以使用

dat %>% 
     mutate(boxed = ifelse(Lat>=5 & Lat <=25 & Lon>=115 & Lon<135, 1,0)) 

如果你要繪製的軌道

library(ggplot2) 

dat %>% 
    mutate(boxed = ifelse(Lat>=5 & Lat <=25 & Lon>=115 & Lon<135, 1,0)) %>% 
    ggplot(aes(Lon,Lat, color=factor(boxed)))+geom_point() 
1

此代碼糾正了基本問題,但我不知道它是否能完全解決您的問題。您的原始代碼似乎假設CY和SN的組合在數據集中是唯一的,我認爲這是不真實的。對於同一對,必須有不同測量值的組合。這個版本保存bounded值,然後合併對這個bounded

library(assertthat) 

jtwc <- read.csv("~/Downloads/1979-1993_TC.csv", header=T, sep=",") 

latmin <-5.00 
latmax <- 25.00 
lonmin <- 115.00 
lonmax <- 135.00 

# adjust for negative lat 
jtwc$Lon <- ifelse(jtwc$Lon < 0, jtwc$Lon + 360, jtwc$Lon) 

# derive the bounded points 
jtwc.bounded <- jtwc[jtwc$Lat >= latmin & jtwc$Lat <= latmax & jtwc$Lon >= lonmin & jtwc$Lon <= lonmax,] 

# all these are TRUE 
assert_that (all(jtwc.bounded$Lat >= latmin)) 
assert_that (all(jtwc.bounded$Lat <= latmax)) 
assert_that (all(jtwc.bounded$Lon >= lonmin)) 
assert_that (all(jtwc.bounded$Lon <= lonmax)) 


jtwc.unique <- unique(jtwc.bounded[,c(1,2)]) 

# merge with bounded (
jtwc.filter <- merge(jtwc.bounded, jtwc.unique, all.x = F, all.y = T, sort = F) 

assert_that (all(jtwc.filter$Lat >= latmin)) 
assert_that (all(jtwc.filter$Lat <= latmax)) 
assert_that (all(jtwc.filter$Lon >= lonmin)) 
assert_that (all(jtwc.filter$Lon <= lonmax)) 


jtwc.filter <- jtwc.filter[with(jtwc.filter,order(Year,Month,Day,Hour,CY)),] 

write.table(jtwc.filter,file = "test2_jul_par_1979-1993.csv", sep = ",", row.names = F)