Department of Mathematics
University of Manitoba*
Recent movement (5-10 years): governments (local or higher) create portals where data are centralised and published
allTrees = read.csv("https://data.winnipeg.ca/api/views/hfwk-jp4h/ro
After this,
dim(allTrees)
## [1] 300846
15
elms_idx = grep("American Elm", allTrees$Common.Name, ignore.case = TRUE)
elms = allTrees[elms_idx, ]
We are left with 54,036 American elms
(Needs a relatively large machine here - about 50GB RAM)
elms_xy = cbind(elms$X, elms$Y)
D = dist(elms_xy)
idx_D = which(D<50)
indices_LT
is a large \(N(N-1)/2\times 2\) matrix with indices (orig,dest) of trees in the pairs of elms, so indices_LT[idx_D]
are the pairs under consideration
Keep a little more..
indices_LT_kept = as.data.frame(cbind(indices_LT[idx_D,],
D[idx_D]))
colnames(indices_LT_kept) = c("i","j","dist")
tree_locs_orig = cbind(elms_latlon$lon[indices_LT_kept$i],
elms_latlon$lat[indices_LT_kept$i])
tree_locs_dest = cbind(elms_latlon$lon[indices_LT_kept$j],
elms_latlon$lat[indices_LT_kept$j])
tree_pairs = do.call(
sf::st_sfc,
lapply(
1:nrow(tree_locs_orig),
function(i){
sf::st_linestring(
matrix(
c(tree_locs_orig[i,],
tree_locs_dest[i,]),
ncol=2,
byrow=TRUE)
)
}
)
)
library(tidyverse)
# Get bounding polygon for Winnipeg
bb_poly = osmdata::getbb(place_name = "winnipeg",
format_out = "polygon")
# Get roads
roads <- osmdata::opq(bbox = bb_poly) %>%
osmdata::add_osm_feature(key = 'highway',
value = 'residential') %>%
osmdata::osmdata_sf () %>%
osmdata::trim_osmdata (bb_poly)
# Get rivers
rivers <- osmdata::opq(bbox = bb_poly) %>%
osmdata::add_osm_feature(key = 'waterway',
value = "river") %>%
osmdata::osmdata_sf () %>%
osmdata::trim_osmdata (bb_poly)
st_crs(tree_pairs) = sf::st_crs(roads$osm_lines$geometry)
iroads = sf::st_intersects(x = roads$osm_lines$geometry,
y = tree_pairs)
irivers = sf::st_intersects(x = rivers$osm_lines$geometry,
y = tree_pairs)
tree_pairs_roads_intersect = c()
for (i in 1:length(iroads)) {
if (length(iroads[[i]])>0) {
tree_pairs_roads_intersect = c(tree_pairs_roads_intersect,
iroads[[i]])
}
}
tree_pairs_roads_intersect = sort(tree_pairs_roads_intersect)
to_keep = 1:dim(tree_locs_orig)[1]
to_keep = setdiff(to_keep,tree_pairs_roads_intersect)
library(wbstats)
pop_data_CTRY <- wb_data(country = "ZAF", indicator = "SP.POP.TOTL",
mrv = 100, return_wide = FALSE)
y_range = range(pop_data_CTRY$value)
y_axis <- make_y_axis(y_range)
png(file = "pop_ZAF.png",
width = 800, height = 400)
plot(pop_data_CTRY$date, pop_data_CTRY$value * y_axis$factor,
xlab = "Year", ylab = "Population", type = "b", lwd = 2,
yaxt = "n")
axis(2, at = y_axis$ticks, labels = y_axis$labels, las = 1)
dev.off()
crop_figure("pop_ZAF.png")