[R-sig-Geo] Projected SpatialLinesDataFrame has a segment spanning the globe

Andy South southandy at gmail.com
Fri Nov 1 14:15:59 CET 2013


Ry Guy <rr2box42 <at> gmail.com> writes:

> Hi All,
> 
> I'm trying to use the 'coastsCoarse' dataset from library 'rworldmap' as a 
base map for plotting a global
> dataset. It works fine when I use unprojected data. However, if I project 
the data and base map,
> coastsCoarse ends up with one horizontal line segment that spans the whole 
map. You can see what I mean like this:

Hi Ryan,
I think I've identified the offending segment (see below), but haven't yet 
been able to remove it. I'm sure someone else on the list better at 
manipulating sp objects will be able to help with getting around the error 
below :
'setting coordinates cannot be done on Spatial objects, where they have 
already been set'
Once we've fixed this I'll try fixing in rworldmap by removoing the 
corresponding point in the unprojected file.
Best wishes,
Andy

> 
> # ---------------------------------------
> library(rgdal)
> library(rworldmap)
> 
> data(coastsCoarse)
> plot(coastsCoarse) # Looks good
> 
> mercator.proj4 = "+proj=merc +lon_0=0 +k=1 +x_0=0 +y_0=0 +ellps=WGS84 
+datum=WGS84 +units=m +no_defs"
> coastsMerc = spTransform(coastsCoarse, CRS(mercator.proj4))
> spplot(coastsMerc) # horizontal line across plot connects east and west 
Beringia...

#the problem is with line 94
plot(coastsMerc[94,])

#look at the last and the first point see which is the problem
coordinates(coastsMerc[94,])

#looks like the last point is the problem try removing it
numToRemove <- nrow(coastsMerc[94,]@lines[[1]]@Lines[[1]]@coords) 


coordinates(coastsMerc[94,]) <- 
coastsMerc[94,]@lines[[1]]@Lines[[1]]@coords[-numToRemove,]
#Error in `coordinates<-`(`*tmp*`, value = c(-1809707.97165956, 
-1823151.78413843,  : 
#setting coordinates cannot be done on Spatial objects, where they have 
already been set


> It seems like the boundary of northern Russia didn't get cut off 
correctly. I imagine the fix is
> straightforward, but I've been banging away at it to no avail. Does anyone 
have a simple solution that
> either (a) locates and removes the offending pan-global segment or (b) 
doesn't produce it to begin with?
> 
> Thanks much,
> Ryan
>



More information about the R-sig-Geo mailing list