st_interpolate_aw returns error - replacement has x rows, data has y
I am working to re-aggregate population counts from one administrative level to another. One particular row keeps returning the error "replacement has 4 rows, data has 2". I have tried validating the geometries, but it has not worked.
Even better, I was preparing the dummy data for postin this question at StackOverflow (which I didnt do in the end because of an excess in characters due to using dput()). After saving the dput() of the sf layers into a text file, the command line works after re-assigning the variables. This only works when using the dput output saved in a textfile, but it does not work if I directly do
st_interpolate_aw(dput(geography1),dput(geography2), extensive = TRUE).
This returns the same error.
Is there any known reason why the error might be happening, and how saving the dput() output into a .txt file might be removing the error? I have tried saving to a geojson file and then reloading, but the error remains. I have also tried using st_buffer(0) and st_make_valid(). Nothing worked so far.
I am attaching the two files with the output of dput() that were giving me the error, but since I have saved them, they will probably not reproduce it.
These .geojson files might reproduce the error when running
st_interpolate_aw(SA1_suburb, suburb, extensive = TRUE)
As an update - I got to share the geojson files in StackOverflow and somebody pointed that it might be an issue related to S2. See link: https://stackoverflow.com/questions/73962700/st-interpolate-aw-returns-error-replacement-has-x-rows-data-has-y/73963282#73963282
Is there any known reason why the error might be happening, and how saving the dput() output into a .txt file might be removing the error?
I suspect that when saving using dput() you lost the precision of the coordinates and that solved this problem.
SA1_suburb = read_sf("SA1_suburb_1221.geojson")
print(st_coordinates(SA1_suburb)[1, 1:2], digits = 18)
#> 142.7316964610000696 -36.5496727679999367
tmp = tempfile()
dput(SA1_suburb, tmp)
SA1_suburb = dget(tmp)
print(st_coordinates(SA1_suburb)[1, 1:2], digits = 18)
#> 142.7316964610000127 -36.5496727679999012
I have tried saving to a geojson file and then reloading, but the error remains. I have also tried using st_buffer(0) and st_make_valid().
I don't get this error if I use st_make_valid() beforehand or turn off the {s2} engine.
SA1_suburb = st_make_valid(SA1_suburb)
suburb = st_make_valid(suburb)
st_interpolate_aw(SA1_suburb, st_geometry(suburb), extensive = TRUE)
sf_use_s2(FALSE)
SA1_suburb = st_transform(SA1_suburb, crs = 3457)
suburb = st_transform(suburb, crs = 3457)
st_interpolate_aw(SA1_suburb, st_geometry(suburb), extensive = TRUE)
Thank you kadyb! yes, I went for disabling the S2 engine when this happens, since it seemed more explainable. But my intuition is that the cause might be similar - a very small change in the coordinates makes the very slightly overlap different, changing the some of the intersections from a line to a polygon whose area can be measured (see SO link).
Perhaps it would make sense to add this case as a warning in the package?
I have proposed a pull request that eliminates linestring and multilinestring geometries from the intermediate object used in sf::st_interpolate_aw() call. I was able to 1) reproduce the error and 2) verify that the (small) edit in code avoids the error even in {s2} setting (warning of spatially constant or uniform attributes remains, and can be in this case safely ignored).
The solution I proposed in the (now closed) pull request #2006 did not generalise, and I have closed the pull request.
I analysed the issue in more detail, and the root cause seems to be that in the old days of GEOS sf::st_intersection() could be relied to produce 1 item per combination of intersecting polygons (in case of multiple intersections producing a GEOMETRYCOLLECTION object, which then had to be handled).
This behaviour has been relied on heavily in the current implementation of sf::st_interpolate_aw() to build an index idx of intersecting polygons, which is then used to create a temporary working data frame of partial areas to interpolate from.
In the S2 world this assumption is no longer met, and the output of intersection of a one row object by two rows object is actually 4 list items. In the current code this leads to attempt to place a length 4 vector in a 2 row data frame, which crashes the function.
I am unsure what causes the discrepancy in intersections - but it is not easy to work around it in the context of the current implementation of sf::st_interpolate_aw() using indices from the intersection.
To illustrate the issue in more detail consider this piece of code; it builds on the geojsons provided by @phcantis - I was not able to reproduce the behaviour using more "standard" data, such as the North Carolina shapefile - and shows the discrepancy in behaviour of st_intersection in the old world and the new. The spatial objects themselves seem legit, but the intersection output is unexpected.
I am afraid that if I was to make the area interpolation function work around this I would have to entirely rewrite the function - as the idx object can no longer be relied upon - and I am not 100% certain I want to do that...
library(sf)
library(dplyr)
big <- st_read("SA1_suburb_1221.geojson")
small <- st_read("suburb_1221.geojson")
# both shapes are valid
st_is_valid(big)
st_is_valid(small)
# calculate intersection in S2 world...
isection <- st_intersection(st_geometry(big), st_geometry(small))
# intersection has index of 2 rows
attr(isection, "idx")
# [,1] [,2]
# [1,] 1 1
# [2,] 2 1
# but its actual length is 4!
length(isection)
# [1] 4
# compare to the old world of GEOS:
sf_use_s2(F)
isection <- st_intersection(st_geometry(big), st_geometry(small))
# 2 rows of indices...
attr(isection, "idx")
# intersection of 2 list items
length(isection)