r-spatial / sf

Simple Features for R
https://r-spatial.github.io/sf/
Other
1.33k stars 293 forks source link

st_sample is not type stable #1025

Closed MilesMcBain closed 5 years ago

MilesMcBain commented 5 years ago
library(magrittr)
library(sf)
#> Linking to GEOS 3.6.1, GDAL 2.2.3, PROJ 4.9.3
replicate(25, {
structure(list(structure(list(structure(c(1482976.98159302, 1490490.34132821, 
                                          1471653.34876896, 1445324.99320037, 1437796.45137175, 1456611.29533523, 
                                          1482976.98159302, 975247.176334977, 1000198.70957642, 1018248.61417219, 
                                          1011374.41697448, 986453.581994608, 968376.086117435, 975247.176334977
                                          ), .Dim = c(7L, 2L))), class = c("XY", "POLYGON", "sfg"))), class = c("sfc_POLYGON", 
                                                                                                                "sfc"), precision = 0, bbox = structure(c(xmin = 1437796.45137175, 
                                                                                                                                                          ymin = 968376.086117435, xmax = 1490490.34132821, ymax = 1018248.61417219
                                                                                                                                                          ), class = "bbox"), crs = structure(list(epsg = 4462L, proj4string = "+proj=lcc +lat_1=-18 +lat_2=-36 +lat_0=-27 +lon_0=132 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs"), class = "crs"), n_empty = 0L) %>%
  sf::st_sample(size =1, exact = TRUE) %>%
  class()
}) %>% unlist()
#>      [,1]        [,2]        [,3]           [,4]           [,5]       
#> [1,] "sfc_POINT" "sfc_POINT" "sfc_GEOMETRY" "sfc_GEOMETRY" "sfc_POINT"
#> [2,] "sfc"       "sfc"       "sfc"          "sfc"          "sfc"      
#>      [,6]           [,7]        [,8]           [,9]        [,10]      
#> [1,] "sfc_GEOMETRY" "sfc_POINT" "sfc_GEOMETRY" "sfc_POINT" "sfc_POINT"
#> [2,] "sfc"          "sfc"       "sfc"          "sfc"       "sfc"      
#>      [,11]          [,12]       [,13]          [,14]       [,15]      
#> [1,] "sfc_GEOMETRY" "sfc_POINT" "sfc_GEOMETRY" "sfc_POINT" "sfc_POINT"
#> [2,] "sfc"          "sfc"       "sfc"          "sfc"       "sfc"      
#>      [,16]       [,17]       [,18]       [,19]          [,20]      
#> [1,] "sfc_POINT" "sfc_POINT" "sfc_POINT" "sfc_GEOMETRY" "sfc_POINT"
#> [2,] "sfc"       "sfc"       "sfc"       "sfc"          "sfc"      
#>      [,21]       [,22]          [,23]       [,24]       [,25]         
#> [1,] "sfc_POINT" "sfc_GEOMETRY" "sfc_POINT" "sfc_POINT" "sfc_GEOMETRY"
#> [2,] "sfc"       "sfc"          "sfc"       "sfc"       "sfc"

sessionInfo()
#> R version 3.5.3 (2019-03-11)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows 10 x64 (build 16299)
#> 
#> Matrix products: default
#> 
#> locale:
#> [1] LC_COLLATE=English_Australia.1252  LC_CTYPE=English_Australia.1252   
#> [3] LC_MONETARY=English_Australia.1252 LC_NUMERIC=C                      
#> [5] LC_TIME=English_Australia.1252    
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] sf_0.7-3     magrittr_1.5
#> 
#> loaded via a namespace (and not attached):
#>  [1] Rcpp_1.0.1      class_7.3-15    digest_0.6.18   grid_3.5.3     
#>  [5] DBI_1.0.0       units_0.6-2     evaluate_0.13   e1071_1.7-1    
#>  [9] highr_0.8       stringi_1.4.3   rmarkdown_1.12  tools_3.5.3    
#> [13] stringr_1.4.0   xfun_0.6        yaml_2.2.0      compiler_3.5.3 
#> [17] classInt_0.3-1  htmltools_0.3.6 knitr_1.22

Created on 2019-04-12 by the reprex package (v0.2.1)

sfc_GEOMETRY seems to show up more when size is low. I'm not sure exactly what is happening but something to do with only getting 1 point vs getting more than 1 and truncating?

edit: included library(sf) in reprex so it shows version in session info.

tim-salabim commented 5 years ago

I get

     [,1]        [,2]        [,3]        [,4]        [,5]        [,6]        [,7]       
[1,] "sfc_POINT" "sfc_POINT" "sfc_POINT" "sfc_POINT" "sfc_POINT" "sfc_POINT" "sfc_POINT"
[2,] "sfc"       "sfc"       "sfc"       "sfc"       "sfc"       "sfc"       "sfc"      
     [,8]        [,9]        [,10]       [,11]       [,12]       [,13]       [,14]      
[1,] "sfc_POINT" "sfc_POINT" "sfc_POINT" "sfc_POINT" "sfc_POINT" "sfc_POINT" "sfc_POINT"
[2,] "sfc"       "sfc"       "sfc"       "sfc"       "sfc"       "sfc"       "sfc"      
     [,15]       [,16]       [,17]       [,18]       [,19]       [,20]       [,21]      
[1,] "sfc_POINT" "sfc_POINT" "sfc_POINT" "sfc_POINT" "sfc_POINT" "sfc_POINT" "sfc_POINT"
[2,] "sfc"       "sfc"       "sfc"       "sfc"       "sfc"       "sfc"       "sfc"      
     [,22]       [,23]       [,24]       [,25]      
[1,] "sfc_POINT" "sfc_POINT" "sfc_POINT" "sfc_POINT"
[2,] "sfc"       "sfc"       "sfc"       "sfc"      
> sessionInfo()
R version 3.5.3 (2019-03-11)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 17763)

Matrix products: default

locale:
[1] LC_COLLATE=German_Germany.1252  LC_CTYPE=German_Germany.1252   
[3] LC_MONETARY=German_Germany.1252 LC_NUMERIC=C                   
[5] LC_TIME=German_Germany.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] sf_0.7-4     magrittr_1.5

loaded via a namespace (and not attached):
[1] compiler_3.5.3 class_7.3-15   DBI_1.0.0      tools_3.5.3    units_0.6-2   
[6] Rcpp_1.0.1     grid_3.5.3     e1071_1.7-0.1  classInt_0.3-1
edzer commented 5 years ago

Thanks! Yes, this has been corrected in 0.7-4 (which is stil on github, to be released this month).

MilesMcBain commented 5 years ago

Ah thanks!