Created
November 7, 2019 19:03
-
-
Save zentree/d05f470588568c025618999b8bdf57ff 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(dplyr) | |
library(ggplot2) | |
library(osmdata) | |
library(sf) | |
library(purrr) | |
library(cowplot) | |
library(spatstat) | |
library(maptools) | |
theme_set(theme_minimal()) | |
# Christchurch limits | |
chch <- st_read('./data/chchshape/Christchurch_TA.shp') | |
# Read green areass data from Openstreetmap | |
greens <- opq(bbox = 'christchurch, new zealand') %>% | |
add_osm_feature(key = 'leisure', value = 'park') %>% | |
osmdata_sf() | |
# Polygons to Spatial format (for part A) | |
parks_poly <- greens$osm_polygons %>% | |
st_transform(2193) | |
parks_inside <- parks_poly[chch,] | |
part_a <- ggplot() + geom_sf(data = parks_inside, fill = 'chartreuse4') + | |
geom_sf(data = chch, alpha = 0, colour = 'black') + | |
coord_sf(xlim = c(1550000, 1581950), ylim = c(5165606, 5194534)) | |
parks_inside <- as_Spatial(parks_inside) | |
part_a <- tm_shape(parks_inside) + tm_polygons('chartreuse4') + | |
tm_shape(chch, bbox = tmaptools::bb(c(1554369, 1581950, 5165606, 5194534), 2, 2)) + | |
tm_borders('black') | |
# Extracting park polygons' centroids (to start part B) | |
parks <- greens$osm_polygons %>% | |
st_transform(2193) %>% | |
select(osm_id) %>% | |
mutate(area = as.numeric(st_area(.))) %>% | |
st_set_geometry(value = st_centroid(st_geometry(.))) %>% | |
mutate(x = map_dbl(geometry, 1), | |
y = map_dbl(geometry, 2)) | |
# Tesselation loosely based on https://mgimond.github.io/Spatial/interpolation-in-r.html | |
parks_ng <- as_Spatial(parks[chch,]) | |
parks_ng@bbox <- as_Spatial(chch)@bbox | |
tess2 <- as(dirichlet(as.ppp(parks_ng)), 'SpatialPolygons') | |
proj4string(tess2) <- proj4string(parks_ng) | |
parks_tess2 <- over(tess2, parks_ng) | |
parks_tess2_df <- SpatialPolygonsDataFrame(tess2, parks_tess2) | |
parks_tess2_clipped <- raster::intersect(as_Spatial(chch), parks_tess2_df) | |
part_b <- ggplot() + | |
geom_sf(data = st_as_sf(parks_tess2_clipped), aes(fill = log10(area))) + | |
coord_sf(xlim = c(1550000, 1581950), ylim = c(5165606, 5194534)) + | |
scale_fill_gradient(low = '#b2e2e2', high = '#006d2c') + | |
theme(legend.position="none") | |
plot_grid(part_a, part_b) | |
ggsave('./outputs/08-parks-voronoid-green.png') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment