-
Notifications
You must be signed in to change notification settings - Fork 301
Description
Describe the bug
I have a simple GeoJSON (attached, albeit as .json due to Github upload restrictions) with 1 MULTIPOLYGON consisting of 2 sub-polygons. Using st_cast
to split them into two individual POLYGONs results in rownames 1
and 2
. But if I add a data column before the cast, the rownames become 1
and 1.1
. Is this intended?
Expected behaviour: The rownames after splitting are identical regardless of whether more data is attached or not.
Desired resolution: The second behaviour (sub-indexing rather than re-indexing) is better for us because it allows tracking to which feature in the file each polygon originally belonged to.
To Reproduce
library(sf)
library(geojsonsf)
sf_use_s2(FALSE)
packageVersion("sf")
multipoly <- geojson_sf("simple-multipolygon.geojson")
multipoly
as_polygons <- st_cast(multipoly, "POLYGON")
as_polygons
rownames(as_polygons)
multipoly$whatever <- 1
as_polygons2 <- st_cast(multipoly, "POLYGON")
as_polygons2
rownames(as_polygons2)
all(rownames(as_polygons) == rownames(as_polygons2)) # should be TRUE
With output:
> library(sf)
> library(geojsonsf)
> sf_use_s2(FALSE)
> packageVersion("sf")
[1] ‘1.0.21’
> multipoly <- geojson_sf("simple-multipolygon.geojson")
> multipoly
Simple feature collection with 1 feature and 0 fields
Geometry type: MULTIPOLYGON
Dimension: XY
Bounding box: xmin: -84.52956 ymin: 10.26753 xmax: -84.52694 ymax: 10.27026
Geodetic CRS: WGS 84
geometry
1 MULTIPOLYGON (((-84.52782 1...
> as_polygons <- st_cast(multipoly, "POLYGON")
> as_polygons
Simple feature collection with 2 features and 0 fields
Geometry type: POLYGON
Dimension: XY
Bounding box: xmin: -84.52956 ymin: 10.26753 xmax: -84.52694 ymax: 10.27026
Geodetic CRS: WGS 84
geometry
1 POLYGON ((-84.52782 10.2702...
2 POLYGON ((-84.52817 10.2678...
> rownames(as_polygons)
[1] "1" "2"
> multipoly$whatever <- 1
> as_polygons2 <- st_cast(multipoly, "POLYGON")
Warning message:
In st_cast.sf(multipoly, "POLYGON") :
repeating attributes for all sub-geometries for which they may not be constant
> as_polygons2
Simple feature collection with 2 features and 1 field
Geometry type: POLYGON
Dimension: XY
Bounding box: xmin: -84.52956 ymin: 10.26753 xmax: -84.52694 ymax: 10.27026
Geodetic CRS: WGS 84
whatever geometry
1 1 POLYGON ((-84.52782 10.2702...
1.1 1 POLYGON ((-84.52817 10.2678...
> rownames(as_polygons2)
[1] "1" "1.1"
> all(rownames(as_polygons) == rownames(as_polygons2)) # should be TRUE
[1] FALSE
Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/lib/libRlapack.dylib; LAPACK version 3.12.0
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
time zone: Europe/Berlin
tzcode source: internal
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] geojsonsf_2.0.3 sf_1.0-21
loaded via a namespace (and not attached):
[1] later_1.4.2 R6_2.6.1 swagger_5.17.14.1 e1071_1.7-16 magrittr_2.0.3 webutils_1.2.2
[7] KernSmooth_2.23-26 plumber_1.3.0 classInt_0.4-11 lifecycle_1.0.4 promises_1.3.3 cli_3.6.5
[13] grid_4.4.3 DBI_1.2.3 proxy_0.4-27 class_7.3-23 compiler_4.4.3 tools_4.4.3
[19] Rcpp_1.0.14 rlang_1.1.6 jsonlite_2.0.0 crayon_1.5.3 units_0.8-7 stringi_1.8.7
sf::sf_extSoftVersion()
GEOS GDAL proj.4 GDAL_with_GEOS USE_PROJ_H PROJ
"3.13.0" "3.8.5" "9.5.1" "true" "true" "9.5.1"
CC @azomorod