# Combining zoning systems

This vignettes demonstrates possibilities when zoning systems from different cities meet. It raises the question: how should different systems be combined geographically?

You need the latest version of the package:

    remotes::install_github("zonebuilders/zonebuilder")

For this demo, we need the following libraries:

    library(zonebuilder) # for the zoning system
    library(sf)          # for processing spatial data
    library(dplyr)       # for processing general data
    library(tmap)        # for visualizing spatial data

We will apply the zoning system to the main Dutch cities, and analyse commuting patterns between the zones. The data can be read as follows:

    NLD_cities = readRDS(url("https://github.com/zonebuilders/zonebuilder/releases/download/0.0.1/NLD_cities.Rds"))
    NLD_wijk_od = readRDS(url("https://github.com/zonebuilders/zonebuilder/releases/download/0.0.1/NLD_wijk_od.Rds"))
    NLD_wijk_centroids = readRDS(url("https://github.com/zonebuilders/zonebuilder/releases/download/0.0.1/NLD_wijk_centroids.Rds"))

Let’s take a look at the `NLD_cities` data:

    NLD_cities %>% arrange(desc(population))

…and plot it on an interactive map:

    tmap_mode("view") # enable interactive mode in tmap
    qtm(NLD_cities, symbols.size = "population")

The following code chunk generated zones for the Dutch cities:

    zbs = do.call(rbind, lapply(1:nrow(NLD_cities), function(i) {
      ci = NLD_cities[i, ]

      # Amsterdam 5, Eindhoven-Rotterdam 4, Roermond-Zeeland 2, others 3
      nrings = ifelse(ci$population < 60000, 2,
               ifelse(ci$population < 220000, 3,
               ifelse(ci$population < 800000, 4, 5)))

      zb = zb_zone(x = ci, n_circles = nrings) %>%
        mutate(name = ci$name,
               labelplus = paste(ci$name, label, sep = "_"))

      zb
    }))

    tm_basemap("OpenStreetMap") +
    tm_shape(zbs) +
      tm_polygons(col = "circle_id", id = "labelplus", style = "cat", palette = "YlOrBr", alpha = 0.7) +
      tm_scale_bar()