Skip to content

Instantly share code, notes, and snippets.

@bohdanszymanik
Last active July 19, 2021 22:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bohdanszymanik/f23912fb6b162bd4803b4fe6354036d8 to your computer and use it in GitHub Desktop.
Save bohdanszymanik/f23912fb6b162bd4803b4fe6354036d8 to your computer and use it in GitHub Desktop.
R sample to count locations in areas
##################################################################################
# Import school locations
# Import some areas
# Join counts
# write out
library(readxl)
library(writexl)
library(sf)
HOME <- Sys.getenv("HOME")
setwd(paste(HOME, "\\Documents\\LocalProjects\\SchoolsInAreas", sep=""))
getwd()
# first get the column names
cNames <- read_excel(path="Schools.xlsx", sheet="Analysis of directory", skip=1, n_max=1) %>% names()
# then the raw data
schs <- read_excel(path="Schools.xlsx", sheet="Analysis of directory", skip=3, col_names=cNames[1:42])
schsSF <- st_as_sf(schs, coords = c("Longitude", "Latitude"), crs=4326)
pAreas = st_read("layers\\area.gdb")
pAreas_wgs84 <- st_transform(pAreas, 4326)
schoolsInAreas <- st_join(schsSF, pAreas_wgs84, join=st_intersects)
write_xlsx(schoolsInAreas, "schoolsInAreas.xlsx")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment