-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnew_attempt.R
135 lines (103 loc) · 3.85 KB
/
new_attempt.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
library(sf)
library(dplyr)
library(stringdist)
library(mapview)
library(osmdata)
st = AOI::aoi_get(state = "conus", county = "all") %>%
st_transform(5070)
this.county = st[1,]
mapview(st[1,]) + these.dams
these.dams = all.dams[st[1,],]
all.dams = readRDS("./data/nid_cleaned_5070.rds")
gnis.5070 = readRDS("./data/gnis_sf.rds")
obj.names = c('name', 'alt_name', "FEATURE_NAME", 'gnis_name')
ll = list()
for(i in 1:10){
dams = st_transform(all.dams[i,], 5070)
if(dams$river == "UNKNOWN"){ dams$river = NA}
on.trib = grepl("TR|OS|TRIB", dams$river)
# Set the search area: searching within a search radius
bb = st_buffer(this.county, 2500)
fl = nhdplusTools::get_nhdplus(bb)
if(!is.null(nrow(fl))){
fl = fl %>%
dplyr::select(ID = comid, gnis_name) %>%
st_transform(5070)
} else {
fl = NULL
}
wb = nhdplusTools::get_waterbodies(bb)
if(!is.null(nrow(wb))){
wb = wb %>%
dplyr::select(ID = comid, gnis_name) %>%
st_transform(5070)
} else {
wb = NULL
}
osm_query = opq(st_transform(bb, 4326))
osm_dam = osmdata_sf(add_osm_feature(osm_query, key = "waterway", value = "dam"))
osm_ww = osmdata_sf(add_osm_feature(osm_query, key = "waterway", value = c("river", "stream")))
osm_nat = osmdata_sf(add_osm_feature(osm_query, key = "natural", value = "water"))
osm_dam_lines = prepOSM(osm_dam$osm_lines)
osm_ww_lines = prepOSM(osm_ww$osm_lines)
osm_ww_poly = prepOSM(osm_nat$osm_polygons)
nhd_int = wb_fl_intersect(wb = wb, fl, dams, names, "nhd_med")
osm_int = wb_fl_intersect(wb = osm_ww_poly, fl = osm_ww_lines, dams, names, type = "osm")
gnis = mutate(gnis.5070[bb,], ID = FEATURE_ID)
info = list(osm_ww_poly = osm_ww_poly,
osm_ww_lines = osm_ww_lines,
osm_dams_lines = osm_dams_lines,
gnis = gnis,
nhd_med_fl = fl,
nhd_med_wb = wb,
nhd_int = nhd_int,
osm_int = osm_int )
rank = c(1,1,1,2,2,2,0,0)
o = lapply(seq_along(info), function(x){
name_dist_comp(dams,
obj = info[[x]],
obj.names = obj.names,
obj.id = "ID",
context = names(info)[x],
fl_list = list(fl, osm_ww_lines),
rank = rank[x])
})
out = do.call(rbind, o) %>%
mutate(simular = jw < (.1+ min(jw)), closest = snap_dist < (10+ min(snap_dist))) %>%
arrange(rank, -simular, -closest, -snap_dist) %>%
#filter(jw < .35 | is.na(jw)) %>%
filter(snap_dist < 300) %>%
mutate(ord = 1:n(),
onn = sum(on_network, na.rm = TRUE) > 0,
onn2 = !is.na(dams$river),
mainstem = all(!is.na(dams$river), !on.trib),
on_network = sum(onn, onn2) > 0,
onn = NULL, onn2 = NULL)
pts = st_as_sf(out, coords = c("X", "Y"), crs = 5070)
mapview(pts[1,]) + dams + fl + wb
st = AOI::aoi_get(state = "conus", county = "all") %>%
st_transform(st_crs(pts)) %>%
st_filter(pts[1,])
xx = nhdplusTools::get_huc12(pts[1,])
nldi = dataRetrieval::findNLDI(location = st_transform(pts[1,], 4326))
out2 = out %>%
select(NIDID = damname, context, nearest_line_id) %>%
tidyr::pivot_wider(id_cols = NIDID,
values_from = nearest_line_id,
names_from = context)
out2[setdiff(names(info), names(out2))] <- NA
ll[[i]] = cbind(mutate(out2, huc12 = xx$huc12,
nldi.pip = nldi$comid,
state = st$state_name,
county = st$name,
min_snap = min(out$snap_dist)),
select(out[1,],
realization = context,
dam.name, obj.name, jw,
on_network, mainstem,
chosen_snap = snap_dist, X, Y))
message(i)
}
sep = do.call(rbind, ll)
pts = st_as_sf(sep, coords = c("X", "Y"), crs = 5070)
mapview(pts, col.regions = "red") + all.dams[9,]