# Get health facility locations
hf <- bohemia::mopeia_health_facilities
# Get distance from each cluster centroid to each health facilities
yy <- spTransform(master_buf, proj4string(bohemia::mop2))
xx <- spTransform(master_hull, proj4string(bohemia::mop2))
hfs <- hf$name
for(j in 1:length(hfs)){
xx@data[,paste0('km_to_', hfs[j])] <- as.numeric(NA)
hf_coords <- hf[j,c('lng', 'lat')]
out <- distance_to_hf <- calculate_distance(shp = xx,
lat = hf_coords$lat,
lng = hf_coords$lng)
xx@data[,paste0('km_to_', hfs[j])] <- round(out$distance / 1000, digits = 1)
}
xx_coords <- coordinates(xx)
xx_coords <- data.frame(xx_coords)
names(xx_coords) <- c('lng', 'lat')
xx@data <- bind_cols(xx@data, xx_coords)
# Identify the nearest health facility
km_names <- names(xx@data)[which(grepl('km_to_', names(xx@data)))]
closest_indices <- apply(xx@data[,km_names], 1, which.min)
closest_names <- km_names[closest_indices]
closest_names <- gsub('km_to_Centro de Saude de |km_to_Centro de Saude ', '', closest_names)
xx@data$nearest_health_facility <- closest_names
xx@data <- xx@data[,c('cluster',
'nearest_health_facility',
km_names,
'lng', 'lat')]
# Now get distance from each health facility to each cluster centroid
coordinates(hf) <- ~lng+lat
proj4string(hf) <- proj4string(bohemia::mop2)
clusters <- xx@data$cluster
for(j in 1:length(clusters)){
hf@data[,paste0('km_to_cluster_', clusters[j])] <- as.numeric(NA)
cluster_coords <- xx[j,c('lng', 'lat')]
out <- calculate_distance(shp = hf,
lat = cluster_coords$lat,
lng = cluster_coords$lng)
hf@data[,paste0('km_to_cluster_', clusters[j])] <- round(out$distance / 1000, digits = 1)
}
hf@data <- hf@data %>% dplyr::select(-facility_number,
-details,
-district)
# Get distance to Mopeia sede for health facilities
hq <- tibble(lng = 35.711553, lat = -17.979446, name = 'Mopeia Sede') %>%
mutate(x = lng, y = lat)
coordinates(hq) <- ~lng+lat
proj4string(hq) <- proj4string(bohemia::mop2)
out <- calculate_distance(shp = hf, lat = hq$y, lng = hq$x)
hf@data$km_to_hq <- round(out$distance / 1000, digits = 2)
# Get distance to Mopeia sede for clusters
out <- calculate_distance(shp = xx, lat = hq$y, lng = hq$x)
xx@data$km_to_hq <- round(out$distance / 1000, digits = 2)
# Get number of people in each cluster
np <- hh_level %>%
dplyr::distinct(hh_id, .keep_all = TRUE) %>%
filter(cluster %in% cluster_borders@data$cluster) %>%
group_by(cluster) %>%
summarise(people_in_core = sum(n_members[status == 'core']),
people_in_buffer = sum(n_members[status == 'buffer']),
people = sum(n_members))
xx@data <- left_join(xx@data, np)
Could you help get a map that shows the health facilities and their distances to clusters and Mopeia sede? If possible, the same map would show how many people are in each cluster.
l <- leaflet(width = '100%') %>%
addProviderTiles(providers$Stamen.Toner) %>%
addPolylines(data = yy,
color = 'black',
) %>%
addPolygons(data = xx,
stroke = 0,
fillColor = 'red',
fillOpacity = 0.5,
color = 'red',
weight = 1,
popup = popupTable(xx),
label = paste0('Cluster number ', xx@data$cluster, '. Closest HF: ', xx@data$nearest_health_facility, ' . KM to HQ: ', xx@data$km_to_hq, '. People: ', xx@data$people)) %>%
addMarkers(data = hf,
popup = popupTable(hf),
label = paste0(hf$name, '. KM to HQ: ', hf@data$km_to_hq)) %>%
addCircleMarkers(data = hq,
popup = popupTable(hq@data),
label = hq@data$name)
l