我在 R 中创建了两个 sf 对象,一个用于动物位置,一个用于涡轮位置。涡轮机 sf 包括施工开始和停止日期,以及每个结构的运行日期。我试图做的是根据每只动物的时间戳(pronghorn_sf 屏幕截图中的“日期时间”列)过滤涡轮机建造/运行日期。我想测量数据集中每个动物位置到最近正在建设的涡轮机的距离。我在下面包含了我的尝试,它似乎只测量 sf 对象中奇数行的距离(输出屏幕截图),而不是匹配动物时间戳和涡轮机施工周期的日期。我对编写循环相当陌生,非常感谢任何建议。
下面的代码片段:
# ensure the sf objects are using the same crs
pronghorn_sf <- st_transform(pronghorn_sf, st_crs(Turbine_sf))
# create distance columns using a vectorized approach
pronghorn_sf$construction_distance1 <- apply(st_coordinates(pronghorn_sf), 1, function(coords) {
current_date <- pronghorn_sf$DateTime[which(st_coordinates(pronghorn_sf) == coords)]
# filter turbines under construction during a pronghorn location's timestamp
turbines_under_construction <- Turbine_sf %>%
filter(current_date >= Phase1_Construction_Start_Date & current_date <= Phase1_Construction_End_Date)
if (nrow(turbines_under_construction) > 0) {
# calculate the distance only if relevant turbines exist
min(st_distance(st_sfc(st_point(coords), crs = st_crs(Turbine_sf)), turbines_under_construction))
} else {
NA
}
})
数据
(1) 25 行
pronghorn_sf
(sf
-对象):
> dput(pronghorn_sf1)
structure(list(AID = structure(c(137L, 137L, 137L, 137L, 137L,
137L, 137L, 137L, 137L, 137L, 137L, 137L, 137L, 137L, 137L, 139L,
139L, 139L, 139L, 139L, 139L, 139L, 139L, 139L, 139L), levels = c("BH_002",
"BH_003", "BH_004", "BH_005", "BH_006", "BH_007", "BH_008", "BH_009",
"BH_010", "BH_011", "BH_012", "BH_013", "BH_014", "BH_015", "BH_016",
"BH_017", "BH_019", "BH_020", "BH_021", "BH_022", "BH_024", "BH_025",
"BH_026", "BH_028", "BH_029", "BH_030", "BH_031", "BH_033", "BH_035",
"BH_036", "BH_037", "BH_038", "BH_039", "BH_040", "BH_041", "BH_042",
"BH_043", "BH_044", "BH_046", "BH_047", "BH_048", "BH_049", "BH_051",
"BH_052", "BH_053", "BH_054", "BH_055", "BH_056", "BH_057", "BH_058",
"SB_001", "SB_002", "SB_004", "SB_006", "SB_007", "SB_008", "SB_009",
"SB_010", "SB_011", "SB_012", "SB_014", "SB_015", "SB_016", "SB_017",
"SB_018", "SB_019", "SB_020", "SB_021", "SB_022", "SB_024", "SB_025",
"SB_028", "SB_029", "SB_031", "SB_032", "SB_033", "SB_034", "SB_035",
"SB_036", "SB_037", "SB_038", "SB_039", "SB_040", "SB_041", "SB_043",
"SB_044", "SB_045", "SB_046", "SB_047", "SB_049", "SB_052", "SB_053",
"SB_054", "SB_055", "SB_056", "SB_057", "SB_058", "SB_059", "SB_060",
"SB_061", "SB_062", "SB_063", "SB_064", "SB_065", "SB_067", "SB_068",
"SB_069", "SB_070", "SB_071", "SB_072", "SB_074", "SB_075", "SB_076",
"SB_077", "SB_078", "SB_079", "SB_080", "SB_081", "SB_083", "SB_084",
"SB_085", "SB_086", "SB_087", "SB_088", "SB_089", "SB_090", "SB_091",
"SB_092", "SB_093", "SB_094", "SB_095", "SB_096", "SB_097", "SB_098",
"SB_099", "SB_100", "SB_101", "SB_102", "SB_103", "SB_104", "SB_105",
"SB_106", "SB_108", "SB_109", "SB_110", "SB_111", "SB_112", "SB_113",
"SB_114", "SB_115", "SB_116", "SB_117", "SB_118", "SB_119", "SB_120",
"SB_121", "SB_122", "SB_123", "SB_124", "SB_125", "SB_126", "SB_127",
"SB_128", "SB_130", "SB_131", "SB_132", "SB_133", "SB_134", "SB_136",
"SB_137", "SB_138", "SB_139", "SB_140", "SB_141", "SB_143", "SB_146",
"SB_147", "SB_148", "SB_150", "SB_152", "SB_153", "SB_154", "SB_155",
"SB_156", "SB_157", "SB_158", "SB_159", "SB_160", "SB_161", "SB_162",
"SB_163", "SB_165", "SB_166", "SB_167", "SB_168", "SB_169", "SB_171",
"SB_172", "SB_174", "SB_175", "SB_176", "SB_180", "SB_181", "SB_182",
"SB_183", "SB_184", "SB_185", "SB_186", "SB_187", "SB_188", "SB_189",
"SB_190", "SB_191", "SB_192", "SB_193", "SB_194", "SB_195", "SB_196",
"SB_202", "SB_205", "SB_210", "SB_211", "SB_212", "SB_213", "SB_214",
"SB_215", "SB_216", "SB_217", "SB_218", "SB_219", "SB_220", "SB_221",
"SB_222", "SB_223", "SB_224", "SB_225", "SB_226", "SB_227", "SB_228",
"SB_230", "SB_231", "SB_232", "SB_233", "SB_234", "SB_235", "SB_236",
"SB_237", "SB_238", "SB_240", "SB_241", "SB_243", "SB_244", "SB_245",
"SB_246", "SB_247", "SB_248"), class = "factor"), DateTime = structure(c(1671854437,
1671868837, 1671883237, 1671897642, 1671912037, 1671926437, 1671940837,
1671955242, 1671969644, 1671984058, 1671998437, 1672012837, 1672027237,
1672041646, 1672056037, 1567447242, 1567454404, 1567461604, 1567468803,
1567476091, 1567483204, 1567490404, 1567497609, 1567612804, 1567620058
), class = c("POSIXct", "POSIXt"), tzone = "UTC"), YNorthing = c(4640831.29,
4640687.169, 4642397.708, 4644840.306, 4646350.756, 4646919.927,
4646534.687, 4646865.79, 4647306.727, 4647076.159, 4649330.737,
4649855.958, 4649766.786, 4650129.319, 4650288.699, 4650026,
4649935, 4650148, 4650218, 4650226, 4650345, 4650342, 4650351,
4650040, 4649923), XEasting = c(13424219.597, 13425259.957, 13417594.318,
13415562.718, 13416285.702, 13415025.005, 13414998.247, 13415298.28,
13415021.033, 13416268.994, 13416506.255, 13416930.762, 13417068.436,
13417213.538, 13416885.97, 429788, 429323, 429119, 429078, 428974,
428886, 428883, 429023, 429870, 429600), x = c(424219.594584907,
425259.960358151, 417594.314800596, 415562.71890358, 416285.698693028,
415025.006157442, 414998.250352198, 415298.279154033, 415021.035323201,
416268.994737265, 416506.253844386, 416930.762803482, 417068.435627463,
417213.538974703, 416885.968245001, 429788.134621699, 429322.978335433,
429118.858664836, 429078.310284856, 428973.536615617, 428885.689295124,
428882.513327488, 429023.407508244, 429870.020439793, 429599.915982921
), y = c(4640831.28886158, 4640687.163379, 4642397.70379822,
4644840.30088953, 4646350.75440094, 4646919.92935201, 4646534.68665156,
4646865.79303352, 4647306.72834182, 4647076.15648437, 4649330.73207423,
4649855.95795628, 4649766.78705397, 4650129.32171694, 4650288.69561457,
4650026.21325154, 4649935.33876174, 4650147.91029761, 4650217.82758664,
4650226.20503632, 4650344.90043192, 4650342.04521013, 4650351.29340809,
4650039.8382759, 4649922.70138309), geometry = structure(list(
structure(c(-105.9138196, 41.9157991), class = c("XY", "POINT",
"sfg")), structure(c(-105.9012573, 41.9146003), class = c("XY",
"POINT", "sfg")), structure(c(-105.9939202, 41.9292418), class = c("XY",
"POINT", "sfg")), structure(c(-106.0187703, 41.9510235), class = c("XY",
"POINT", "sfg")), structure(c(-106.0102634, 41.9647027), class = c("XY",
"POINT", "sfg")), structure(c(-106.0255573, 41.9696934), class = c("XY",
"POINT", "sfg")), structure(c(-106.0258245, 41.9662213), class = c("XY",
"POINT", "sfg")), structure(c(-106.0222519, 41.9692353), class = c("XY",
"POINT", "sfg")), structure(c(-106.0256611, 41.9731762), class = c("XY",
"POINT", "sfg")), structure(c(-106.0105682, 41.9712334), class = c("XY",
"POINT", "sfg")), structure(c(-106.0080253, 41.9915617), class = c("XY",
"POINT", "sfg")), structure(c(-106.0029752, 41.9963364), class = c("XY",
"POINT", "sfg")), structure(c(-106.0013006, 41.9955479), class = c("XY",
"POINT", "sfg")), structure(c(-105.9996, 41.9988279), class = c("XY",
"POINT", "sfg")), structure(c(-106.0035772, 42.0002286), class = c("XY",
"POINT", "sfg")), structure(c(-105.847774, 41.999121), class = c("XY",
"POINT", "sfg")), structure(c(-105.853379, 41.998261), class = c("XY",
"POINT", "sfg")), structure(c(-105.855869, 42.000157), class = c("XY",
"POINT", "sfg")), structure(c(-105.856367, 42.000783), class = c("XY",
"POINT", "sfg")), structure(c(-105.857633, 42.000849), class = c("XY",
"POINT", "sfg")), structure(c(-105.858708, 42.00191), class = c("XY",
"POINT", "sfg")), structure(c(-105.858746, 42.001884), class = c("XY",
"POINT", "sfg")), structure(c(-105.857046, 42.00198), class = c("XY",
"POINT", "sfg")), structure(c(-105.846787, 41.999251), class = c("XY",
"POINT", "sfg")), structure(c(-105.850034, 41.998172), class = c("XY",
"POINT", "sfg"))), class = c("sfc_POINT", "sfc"), precision = 0, bbox = structure(c(xmin = -106.0258245,
ymin = 41.9146003, xmax = -105.846787, ymax = 42.00198), class = "bbox"), crs = structure(list(
input = "EPSG:4326", wkt = "GEOGCRS[\"WGS 84\",\n ENSEMBLE[\"World Geodetic System 1984 ensemble\",\n MEMBER[\"World Geodetic System 1984 (Transit)\"],\n MEMBER[\"World Geodetic System 1984 (G730)\"],\n MEMBER[\"World Geodetic System 1984 (G873)\"],\n MEMBER[\"World Geodetic System 1984 (G1150)\"],\n MEMBER[\"World Geodetic System 1984 (G1674)\"],\n MEMBER[\"World Geodetic System 1984 (G1762)\"],\n MEMBER[\"World Geodetic System 1984 (G2139)\"],\n ELLIPSOID[\"WGS 84\",6378137,298.257223563,\n LENGTHUNIT[\"metre\",1]],\n ENSEMBLEACCURACY[2.0]],\n PRIMEM[\"Greenwich\",0,\n ANGLEUNIT[\"degree\",0.0174532925199433]],\n CS[ellipsoidal,2],\n AXIS[\"geodetic latitude (Lat)\",north,\n ORDER[1],\n ANGLEUNIT[\"degree\",0.0174532925199433]],\n AXIS[\"geodetic longitude (Lon)\",east,\n ORDER[2],\n ANGLEUNIT[\"degree\",0.0174532925199433]],\n USAGE[\n SCOPE[\"Horizontal component of 3D system.\"],\n AREA[\"World.\"],\n BBOX[-90,-180,90,180]],\n ID[\"EPSG\",4326]]"), class = "crs"), n_empty = 0L)), sf_column = "geometry", agr = structure(c(AID = 1L,
DateTime = 1L, YNorthing = 1L, XEasting = 1L, x = 1L, y = 1L), levels = c("constant",
"aggregate", "identity"), class = "factor"), row.names = 145350:145374, class = c("sf",
"data.frame"))
> dput(Turbine_sf1)
structure(list(name = c("Seven Mile Hill I & II", "Seven Mile Hill I & II",
"Seven Mile Hill I & II", "Seven Mile Hill I & II", "Seven Mile Hill I & II",
"Seven Mile Hill I & II", "Seven Mile Hill I & II", "Seven Mile Hill I & II",
"Seven Mile Hill I & II", "Seven Mile Hill I & II", "Seven Mile Hill I & II",
"Seven Mile Hill I & II", "Seven Mile Hill I & II", "Seven Mile Hill I & II",
"Seven Mile Hill I & II", "Seven Mile Hill I & II", "Seven Mile Hill I & II",
"Seven Mile Hill I & II", "Seven Mile Hill I & II", "Seven Mile Hill I & II",
"Seven Mile Hill I & II", "Seven Mile Hill I & II", "Seven Mile Hill I & II",
"Seven Mile Hill I & II", "Seven Mile Hill I & II", "Seven Mile Hill I & II",
"Seven Mile Hill I & II", "Seven Mile Hill I & II", "Seven Mile Hill I & II",
"Seven Mile Hill I & II", "Seven Mile Hill I & II", "Seven Mile Hill I & II",
"Seven Mile Hill I & II", "Seven Mile Hill I & II", "Ekola Flats",
"Ekola Flats", "Ekola Flats", "Ekola Flats", "Ekola Flats", "Ekola Flats",
"Ekola Flats", "Ekola Flats", "Ekola Flats", "Ekola Flats", "Ekola Flats",
"Ekola Flats", "Ekola Flats", "Ekola Flats", "Ekola Flats", "Ekola Flats",
"Ekola Flats"), pronghorn_overlap = c("Yes", "Yes", "Yes", "Yes",
"Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes",
"Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes",
"Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes",
"Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes",
"Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes",
"Yes", "Yes"), Phase1_Construction_Start_Date = structure(c(13977,
13977, 13977, 13977, 13977, 13977, 13977, 13977, 13977, 13977,
13977, 13977, 13977, 13977, 13977, 13977, 13977, 13977, 13977,
13977, 13977, 13977, 13977, 13977, 13977, 13977, 13977, 13977,
13977, 13977, 13977, 13977, 13977, 13977, 18044, 18044, 18044,
18044, 18044, 18044, 18044, 18044, 18044, 18044, 18044, 18044,
18044, 18044, 18044, 18044, 18044), class = "Date"), Phase1_Construction_End_Date = structure(c(14183,
14183, 14183, 14183, 14183, 14183, 14183, 14183, 14183, 14183,
14183, 14183, 14183, 14183, 14183, 14183, 14183, 14183, 14183,
14183, 14183, 14183, 14183, 14183, 14183, 14183, 14183, 14183,
14183, 14183, 14183, 14183, 14183, 14183, 18261, 18261, 18261,
18261, 18261, 18261, 18261, 18261, 18261, 18261, 18261, 18261,
18261, 18261, 18261, 18261, 18261), class = "Date"), Phase2_Construction_Start_Date = structure(c(NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, 18322, 18322, 18322, 18322, 18322, 18322, 18322, 18322, 18322,
18322, 18322, 18322, 18322, 18322, 18322, 18322, 18322), class = "Date"),
Phase2_Construction_End_Date = structure(c(NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
18551, 18551, 18551, 18551, 18551, 18551, 18551, 18551, 18551,
18551, 18551, 18551, 18551, 18551, 18551, 18551, 18551), class = "Date"),
Phase3_Construction_Start_Date = structure(c(NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_), class = "Date"), Phase3_Construction_End_Date = structure(c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_), class = "Date"), Operation_Date = structure(c(14232,
14232, 14232, 14232, 14232, 14232, 14232, 14232, 14232, 14232,
14232, 14232, 14232, 14232, 14232, 14232, 14232, 14232, 14232,
14232, 14232, 14232, 14232, 14232, 14232, 14232, 14232, 14232,
14232, 14232, 14232, 14232, 14232, 14232, 18611, 18611, 18611,
18611, 18611, 18611, 18611, 18611, 18611, 18611, 18611, 18611,
18611, 18611, 18611, 18611, 18611), class = "Date"), Operation_End_Date = structure(c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_), class = "Date"), Second_Operation_Start_Date = structure(c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_), class = "Date"), Second_Operation_End_Date = structure(c(NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_), class = "Date"), geometry = structure(list(
structure(c(-106.38279, 41.957691), class = c("XY", "POINT",
"sfg")), structure(c(-106.434891, 41.971191), class = c("XY",
"POINT", "sfg")), structure(c(-106.411491, 41.946693), class = c("XY",
"POINT", "sfg")), structure(c(-106.37899, 41.919693), class = c("XY",
"POINT", "sfg")), structure(c(-106.365585, 41.945694), class = c("XY",
"POINT", "sfg")), structure(c(-106.43399, 41.969692), class = c("XY",
"POINT", "sfg")), structure(c(-106.426491, 41.957592), class = c("XY",
"POINT", "sfg")), structure(c(-106.412491, 41.959293), class = c("XY",
"POINT", "sfg")), structure(c(-106.411591, 41.948292), class = c("XY",
"POINT", "sfg")), structure(c(-106.372787, 41.940292), class = c("XY",
"POINT", "sfg")), structure(c(-106.366089, 41.926693), class = c("XY",
"POINT", "sfg")), structure(c(-106.41259, 41.943092), class = c("XY",
"POINT", "sfg")), structure(c(-106.377586, 41.917091), class = c("XY",
"POINT", "sfg")), structure(c(-106.412888, 41.956093), class = c("XY",
"POINT", "sfg")), structure(c(-106.383286, 41.950592), class = c("XY",
"POINT", "sfg")), structure(c(-106.366592, 41.921993), class = c("XY",
"POINT", "sfg")), structure(c(-106.36599, 41.928093), class = c("XY",
"POINT", "sfg")), structure(c(-106.411987, 41.957592), class = c("XY",
"POINT", "sfg")), structure(c(-106.425591, 41.962013), class = c("XY",
"POINT", "sfg")), structure(c(-106.411888, 41.952991), class = c("XY",
"POINT", "sfg")), structure(c(-106.366089, 41.923393), class = c("XY",
"POINT", "sfg")), structure(c(-106.365585, 41.952194), class = c("XY",
"POINT", "sfg")), structure(c(-106.425591, 41.959091), class = c("XY",
"POINT", "sfg")), structure(c(-106.435486, 41.962193), class = c("XY",
"POINT", "sfg")), structure(c(-106.372787, 41.936092), class = c("XY",
"POINT", "sfg")), structure(c(-106.420792, 41.971294), class = c("XY",
"POINT", "sfg")), structure(c(-106.372787, 41.938892), class = c("XY",
"POINT", "sfg")), structure(c(-106.383392, 41.947693), class = c("XY",
"POINT", "sfg")), structure(c(-106.383369, 41.949146), class = c("XY",
"POINT", "sfg")), structure(c(-106.366692, 41.916294), class = c("XY",
"POINT", "sfg")), structure(c(-106.382187, 41.927792), class = c("XY",
"POINT", "sfg")), structure(c(-106.43399, 41.968094), class = c("XY",
"POINT", "sfg")), structure(c(-106.411789, 41.945091), class = c("XY",
"POINT", "sfg")), structure(c(-106.366287, 41.956993), class = c("XY",
"POINT", "sfg")), structure(c(-106.202492, 41.625603), class = c("XY",
"POINT", "sfg")), structure(c(-106.19799, 41.642895), class = c("XY",
"POINT", "sfg")), structure(c(-106.291626, 41.913994), class = c("XY",
"POINT", "sfg")), structure(c(-106.196388, 41.645935), class = c("XY",
"POINT", "sfg")), structure(c(-106.209488, 41.611423), class = c("XY",
"POINT", "sfg")), structure(c(-106.202187, 41.628925), class = c("XY",
"POINT", "sfg")), structure(c(-106.201286, 41.636032), class = c("XY",
"POINT", "sfg")), structure(c(-106.278893, 41.941605), class = c("XY",
"POINT", "sfg")), structure(c(-106.355293, 41.963017), class = c("XY",
"POINT", "sfg")), structure(c(-106.328705, 41.919952), class = c("XY",
"POINT", "sfg")), structure(c(-106.341125, 41.919991), class = c("XY",
"POINT", "sfg")), structure(c(-106.205193, 41.618633), class = c("XY",
"POINT", "sfg")), structure(c(-106.328712, 41.923618), class = c("XY",
"POINT", "sfg")), structure(c(-106.250381, 41.917053), class = c("XY",
"POINT", "sfg")), structure(c(-106.253632, 41.919395), class = c("XY",
"POINT", "sfg")), structure(c(-106.341705, 41.923336), class = c("XY",
"POINT", "sfg")), structure(c(-106.207588, 41.615055), class = c("XY",
"POINT", "sfg"))), class = c("sfc_POINT", "sfc"), precision = 0, bbox = structure(c(xmin = -106.435486,
ymin = 41.611423, xmax = -106.196388, ymax = 41.971294), class = "bbox"), crs = structure(list(
input = "EPSG:4326", wkt = "GEOGCRS[\"WGS 84\",\n ENSEMBLE[\"World Geodetic System 1984 ensemble\",\n MEMBER[\"World Geodetic System 1984 (Transit)\"],\n MEMBER[\"World Geodetic System 1984 (G730)\"],\n MEMBER[\"World Geodetic System 1984 (G873)\"],\n MEMBER[\"World Geodetic System 1984 (G1150)\"],\n MEMBER[\"World Geodetic System 1984 (G1674)\"],\n MEMBER[\"World Geodetic System 1984 (G1762)\"],\n MEMBER[\"World Geodetic System 1984 (G2139)\"],\n ELLIPSOID[\"WGS 84\",6378137,298.257223563,\n LENGTHUNIT[\"metre\",1]],\n ENSEMBLEACCURACY[2.0]],\n PRIMEM[\"Greenwich\",0,\n ANGLEUNIT[\"degree\",0.0174532925199433]],\n CS[ellipsoidal,2],\n AXIS[\"geodetic latitude (Lat)\",north,\n ORDER[1],\n ANGLEUNIT[\"degree\",0.0174532925199433]],\n AXIS[\"geodetic longitude (Lon)\",east,\n ORDER[2],\n ANGLEUNIT[\"degree\",0.0174532925199433]],\n USAGE[\n SCOPE[\"Horizontal component of 3D system.\"],\n AREA[\"World.\"],\n BBOX[-90,-180,90,180]],\n ID[\"EPSG\",4326]]"), class = "crs"), n_empty = 0L)), sf_column = "geometry", agr = structure(c(name = 1L,
pronghorn_overlap = 1L, Phase1_Construction_Start_Date = 1L,
Phase1_Construction_End_Date = 1L, Phase2_Construction_Start_Date = 1L,
Phase2_Construction_End_Date = 1L, Phase3_Construction_Start_Date = 1L,
Phase3_Construction_End_Date = 1L, Operation_Date = 1L, Operation_End_Date = 1L,
Second_Operation_Start_Date = 1L, Second_Operation_End_Date = 1L
), levels = c("constant", "aggregate", "identity"), class = "factor"), row.names = 225:275, class = c("sf",
"data.frame"))
我希望在 pronghorn_sf 对象中得到一个新列(我可能会将其转换为数据框或数据表),其中包括到最近在建涡轮机的距离(以米为单位)。如果动物的时间戳超出了涡轮机施工期,我想用 NA 填充这些行。
这个答案是为了让OP开始,并且会改变。
这是一个
{data.table}
的方法,希望可以帮助您入门。我们假设相似的距离值是由于样本数据的提供而产生的。
我们检查了
crs
和pronghorn_sf
的turbine_sf
(我重命名为小写)。他们是一样的。我已经很多年没有使用 data.table()
进行编码了,所以希望语法不会太糟糕。也许有人在评论中建议改进。
我们将
DateTime
的 pronghorn_sf
重新编码为 Date
,以与 Phase1_Construction_Start_Date
和 Phase1_Construction_End_Date
的 turbine_sf
保持一致。
连接看起来像是对日期进行过滤的 R 方式。为了跟踪观察结果,需要
id
。我们假设 names
中的 turbine_sf
可用于明确识别 涡轮机。此外,我们假设AID
中的pronghorn_sf
可以用来识别pronghorns
。如果不是这种情况,我们应该为两者添加 id。
变量选择不是必需的,但它有助于跟踪。
library(data.table)
pronghorn_sf = setDT(pronghorn_sf)[, date := as.POSIXct(DateTime, tz = "UTC")][
, .(AID, date, DateTime, geometry)]
turbine_sf = setDT(turbine_sf)[, `:=`(
Phase1_Construction_Start_Date = as.POSIXct(Phase1_Construction_Start_Date, tz ="UTC"),
Phase1_Construction_End_Date = as.POSIXct(Phase1_Construction_End_Date, tz ="UTC"))][
, .(name, Phase1_Construction_Start_Date, Phase1_Construction_End_Date, geometry)]
X = pronghorn_sf[turbine_sf, on = .(date >= Phase1_Construction_Start_Date, date <= Phase1_Construction_End_Date),
.(AID, date, DateTime, Phase1_Construction_Start_Date, Phase1_Construction_End_Date, name, geometry,
"geometry2" = i.geometry)]
X[!is.na(AID), ed := sf::st_distance(geometry, geometry2, by_element = TRUE)][!is.na(ed), .SD[ed==min(ed)], by = AID]
这给出了
AID date DateTime Phase1_Construction_Start_Date Phase1_Construction_End_Date name geometry geometry2 ed
<fctr> <POSc> <POSc> <POSc> <POSc> <char> <sfc_POINT> <sfc_POINT> <units>
1: SB_103 2019-05-28 2019-09-03 06:00:04 2019-05-28 2019-12-31 Ekola Flats POINT (-105.8587 42.00188) POINT (-106.2504 41.91705) 33728.82 [m]
2019 年 5 月 28 日 06:00:04 动物
SB_103
距离涡轮机 "Ekola Flats"
33728.82 米(约 21 英里)。这是最近的距离。很难通过数据的选择来验证结果,因为在过滤(连接)之后,只剩下SB_103
。
只要 id 和时间戳可用,将结果分配给另一个数据结构应该不是问题。如果您遇到困难,请告诉我。希望收到您的回复。