-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathprofvis_example.R
More file actions
193 lines (157 loc) · 4.43 KB
/
Copy pathprofvis_example.R
File metadata and controls
193 lines (157 loc) · 4.43 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
library(shiny)
library(profvis)
profvis({
runApp(display.mode = "normal")
})
#Creat same example data:
library(dplyr)
library(soilprofile2)
library(ggplot2)
profvis({
geom_example <- data.frame(name = c(1, 2),
from1 = c(0,20),
to1 = c(20, 40)
) %>%
cord_setting(plot_width = 3)
attri_example <- data.frame(name = c(1, 2),
rgb_col = c("#6F5F4CFF", "#947650FF"),
nameC = c("Ah", "Bv")
)
##
sf_example <- sf_polygon(df_geom = geom_example, df_attri = attri_example)
## Line attributes data frame:
lattri_example <- data.frame(name= c(1,2),
numberX = c(2, 10),
sd = c(1,1),
sm = c(TRUE, TRUE)
)
## Apply the line_mod fuction
line_example <- line_mod(df = geom_example,
line_attri = lattri_example)
## Split the Profile with the new line shape:
example_profile <- split_polygon(polygon = sf_example,
line = line_example)
## Attributes table with the specifications for the transition:
##
df_smooth <- data.frame(
buffer_size = c(5),
buffer_number = c(30),
nSides = c(10),
rate = c(15),
name = c(2)
)
#Applying the function
smooth_profile <- smooth_trans(lmod = line_example,
shape_mod = example_profile,
attr_df = df_smooth)
})
profvis({
lmod = line_example
shape_mod = example_profile
attr_df = df_smooth
shape = 10
seed = 33
smoothness = 2
stopifnot("sf" %in% class(lmod))
stopifnot("sf" %in% class(shape_mod))
stopifnot("data.frame" %in% class(attr_df))
stopifnot(c("buffer_size","buffer_number","nSides","rate","name") %in% colnames(attr_df))
##
#select attributes
##
df <- attr_df
attr_default <- shape_mod
sf::st_geometry(attr_default) <- NULL
##
#select geometrys
##
shape_mod <- shape_mod %>%
dplyr::select_(~ name, ~ geometry)
df_line <- lmod %>%
dplyr::select_(~name, ~geometry)
##
#Use polygons to create a smooth transition:
##
##
#1 Buffer:
##
df_buffer <- df_line %>%
dplyr::right_join(df, by = "name") %>%
#dplyr::group_by_(~name) %>%
sf::st_buffer(dist = df$buffer_size, endCapStyle = "FLAT") %>%
dplyr::filter_(~ buffer_size != 0) %>%
sf::st_intersection(shape_mod) %>%
dplyr::select_(~name, ~nSides, ~rate, ~buffer_number)
##
#Distribute points on the buffer:
##
set.seed(seed)
df_sample <- sf::st_sample(df_buffer, size = df_buffer$buffer_number)
df_inter <- sf::st_intersection(df_buffer, df_sample)
##
#create the polygone:
##
temp0 <- df_inter %>%
dplyr::group_by_(~name) %>%
dplyr::mutate_(area_size = ~rgamma(n(), shape = shape, rate = max(rate))) %>%
dplyr::ungroup()
temp1 <- point_2_polygon(temp0)
##
#smooth the polygons
##
temp1 <- smoothr::smooth(temp1, method = "ksmooth", smoothness = smoothness)
##
#Get id from neighboring horizon
##
tf <- sf::st_intersects(temp1, shape_mod$geometry, sparse = F )
tf1 <- cbind(tf, temp1$name)
index <- ncol(tf1)
for(i in 1:(index-1)){
for(j in 1:nrow(tf1)){
if(tf1[j,i] == 1){
if(i == tf1[j,index]){
tf1[j,index] <- tf1[j,index] -1
}
}
}
}
temp1$name <- tf1[,index] #Overwrite names
#clean cut
temp_int <- temp1 %>%
dplyr::rowwise() %>%
dplyr::mutate_(int = ~ dplyr::if_else(any(sf::st_intersects(geometry, df_line, sparse = F)) == T, T, F)) %>%
sf::st_sf() %>%
dplyr::select_(~name, ~int)
##
#Resolving the polygons lying on the border
##
#Select all polygons on the border
temp2 <- temp_int %>%
dplyr::filter_(~ int == T) %>%
dplyr::select_(~ name) %>%
dplyr::group_by_(~ name) %>%
dplyr::summarise(do_union = F) %>%
sf::st_buffer(0.0) %>%
sf::st_intersection(sf::st_union(shape_mod))
shape_mod1 <- sf::st_difference(shape_mod, sf::st_combine(temp2)) %>%
dplyr::select_(~name) %>%
rbind(temp2) %>%
dplyr::group_by_(~name) %>%
dplyr::summarise(do_union = T)
#2. and all others
temp3 <- temp_int %>%
dplyr::filter_(~ int == F) %>%
dplyr::select_(~ name) %>%
dplyr::group_by_(~ name) %>%
dplyr::summarise(do_union = F) %>%
sf::st_buffer(0.0) %>%
sf::st_intersection(sf::st_union(shape_mod1))
shape_mod2 <- sf::st_difference(shape_mod1, sf::st_combine(temp3)) %>%
dplyr::select_(~name) %>%
rbind(temp3) %>%
dplyr::group_by_(~name) %>%
dplyr::summarise(do_union = FALSE) %>%
dplyr::left_join(attr_default, by = "name")
})
library(shinytest)
recordTest()