I have a big shape file with approx 180.000 250m^2 polygons. I want to create a spatial weight matrix (queen contiguity). So a 1 if its a neighbour, 0 otherwise. But, there are several polygons without any neighbours (islands).
How can I impute the nearest neighbour for those units that do not have any direct neighbour?
(working with sf or sp package - in R)
..........................................................................
using a sample of the data and plotting it, it would look like:
I can then create a W-Matrix with the following code:
Create NB List
sample_queen_nb <- poly2nb(sp.sample, row.names=sp.sample$ID, queen=T)
# Create Weights/list
sample_W.list.queen <- nb2listw(sample_queen_nb, style="W", zero.policy = TRUE)
# Create Matrix
sample_W.queen <- listw2mat(sample_W.list.queen)
and check its connectivity with plotting it:
# Plot ShpPolygons
plot(sp.sample)
# plot weightsmatrix to see connectivity
plot(sample_queen_nb, coords, add=TRUE, col="green", cex=0.5)
getting this:
As you can see - there is an island that's not connected. I would like to impute the neared neighbouring unit for this island-grid-cell. How can I do that?