1

I am trying to project a shapefile of the world that was downloaded from naturalearthdata.com

shp is a shapefile of the worlds landmasses that I downloaded. When I use the code class(shp) R tells me that it has a class of character.

When I read the file using st_read(shp) it comes up as the following;

Simple feature collection with 11 features and 3 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -180 ymin: -90 xmax: 180 ymax: 83.6341
Geodetic CRS:  WGS 84

I am trying to use the following code to reproject the shapefile

shp <- st_transform(shp, "+proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=23 +lon_0=-96 +x_0=0 
+y_0=0 +ellps=GRS80 +datum=NAD83 +units=m +no_defs")

However I keep getting the following error;


Error in UseMethod("st_transform") : 
  no applicable method for 'st_transform' applied to an object of class "character"

From what I can see online its because my data needs to be a dataframe eg when I use the code class(shp) it should come back as a class of dataframe. I am not sure why the shapefile is a character class but I downloaded two other shapefiles of the worlds landmasses from different sources and both of them were also character classes so I'm not sure what to do about this.

I've tried to transform the shapefile into a dataframe by using as_data_frame(as.list(shp)) and other variations of this but with no success.

I was wondering if there is a way to project my data into a new CRS even as a character class or if anyone had suggestions to transform the data.

1
  • What error are you getting? What is shp when you run it? Why do you think the shapefile has "class of character"? Have you read the shapefile using st_read? The transform works fine for me when I try it on some test data. Please edit your question and let us know what shp is and what the error message is.
    – Spacedman
    Commented Mar 11, 2022 at 21:43

1 Answer 1

1

It looks like you are not actually reading in the shapefile into R. The process should be something like:

shp = "/path/to/data.shp" # class(shp) is character
shp = st_read(shp) # class(shp) is now an sf data frame
shp = st_transform(shp, newcrs) # show is now transformed

and I think you are missing the second step there.

For clarity, don't shy away from using new names, eg:

shp_path = "/path/to/data.shp"
shp_sf = st_read(shp_path)
shp_sf_newcrs = st_transform(shp_sf, new_crs)

And also (and I appreciate this you are probably using shp because its a placeholder for a real name so maybe you know this) don't call it shp, make it meaningful like london or bird_nests or something.

2
  • I am still getting the same error of ``` Error in UseMethod("st_transform") : no applicable method for 'st_transform' applied to an object of class "character" ``` When I try to run the last line of the code, also the st_read function is giving me the same output as before
    – Lucy
    Commented Mar 15, 2022 at 23:14
  • I don't know what else to say except you must be running st_transform on a character string and not an sf spatial object. If you can edit your q to show your exact command from start to finish, and the error message, that will help greatly. Cut and paste from your R session to prevent errors.
    – Spacedman
    Commented Mar 15, 2022 at 23:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.