I am using R to filter Tropical cyclone tracks passing over a grid box. I have a csv file containing the tracks and convert them to a shapefile.
I wanted to filter only the points with the same identifier (the "SN" column in the sample data below) that passed over a specified grid box (5N to 25N and 115E to 135 E). Below is the code that I am using and the link to the data.
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)
Problem:
This code does not work properly. When I ran the script, I still see tracks outside the box.
Can anyone suggest any way to improve this?
I'll appreciate any help.
This code corrects the base issue, but I don't know if it fully solves your problem. Your original code seems to assume that the combination of CY and SN are unique in the dataset, which I believe is untrue. There must be combinations with different measurements for the same pair. This version saves the
bounded
values and then merges against thisbounded
tableHere's an approach that uses
data.table
to do the data manipulation, then usesgoogleway
to plot the tracks on google mapsThen when hovering over the lines, you can see the ones that pass through
To filter you can use
conversely if you want to maintain the original data frame you could use
If you want to plot the tracks