Skip to content

Does sp::coordinates() behave correct?

5 messages · Sarah Goslee, Edzer Pebesma, Rainer Hurling

#
I am using  'coordinates(x) <- ~x+y'  to convert data.frames into
SpatialPointsDataFrames.

It seems, that some coordinates will be rounded(?), when they are
transformed by coordinates():


library(maptools)

# Defining some Datapoints with coordinates
gis.k <- data.frame(name=c("K0001","K0002","K0016","K0020"),
                    rsoll=c(3469125,3469375,3471875,3472500),
                    hsoll=c(5521625,5521125,5522875,5524510))

# Converting them into a SpatialPointsDataFrame
gis.k$x <- gis.k$rsoll
gis.k$y <- gis.k$hsoll
coordinates(gis.k) <- ~x+y

# Show contents of new SpatialPointsDataFrame
gis.k
         coordinates  name   rsoll   hsoll
1 (3469120, 5521620) K0001 3469125 5521625
2 (3469380, 5521120) K0002 3469375 5521125
3 (3471880, 5522880) K0016 3471875 5522875
4 (3472500, 5524510) K0020 3472500 5524510


In lines 1 to 3 the coordinates from rsoll and hsoll are rounded, for
example 3469125 into 3469120 and 3471875 into 3471880. (In some cases
they are rounded up, in others round down?) Only coordinates with last
digit =0 are not changed.

Is this an expected behaviour and if this is right, why? If not, what
would be the correct solution to convert these coordinates? Or do I
missunderstand something here.

Any help is appreciated. Thanks in advance,
Rainer Hurling
#
Hi,

It's purely a printing issue; the coordinates are unchanged:
x       y
[1,] 3469125 5521625
[2,] 3469375 5521125
[3,] 3471875 5522875
[4,] 3472500 5524510

A number of print methods in R round things to 'look nice', which can
lead to unexpected side-effects, like confused users.

Thanks for the short reproducible example; much appreciated.

Sarah
On Mon, Oct 21, 2013 at 3:27 PM, Rainer Hurling <rhurlin at gwdg.de> wrote:

  
    
#
Hi Sarah,

thanks for clearing this up and the very fast answer.

Greetings from G?ttingen in Germany,
Rainer


Am 21.10.2013 21:31, schrieb Sarah Goslee:
#
yes, and

print(gis.k, digits=7)

gives:
coordinates  name   rsoll   hsoll
1 (3469125, 5521625) K0001 3469125 5521625
2 (3469375, 5521125) K0002 3469375 5521125
3 (3471875, 5522875) K0016 3471875 5522875
4 (3472500, 5524510) K0020 3472500 5524510

I will update the print methods for Spatial objects in sp to use
[1] 7

as a default for digits, instead of 6.
On 10/21/2013 09:41 PM, Rainer Hurling wrote:

  
    
#
Am 22.10.2013 08:58 (UTC+1) schrieb Edzer Pebesma:
After Sarah's answer I remembered, that this is only a printing issue.
Thanks for your further example to print exact coordinates.
That would be very nice! Until now we always have to remember, when
print spatial objects directly [= print(gis.k)], that there could be
some 'pretty printing at work' ;)

Just a small thought: Do 7 digits will fit all usual cases of coordinate
types? Or would it be better to calculate the longest value (number of
digits) before printing coordinates? But I fear, this would slow down
printing to much ...

Thanks for your very good and important sp package. We almost daily use
it heavily,
Rainer