-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
poly2nb doesn't find all neighbors #162
Comments
Will look when I get a chance. Could you rephrase the dplyr parts in base R please, I do not use tidy verbs ever, cognitive overload. The first count extraction looks like |
And in general,
This indicates trouble of some kind, and that many unexpected subgraphs suggests that default |
Base R repro
df <- sf::st_read("tokyo.shp")
swm <- foreign::read.dbf(r"{tokio-contig-swm.dbf}")
nb <- spdep::poly2nb(df)
cards <- spdep::card(nb)
# count nbs spdep
spdep_counts <- data.frame(i = df$SOURCE_, n_spdep = cards)
# count nbs arcgis pro
arc_counts <- aggregate(SOURCE_ID ~ NID, FUN = length, data = swm[, c("SOURCE_ID", "NID")])
colnames(arc_counts) <- c("i", "n_arc")
# join & sort
combined <- merge(spdep_counts, arc_counts)
combined[order(combined$n_arc, decreasing = TRUE),]
#> i n_spdep n_arc
#> 39 39 4 12
#> 183 183 12 12
#> 44 44 1 11
#> 9 9 7 10
#> 45 45 5 10
#> 171 171 9 10
#> 3 3 2 9
#> 40 40 2 9
#> truncated here.... SWM conversions
Yes! Mark shared this code with me some time ago and wants to be able to share this with the R community so that we can transfer spatial weights to and fro languages & tools. There is the file format which is a binary representation and then the output of the "convert swm to table" tool which is shared in the Would you be interested in having this functionality housed in Snap tolerancesI think this is a problem that is more noticeable with simplified geometries as is the case here. I think the snap tolerance can be increased from 1.5e-08 which is quite small to something a bit bigger based on the units as you suggest. Though, of course, there is an issue of scale and having the snap tolerance be more conservative than less is probably a good thing. That said, I think most people might not take the time to visually inspect that all of their polygons have the neighbors that they might expect! 10mm feels like a fair amount of wiggle room! It is in most cases just rounding error that one might expect--and is surely less than the error introduced by the polygon simplifications. |
Yes, I think that adding at least reading the DBF file format and converting to listw would be sensible. I recall that the binary format also worked, and that would also be fine, but both might need maintenance if anything changed in the SWM specification. Shall I look at a default 10mm snap value for I agree that visual inspection is unlikely, but might users usually run |
Yes, I think a default 10mm snap is good! I do think a suppressable warning for no-neighbor features is a good idea. More often than not that might lead to an invalid analysis or misinterpretation of analytic results. |
Today I ran into a scenario where spdep was not finding all of the neighbors for a(n oddly shaped polygon).
Below is the comparison with the neighborhoods identified via ArcGIS Pro and the neighborhoods identified via spdep. There are 9 missing contiguous neighbors from spdep. I tried increasing the
snap
distance to no avail.Attached is a zip containing the shapefile as well as a
dbf
file of the spatial weights matrix created by ArcGIS Pro.tokyo.zip
The text was updated successfully, but these errors were encountered: