Skip to content
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

write listw to ArcGIS SWM dbf format #171

Merged
merged 9 commits into from
Nov 24, 2024
Merged

Conversation

JosiahParry
Copy link
Contributor

This PR introduces a new function that enables a user to write a listw object to the .dbf format that can be converted into a binary .swm file within ArcGIS Pro.

R/read.gwt2nb.R Outdated Show resolved Hide resolved
R/read.gwt2nb.R Outdated
weight <- unlist(listw$weights)
# construct a data frame from the flattened structure
# note that the columns _must_ be integers for the ArcGIS Pro tool to work
res <- data.frame(as.integer(from), as.integer(to), weight)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly here, the region.id must be coercible to integer to be compatible with the swm file format.

Should tryCatch() be used to test if the data is coercible?

@JosiahParry JosiahParry marked this pull request as ready for review October 29, 2024 21:41
@JosiahParry
Copy link
Contributor Author

Reprex:

library(spdep)
#> Loading required package: spData
#> To access larger datasets in this package, install the spDataLarge
#> package with: `install.packages('spDataLarge',
#> repos='https://nowosad.github.io/drat/', type='source')`
#> Loading required package: sf
#> Linking to GEOS 3.11.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE

# read the NC SIDS dataset 
nc_sf <- sf::st_read(system.file("gpkg/nc.gpkg", package="sf")[1])
#> Reading layer `nc.gpkg' from data source 
#>   `/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/sf/gpkg/nc.gpkg' 
#>   using driver `GPKG'
#> Simple feature collection with 100 features and 14 fields
#> Geometry type: MULTIPOLYGON
#> Dimension:     XY
#> Bounding box:  xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
#> Geodetic CRS:  NAD27

# create a UniqueID field
nc_sf$UniqueID <- as.character(1:nrow(nc_sf))

# create a listw object
listw <- nb2listw(
  poly2nb(nc_sf$geom)
)

# create temporary file
tmp <- tempfile(fileext = ".dbf")

# write it out
write.swmdbf(listw, tmp, "UniqueID", region.id = nc_sf$UniqueID)
#> Loading required namespace: foreign

# read it back
read.swmdbf2listw(tmp, style = "B")
#> Warning in read.swmdbf2listw(tmp, style = "B"): region.id not given, c(MYID,
#> NID) range is 1:100
#> Characteristics of weights list object:
#> Neighbour list object:
#> Number of regions: 100 
#> Number of nonzero links: 490 
#> Percentage nonzero weights: 4.9 
#> Average number of links: 4.9 
#> 
#> Weights style: B 
#> Weights constants summary:
#>     n    nn  S0       S1       S2
#> B 100 10000 100 44.65023 410.4746

@rsbivand
Copy link
Member

Thanks, this looks sensible, do you @JosiahParry or @scw have any further changes?

@JosiahParry JosiahParry changed the title [Draft] write listw to ArcGIS SWM dbf format write listw to ArcGIS SWM dbf format Nov 1, 2024
@JosiahParry
Copy link
Contributor Author

@rsbivand I've gone ahead and fixed the spaces v tab issue. That should be resolved. I'm good with this one!

@mjanikas shared some code with me a while ago I rediscovered for reading the binary version of the swm. I'll try and prepare a PR for that in the coming days. Many of us use {spdep} for validation of statistics and ideas and having a way to convert to and from is proving to make our work much nicer! Thank you for being receptive. 🙏🏼

@rsbivand
Copy link
Member

CI stuck on rgeoda missing from CRAN because BH geometry headers are broken in clang 19 GeoDaCenter/rgeoda#49 eddelbuettel/bh#101. I guess I'll drop the remainder of rgeoda until BH is updated next month or thereafter.

@JosiahParry
Copy link
Contributor Author

Oof! The issue is looks to be for Fedora 36 which reached end of life 18 months ago. FWIW I have a CRAN package that does not compile on Fedora 36. As long as the error is informative and occurs during the configure step with a note in cran-comments.md, it seems to be acceptable.

@rsbivand
Copy link
Member

No, it applies for forthcoming clang, Boost headers are not defensively written, and have fixed this upstream, but a new BH package release is needed.

@rsbivand
Copy link
Member

Problems also because of https://forum.posit.co/t/r-cmd-check-fails-on-macos-latest-release-r-lib-actions-setup-r-v2/195084. Local R CMD check is more reliable than CI, at least in this case. This PR seems to be associated with a stale version of main, as only macOS fails for my commit to remove rgeoda.

Check from and to for coercion to integer and trap failures
@rsbivand rsbivand merged commit 00f3380 into r-spatial:main Nov 24, 2024
0 of 4 checks passed
@rsbivand
Copy link
Member

Apart from the macOS CI, looks good!

@rsbivand
Copy link
Member

1.3-7 submitted 13:41 CET, online 14:58 CET

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants