r-spatial / sf

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

rbind() error : Error in st_sf(x) : no simple features geometry column present #480

Closed lucasmation closed 7 years ago

lucasmation commented 7 years ago

I am trying to rbind two data.frames that contain spatial columns that were imported from shapefiles using st_read. The code errors in the rbind part

g1 <- st_read(file1, stringsAsFactors = F,)
colnames(g1)[22] <- "emp_centroid"
g2 <- st_read(file2, stringsAsFactors = F)
colnames(g2)[22] <- "emp_polygon"
g2$emp_centroid <- st_centroid(g2$emp_polygon)
g2$emp_polygon <- NULL

emp_geom <- rbind(g1,g2)
Error in st_sf(x) : no simple features geometry column present

sory but at the moment I can not provide a reproducible example (data is not public yet). I don´t know if this is relevant to this problem but: the encoding in the original shapefiles may be Latin1 (instead of UTF-8) (Brazilian files) (I actually don´t know how to check that)

lucasmation commented 7 years ago

also, I am using the current dev version of sf (0.5-5)

edzer commented 7 years ago

try replacing the geometry column using the sf api:

st_geometry(g2) <- st_centroid(g2)

instead of removing the geometry column and adding a new one.

lucasmation commented 7 years ago

That does not work:

g2 <- st_read(file2, stringsAsFactors = F)

Reading layer `#BR_POLIGONAIS' from data source `\\storage4\bases\RESTRITO\aval_MCMV\data\Mcidades_empreendimentos\#BR_POLIGONAIS.shp' using driver `ESRI Shapefile'
Simple feature collection with 2149 features and 21 fields
geometry type:  POLYGON
dimension:      XYZ
bbox:           xmin: -69.90797 ymin: -32.08219 xmax: -34.82826 ymax: 2.853777
epsg (SRID):    NA
proj4string:    +proj=longlat +ellps=GRS80 +no_defs

st_geometry(g2) <- st_centroid(g2)

Error: inherits(value, "sfc") || is.character(value) is not TRUE
In addition: Warning message:
In st_centroid.sfc(st_geometry(x), of_largest_polygon = of_largest_polygon) :
  st_centroid does not give correct centroids for longitude/latitude data

I am a bit confused. g2 is a data.frame, containing 22 columns, the last of which is "geometry". But in st_geometry(g2) <- st_centroid(g2) I am passing the hole data frame. is that correct?

In any case, back to the original errror, in the sf data model, shouldn't the geom column be a column like any other in a data.frame. If the geom columns of two data frames has the same name and type ( in this case sfc_POINT) they should allow rbding... no?

tim-salabim commented 7 years ago

I think you need

st_geometry(g2) <- st_geometry(st_centroid(g2))

edzer commented 7 years ago

Or the shorter

g2 <- st_centroid(g2)

About your question: yes but no. sf objects try to keep track which column is the geometry. This means that you need to use sf's api to remove or reset geometry columns, e.g.

st_geometry(g2) <- NULL
st_geometry(g2) <- # new geometry list-column
lucasmation commented 7 years ago

it now works with the suggested changes

g1 <- st_read(file1, stringsAsFactors = F,)
g2 <- st_read(file2, stringsAsFactors = F)
st_geometry(g2) <- st_geometry(st_centroid(g2))

#diffeernt binding options (all work)
emp_geom <- rbind(g1,g2)
emp_geom <- bind_rows(g1,g2)
Warning messages:
1: In bind_rows_(x, .id) :
  Vectorizing 'sfc_POINT' elements may not preserve their attributes
2: In bind_rows_(x, .id) :
  Vectorizing 'sfc_POINT' elements may not preserve their attributes
lucasmation commented 7 years ago

So this is why I found it so hard to understand the sf documentation.

I think idea of subsuming the geographic information as a column in a data.frame is great. But manipulating that column should be like manipulating any other column (with special spatial functions), just like in Postgis. Refering to the hole data.frame seems a bit counterintuitive. Do you have plans to introduce this in the future?

Also, in the current API, how can I rename the geometry column?

can I have multiple geoms in the same data.frame? I need two geom columns in order to calcualte distances between them...

edzer commented 7 years ago

Feel free to add as many geometry columns to data.frames as you like, but don't expect the functions in sf to understand how to work with them. A simple feature can have only one geometry. That is why sf objects keep track which geometry column is active.

For distances between pairs of geoms in two columns, use

st_distance(df$geom1, df$geom2, by_element = TRUE)
lucasmation commented 7 years ago

Also, something I just noticed: rbind preserves the geom type. bind_rows seems to change the variable type of the geometry column:

g1 <- st_read(file1, stringsAsFactors = F,)
g2 <- st_read(file2, stringsAsFactors = F)
g1 %>% str
g2 %>% str
#in both cases we get:
Classes ‘sf’ and 'data.frame':  2149 obs. of  22 variables:
...
$ geometry  :sfc_POINT of length 2149; first list element: Classes 'XY', 'POINT', 'sfg'  num [1:2] -46.4 -23.5
 - attr(*, "sf_column")= chr "geometry"
 - attr(*, "agr")= Factor w/ 3 levels "constant","aggregate",..: NA NA NA NA NA NA NA NA NA NA ...
  ..- attr(*, "names")= chr  "COD_OP" "COD_AGRUP" "NOME_OP" "UF" ...

emp_geom <- rbind(g1,g2)
emp_geom %>% str
Classes ‘sf’ and 'data.frame':  3434 obs. of  22 variables:
...
geometry  :sfc_POINT of length 3434; first list element: Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -48.2 -21.7 0
 - attr(*, "sf_column")= chr "geometry"
 - attr(*, "agr")= Factor w/ 3 levels "constant","aggregate",..: NA NA NA NA NA NA NA NA NA NA ...
  ..- attr(*, "names")= chr  "COD_OP" "COD_AGRUP" "NOME_OP" "UF" ...

emp_geom <- bind_rows(g1,g2)
emp_geom %>% str
Classes ‘sf’ and 'data.frame':  3434 obs. of  22 variables:
...
geometry  :List of 3434
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -48.2 -21.7 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -46 -23.4 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -47.4 -23.4 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -46.8 -24.2 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -46.8 -24.2 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -49.3 -20.8 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -37.5 -10.7 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -45.9 -23.2 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -47.8 -21.1 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -47.8 -21.1 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -48.9 -26.5 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -46.8 -24.2 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -49.3 -20.8 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -46.8 -23.5 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -47.9 -22 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -46.8 -23.5 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -46.8 -24.2 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -47.8 -21.1 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -46.3 -23.5 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -46 -23.4 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -46.7 -23.7 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -51.4 -22.1 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -50.3 -21.3 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -50.4 -22.7 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -46.8 -24.2 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -45.9 -23.3 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -49.9 -23 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -46.8 -23.5 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -48.6 -20.6 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -46.8 -24.2 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -47.4 -23.4 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -48.25 -7.22 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -50.5 -21.2 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -51.4 -22.1 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -46.8 -24.2 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -48.9 -10.2 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -49.9 -23 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -45.8 -23.2 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -46.8 -24.2 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -46 -23.4 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -46.2 -23.6 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -50.5 -21.2 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -45.9 -23.3 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -46.4 -23.9 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -46.8 -24.2 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -46.8 -23.6 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -47.8 -21.1 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -48.9 -26.5 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -48.2 -21.7 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -49.4 -20.7 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -49.4 -20.7 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -46.8 -24.2 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -46.8 -24.2 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -49.9 -23 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -46.8 -24.2 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -49.3 -20.8 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -45.2 -22.8 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -46.8 -23.6 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -48.9 -10.2 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -48.6 -20.6 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -46.8 -23.6 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -46.8 -24.2 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -46.4 -23.9 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -49.1 -11.7 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -46.3 -23.5 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -47.8 -21.1 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -49.3 -20.8 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -46.8 -24.2 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -48.2 -21.7 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -48.2 -21.7 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -47.9 -22 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -46.8 -23.6 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -46.5 -23.6 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -46.8 -24.2 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -49.1 -11.7 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -47.8 -21.1 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -49.1 -11.7 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -50.3 -21.3 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -46.8 -23.6 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -45.9 -23.3 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -45.9 -23.2 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -46.3 -23.6 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -46.7 -23.7 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -46.4 -23.6 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -46.5 -23.7 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -49.4 -20.7 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -48.5 -20.9 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -46.4 -23.9 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -46.8 -24.2 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -46.8 -23.5 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -47.9 -22 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -46.4 -23.9 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -45.9 -23.3 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -46.4 -23.9 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -50.4 -22.7 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -46.8 -23.6 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -49.4 -20.7 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -45.8 -23.2 0
  ..$ :Classes 'XYZ', 'POINT', 'sfg'  num [1:3] -48.6 -20.6 0
  .. [list output truncated]
lucasmation commented 7 years ago

@edzer tks a lot!

last question for me to finish my workflow:

edzer commented 7 years ago
> library(sf)
Linking to GEOS 3.5.1, GDAL 2.2.0, proj.4 4.9.2, lwgeom 2.3.3 r15473
> demo(nc, ask = FALSE, echo = FALSE)
Reading layer `nc.gpkg' from data source `/home/edzer/R/x86_64-pc-linux-gnu-library/3.4/sf/gpkg/nc.gpkg' using driver `GPKG'
Simple feature collection with 100 features and 14 fields
Attribute-geometry relationship: 0 constant, 8 aggregate, 6 identity
geometry type:  MULTIPOLYGON
dimension:      XY
bbox:           xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
epsg (SRID):    4267
proj4string:    +proj=longlat +datum=NAD27 +no_defs
> nc$xx = nc$geom
> st_geometry(nc) = "xx"
> attr(nc, "sf_column")
[1] "xx"
edzer commented 7 years ago

Any reason to leave this open?

lucasmation commented 7 years ago

@edzer , actually there is still a problem and I was about to post a reproducible example (sorry for taking this long)

remove.packages("ggplot2")
devtools::install_github("tidyverse/ggplot2")
library(dplyr)
library(ggplot2)

#Download the data from (EDIT: NOW AUTHOMATED (BUT SOMETIMES FAILS):
#ftp://geoftp.ibge.gov.br/organizacao_do_territorio/malhas_territoriais/malhas_de_setores_censitarios__divisoes_intramunicipais/censo_2010/setores_censitarios_shp

download_from_ftp <- function(link, root_path){
  group_download<- function(files, links, dest){
      for(y in seq_along(files)){
        print(paste(c(dest,files[y]),collapse = "/"))
        print(links[y])
        try({
          download.file(links[y],destfile = paste(c(dest, files[y]),collapse = "/"), mode = "wb")
        })
      }
  }
    subdir.names <- RCurl::getURL(link, ftp.use.epsv = FALSE, ftplistonly = TRUE,
                               crlf = TRUE) %>% strsplit(.,  split = "\r*\n") %>% .[[1]]  
    subdir.links <- paste(link, subdir.names, "/", sep = "")
    for(sb in subdir.links){
      new_dir.sb<- paste0(root_path,"/", gsub(sb, pattern = "/+$", replacement = "", perl = TRUE) %>% gsub(pattern = ".+/", replacement = ""))
      sb.files<- RCurl::getURL(sb, ftp.use.epsv = FALSE, ftplistonly = TRUE,
                               crlf = TRUE) %>% strsplit(.,  split = "\r*\n") %>% .[[1]]
      sb.files.links<- paste(sb, sb.files, sep = "")
      dir.create(new_dir.sb)
      group_download(sb.files, sb.files.links, new_dir.sb)
    }
}

link <- "ftp://geoftp.ibge.gov.br/organizacao_do_territorio/malhas_territoriais/malhas_de_setores_censitarios__divisoes_intramunicipais/censo_2010/setores_censitarios_shp/"
root_path<- "C:/.../your_working_directory"
download_from_ftp(link, root_path)

#unip
files <- list.files(pattern = '*_setores_censitarios.zip',recursive = T)  
files %>% sapply(unzip)

#reading one file and ploting it: works
g <- st_read("11SEE250GC_SIR.shp", stringsAsFactors = F)
g %>% ggplot() + geom_sf()

#reading several files, rbinding them and ploting: ERROR
sc20010_geom <- geofiles %>% lapply(st_read,stringsAsFactors = F) %>% rbind
sc20010_geom %>% ggplot() + geom_sf()
Error: ggplot2 doesn't know how to deal with data of class matrix.

sc20010_geom <- geofiles %>% lapply(st_read,stringsAsFactors = F) %>% bind_rows
sc20010_geom %>% ggplot() + geom_sf()
sc20010_geom %>% ggplot() + geom_sf()
Error in .subset2(x, i, exact = exact) : 
  attempt to select less than one element in get1index

how can I do that?

EDIT: added automatic data download as requested by @edzer EDIT2: @edzer, I had forgotten the link and rootpath arguments. Corrected. Try again now.

edzer commented 7 years ago

Please make the download part of the script.

lucasmation commented 7 years ago

This ftp website is very tricky , the usual Rcurl solution for download does not work. What I is copy manually, which is quick: open the ftp site in a "File Explorer" window of Windows, where I see all the folders and files. I then mark all files , copy and paste in a the working directory. Maybe in linux wget would work, as it is more roubust than Rcurl

edzer commented 7 years ago

wget didn't work for me.

lucasmation commented 7 years ago

@edzer, with the help of @nicolassoarespinto I managed to create a download function that works. Se edit in the code above. Still, the ftp server sometimes gives an error halfway through (especially after the first or second try), but as long as a few states get downloaded the referred error should be reproducible

edzer commented 7 years ago
Error in RCurl::getURL(link, ftp.use.epsv = FALSE, ftplistonly = TRUE,  : 
  object 'link' not found
lucasmation commented 7 years ago

corrected that (see edit2 above). Should work now. Let me know

lucasmation commented 7 years ago

If the above download function does not work this simpler version should do the download

ftp.root <- "ftp://geoftp.ibge.gov.br/organizacao_do_territorio/malhas_territoriais/malhas_de_setores_censitarios__divisoes_intramunicipais/censo_2010/setores_censitarios_shp/"
ufs<- c("ac", "al","am","ap","ba","ce", "df", "es", "go", "ma", "mg", "ms", "mt", "pa", "pb", "pe", "pi", "pr", "rj", "rn", "ro","rr", "rs", "sc", "se", "sp", "to") 
links<- paste0(ftp.root, ufs, "/",ufs, "_setores_censitarios.zip")
dests<- paste0(ufs, "_setores_censitarios.zip")
mapply(links, dests, FUN = download.file, MoreArgs = list(mode = "wb"))

FYI, these are the enumeration districts of the 2010 Brazilian Census. They are a big and rich set of shapefiles, with some common real-life problems (topological erros, etc). A nice test bed for the sf package

edzer commented 7 years ago

This download takes hours and timed out after 10% done. Please provide a much smaller example that demonstrates your point.

lucasmation commented 7 years ago

I need at least two .shp files to reproduce the error (and I could not reproduce it by doubling your demo nc object). I restricted to just 2 small .shp files. This should be easy to download (it downloads in 3s here) Use the updated code bellow, including further errors

remove.packages("ggplot2")
devtools::install_github("tidyverse/ggplot2")
library(dplyr)
library(ggplot2)

#download (small) data
library(dplyr)
ftp.root<- "ftp://geoftp.ibge.gov.br/organizacao_do_territorio/malhas_territoriais/malhas_de_setores_censitarios__divisoes_intramunicipais/censo_2010/setores_censitarios_shp/"
ufs<- c("ac", "al")
links<- paste0(ftp.root, ufs, "/",ufs, "_setores_censitarios.zip")
dests<- paste0(ufs, "_setores_censitarios.zip")
mapply(links, dests, FUN = download.file, MoreArgs = list(mode = "wb"))

#unip
files <- list.files(pattern = '*_setores_censitarios.zip',recursive = F)  
files %>% sapply(unzip)

#reading one file and ploting it: works
g <- st_read("12SEE250GC_SIR.shp", stringsAsFactors = F)
g %>% ggplot() + geom_sf()

#reading several files, rbinding them and ploting: ERROR
geofiles <- list.files(pattern = '*.shp')  

#rdind errors
sc20010_geom <- geofiles %>% lapply(st_read,stringsAsFactors = F) %>% rbind
plot(sc20010_geom)
#Error in xy.coords(x, y, xlabel, ylabel, log) : 
#(list) object cannot be coerced to type 'double'
sc20010_geom %>% ggplot() + geom_sf()
#Error: ggplot2 doesn't know how to deal with data of class matrix.
sc20010_geom <- st_centroid(sc20010_geom)
# Error in UseMethod("st_centroid") : 
#   no applicable method for 'st_centroid' applied to an object of class "c('matrix', 'list')"

#bind_rows errors
sc20010_geom <- geofiles %>% lapply(st_read,stringsAsFactors = F) %>% bind_rows
plot(sc20010_geom)
#Error in .subset2(x, i, exact = exact) : 
#  attempt to select less than one element in get1index
sc20010_geom %>% ggplot() + geom_sf()
#Error in .subset2(x, i, exact = exact) : 
#  attempt to select less than one element in get1index
sc20010_geom <- st_centroid(sc20010_geom)
# Error in .subset2(x, i, exact = exact) : 
#   attempt to select less than one element in get1index
edzer commented 7 years ago

Thanks. Try

sc20010_geom <- geofiles %>% lapply(st_read,stringsAsFactors = F) %>% do.call(rbind, .)

the output of lapply is a list, but rbind doesn't want a list as input.

lucasmation commented 7 years ago

Good point. Now the plot at least compiles but the result is a 3 point graph, not the intended map. Se the difference from:

g <- st_read("12SEE250GC_SIR.shp", stringsAsFactors = F)
plot(g)
#a nice plot

sc20010_geom <- geofiles %>% sapply(st_read,stringsAsFactors = F) %>%  do.call(rbind, .)
plot(sc20010_geom)
#a 3 point scatterplot
edzer commented 7 years ago

Maybe you should bind more areas.

lucasmation commented 7 years ago

No, that is not it. Each .shp has many polygons. Let me show the outputs:

image

g2 <- st_read("27SEE250GC_SIR.shp", stringsAsFactors = F)
plot(g2)

image

g3 <- rbind(g1,g2) g3 %>% ggplot() + geom_sf()

image

sc20010_geom <- geofiles %>% sapply(st_read,stringsAsFactors = F) %>%  do.call(rbind, .)
sc20010_geom %>% ggplot() + geom_sf()
#Error: ggplot2 doesn't know how to deal with data of class matrix.
plot(sc20010_geom)

image

edzer commented 7 years ago

Do you see any error messages after the big

sc20010_geom <- geofiles %>% sapply(st_read,stringsAsFactors = F) %>%  do.call(rbind, .)

? Why do you use sapply and not lapply?

lucasmation commented 7 years ago

great. That is what I was missing. It was an R problem, not an sf problem after all. I am now testing on the full sample!