-
Notifications
You must be signed in to change notification settings - Fork 0
/
make.R
190 lines (139 loc) · 6.19 KB
/
make.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
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
library(sf)
library(jsonlite)
library(tidyverse)
library(purrr)
library(geojsonio)
library(geojsonsf)
#####################################################
## 1. read in the file
#####################################################
d <- read_csv("Dam.csv", col_types = cols(.default = "c")) #%>% select(-fips,-state_uri)
#####################################################
## 2. Create a unique identifier
#####################################################
## 2.1 # this is just for demonstration purposes
# probably a better way that is persistent over time
d$hydrosource_id <- 1:nrow(d)
## 2.2 Make a URI based on the unique identifier for the geoconnex system
# This is "https://geoconnex.us/<organizational namespace>/<other>/<path>/<pattern>/<identifier>
d$uri <- paste0("https://geoconnex.us/ornl/hydrosource/dams/",
d$hydrosource_id)
d$dam_nam <- gsub('"',"",d$dam_name)
######################################################
## 3. Reference other geoconnex resources
######################################################
## 3.1 States
# Use FIPS codes for States
# read source file
fips <- read_csv("https://raw.githubusercontent.com/internetofwater/fips-codes/master/state_fips_master.csv")
# take only state abbr and fips
fips <- select(fips,state_abbr,fips)
# convert fips to 2-digit code with leading 0s for 1-digit ones
fips$fips <- formatC(fips$fips,
width = 2,
format = "fg",
flag = "0")
# create geoconnex ids for states to merge in
fips$state_uri <- paste0("https://geoconnex.us/ref/states/",fips$fips)
# merge in FIPS
d <- left_join(d,fips,by=c("state"="state_abbr"))
## 3.2 HUC12
# use geoconnex codes for huc12
# NOTE: some of these HUCs are just scientific notation, or 8 digits, or missing leading 0s. Double check!
# add leading 0s for 11-digit hucs
d$huc12<- formatC(as.numeric(d$huc),
width = 12,
format = "fg",
flag = "0")
d$huc12_uri <- paste0("https://geoconnex.us/nhdplusv2/huc12/",d$huc12)
## 3.3 comid
# use geoconnex codes for comid
d$comid <- NA # NOTE: No COMID, add if available
d$comid_uri <- paste0("https://geoconnex.us/nhdplusv2/comid/",d$comid)
# write_csv(d,"dams.csv",append=FALSE)
######################################################
## 4. Convert to geojson
######################################################
# 4.1 Convert to simple features
g <- st_as_sf(d,coords=c("longitude","latitude"))
# 4.2 encode identifiers in different datasets
# id_eha
j <- g
j$`@type` <- "schema:PropertyValue"
j$`schema:PropertyID` = "eha_ptid"
j$`schema:value` <- j$eha_ptid
j <- j %>% st_drop_geometry() %>% select(`@type`,`schema:PropertyID`,`schema:value`)
id_eha <- j %>% mutate(id_eha = pmap(.,
~toJSON(list("@type"="schema:PropertyValue",
"schema:PropertyID" = "eha_ptid",
"schema:value" = ..3),
auto_unbox=TRUE))) %>%
select(id_eha)
# id_hillari
j <- g
j$`@type` <- "schema:PropertyValue"
j$`schema:PropertyID` = "hilarriid"
j$`schema:value` <- j$hilarriid
j <- j %>% st_drop_geometry() %>% select(`@type`,`schema:PropertyID`,`schema:value`)
id_hilarri <- j %>% mutate(id_hilarri = pmap(.,
~toJSON(list("@type"="schema:PropertyValue",
"schema:PropertyID" = "hilarriid",
"schema:value" = ..3),
auto_unbox=TRUE))) %>%
select(id_hilarri)
# id_fcdocket
j <- g
j$`@type` <- "schema:PropertyValue"
j$`schema:PropertyID` = "fcdocket"
j$`schema:value` <- j$fcdocket
j <- j %>% st_drop_geometry() %>% select(`@type`,`schema:PropertyID`,`schema:value`)
id_fcdocket <- j %>% mutate(id_fcdocket = pmap(.,
~toJSON(list("@type"="schema:PropertyValue",
"schema:PropertyID" = "fcdocket",
"schema:value" = ..3),
auto_unbox=TRUE))) %>%
select(id_fcdocket)
# id_nid
j <- g
j$`@type` <- "schema:PropertyValue"
j$`schema:PropertyID` = "nidid"
j$`schema:value` <- j$nidid
j <- j %>% st_drop_geometry() %>% select(`@type`,`schema:PropertyID`,`schema:value`)
id_nid <- j %>% mutate(id_nid = pmap(.,
~toJSON(list("@type"="schema:PropertyValue",
"schema:PropertyID" = "nidid",
"schema:value" = ..3),
auto_unbox=TRUE))) %>%
select(id_nid)
# 4.3 add all encoded ids
g <- cbind(g,id_eha,id_hilarri,id_fcdocket,id_nid)
#############################################################
# 5 write geojson file
###########################################################
# 5.1 add final geoconnex types
g$hyf = "http://www.opengeospatial.org/standards/waterml2/hy_features/HY_HydroLocation"
g$hyf_type = "http://www.opengeospatial.org/standards/waterml2/hy_features/dam"
g$dam_name <- gsub("\"","'",g$dam_name)
# write file
#test <- g[573,]
#geojson_write(test,file="test.geojson")
geojson_write(g,file="dams.geojson")
x <- readLines("dams.geojson")
x <- gsub('["','',x,fixed=TRUE)
x <- gsub('"]','',x,fixed=TRUE)
x <- gsub('\\','',x,fixed=TRUE)
# clean internal quotes and brackets "[ ]"
unlink("dams.geojson")
writeLines(x, con="dams.geojson")
#############################################################
# 6 Make PIDS
###########################################################
pids <- select(d,uri,hydrosource_id)
pids <- pids %>%
mutate(id = uri,
target = paste0("http://localhost:5000/collections/dams/items/",hydrosource_id),
creator = "email address",
description = "dam in ORNL Hydrosource datasets"
) %>%
select(id, target, creator, description)
write_csv(pids,"dams_pids.csv", append=FALSE)