Title: | Geospatial Analytics Utility functions |
---|---|
Description: | R Spatial functions for HIV/AIDS related Geospatial Analytics. |
Authors: | Baboyma Kagniniwa [aut, cre] |
Maintainer: | Baboyma Kagniniwa <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.4.0 |
Built: | 2024-11-05 04:44:57 UTC |
Source: | https://github.com/USAID-OHA-SI/gisr |
Generate administrative boundaries map
admins_map(countries, add_neighbors = FALSE)
admins_map(countries, add_neighbors = FALSE)
countries |
list of countries to map |
add_neighbors |
should the map include the neighbor countries |
ggplot admin map
## Not run: library(gisr) admins_map(countries = list("Zambia")) admins_map(countries = list("Zambia"), add_neighbors = TRUE) ## End(Not run)
## Not run: library(gisr) admins_map(countries = list("Zambia")) admins_map(countries = list("Zambia"), add_neighbors = TRUE) ## End(Not run)
Assess facility geo-location reporting levels
assess_facilities(.data)
assess_facilities(.data)
.data |
Datim organisation units data frame |
## Not run: assess_facilities(df_sites) df_sites %>% assess_facilities() ## End(Not run)
## Not run: assess_facilities(df_sites) df_sites %>% assess_facilities() ## End(Not run)
Check columns length
check_columns(spdf)
check_columns(spdf)
spdf |
input spatial data frame |
list of columns with more than 10 characters
Extract Country Polygons
cntry_polygons(spdf, cntry, attrs)
cntry_polygons(spdf, cntry, attrs)
spdf |
VcPolygons data as Spatial Data Frame |
cntry |
Country name |
attrs |
Country orgunits, output of |
list of spatial data frames
## Not run: cntry = "Nigeria" spdf <- gisr::get_vcpolygons(path = glamr::si_path("path_vector"), name = "VcPepfarPolygons.shp") df_attrs <- grabr::get_attributes(cntry) cntry_polygons(spdf = spdf, cntry = "Zambia", attrs = df_attrs) ## End(Not run)
## Not run: cntry = "Nigeria" spdf <- gisr::get_vcpolygons(path = glamr::si_path("path_vector"), name = "VcPepfarPolygons.shp") df_attrs <- grabr::get_attributes(cntry) cntry_polygons(spdf = spdf, cntry = "Zambia", attrs = df_attrs) ## End(Not run)
Download shapefile zipfile from googledrive
download_shapefiles( country, org_label = "country", drive_folder = NULL, dest_file = NULL, overwrite = TRUE, unzip = FALSE )
download_shapefiles( country, org_label = "country", drive_folder = NULL, dest_file = NULL, overwrite = TRUE, unzip = FALSE )
country |
PEPFAR Countryname |
org_label |
Orgunit label, default is set to country |
drive_folder |
Googledrive id for all PEPFAR Spatial files |
dest_file |
Full file name where to download zipped shapefile |
overwrite |
Should the process overwrite existing files |
unzip |
Should the zipfile be unzipped |
## Not run: cntry <- "Zambia" download_shapefiles(country = cntry) download_shapefiles(country = cntry, org_label = "snu1", unzip = TRUE) download_shapefiles(country = cntry, org_label = "psnu", unzip = TRUE) ## End(Not run)
## Not run: cntry <- "Zambia" download_shapefiles(country = cntry) download_shapefiles(country = cntry, org_label = "snu1", unzip = TRUE) download_shapefiles(country = cntry, org_label = "psnu", unzip = TRUE) ## End(Not run)
View attributes from simple feature object
dview(geodata, console = FALSE)
dview(geodata, console = FALSE)
geodata |
spatial data |
console |
view in console? default false |
## Not run: library(gisr) adm0 <- get_admin0("Ghana") adm0 %>% dview(console = TRUE) ## End(Not run)
## Not run: library(gisr) adm0 <- get_admin0("Ghana") adm0 %>% dview(console = TRUE) ## End(Not run)
Explore facility locations
explore_facilities(.data, cntry, terr_path = NULL)
explore_facilities(.data, cntry, terr_path = NULL)
.data |
Datim organisation units data frame |
cntry |
Country name |
terr_path |
Path to terrain raster dataset |
## Not run: explore_facilities(df_sites, cntry = "saturn") df_sites %>% explore_facilities("saturn") ## End(Not run)
## Not run: explore_facilities(df_sites, cntry = "saturn") df_sites %>% explore_facilities("saturn") ## End(Not run)
Save spatial data as shapefile
export_spdf(spdf, name)
export_spdf(spdf, name)
spdf |
sf object |
name |
filename with full path |
boolean
## Not run: library(gisr) library(sf) shp <- get_admin0(countries = "Nigeria") export_spdf(spdf = shp, name = "./GIS/nga_country_boundaries") export_spdf(spdf = shp, name = "./GIS/nga_country_boundaries.shp") ## End(Not run)
## Not run: library(gisr) library(sf) shp <- get_admin0(countries = "Nigeria") export_spdf(spdf = shp, name = "./GIS/nga_country_boundaries") export_spdf(spdf = shp, name = "./GIS/nga_country_boundaries.shp") ## End(Not run)
Extract Orgunit Boundaries Attributes
extract_attributes( country, username, password, folderpath, prefix = "orghierarchy", baseurl = NULL )
extract_attributes( country, username, password, folderpath, prefix = "orghierarchy", baseurl = NULL )
country |
OU/country |
username |
Datim username |
password |
Datim password |
folderpath |
Local directory of files |
prefix |
Prefix for filename |
baseurl |
Datim URL |
df
## Not run: library(gisr) extract_attributes(country = "Nigeria") extract_attributes(country = "Nigeria", folderpath = glamr::si_path("path_vector")) ## End(Not run)
## Not run: library(gisr) extract_attributes(country = "Nigeria") extract_attributes(country = "Nigeria", folderpath = glamr::si_path("path_vector")) ## End(Not run)
PEPFAR VcPolygons are shared with orgunit uids only, making hard for analysts to identify specific polygon each orgunit level. This function extract orgunt attributes from Datim and append them to the global shapefile, allowing analysts to filter and work only with subset.
extract_boundaries( spdf, country, level = 3, username, password, export = FALSE, name = NULL )
extract_boundaries( spdf, country, level = 3, username, password, export = FALSE, name = NULL )
spdf |
PEPFAR Global Shapefile |
country |
Country name |
level |
Orgunit level |
username |
Datim username |
password |
Datim password |
export |
Export extract as shapefile? |
name |
Export filename |
sf object with orgunit attributes
Extract facility sites
extract_facilities(.data)
extract_facilities(.data)
.data |
Datim organisation units data frame - with label and coordinates columns |
## Not run: grabr::datim_pull_hierarchy(...) %>% extract_facilities() ## End(Not run)
## Not run: grabr::datim_pull_hierarchy(...) %>% extract_facilities() ## End(Not run)
Extract location data
extract_locations( country, username, password, level = NULL, add_geom = TRUE, baseurl = NULL )
extract_locations( country, username, password, level = NULL, add_geom = TRUE, baseurl = NULL )
country |
PEPFAR Operating Unit or Country name |
username |
Datim Account Username |
password |
Datim Account password |
level |
PEPFAR Org Level, optional |
add_geom |
Include geometry column, default value is true |
baseurl |
Datim URL |
A dataframe or Null if not match
## Not run: extract_locations("<saturn>", "<my_username>", "<my_password>") ## End(Not run)
## Not run: extract_locations("<saturn>", "<my_username>", "<my_password>") ## End(Not run)
Extract raster data for an AOI (Countries)
extract_raster(countries, ras, mask = FALSE, buffer = 0.1)
extract_raster(countries, ras, mask = FALSE, buffer = 0.1)
countries |
List of the country names or sf object |
ras |
RasterLayer or Path to raster file |
mask |
Should the extracted data match the exact boundary limits? |
buffer |
Extend AOI extent by x |
spdf spatial dataframe
## Not run: library(gisr) library(sf) get_terrain(countries = list("Zambia")) get_terrain(countries = list("Zambia"), mask = TRUE) get_terrain(countries = list("Zambia"), buffer = .5, terr = "../../HDX_Data") ## End(Not run)
## Not run: library(gisr) library(sf) get_terrain(countries = list("Zambia")) get_terrain(countries = list("Zambia"), mask = TRUE) get_terrain(countries = list("Zambia"), buffer = .5, terr = "../../HDX_Data") ## End(Not run)
Extract Road Network data from OSM
extract_roads(aoi, radius = NULL, clip = FALSE)
extract_roads(aoi, radius = NULL, clip = FALSE)
aoi |
Area of Interest as sf object |
radius |
Buffer redius in meters, default = 1000m |
clip |
Should the output be clipped to the AOI? Default is FALSE |
## Not run: library(gisr) adm_zmb <- get_admin1(countries = "Zambia") %>% dplyr::select(name) %>% dplyr::filter(name == 'Lusaka') adm_zmb %>% extract_roads() %>% gview() ## End(Not run)
## Not run: library(gisr) adm_zmb <- get_admin1(countries = "Zambia") %>% dplyr::select(name) %>% dplyr::filter(name == 'Lusaka') adm_zmb %>% extract_roads() %>% gview() ## End(Not run)
Get attributes from feature class
gattributes(geodata)
gattributes(geodata)
geodata |
spatial data |
attribures as data frame
## Not run: library(gisr) # Admin level 0 [country] geodata adm0 <- get_admin0("Ghana") # Extract attrs from geodata .df <- gattributes(geodata = adm0) head(.df) ## End(Not run)
## Not run: library(gisr) # Admin level 0 [country] geodata adm0 <- get_admin0("Ghana") # Extract attrs from geodata .df <- gattributes(geodata = adm0) head(.df) ## End(Not run)
Report locations data completeness
generate_facilities_report( cntry, user, pass, terr_path = NULL, output_folder = NULL )
generate_facilities_report( cntry, user, pass, terr_path = NULL, output_folder = NULL )
cntry |
Country name |
user |
Datim account username |
pass |
Datim account password (glamr::mypwd is recommended) |
terr_path |
Path to terrain raster data |
output_folder |
Output folder |
## Not run: generate_sites_report(cntry = "saturn", mer_sites = sites) ## End(Not run)
## Not run: generate_sites_report(cntry = "saturn", mer_sites = sites) ## End(Not run)
Generate a buffer around an Area of Interest
geo_fence(aoi, radius = 1000, append = TRUE)
geo_fence(aoi, radius = 1000, append = TRUE)
aoi |
Area of Interest as sf object |
radius |
Buffer redius in meters, default = 1000m |
append |
Should the buffered area be appended to the AOI? Default is TRUE |
simple feature class
## Not run: library(gisr) adm <- get_admin0(countries = "Zambia") adm %>% geo_fence(radius = 5000, append = TRUE) %>% gview() adm %>% geo_fence(radius = 5000, append = FALSE) %>% gview() ## End(Not run)
## Not run: library(gisr) adm <- get_admin0(countries = "Zambia") adm %>% geo_fence(radius = 5000, append = TRUE) %>% gview() adm %>% geo_fence(radius = 5000, append = FALSE) %>% gview() ## End(Not run)
Get neighbors of a given contry
geo_neighbors(src, countries, var = "sovereignt", crs = 4326, crop = FALSE)
geo_neighbors(src, countries, var = "sovereignt", crs = 4326, crop = FALSE)
src |
Source spatial data frame |
countries |
countries of interest |
var |
Variable name |
crs |
Coordinates reference system, default is WGS84 (EPGS:4326) |
crop |
Crop sfc to focus countries extent? |
simple feature class
## Not run: library(gisr) cntry <- "Zambia" shp_ne <- get_necountries() # Country + neighbors boundaries cntries <- geo_neighbors(src = shp_ne, countries = cntry) # Country + neighbors boundaries: crop to country extent cntries <- geo_neighbors(src = shp_ne, countries = cntry, crop = TRUE) cntries %>% gview() ## End(Not run)
## Not run: library(gisr) cntry <- "Zambia" shp_ne <- get_necountries() # Country + neighbors boundaries cntries <- geo_neighbors(src = shp_ne, countries = cntry) # Country + neighbors boundaries: crop to country extent cntries <- geo_neighbors(src = shp_ne, countries = cntry, crop = TRUE) cntries %>% gview() ## End(Not run)
sf boundaries data for a given country
get_admin0(countries, scale = c("medium", "large", "small"), crs = 4326)
get_admin0(countries, scale = c("medium", "large", "small"), crs = 4326)
countries |
list of country names |
scale |
spatial resolution of the geodata |
crs |
coordinates reference system |
simple feature class
## Not run: library(gisr) get_admin0(countries = list("Zambia")) ## End(Not run)
## Not run: library(gisr) get_admin0(countries = list("Zambia")) ## End(Not run)
sf boundaries data for a given country
get_admin1(countries, crs = 4326)
get_admin1(countries, crs = 4326)
countries |
list of country names |
crs |
coordinates reference system |
simple feature class
## Not run: library(gisr) get_admin1(countries = list("Zambia")) ## End(Not run)
## Not run: library(gisr) get_admin1(countries = list("Zambia")) ## End(Not run)
Get Attributes Data for Orgunit Boundaries
get_attributes( country, username, password, folderpath = NULL, search = "orghierarchy", baseurl = NULL )
get_attributes( country, username, password, folderpath = NULL, search = "orghierarchy", baseurl = NULL )
country |
OU/country |
username |
Datim username |
password |
Datim password |
folderpath |
Local directory of files |
search |
Search keyword |
baseurl |
Datim URL |
OU Orgunit level as df
This will attempt to read data from local directory when folderpath is not set to null. If null, username and password will be required
## Not run: library(gisr) get_attributes(country = "Nigeria") ## End(Not run)
## Not run: library(gisr) get_attributes(country = "Nigeria") ## End(Not run)
Get Basemap
get_basemap(spdf, terr, country = NULL, add_admins = FALSE)
get_basemap(spdf, terr, country = NULL, add_admins = FALSE)
spdf |
PEPFAR ORGs Spatial Data |
terr |
RasterLayer |
country |
OU or Country Name |
add_admins |
Should the sub-admins be added? Default is false |
ggplot plot of base map
## Not run: library(gisr) library(sf) shp <- get_pepfar_shp(shp_path = glamr::si_path("path_vector"), add_attr = TRUE) ras <- get_raster(terr_path = glamr::si_path("path_raster")) get_basemap(spdf = shp, country = "Nigeria", terr = ras) ## End(Not run)
## Not run: library(gisr) library(sf) shp <- get_pepfar_shp(shp_path = glamr::si_path("path_vector"), add_attr = TRUE) ras <- get_raster(terr_path = glamr::si_path("path_raster")) get_basemap(spdf = shp, country = "Nigeria", terr = ras) ## End(Not run)
Create square grids
get_grids(spdf, size = 15000, clip = TRUE)
get_grids(spdf, size = 15000, clip = TRUE)
spdf |
input spatial data frame |
size |
size of each hex bin in meters, default set to 15K meters (15KM) |
clip |
Should the output be cliped to the input boundaries? Default is TRUE |
country hex polygon as feature class
## Not run: library(gisr) shp <- get_admin0(countries = "Nigeria") get_grids(shp, 10000) ## End(Not run)
## Not run: library(gisr) shp <- get_admin0(countries = "Nigeria") get_grids(shp, 10000) ## End(Not run)
Create hexagonal grids
get_hexbins(spdf, size = 15000, clip = TRUE)
get_hexbins(spdf, size = 15000, clip = TRUE)
spdf |
input spatial data frame |
size |
size of each hex bin in meters, default set to 15K meters (15KM) |
clip |
Should the output be cliped to the input boundaries? Default is TRUE |
country hex polygon as feature class
## Not run: library(gisr) shp <- get_admin0(countries = "Nigeria") get_hexbins(shp, 10000) ## End(Not run)
## Not run: library(gisr) shp <- get_admin0(countries = "Nigeria") get_hexbins(shp, 10000) ## End(Not run)
Get Natural Earth Polygons
get_nepolygons( scale = c("large", "small", "medium"), type = c("countries", "map_units", "sovereignty", "tiny_countries") )
get_nepolygons( scale = c("large", "small", "medium"), type = c("countries", "map_units", "sovereignty", "tiny_countries") )
scale |
Scale of the map - options are 'large', 'small', 'medium' |
type |
country type - options are 'countries', 'map_units', 'sovereignty', 'tiny_countries' |
world countries as sf object
## Not run: library(gisr) shp_ne <- get_nepolygons() shp_ne <- get_nepolygons(scale = "large", type = "countries") ## End(Not run)
## Not run: library(gisr) shp_ne <- get_nepolygons() shp_ne <- get_nepolygons(scale = "large", type = "countries") ## End(Not run)
Get Terrain Raster dataset
get_raster(folderpath, name = NULL, rename = FALSE, ...)
get_raster(folderpath, name = NULL, rename = FALSE, ...)
folderpath |
Path to raster file, default will be |
name |
Name of the raster file (with extension), default is set to terrain raster |
rename |
Should the RasterLayer be renamed? If yes, the name is changed to |
... |
Additional arguments to be passed to |
RasterLayer
## Not run: library(glamr) library(gisr) get_raster() get_raster(name = "sample.tif") get_raster(path = "./geodata/raster", name = "sample.tif") ## End(Not run)
## Not run: library(glamr) library(gisr) get_raster() get_raster(name = "sample.tif") get_raster(path = "./geodata/raster", name = "sample.tif") ## End(Not run)
Get terrain data for an AOI (Countries)
get_terrain(countries, terr, mask = FALSE, buffer = 0.1)
get_terrain(countries, terr, mask = FALSE, buffer = 0.1)
countries |
List of the country names or sf object |
terr |
RasterLayer or Path to terrain raster file |
mask |
Should the extracted data match the exact boundary limits? |
buffer |
Extend AOI extent by x |
spdf spatial dataframe
get_terrain()
will evantually be replaced by extract_raster()
## Not run: library(gisr) library(sf) get_terrain(countries = list("Zambia")) get_terrain(countries = list("Zambia"), mask = TRUE) get_terrain(countries = list("Zambia"), buffer = .5, terr = "../../HDX_Data") ## End(Not run)
## Not run: library(gisr) library(sf) get_terrain(countries = list("Zambia")) get_terrain(countries = list("Zambia"), mask = TRUE) get_terrain(countries = list("Zambia"), buffer = .5, terr = "../../HDX_Data") ## End(Not run)
Get PEPFAR Visual Crossing Polygons
get_vcpolygons(folderpath, name = NULL)
get_vcpolygons(folderpath, name = NULL)
folderpath |
Path to PEPFAR Global Shapefile |
name |
Name or pattern of shapefile |
sf object
## Not run: library(gisr) shp_pepfar <- get_vcpolygons() shp_pepfar <- get_vcpolygons(folderpath = glamr::si_path("path_vector")) shp_pepfar <- get_vcpolygons(folderpath = "./GIS", name = "VcPepfarPolygons.shp") ## End(Not run)
## Not run: library(gisr) shp_pepfar <- get_vcpolygons() shp_pepfar <- get_vcpolygons(folderpath = glamr::si_path("path_vector")) shp_pepfar <- get_vcpolygons(folderpath = "./GIS", name = "VcPepfarPolygons.shp") ## End(Not run)
Plot sf features
gview(geodata, ...)
gview(geodata, ...)
geodata |
spatial data as sf object |
... |
arguments passed to geom_sf |
## Not run: library(gisr) adm0 <- get_admin0("Ghana") adm0 %>% dplyr::select(name) %>% gview() ## End(Not run)
## Not run: library(gisr) adm0 <- get_admin0("Ghana") adm0 %>% dplyr::select(name) %>% gview() ## End(Not run)
Export spatial data as shapefile with flags
spdf_export(spdf, name)
spdf_export(spdf, name)
spdf |
sf object |
name |
filename with full path |
boolean
## Not run: library(gisr) library(sf) shp <- get_admin0(countries = "Nigeria") export_spdf(spdf = shp, name = "./GIS/nga_country_boundaries") export_spdf(spdf = shp, name = "./GIS/nga_country_boundaries.shp") ## End(Not run)
## Not run: library(gisr) library(sf) shp <- get_admin0(countries = "Nigeria") export_spdf(spdf = shp, name = "./GIS/nga_country_boundaries") export_spdf(spdf = shp, name = "./GIS/nga_country_boundaries.shp") ## End(Not run)
Generate Point Spatial DataFrame
spdf_points(.data, lat = "latitude", long = "longitude", crs = 4326)
spdf_points(.data, lat = "latitude", long = "longitude", crs = 4326)
.data |
Location data as a data frame, use |
lat |
Column name for latitude, default value is latitude |
long |
Column name for longitude, default value is longitude |
crs |
Coordinate Reference System, default value is EPSG Code for WGS 1984 |
list of spatial data frames
## Not run: cntry <- "Ethiopia" level_fac <- grabr::get_ouorglevel(operatingunit = cntry, org_type = "facility") df_facs <- extract_locations(country = cntry, level = level_fac) df_facs <- df_facs %>% extract_facilities() df_locs <- df_facs %>% select(-c(geom_type:nested)) spdf <- spdf_points(.data = df_locs) ## End(Not run)
## Not run: cntry <- "Ethiopia" level_fac <- grabr::get_ouorglevel(operatingunit = cntry, org_type = "facility") df_facs <- extract_locations(country = cntry, level = level_fac) df_facs <- df_facs %>% extract_facilities() df_locs <- df_facs %>% select(-c(geom_type:nested)) spdf <- spdf_points(.data = df_locs) ## End(Not run)
Generate a terrain map
terrain_map( countries, adm0 = NULL, adm1 = NULL, add_neighbors = FALSE, add_labels = FALSE, mask = FALSE, terr = NULL )
terrain_map( countries, adm0 = NULL, adm1 = NULL, add_neighbors = FALSE, add_labels = FALSE, mask = FALSE, terr = NULL )
countries |
List of countries to map |
adm0 |
Admin 0 boundaries, optional sf geodata |
adm1 |
Admin 1 boundaries, optional sf geodata |
add_neighbors |
Should the map include the neighbor countries |
add_labels |
Add neighbors countries's names (works only when add_neighbors is TRUE) |
mask |
Should the extracted data match the exact boundary limits? |
terr |
RasterLayer dataset or Path for terrain raster file |
ggplot basemap
## Not run: library(gisr) gisr::terrain_map(countries = list("Zambia")) gisr::terrain_map(countries = list("Zambia"), add_neighbors = TRUE) ## End(Not run)
## Not run: library(gisr) gisr::terrain_map(countries = list("Zambia")) gisr::terrain_map(countries = list("Zambia"), add_neighbors = TRUE) ## End(Not run)
Compress all shapefile components into a zipped file
zip_shapefiles(filename, folderpath = NULL)
zip_shapefiles(filename, folderpath = NULL)
filename |
Shapefile full path and name |
folderpath |
Where to place the zipped files |
Boolean