1

Lets assume we have a point (described by latitude and longitude) (WGS84) and we form a SpatialPointDataFrame (gData.init). I would like to change the projection (transform) and then use the planar coordinates to estimate distances and intersection points using simple line-point methods. I use the following code to perform the transformation.

library(rgeos)
library(sp)

longitude = 22.954638
latitude  = 40.617048
gData.init = data.frame(longitude,latitude) 
gData.init$id <- as.numeric(rownames(gData.init))
coordinates(gData.init) <- gData.init[c("longitude", "latitude")]
proj4string(gData.init) <- "+proj=longlat +datum=WGS84"
gDataIn2100 <- spTransform( gData.init, CRS("+init=epsg:2100") ) 

Now I want to save the coordinates in any data type object; when I do this using the following code

gDataIn2100@coords

I get maximum one decimal:

     longitude latitude
[1,]  411425.8  4496486

However when I print coordinates (I like lets say my coordinates to be more precise)

print(coordinates(gDataIn2100), digits = 12)

Then the resulting coordinates are somewhat different:

         longitude      latitude
[1,] 411425.810118 4496486.37561

This I think causes different estimation of minimum distances between a line and my point in case of using gDistance and by estimating the distance using LinkPointMinDistance

What do I do wrong?

Manos C
  • 333
  • 4
  • 16
  • You already have an answer to your digits question. It may be worth adding that, if the only reason you are projecting (transforming) the coordinates is to calculate distance, then you can do this more accurately from the original lat long. E.g. see http://stackoverflow.com/questions/32363998/function-to-calculate-geospatial-distance-between-two-points-lat-long-using-r – dww Apr 21 '16 at 12:37
  • Thanks for the suggestion. I have tried using the dist2Line function {geosphere}, however it is much slower that the gDistance function. A pro is that it also estimates several additional parameters (intersection point) and ID of line. – Manos C Apr 21 '16 at 13:17

1 Answers1

4

DataIn2100@coords is equivalent to print(DataIn2100@coords, digits = getOption("digits"))

The decimals are only dropped when rendered to the screen. They are stored as numeric and have the precision of a floating point.

Note that coordinates(DataIn2100) is the recommended way to get the coordinates.

Thierry
  • 18,049
  • 5
  • 48
  • 66
  • You are right. Got lost in some other problems I was facing, so I did not check that saving the coordinates(DataIn2100) in a file also brings back the lost decimals. Thank you for clarifying it. – Manos C Apr 21 '16 at 12:58