Reputation: 717
I am running into some projection issues in R that I do not understand.
I have downloaded the following global dataset:
http://www.naturalearthdata.com/downloads/110m-physical-vectors/110m-land/
I then am creating maps using different projections to teach the concept of spatial projection.
I have successfully mapped and reprojected / mapped the data in lat/lon WGS84 and reprojected to Robinson
library(rgdal)
library(ggplot2)
setwd("~/Documents/data")
# read shapefile
worldBound <- readOGR(dsn="Global/Boundaries/ne_110m_land",
layer="ne_110m_land")
# convert to dataframe
worldBound_df <- fortify(worldBound)
# plot map
ggplot(worldBound_df, aes(long,lat, group=group)) +
geom_polygon() +
labs(title="World map (longlat)") +
coord_equal() +
ggtitle("Geographic - WGS84 Datum")
# reproject from longlat to robinson
worldBound_robin <- spTransform(worldBound,
CRS("+proj=robin"))
worldBound_df_robin <- fortify(wmap_robin)
ggplot(worldBound_df_robin, aes(long,lat, group=group)) +
geom_polygon() +
labs(title="World map (robinson)") +
coord_equal()
Now - when i try to project to Mercator WGS84 i run into issues.
# reproject from longlat to mercator
worldBound_merc <- spTransform(worldBound,
CRS("+init=epsg:3395"))
# make ggplot happy
worldBound_df_merc <- fortify(worldBound_merc)
# plot map
ggplot(worldBound_df_merc, aes(long,lat, group=group)) +
geom_polygon() +
labs(title="World map (Mercator WGS84)") +
coord_equal()
I get the error: Error in .spTransform_Polygon(input[[i]], to_args = to_args, from_args = from_args, : failure in Polygons 8 Polygon 1 points In addition: Warning message: In .spTransform_Polygon(input[[i]], to_args = to_args, from_args = from_args, : 2 projected point(s) not finite
The error is on the spTransform function. It almost seems as if it can not calculate a finite xy coordinate from lat long to mercator for some points but i don't understand how to fix /address this. My searches on this site and others have resulted in other instances of this error, but not a good explanation of what triggers the error when projecting the data so i can fix it.
Thank you for any guidance! Leah
Resolution Code: For those who run into this - i just cropped the data to be able to plot using Mercator. This is just a demonstration so it's ok to lose some of the data for visual, mapping purposes.
# create extent object from world data
newExt <- extent(worldBound)
# redefine the extent to the limits of mercator EPSG 3395
newExt@ymin <- -80
newExt@ymax <- 80
# crop data to new extent
merc_WorldBound <- crop(worldBound,
newExt)
# reproject from longlat to mercator
worldBound_merc <- spTransform(merc_WorldBound,
CRS("+init=epsg:3395"))
Upvotes: 2
Views: 1899
Reputation: 1294
The issue appears to be points that lie outside the bounds of the epsg:3395 projection (-180, -80, 180, 84) http://spatialreference.org/ref/epsg/wgs-84-world-mercator/. To correct this you can clip the shapefile to an appropriate extent and then perform the reprojection.
library(raster)
library(rgdal)
worldBoundClipped <- crop(worldBound,extent(-180,180,-84,80))
worldBound_merc <- spTransform(worldBoundClipped,CRS("+init=epsg:3395"))
Upvotes: 4