Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

intermittent error converting sf to SpatVector #1098

Closed
bmcnellis opened this issue Apr 6, 2023 · 3 comments
Closed

intermittent error converting sf to SpatVector #1098

bmcnellis opened this issue Apr 6, 2023 · 3 comments

Comments

@bmcnellis
Copy link

I use some spData objects as part of a workflow, including state shapes for cropping:

states <- spData::us_states[which(spData::us_states$NAME %in% c('Colorado', 'Utah')), ]

Sometimes a direct conversion works with terra:

states <- terra::vect(states)

But lately its been giving me this error:

Error: [as,sf] coercion failed. You can try coercing via a Spatial* (sp) class

Using as(states, 'Spatial') also appears broken:

Error in st_geometry.sf(from) : attr(obj, "sf_column") does not point to a geometry column. Did you rename it, without setting st_geometry(obj) <- "newname"?

Using R 4.2.2 with spData 2.2.2, sf 1.0-9, sp 1.6-0, terra 1.7-22

@rhijmans
Copy link
Member

rhijmans commented Apr 6, 2023

This works if you load "sf" first

library(sf)
states <- spData::us_states
states <- states[states$NAME %in% c('Colorado', 'Utah'), ]
v <- terra::vect(states)
v
# class       : SpatVector 
# geometry    : polygons 
# dimensions  : 2, 6  (geometries, attributes)
# extent      : -114.0506, -102.0422, 36.99243, 42.00157  (xmin, xmax, ymin, ymax)
# coord. ref. : lon/lat NAD83 (EPSG:4269) 
# names       : GEOID     NAME REGION             AREA total_pop_10 total_pop_15
# type        : <chr>    <chr> <fact>            <chr>        <num>        <num>
# values      :    08 Colorado   West 269573.057693748    4.887e+06    5.279e+06
#                  49     Utah   West 219859.796916458    2.657e+06    2.903e+06

With the above I get

states
#Simple feature collection with 2 features and 6 fields
#Geometry type: MULTIPOLYGON
#Dimension:     XY
#Bounding box:  xmin: -114.0506 ymin: 36.99243 xmax: -102.0422 ymax: 42.00157
#Geodetic CRS:  NAD83
#   GEOID     NAME REGION            AREA total_pop_10 total_pop_15                       geometry
#3     08 Colorado   West 269573.1 [km^2]      4887061      5278906 MULTIPOLYGON (((-109.0501 4...
#45    49     Utah   West 219859.8 [km^2]      2657236      2903379 MULTIPOLYGON (((-114.0417 4...

But if I do not run library(sf) I get

states
#   GEOID     NAME REGION     AREA total_pop_10 total_pop_15 geometry
#3     08 Colorado   West 269573.1      4887061      5278906   -109.05008, -108.25065, -107.62562, ...
#45    49     Utah   West 219859.8      2657236      2903379   -114.04172, -113.89326, -113.24916, ...

And the coercion fails. Even though in both cases I get

class(states)
#[1] "sf"         "data.frame"

@bmcnellis
Copy link
Author

This works, thanks! I was indeed using class to see if the methods were importing correctly, which I suppose was not the case! Perhaps adding that suggestion to the error message would be helpful for future users?

Thanks for all your work on this package!

@rhijmans
Copy link
Member

rhijmans commented Apr 6, 2023

This now gives a clearer error message:

states <- terra::vect(states)
#Error: [as,sf] the geometry column is not valid (perhaps first load the sf package)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants