From https://observablehq.com/@jwolondon/gridmap-allocation. Returns an LP solution allocating 2D points to a grid.
Arguments
- pts
tibble of geographic points (x,y) to be transformed for allocation to grid.
- grd
tibble defining grid positions.
- compactness
Optional parameter between 0 and 1 where 0 allocates towards edges, 0.5 preserves scaled geographic location and 1 allocates towards centre of grid. Default is 1 (compact cluster).
Examples
library(tibble)
library(sf)
#> Linking to GEOS 3.12.1, GDAL 3.8.4, PROJ 9.4.0; sf_use_s2() is TRUE
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
pts <- london_boroughs |> st_drop_geometry() |> select(area_name, x = easting, y = northing)
grd <- grid_locations(n_row = 8, n_col = 8, spacers = list())
points_normalised(pts, grd, .6)
#> area_name x y
#> 1 City of London 4.605442 4.779046
#> 2 Barking and Dagenham 6.676165 5.505787
#> 3 Barnet 3.405588 6.794108
#> 4 Bexley 6.870485 3.630489
#> 5 Brent 2.886190 5.769615
#> 6 Bromley 5.905485 1.745306
#> 7 Camden 3.946936 5.346249
#> 8 Croydon 4.812824 1.558629
#> 9 Ealing 2.541721 4.895743
#> 10 Enfield 4.665636 7.655574
#> 11 Greenwich 5.969046 3.717334
#> 12 Hackney 4.898739 5.637604
#> 13 Hammersmith and Fulham 3.458646 4.127713
#> 14 Haringey 4.454618 6.328095
#> 15 Harrow 2.312658 6.403114
#> 16 Havering 7.655574 5.972382
#> 17 Hillingdon 1.344426 5.120608
#> 18 Hounslow 1.960916 3.539768
#> 19 Islington 4.440613 5.416616
#> 20 Kensington and Chelsea 3.713295 4.332224
#> 21 Kingston upon Thames 2.871781 2.071167
#> 22 Lambeth 4.435227 3.667902
#> 23 Lewisham 5.347035 3.225538
#> 24 Merton 3.755310 2.481739
#> 25 Newham 5.728402 5.160735
#> 26 Redbridge 6.104248 6.353101
#> 27 Richmond upon Thames 2.804046 3.090619
#> 28 Southwark 4.815921 3.713651
#> 29 Sutton 3.928622 1.344426
#> 30 Tower Hamlets 5.139114 4.797074
#> 31 Waltham Forest 5.271354 6.507987
#> 32 Wandsworth 3.631823 3.379261
#> 33 Westminster 4.051435 4.684641