0

The code below shows the outline of Canada obtained from GDAM as a Spatial Polygons Dataframe with a CRS of EPSG:4326. I wish to transform this to EPSG:3857 (Mercator). Yet, I obtain the following error.

Why?

Error in UseMethod("st_transform") : 
  no applicable method for 'st_transform' applied to an object of class "c('SpatialPolygonsDataFrame', 'SpatialPolygons', 'Spatial', 'SpatialVector')"

==========

library(mapdata)
library(sf)

# Get Canada as spatial polygons data frame
# GADM data is +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
# CRS("+init=epsg:4326")
canada <- getData("GADM", country = "CAN", level = 1)

# Transform to Mercator projection *** ERROR ***
canada_t <- st_transform(canada, "+init=epsg:3857")
  • 1
    First, check the class of you object. In your example it is likely sp and not sf. Second look at spTransform. – Jeffrey Evans Jan 28 at 2:54
  • Or convert Canada to an sf with canada = st_as_sf(canada) – Spacedman Jan 28 at 15:06
  • Also, getData needs library(raster), and not mapdata – Spacedman Jan 28 at 15:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.