Open barracuda156 opened 4 weeks ago
This looks like https://github.com/r-spatial/spdep/blob/main/vignettes%2Fsids.Rmd#L51. Your platform appears to be rather outdated, so the underlying problem is not connected to this package. Can sf
read this or any other external file? What versions of R, sf
and GDAL are installed?
Please run and report all output:
library(sf)
packageVersion("sf")
sf_extSoftVersion()
nc <- st_read(system.file("shapes/sids.shp", package="spData")[1])
st_crs(nc) <- "+proj=longlat +datum=NAD27"
sf_use_s2(FALSE)
a <- st_geometry(nc)
plot(a, axes=TRUE)
b <- st_centroid(a, of_largest_polygon=TRUE)
c <- st_coordinates(b)
text(c, label=nc$FIPSNO, cex=0.5)
If nc
cannot be read, the problem may be in the GDAL version used with sf
. If the plot of a
works, then GDAL is OK for 32-bit and big-endian data. If the failure is at the creation of b
, it would suggest that GEOS is unhappy with 32-bit and/or big-endian data.
@rsbivand Thank you very much. Here is what I get:
36-231% /opt/local/bin/R
R version 4.4.0 (2024-04-24) -- "Puppy Cup"
Copyright (C) 2024 The R Foundation for Statistical Computing
Platform: powerpc-apple-darwin10.0.0d2 (32-bit)
> library(sf)
Linking to GEOS 3.12.1, GDAL 3.8.5, PROJ 9.4.0; sf_use_s2() is TRUE
> packageVersion("sf")
[1] ‘1.0.16’
> sf_extSoftVersion()
GEOS GDAL proj.4 GDAL_with_GEOS USE_PROJ_H
"3.12.1" "3.8.5" "9.4.0" "true" "true"
PROJ
"9.4.0"
> nc <- st_read(system.file("shapes/sids.shp", package="spData")[1])
Reading layer `sids' from data source
`/opt/local/Library/Frameworks/R.framework/Versions/4.4/Resources/library/spData/shapes/sids.shp'
using driver `ESRI Shapefile'
Simple feature collection with 100 features and 22 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
CRS: NA
> st_crs(nc) <- "+proj=longlat +datum=NAD27"
> sf_use_s2(FALSE)
Spherical geometry (s2) switched off
> a <- st_geometry(nc)
> plot(a, axes=TRUE)
> b <- st_centroid(a, of_largest_polygon=TRUE)
Error in CPL_geodetic_area(st_geometry(x), p$SemiMajor, p$InvFlattening) :
Unknown WKB type (648)! Full WKB type number was (50331648).
> c <- st_coordinates(b)
Error: object 'b' not found
> text(c, label=nc$FIPSNO, cex=0.5)
Error in as.double(y) :
cannot coerce type 'builtin' to vector of type 'double'
Plot generation seemed to have worked fine.
Should I open an issue with GEOS upstream then?
Thanks! Could you try:
b <- st_centroid(a, of_largest_polygon=FALSE)
If that fails, next:
st_crs(a) <- NA
b <- st_centroid(a, of_largest_polygon=TRUE)
> b <- st_centroid(a, of_largest_polygon=FALSE)
Warning message:
In st_centroid.sfc(a, of_largest_polygon = FALSE) :
st_centroid does not give correct centroids for longitude/latitude data
> st_crs(a) <- NA
> b <- st_centroid(a, of_largest_polygon=TRUE)
> c <- st_coordinates(b)
> text(c, label=nc$FIPSNO, cex=0.5)
Error in text.default(c, label = nc$FIPSNO, cex = 0.5) :
plot.new has not been called yet
The last command launched X window, but an empty one.
Error caused by missing plot(a)
in same context.
library(sf)
packageVersion("sf")
sf_extSoftVersion()
nc <- st_read(system.file("shapes/sids.shp", package="spData")[1])
st_crs(nc) <- "+proj=longlat +datum=NAD27"
sf_use_s2(FALSE)
a <- st_geometry(nc)
plot(a, axes=TRUE)
b <- st_centroid(a, of_largest_polygon=FALSE)
c <- st_coordinates(b)
text(c, label=nc$FIPSNO, cex=0.5)
may work, the culprit seems to be CPL_geodetic_area
which I'm still looking for. Please also try:
library(sf)
packageVersion("sf")
sf_extSoftVersion()
nc <- st_read(system.file("shapes/sids.shp", package="spData")[1])
st_crs(nc) <- "+proj=longlat +datum=NAD27"
sf_use_s2(TRUE)
a <- st_geometry(nc)
plot(a, axes=TRUE)
b <- st_centroid(a, of_largest_polygon=TRUE)
c <- st_coordinates(b)
text(c, label=nc$FIPSNO, cex=0.5)
If I proceed after the first warning, then it the end nothing happens, no error, no output:
> st_crs(nc) <- "+proj=longlat +datum=NAD27"
> sf_use_s2(FALSE)
Spherical geometry (s2) switched off
> a <- st_geometry(nc)
> plot(a, axes=TRUE)
> b <- st_centroid(a, of_largest_polygon=FALSE)
Warning message:
In st_centroid.sfc(a, of_largest_polygon = FALSE) :
st_centroid does not give correct centroids for longitude/latitude data
> c <- st_coordinates(b)
> text(c, label=nc$FIPSNO, cex=0.5)
OK, returning to the original script:
library(sf)
packageVersion("sf")
sf_extSoftVersion()
nc <- st_read(system.file("shapes/sids.shp", package="spData")[1])
st_crs(nc) <- "+proj=longlat +datum=NAD27"
sf_use_s2(FALSE)
a <- st_geometry(nc)
plot(a, axes=TRUE)
b <- st_centroid(a, of_largest_polygon=TRUE)
run traceback()
after the error. st_centroid
goes to lots of other functtions to find the largest polygon - the problem may be there.
Running this, I got plot with numbers in the end:
> library(sf)
Linking to GEOS 3.12.1, GDAL 3.8.5, PROJ 9.4.0; sf_use_s2() is TRUE
> packageVersion("sf")
[1] ‘1.0.16’
> sf_extSoftVersion()
GEOS GDAL proj.4 GDAL_with_GEOS USE_PROJ_H
"3.12.1" "3.8.5" "9.4.0" "true" "true"
PROJ
"9.4.0"
> nc <- st_read(system.file("shapes/sids.shp", package="spData")[1])
Reading layer `sids' from data source
`/opt/local/Library/Frameworks/R.framework/Versions/4.4/Resources/library/spData/shapes/sids.shp'
using driver `ESRI Shapefile'
Simple feature collection with 100 features and 22 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
CRS: NA
> st_crs(nc) <- "+proj=longlat +datum=NAD27"
> sf_use_s2(FALSE)
Spherical geometry (s2) switched off
> a <- st_geometry(nc)
> plot(a, axes=TRUE)
> b <- st_centroid(a, of_largest_polygon=FALSE)
Warning message:
In st_centroid.sfc(a, of_largest_polygon = FALSE) :
st_centroid does not give correct centroids for longitude/latitude data
> c <- st_coordinates(b)
> text(c, label=nc$FIPSNO, cex=0.5)
And also from the second code. Result looks the same, perhaps, without comparing numbers at least.
Original script version with traceback:
> library(sf)
Linking to GEOS 3.12.1, GDAL 3.8.5, PROJ 9.4.0; sf_use_s2() is TRUE
> packageVersion("sf")
[1] ‘1.0.16’
> sf_extSoftVersion()
GEOS GDAL proj.4 GDAL_with_GEOS USE_PROJ_H
"3.12.1" "3.8.5" "9.4.0" "true" "true"
PROJ
"9.4.0"
> nc <- st_read(system.file("shapes/sids.shp", package="spData")[1])
Reading layer `sids' from data source
`/opt/local/Library/Frameworks/R.framework/Versions/4.4/Resources/library/spData/shapes/sids.shp'
using driver `ESRI Shapefile'
Simple feature collection with 100 features and 22 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
CRS: NA
> st_crs(nc) <- "+proj=longlat +datum=NAD27"
> sf_use_s2(FALSE)
Spherical geometry (s2) switched off
> a <- st_geometry(nc)
> plot(a, axes=TRUE)
> b <- st_centroid(a, of_largest_polygon=TRUE)
Error in CPL_geodetic_area(st_geometry(x), p$SemiMajor, p$InvFlattening) :
Unknown WKB type (648)! Full WKB type number was (50331648).
> traceback()
7: CPL_geodetic_area(st_geometry(x), p$SemiMajor, p$InvFlattening)
6: lwgeom::st_geod_area(x)
5: st_area.sfc(pols)
4: st_area(pols)
3: largest_ring(x[multi])
2: st_centroid.sfc(a, of_largest_polygon = TRUE)
1: st_centroid(a, of_largest_polygon = TRUE)
This reason for placing the number at the centroid of the largest polygon is that one county is made up of several parts. Thanks for the traceback!
From https://github.com/r-spatial/lwgeom/blob/9d8078cbfcc33d2739344fa60b9188c6612fcd1d/R/geod.R#L13-L19 could you try:
library(sf)
nc = st_read(system.file("shapes/sids.shp", package="spData")[1])
library(lwgeom)
st_geod_area(nc)
This fails:
> library(sf)
Linking to GEOS 3.12.1, GDAL 3.8.5, PROJ 9.4.0; sf_use_s2() is TRUE
> nc = st_read(system.file("shapes/sids.shp", package="spData")[1])
Reading layer `sids' from data source
`/opt/local/Library/Frameworks/R.framework/Versions/4.4/Resources/library/spData/shapes/sids.shp'
using driver `ESRI Shapefile'
Simple feature collection with 100 features and 22 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
CRS: NA
> library(lwgeom)
Linking to liblwgeom 3.0.0beta1 r16016, GEOS 3.12.1, PROJ 9.4.0
Attaching package: ‘lwgeom’
The following object is masked from ‘package:sf’:
st_perimeter
> st_geod_area(nc)
Error in st_geod_area(nc) : st_is_longlat(x) is not TRUE
Sorry, should have set the coordinate reference system to match:
library(sf)
nc = st_read(system.file("shapes/sids.shp", package="spData")[1])
st_crs(nc) <- "+proj=longlat +datum=NAD27"
library(lwgeom)
st_geod_area(nc)
And this triggered the error we have seen initially!
> library(sf)
Linking to GEOS 3.12.1, GDAL 3.8.5, PROJ 9.4.0; sf_use_s2() is TRUE
> nc = st_read(system.file("shapes/sids.shp", package="spData")[1])
Reading layer `sids' from data source
`/opt/local/Library/Frameworks/R.framework/Versions/4.4/Resources/library/spData/shapes/sids.shp'
using driver `ESRI Shapefile'
Simple feature collection with 100 features and 22 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
CRS: NA
> st_crs(nc) <- "+proj=longlat +datum=NAD27"
> library(lwgeom)
Linking to liblwgeom 3.0.0beta1 r16016, GEOS 3.12.1, PROJ 9.4.0
Attaching package: ‘lwgeom’
The following object is masked from ‘package:sf’:
st_perimeter
> st_geod_area(nc)
Unknown WKB type (296)! Full WKB type number was (100663296).
terminate called after throwing an instance of 'Rcpp::exception'
what(): lwgeom error
OK, thanks, I'll raise an issue with lwgeom
, ping you, and see if @edzer can see anything - maybe the LWGEOM code (or where the WKB binary geometry representation is converted to what LWGEOM wants) is susceptible to big-endedness and/or 32-bit. I'll change the vignette to use s2
and let you when I have committed and pushed.
OK, thanks, I'll raise an issue with
lwgeom
, ping you, and see if @edzer can see anything - maybe the LWGEOM code (or where the WKB binary geometry representation is converted to what LWGEOM wants) is susceptible to big-endedness and/or 32-bit. I'll change the vignette to uses2
and let you when I have committed and pushed.
Thank you!
P. S. Aside of bitness and endianness there is one more potential source of obscure breakages – Darwin ppc ABI uses 4-byte bools. So if some alignments or size of structure assume 1-byte bool and that matters to the code execution, that may cause trouble.
@barracuda156 Could you please check the vignette as just updated in https://github.com/r-spatial/spdep/commit/1fb494364507cc9539901271c202cad69e61f324 ? sids.zip
Does this persist if you set
sf_use_s2(TRUE)
?
No, that was https://github.com/r-spatial/spdep/issues/154#issuecomment-2143769544, second variant, vignette updated to that. lwgeom seems to fall over on big-endian WKB.
I get an error in vignettes here:
Any idea what goes wrong?