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

Edzer Pebesma edzer.pebesma at uni-muenster.de
Fri Nov 1 15:09:42 CET 2013


On 11/01/2013 02:15 PM, Andy South wrote:
> 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.

Please let me know where you learned that you could use coordinates() to
assign coordinates to a Spatial* object.

> 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

a wild guess, but this might work:

coastsMerc at lines[[94]]@Lines[[1]]@coords <-
 coastsMerc[94,]@lines[[1]]@Lines[[1]]@coords[-numToRemove,,drop=FALSE]

> 
> 
>> 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
>>
> 
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
> 

-- 
Edzer Pebesma
Institute for Geoinformatics (ifgi), University of Münster
Heisenbergstraße 2, 48149 Münster, Germany. Phone: +49 251
83 33081 http://ifgi.uni-muenster.de GPG key ID 0xAC227795

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 555 bytes
Desc: OpenPGP digital signature
URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20131101/06d87891/attachment.bin>


More information about the R-sig-Geo mailing list