I have OSM coordinates on the location of 260 Chicagoland supermarkets:
chicagosupermarkets =
Join[
Uncompress[BarcodeRecognize[Import["https://i.stack.imgur.com/kSqkM.png"]]],
Uncompress[BarcodeRecognize[Import["https://i.stack.imgur.com/kWFxR.png"]]]]
Note that the coordinates are listed with longitudes first, which is nonstandard but useful for simple 2D plots.
What I essentially want to do is to calculate the Voronoi cell of each of the supermarkets, but I cannot use the builtin VoronoiMesh
or VoronoiDiagram
because I need to use actual mileages instead of just Euclidean coordinate distances (which are not accurate measures), and the built-in functions don't take a distance measure as an optional argument. I calculate the mileage between two coordinates as follows:
spheremiles[{θ1_, ϕ1_}, {θ2_, ϕ2_}] :=
3959
InverseHaversine[
Haversine[ϕ1 Degree - ϕ2 Degree] +
Cos[ϕ1 Degree] Cos[ϕ2 Degree] Haversine[θ1 Degree - θ2 Degree]]
For reference, this is the same as the standard implementation listed here but for the fact that it takes the longitude as the first input and returns miles instead of kilometers.
Then I can, in principle, generate the cells as follows:
cells =
And @@@
Table[
spheremiles[chicagosupermarkets[[i]], {x, y}] <=
spheremiles[chicagosupermarkets[[j]], {x, y}],
{i, Length[chicagosupermarkets]},
{j, Complement[Range[Length[chicagosupermarkets]], {i}]}];
And this does work to some extent: if I'm patient and careful, I can look at a particular Voronoi cell:
RegionPlot[cells[[1]], {x, -88, -87.5}, {y, 41.8, 42.2}]
But cells[[1]]
is still a list of 259 conditions, not a well-defined region. I want to make it a region so that I can integrate over it (to find its centroid). Because I'm doing this to eventually implement Lloyd's algorithm, I need to be able to generate these regions and work with them as efficiently as possible. The regions don't have to be all that precise -- discretizations would be fine.
I've also tried Region[ImplicitRegion[cells[[1]], {x, y}]]
, but this tanked my computer -- I had to restart.
How do I generate regions for these Voronoi cells as efficiently as possible?