-
-
Save qqplot/5312fcce9c90e3a14f27568145611393 to your computer and use it in GitHub Desktop.
[R] Calculate Distances
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' Vectorize | |
bld_ids <- as.character(df_localdb_dt$bld_id) | |
target_long <- df_localdb_dt$long | |
target_lat <- df_localdb_dt$lat | |
#' Calculate distances | |
dt_dist <- foreach(chunk = isplitVector(1:nrow(df_localdb_dt), chunks=getDoParWorkers()), .packages = c("foreach", "geodist"),.combine = c, .verbose = T) %dopar% { | |
dt_tot <- foreach(x = chunk, .inorder = F, .packages = c("geodist")) %do% { | |
# Reduce the range to search | |
map_to_rn <- df_rndata_dt[lat < target_lat[x] + threshold & lat > target_lat[x] - threshold & | |
long < target_long[x] + threshold & long > target_long[x] - threshold ,] | |
map_to_PS <- df_PSdata_dt[lat < target_lat[x] + threshold & lat > target_lat[x] - threshold & | |
long < target_long[x] + threshold & long > target_long[x] - threshold ,] | |
distRN <- as.vector(geodist(cbind(long=target_long[x], lat=target_lat[x]) , map_to_rn[, c("long", "lat")])) | |
hm_content <- new.env() | |
hm_content$bld_id <- bld_ids[x] | |
hm_content$rn_id <- map_to_rn$rn_id[which.min(distRN)] | |
hm_content$distrn <- distRN[which.min(distRN)] | |
hm_content$pos_cd <- map_to_PS$pos_cd | |
hm_content$distPOS <- as.vector(geodist(cbind(long=target_long[x], lat=target_lat[x]) , map_to_PS[, c("long", "lat")])) | |
return(hm_content) | |
} | |
gc() | |
return(dt_tot) | |
} | |
#' Combind | |
df_comb <- foreach(chunk = isplitVector(dt_dist, chunks=getDoParWorkers()), .packages = c("foreach", "data.table"), .verbose = T) %dopar% { | |
dt_list <- foreach(content = chunk, .packages = c("data.table")) %do% { | |
dt_rn <- data.table( | |
bld_id = rep(content$bld_id, length(content$rn_id)), | |
type = rep("RN", length(content$rn_id)), | |
code = content$rn_id, | |
distance = ceiling(content$distrn), | |
stringsAsFactors = F ) | |
dt_pos <- data.table( | |
bld_id = rep(content$bld_id, length(content$pos_cd)), | |
type = rep("PS", length(content$pos_cd)), | |
code = content$pos_cd, | |
distance = ceiling(content$distPOS), | |
stringsAsFactors = F ) | |
return(rbindlist(l = list(dt_rn, dt_pos), use.names = F)) | |
} | |
gc() | |
return(rbindlist(l = dt_list, use.names = F)) | |
} | |
result <- rbindlist(l=df_comb, use.names = F) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dt_dist <- foreach(chunk = isplitVector(df_localdb_dt_bld$bld_id, chunks=getDoParWorkers()), .packages = c("foreach", "geodist", "dplyr"),.combine = rbind, .verbose = T) %dopar% { | |
dt_tot < - foreach(x = chunk, .combine = rbind, .inorder = F, .packages = c("dplyr", "geodist")) %do% { | |
# Reduce the range to search | |
map_from_localdb <- df_localdb_dt_bld %>% filter( bld_id == x ) %>% dplyr::select( long, lat ) | |
map_to_rn <- df_rndata_dt %>% filter( | |
lat <= map_from_localdb$lat + threshold & lat >= map_from_localdb$lat - threshold & | |
long <= map_from_localdb$long + threshold & long >= map_from_localdb$long - threshold) | |
map_to_PS <- df_PSdata_dt %>% filter( | |
lat <= map_from_localdb$lat + threshold & lat >= map_from_localdb$lat - threshold & | |
long <= map_from_localdb$long + threshold & long >= map_from_localdb$long - threshold | |
rn_nrow <- nrow(map_to_rn) | |
pos_nrow <- nrow(map_to_PS) | |
maxCnt <- min(rn_nrow, pos_nrow) | |
if(maxCnt==0) return(NULL) | |
distRN <- geodist(map_from_localdb, map_to_rn[, c("long", "lat")]) | |
distPS <- geodist(map_from_localdb, map_to_PS[, c("long", "lat")]) | |
idxRN <- order(distRN) %>% head(maxCnt) | |
idxPS <- order(distPS) %>% head(maxCnt) | |
dt <- data.frame(bld_id=rep(x, maxCnt), | |
RN_ID=map_to_rn$eqip_id[idxRN], | |
RN_LOC_BLD_ID=map_to_rn$bld_id[idxRN], | |
distRN= distRN[idxRN], | |
POS_CD=map_to_PS$pos_cd[idxPS], | |
distPOS= distPS[idxPS], | |
stringsAsFactors = FALSE) | |
return(dt) | |
} | |
return(dt_tot) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment