过滤指定网格框上的TC轨迹(纬度点) [英] Filter TC tracks (lat-lon points) over a specified gridbox
问题描述
我正在使用R过滤通过格网盒的热带气旋轨道.我有一个包含轨道的csv文件,并将它们转换为shapefile.
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.
我只想过滤经过指定网格框(5N到25N和115E到135 E)的具有相同标识符(下面的示例数据中的"SN"列)的点.下面是我正在使用的代码和数据链接.
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)
问题:
此代码无法正常工作.运行脚本时,仍然可以看到框外的曲目.
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?
我将不胜感激.
推荐答案
这是一种使用data.table
进行数据处理,然后使用googleway
在Google地图上绘制轨迹的方法
Here's an approach that uses data.table
to do the data manipulation, then uses googleway
to plot the tracks on google maps
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)
然后,当鼠标悬停在行上方时,您会看到通过的行
Then when hovering over the lines, you can see the ones that pass through
这篇关于过滤指定网格框上的TC轨迹(纬度点)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!