forked from aenieblas/water_areas_geoflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrsf_records_mapping.R
212 lines (163 loc) · 10.1 KB
/
grsf_records_mapping.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#Install packages
require(sf)
require(geoflow)
require(geojsonsf)
library(dplyr) # For data manipulation
library(ggplot2) # For plotting
setwd("C:/Users/artur/OneDrive/Documents/FAO/R GIS/GRSF_water_areas_project")
wd = getwd()
#Extract GRSF competency query directly from GRSF website?
#Read GSRF competency query CSV
grsf_records = read.csv("C:/Users/artur/OneDrive/Documents/FAO/R GIS/Learning/water_areas_publication_trial/trial6.csv")
View(grsf_records)
grsf_records_attributes = read.csv("C:/Users/artur/OneDrive/Documents/FAO/R GIS/Learning/water_areas_publication_trial/grsf_records_attributes.csv")
View(grsf_records_attributes)
#Separate areas and species from semantic id in new column
grsf_records$record_area <- (sub('^[^+]*\\+','',grsf_records$grsf_semantic_id))
#grsf_records$species <- sub('\\+.*$', '', grsf_records$grsf_semantic_id)
grsf_records <- merge(grsf_records, grsf_records_attributes[,c("uuid","url","traceability_flag","sdg_flag")],by = "uuid", all.x = TRUE)
#Get all areas from GitHub
all_areas = read.csv("C:/Users/artur/OneDrive/Documents/GitHub/water_areas_vocabulary/all_areas.csv")
View(all_areas)
#Get all shapefiles from GitHub
gpkgs = list.files(jobdir, pattern = "_areas.gpkg", recursive = TRUE, full.names = T)
all_features = do.call("rbind", lapply(gpkgs, function(x){sf::st_read(x)}))
setwd("C:/Users/artur/OneDrive/Documents/GitHub/water_areas_shapefiles/geoflow")
for(gpkg in gpkgs){
file.copy(from = gpkg, to = getwd(), overwrite = TRUE)
}
setwd(wd)
all_features = do.call("rbind", lapply(gpkgs, function(x){sf::st_read(x)}))
#all_features = st_read("C:/Users/artur/OneDrive/Documents/GitHub/water_areas_shapefiles/geoflow/all_areas.gpkg")
#Concatenate namespace and area_code for obtaining grsf_area_code in all_features
all_features$grsf_area_code <- paste(all_features$namespace,all_features$area_code,sep=":")
View(all_features)
#Search each area of records in "all_features" and extract geometries, then join them
#Function to extract and combine polygons for given area codes
#extract_combine_polygons <- function(area_codes, all_features) {
# Convert area codes to lowercase
#area_codes_lower <- tolower(area_codes)
#all_features$grsf_area_code_lower <- tolower(all_features$grsf_area_code)
#Filter GIS data for the given area codes
#filtered_geoms <- all_features[all_features$grsf_area_code %in% area_codes, "geom"]
#filtered_geoms <- all_features[tolower(all_features$grsf_area_code) %in% area_codes, "geom"]
#filtered_geoms <- all_features[all_features$grsf_area_code_lower %in% area_codes_lower, "geom"]
#Combine geometries into a single multipolygon
#combined_multipolygon <- st_union(filtered_geoms)
#Return the combined multipolygon as an sf object
#return(st_sf(geometry = combined_multipolygon))
#}
#Extraction added to grsf_records
#grsf_records$geo_polygon <- mapply(extract_combine_polygons, strsplit(grsf_records$record_area, ";"), list(all_features))
#Function to extract and combine polygons for given area codes in Well-Known Text (WKT) format
#extract_combine_polygons_wkt <- function(area_codes, all_features) {
# Filter GIS data for the given area codes
#filtered_geoms <- all_features[all_features$grsf_area_code %in% area_codes, "geom"]
# Combine geometries into a single multipolygon
#combined_multipolygon <- st_union(filtered_geoms)
# Convert the combined multipolygon to well-known text (WKT) format
#combined_multipolygon_wkt <- st_as_text(combined_multipolygon)
# Return the combined multipolygon in WKT format
#return(combined_multipolygon_wkt)
#}
#Extraction added to grsf_records
#grsf_records$geo_polygon_wkt <- mapply(extract_combine_polygons_wkt, strsplit(grsf_records$record_area, ";"), list(all_features))
# Create centroids for each record's area
#grsf_records$centroid <- lapply(strsplit(grsf_records$record_area, ";"), function(area_codes) {
# Filter GIS data for the given area codes
#filtered_geoms <- all_features[all_features$grsf_area_code %in% area_codes, "geom"]
# Combine geometries into a single multipolygon
#combined_multipolygon <- st_union(filtered_geoms)
# Calculate centroid of the combined geometry
#centroid <- st_point_on_surface(combined_multipolygon)
# Convert centroid to WKT format
#centroid_wkt <- st_as_text(centroid)
# Return centroid in WKT format
#return(centroid_wkt)
#})
################################################################
#TO DO ALL IN ONCE
# Function to extract, combine polygons, and generate required outputs for given area codes
extract_combine_polygons_and_outputs <- function(area_codes, all_features) {
# Convert area codes to lowercase
area_codes_lower <- tolower(area_codes)
all_features$grsf_area_code_lower <- tolower(all_features$grsf_area_code)
# Filter GIS data for the given area codes (case-insensitive)
filtered_geoms <- all_features[all_features$grsf_area_code_lower %in% area_codes_lower, "geom"]
#filtered_geoms <- all_features[all_features$grsf_area_code %in% area_codes, "geom"]
# Combine geometries into a single multipolygon
combined_multipolygon <- st_union(filtered_geoms)
# Convert the combined multipolygon to well-known text (WKT) format
combined_multipolygon_wkt <- st_as_text(combined_multipolygon)
# Calculate centroid of the combined geometry
centroid <- st_point_on_surface(combined_multipolygon)
# Convert centroid to WKT format
centroid_wkt <- st_as_text(centroid)
# Return a list with all required outputs
return(list(multipolygon = st_sf(geometry = combined_multipolygon),
wkt = combined_multipolygon_wkt,
centroid_wkt = centroid_wkt))
}
# Apply the function to grsf_records and extract the required outputs
results <- mapply(extract_combine_polygons_and_outputs, strsplit(grsf_records$record_area, ";"), list(all_features), SIMPLIFY = FALSE)
# Extract individual results and add them to grsf_records
grsf_records$geo_polygon <- lapply(results, function(x) x$multipolygon)
grsf_records$geo_polygon_wkt <- sapply(results, function(x) x$wkt)
grsf_records$centroid <- sapply(results, function(x) x$centroid_wkt)
####################################################################
# Create SF object for grsf_records with the combined geometry (centroids)
grsf_records_sf_centroid = sf::st_sf(
uuid = grsf_records[lengths(grsf_records$centroid) > 0, ]$uuid,
url = grsf_records[lengths(grsf_records$centroid) > 0, ]$url,
grsf_semantic_id = grsf_records[lengths(grsf_records$centroid) > 0, ]$grsf_semantic_id,
short_name = grsf_records[lengths(grsf_records$centroid) > 0, ]$short_name,
grsf_name = grsf_records[lengths(grsf_records$centroid) > 0, ]$grsf_name,
type = grsf_records[lengths(grsf_records$centroid) > 0, ]$type,
traceability_flag = grsf_records[lengths(grsf_records$centroid) > 0, ]$traceability_flag,
sdg_flag = grsf_records[lengths(grsf_records$centroid) > 0, ]$sdg_flag,
record_area = grsf_records[lengths(grsf_records$centroid) > 0, ]$record_area,
geom = st_as_sfc(grsf_records[lengths(grsf_records$centroid) > 0, ]$centroid)
#geom_wkt = grsf_records[lengths(grsf_records$centroid) > 0, ]$centroid
#geo_polygon = as(sfc_geojson(st_as_sfc(grsf_records$geo_polygon_wkt)),"character")
)
View(grsf_records_sf_centroid)
st_crs(grsf_records_sf_centroid)=4326
st_write(grsf_records_sf_centroid, "grsf_records_def.gpkg", driver = "GPKG")
# Create SF object for grsf_records with the combined geometry (centroids)
grsf_records_sf_polygon = sf::st_sf(
uuid = grsf_records[lengths(grsf_records$centroid) > 0, ]$uuid,
url = grsf_records[lengths(grsf_records$centroid) > 0, ]$url,
grsf_semantic_id = grsf_records[lengths(grsf_records$centroid) > 0, ]$grsf_semantic_id,
short_name = grsf_records[lengths(grsf_records$centroid) > 0, ]$short_name,
grsf_name = grsf_records[lengths(grsf_records$centroid) > 0, ]$grsf_name,
type = grsf_records[lengths(grsf_records$centroid) > 0, ]$type,
traceability_flag = grsf_records[lengths(grsf_records$centroid) > 0, ]$traceability_flag,
sdg_flag = grsf_records[lengths(grsf_records$centroid) > 0, ]$sdg_flag,
#record_area = grsf_records[lengths(grsf_records$centroid) > 0, ]$record_area,
geom = st_as_sfc(grsf_records[lengths(grsf_records$centroid) > 0, ]$geo_polygon_wkt)
#geom_wkt = grsf_records[lengths(grsf_records$centroid) > 0, ]$centroid
#geo_polygon = as(sfc_geojson(st_as_sfc(grsf_records$geo_polygon_wkt)),"character")
)
st_crs(grsf_records_sf_polygon)=4326
st_write(grsf_records_sf_polygon, "grsf_records_poly_def.gpkg", driver = "GPKG")
#Only approved records
# Create SF object for grsf_records with the combined geometry (centroids)
grsf_apprecords_sf_centroid = sf::st_sf(
uuid = grsf_records[grsf_records$status == "approved" & lengths(grsf_records$centroid) > 0, ]$uuid,
url = grsf_records[grsf_records$status == "approved" & lengths(grsf_records$centroid) > 0, ]$url,
#grsf_semantic_id has to be with the new areas!!
grsf_semantic_id = grsf_records[grsf_records$status == "approved" & lengths(grsf_records$centroid) > 0, ]$grsf_semantic_id,
short_name = grsf_records[grsf_records$status == "approved" & lengths(grsf_records$centroid) > 0, ]$short_name,
grsf_name = grsf_records[grsf_records$status == "approved" & lengths(grsf_records$centroid) > 0, ]$grsf_name,
type = grsf_records[grsf_records$status == "approved" & lengths(grsf_records$centroid) > 0, ]$type,
status = grsf_records[grsf_records$status == "approved" & lengths(grsf_records$centroid) > 0, ]$status,
traceability_flag = grsf_records[grsf_records$status == "approved" & lengths(grsf_records$centroid) > 0, ]$traceability_flag,
sdg_flag = grsf_records[grsf_records$status == "approved" & lengths(grsf_records$centroid) > 0, ]$sdg_flag,
#record_area = grsf_records[grsf_records$status == "approved" & lengths(grsf_records$centroid) > 0, ]$record_area,
geom = st_as_sfc(grsf_records[grsf_records$status == "approved" & lengths(grsf_records$centroid) > 0, ]$centroid)
#geom_wkt = grsf_records[lengths(grsf_records$centroid) > 0, ]$centroid
#geo_polygon = as(sfc_geojson(st_as_sfc(grsf_records$geo_polygon_wkt)),"character")
)
View(grsf_apprecords_sf_centroid)
st_crs(grsf_apprecords_sf_centroid)=4326
st_write(grsf_apprecords_sf_centroid, "grsf_approved_records_def.gpkg", driver = "GPKG")