numbats / hexmap

Convert a map into a hex map
https://numbats.github.io/hexmap/
GNU General Public License v3.0
6 stars 0 forks source link

Allocate hexagons to regions in original data #3

Closed mitchelloharawild closed 2 years ago

mitchelloharawild commented 2 years ago

Here's what I get from geogrid's hungarian implementation, first step would be making sense of the allocation matrix and using it to join the hexagons onto the original data (oz_electorates) so you can plot them with additional info (say colour filling the hexagons by state)

library(sf)
#> Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1; sf_use_s2() is TRUE
library(hexmap)
library(ozmaps)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
oz_electorates <- abs_ced %>% 
  sfheaders::sf_remove_holes() %>% 
  # st_simplify(dTolerance = 3000) %>% 
  rmapshaper::ms_simplify(keep = 0.0001, keep_shapes = TRUE) %>% 
  mutate(weight=1) %>% 
  st_transform(crs = "+init=epsg:3857")
#> Registered S3 method overwritten by 'geojsonlint':
#>   method         from 
#>   print.location dplyr
#> Warning in CPL_crs_from_input(x): GDAL Message 1: +init=epsg:XXXX syntax is
#> deprecated. It might return a CRS with a non-EPSG compliant axis order.

hex_electorates <- hex_grid(oz_electorates, n_tiles = 151)
#> Warning: attribute variables are assumed to be spatially constant throughout all
#> geometries

dist_matrix <- sp::spDists(
  as(st_centroid(oz_electorates$geometry), "Spatial"),
  as(st_centroid(hex_electorates), "Spatial")
)

hex_allocation <- geogrid:::hungarian_cc(dist_matrix)

lattice::levelplot(hex_allocation)

Created on 2022-10-07 by the reprex package (v2.0.1)

mitchelloharawild commented 2 years ago
library(hexmap)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
australia <- ozmaps::abs_ste %>% filter(NAME != "Other Territories")
grid <- hex_grid(australia, n_tiles = 8)
#> Warning: attribute variables are assumed to be spatially constant throughout all
#> geometries
grid <- tile_allocate(australia, grid)

library(ggplot2)
ggplot() +
  geom_sf(data = australia) +
  geom_sf(data = grid, aes(fill = NAME), alpha = 0.2)

Created on 2022-10-13 by the reprex package (v2.0.1)