如何在R中分割GPS坐标数据

时间:2020-07-13 02:04:46

标签: r gps partitioning

我有一个大范围的经纬度GPS坐标。

我的数据如下:

> head(df2015, 10)
     X    Yield   Latitude Longitude
1   97 40.85889  0.8848444  120.8712
2   98 43.54383  2.1551468  120.8833
3   99 42.33718  3.4424795  120.8776
4  100 39.21862  4.7188642  120.8685
5  101 38.24887  6.0019946  120.8820
6  102 36.95594  7.2819180  120.8943
7  103 34.00766  8.5942431  120.8902
8  104 34.58568  9.8706278  120.8970
9  105 34.47788 11.1475653  120.8912
10 106 34.20532 12.4183101  120.8910

这是一个矩形图(字段)。实际数据在这里:

df2015 <- read.table("https://raw.githubusercontent.com/yamunadhungana/data/master/home.2015.csv", header = TRUE, sep = ",")

plot(df2015$Latitude, df2015$Longitude)

enter image description here

我想知道如何将600m x 400 m大小的地块分成相等大小的4个子字段,并将它们的名称放入我的数据帧df2015中。例如,我想按上面所示的子图A,B,C,D对行进行分组,然后将级别放入我的原始数据框中。

1 个答案:

答案 0 :(得分:1)

这是从基数R开始使用With tmp AS(Select carrover_amount, customer_id from (Select carrover_amount, customer_id from payment_info where to_char(created_date, 'YYYYMM') < '202007' order by created_date desc) where row_num = 1) ,tmp2 AS(Select CASE WHEN category = 1 THEN carryover_amount - payment_amount ELSE carryover_amount + payment_amount END carrover_amount2, customer_id from (Select customer_id, carrover_amount, min(created_date) OVER() as first_dt, payment_amount, created_date from payment_info where to_char(created_date, 'YYYYMM') = '202007' order by created_date) where first_dt = created_date) Select CASE WHEN t1.customer_id IS NOT NULL THEN t1.carrover_amount WHEN t2.customer_id IS NOT NULL THEN t2.carrover_amount2 ELSE b.carryover_amount END carryover from payment_info a LEFT OUTER JOIN tmp t1 ON a.customer_id = t1.customer_id LEFT OUTER JOIN tmp2 t2 ON a.customer_id = t2.customer_id LEFT OUTER JOIN customer_tbl b ON a.customer_id = b.customer_id; 的一种方法:

findInterval

现在,网格位置是df2015 <- read.table("https://raw.githubusercontent.com/yamunadhungana/data/master/home.2015.csv", header = TRUE, sep = ",") pos.matrix <- matrix(LETTERS[c(2,3,1,4)],byrow = TRUE, nrow = 2) pos.matrix # [,1] [,2] #[1,] "B" "C" #[2,] "A" "D" df2015$grid <- apply(cbind(findInterval(df2015[,"Latitude"],seq(0,400,by = 200)), 3-findInterval(df2015[,"Longitude"],seq(0,600,by = 300))), 1,function(x){pos.matrix[x[2],x[1]]}) df2015[1:10,] # X Yield Latitude Longitude grid #1 97 40.85889 0.8848444 120.8712 A #2 98 43.54383 2.1551468 120.8833 A #3 99 42.33718 3.4424795 120.8776 A #4 100 39.21862 4.7188642 120.8685 A #5 101 38.24887 6.0019946 120.8820 A #6 102 36.95594 7.2819180 120.8943 A #7 103 34.00766 8.5942431 120.8902 A #8 104 34.58568 9.8706278 120.8970 A #9 105 34.47788 11.1475653 120.8912 A #10 106 34.20532 12.4183101 120.8910 A 中的新列。您可以使用df2015将data.frame分成网格位置列表。

以下是确认正确分配的可视化视图:

split

enter image description here