Skip to content
Prev 29541 / 29559 Next

Warnings and error during the creation of the neighborhood matrix by recoding old code using deprecated rgdal in CRAN with sf and sp

200% echo everything said by Dr. Bivand below.

In addition, I would focus on the error message:

Empty neighbor sets found (zero.policy: FALSE)


This is the thing you need to solve. Why are there locations without
neighbors? A "cheap way out" which we offer in ArcGIS Pro is to provide a
union of the neighbor sets using KNN and contiguity. This ensures that each
location has at minimum k number of neighbors. You can accomplish this like
the example below. *However*, I would consider the implications of using
distance metrics for polygons. See if what you're doing actually makes
sense conceptually.

library(spdep)

geo <- system.file("shapes/columbus.gpkg", package="spData") |>
  sf::st_read(quiet = TRUE) |>
  sf::st_geometry()

nb_contig <- poly2nb(geo)

# helper from sfdep for st_centroid() -> knearneigh() -> knn2nb()
nb_k1 <- sfdep::st_knn(geo)

union.nb(nb_contig, nb_k1)

On Wed, Dec 17, 2025 at 1:06?AM Roger Bivand via R-sig-Geo <
r-sig-geo at r-project.org> wrote: