Last active
July 16, 2021 03:34
-
-
Save SimonCoulombe/1f2fade5144ecc351164c2e6ae1fe493 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(magrittr) | |
library(dplyr) | |
library(sf) | |
#devtools::install_github("mountainMath/tongfen") | |
library(tongfen) | |
library(cancensus) | |
library(tidygeocoder) | |
get_utm_crs <- function(longitude){ 32600 + ceiling((longitude - -180)/ 6)} # pour calculer des buffers ronds en mètres comme dans le day 5 de https://gitlab.com/dickoa/30daymapchallenge/-/blob/master/day5/day5-blue.R | |
# la UTM zone (genre 19N pour québec à longitude -71 se trouve en comptant combien de zones de 6 degrés de longitude tu es à partir de -180 degrés... le CRS c'est 32600 + numéro de zone) | |
addresses <- tibble(address = "169 route du Long Sault,Saint-André-D'Argenteuil, Québec, Canada") %>% | |
geocode(address = address, method = 'osm',lat = latitude , long = longitude) %>% | |
mutate(utm_crs = get_utm_crs(longitude)) | |
get_circle <- function(lat, lon, radius){ | |
tibble(lat = lat, lon = lon) %>% | |
st_as_sf(coords = c("lon","lat"), crs = 4326) %>% | |
st_transform( crs = get_utm_crs(lon)) %>% | |
st_buffer(dist = units::set_units(radius, km)) %>% | |
st_transform(4326) | |
} | |
get_populations_from_polygon <- function(sf_tibble){ | |
intersecting_das <- cancensus::get_intersecting_geometries("CA16", level = "DA", geometry = sf_tibble, quiet= TRUE) | |
census_data <- get_census("CA16", regions = intersecting_das, geo_format ="sf", quiet = TRUE) | |
populations_in_geometry <- tongfen_estimate( | |
sf_tibble, | |
census_data, | |
meta_for_additive_variables("census_data", "Population") | |
) | |
return(populations_in_geometry) | |
} | |
get_populations_from_lat_lon_radius <- function(lat,lon, radius){ | |
z <- get_circle(lat, lon, radius) | |
get_populations_from_polygon(z) | |
} | |
z <- get_circle(45.56222, -74.33825, 1 ) | |
get_populations_from_polygon(z) | |
get_populations_from_lat_lon_radius(45.56222, -74.33825, 1 ) %>% mapview() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment