Skip to content

Commit

Permalink
fix #271 ondatra
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderDevisscher committed Nov 7, 2024
1 parent fb95e8d commit a4f7491
Showing 1 changed file with 118 additions and 7 deletions.
125 changes: 118 additions & 7 deletions src/get_muskrat_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ muskrat_data_redux <- raw_muskrat_data %>%
filter(samplingProtocol != "casual observation",
!grepl(pattern = "material lost/broken",
x = samplingProtocol))

# Add spatial component ####
gem <- st_read("./data/output/UAT_processing/communes.geojson") %>%
st_transform(4326) %>%
Expand Down Expand Up @@ -67,10 +67,10 @@ for(i in 1:length(muskrat_data$Gemeente)){
}

muskrat_data$Provincie <- apply(sf::st_intersects(provincies,
muskrat_data,
sparse = FALSE), 2,
function(col) {provincies[which(col),
]$NAAM})
muskrat_data,
sparse = FALSE), 2,
function(col) {provincies[which(col),
]$NAAM})

muskrat_data$provincie <- NA

Expand All @@ -81,18 +81,129 @@ for(i in 1:length(muskrat_data$Gemeente)){

# maintain needed columns ####
muskrat_data <- muskrat_data %>%
mutate(provincie = case_when(is.na(provincie) & gemeente == "Maldegem" ~ "Oost-Vlaanderen",
is.na(provincie) & gemeente == "Frasnes-Lez-Anvaing" ~ "Henegouwen",
is.na(provincie) & gemeente == "Zulte" ~ "Oost-Vlaanderen",
is.na(provincie) & gemeente == "Wortegem-Petegem" ~ "Oost-Vlaanderen",
is.na(provincie) & gemeente == "Geraardsbergen" ~ "Oost-Vlaanderen",
is.na(provincie) & gemeente == "Ronse" ~ "Oost-Vlaanderen",
is.na(provincie) & gemeente == "Sint-Laureins" ~ "Oost-Vlaanderen",
is.na(provincie) & gemeente == "Kortrijk" ~ "West-Vlaanderen",
is.na(provincie) & gemeente == "Damme" ~ "West-Vlaanderen",
is.na(provincie) & gemeente == "Hoegaarden" ~ "Vlaams-Brabant",
is.na(provincie) & gemeente == "Graven" ~ "Waals-Brabant",
is.na(provincie) & gemeente == "Poperinge" ~ "West-Vlaanderen",
is.na(provincie) & gemeente == "Huldenberg" ~ "Vlaams-Brabant",
is.na(provincie) & gemeente == "Wervik" ~ "West-Vlaanderen",
is.na(provincie) & gemeente == "Avelgem" ~ "West-Vlaanderen",
is.na(provincie) & gemeente == "Spiere-Helkijn" ~ "West-Vlaanderen",
is.na(provincie) & gemeente == "Overijse" ~ "Vlaams-Brabant",
is.na(provincie) & gemeente == "Waver" ~ "Waals-Brabant",
is.na(provincie) & gemeente == "Edingen" ~ "Henegouwen",
TRUE ~ provincie)) %>%
left_join(provincies %>%
as.data.frame() %>%
select(NAAM, GEWEST),
by = c("provincie" = "NAAM")) %>%
select(year,
individualCount,
NAAM = gemeente,
gemeente,
provincie,
GEWEST,
gewest = GEWEST,
gbifID,
decimalLatitude,
decimalLongitude)

# Check ####
table(muskrat_data$gewest, useNA = "ifany")
table(muskrat_data$provincie, useNA = "ifany")
table(muskrat_data$gemeente, useNA = "ifany")

## Missing provinces ####
### All ####
missing_provinces <- muskrat_data %>%
filter(is.na(provincie))

### With gemeente ####
# These cases are fixed in the previous step.
# If new cases arise they should be added in the previous step.
missing_provinces_gem <- missing_provinces %>%
filter(!is.na(gemeente)) %>%
distinct(gemeente)

if(nrow(missing_provinces_gem) > 0){
write_csv(missing_provinces_gem, "./data/interim/muskrat_missing_provinces_gem.csv")
}else{
message("No missing provinces with gemeente")
file.remove("./data/interim/muskrat_missing_provinces_gem.csv")
}

### Without gemeente but with geometry ####
missing_provinces_geom <- missing_provinces %>%
filter(is.na(gemeente) & !is.na(geometry)) %>%
distinct(geometry) %>%
st_buffer(100)

#### intersect again with gemeentes ####
missing_provinces_geom$Gemeente <- apply(sf::st_intersects(gem,
missing_provinces_geom,
sparse = FALSE), 2,
function(col) {gem[which(col),
]$Gemeente})

missing_provinces_geom$gemeente <- NA

for(i in 1:length(missing_provinces_geom$Gemeente)){
missing_provinces_geom$gemeente[i] <- as.character(missing_provinces_geom$Gemeente[[i]][1])
}

missing_provinces_geom$Provincie <- apply(sf::st_intersects(provincies,
missing_provinces_geom,
sparse = FALSE), 2,
function(col) {provincies[which(col),
]$NAAM})

missing_provinces_geom$provincie <- NA

for(i in 1:length(missing_provinces_geom$Provincie)){
missing_provinces_geom$provincie[i] <- as.character(missing_provinces_geom$Provincie[[i]][1])
}

missing_provinces_geom <- missing_provinces_geom %>%
left_join(provincies %>%
as.data.frame() %>%
select(NAAM, GEWEST),
by = c("provincie" = "NAAM")) %>%
select(gemeente_new = gemeente,
provincie_new = provincie,
gewest_new = GEWEST,
geometry) %>%
filter(!is.na(gemeente_new))

#### readd information to muskrat_data ####
muskrat_data <- muskrat_data %>%
st_join(missing_provinces_geom) %>%
mutate(gemeente = coalesce(gemeente, gemeente_new),
provincie = coalesce(provincie, provincie_new),
gewest = coalesce(gewest, gewest_new)) %>%
select(-gemeente_new, -provincie_new, -gewest_new)

missing_provinces_final <- muskrat_data %>%
filter(is.na(provincie))

leaflet::leaflet(missing_provinces_final) %>%
leaflet::addTiles() %>%
leaflet::addCircles()

if(nrow(missing_provinces_final) > 0){
write_csv(missing_provinces_final, "./data/interim/muskrat_missing_provinces_final.csv")
}else{
message("No missing provinces with geometry")
file.remove("./data/interim/muskrat_missing_provinces_final.csv")
}

muskrat_data <- muskrat_data %>%
filter(!is.na(provincie))

# Export ####
write_csv(muskrat_data, "./data/output/UAT_direct/Ondatra_zibethicus.csv")

0 comments on commit a4f7491

Please sign in to comment.