I am encountering an error I don't understand. I want to change the projection of a SpatialPolygonsDataFrame from +longlat to Mollweide or Robinson. For this I am trying to use spTransform(), but it returns the error: "Error in .spTransform_Polygon(input[[i]], to_args = to_args, from_args = from_args, : failure in Polygons 125 Polygon 1 points 1:4:5"

My question: why am I getting this error?

Here is an MWE:

projection <- "+proj=robin +datum=WGS84 +lat_0=0 +lon_0="

numlons <- 30
numlats <- 50

lat.df <- rep(seq(from=-90, to=90, length.out = numlats), each=numlons)
lon.df <- rep(seq(from=-180, to=180, length.out = numlons), times=numlats)

spobj <- as.vector(array(data=rnorm(n=50*30, mean=0, sd=1)))
spobj <- data.frame("dat"=spobj)
spobj <- SpatialPointsDataFrame(coords=data.frame("longs"=c(lon.df), "lats"=lat.df), 
sp::gridded(spobj) <- TRUE
spobj <- as(spobj, "SpatialGridDataFrame")

### Change projection
spobj <- Grid2Polygons(grd=spobj, zcol="dat", cuts=100) # convert to SpatialPolygon
spobj <- spTransform(spobj, CRS(projection)) # change projection

1 Answer 1


The grid cells are going outside the valid range for lat-long data. When spobj is points (before sp::gridded(spobj) <- TRUE) it is this:

> spobj.pts
class       : SpatialPointsDataFrame 
features    : 1500 
extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
crs         : +proj=longlat +ellps=WGS84 
variables   : 1
names       :               dat 
min values  : -3.85401447598915 
max values  :  3.65817120357171 

but after Grid2Polygons it is this:

> spobj
class       : SpatialPolygonsDataFrame 
features    : 1470 
extent      : -186.2069, 186.2069, -88.16327, 91.83673  (xmin, xmax, ymin, ymax)
crs         : +proj=longlat +ellps=WGS84 

which has latitude past the north pole (+90) and longitudes past the anti-meridian (180 or -180). This is because the grid cells from Grid2Polygons are centred on the points. There's also fewer of them. It looks like its not bothered making grids from the bottom row:

enter image description here

So it looks like that's what Grid2Polygons does, and I'm not sure if that's what its meant to do or not (docs are vague) or what precisely you want to do to make polygon cells from your points. But the basic problem is that you cant project coordinates that aren't valid.

Your Answer

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

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