geocompx / geocompr

Geocomputation with R: an open source book
https://r.geocompx.org/
Other
1.59k stars 584 forks source link

The **st_coordinates** will drop the decimal part. #1019

Closed sy5938 closed 11 months ago

sy5938 commented 11 months ago

The st_coordinates in sf package ,it can retrieve coordinates in matrix form. But how can I choose the number of decimal places to display?(I did try to find some arguments to set the decimal , pity I did not find out.) He will only retain the integer part. Seeing the images below.

sf_coordinates: image

QGIS coordinates: image

Automatically droping the decimal will lose some precision, when we can get the real and precise location by using RTK equipment. I hope there's a solution, that will save a step of exporting.

Robinlovelace commented 11 months ago

Hi @sy5938 you could try st_set_precision() or after converting to numeric values with the round() function in base R. Does that help? If you have a reproducible example to show what you have tried and what you're trying to achieve that could help. Interesting we don't seem to mention st_precision() or related functions in the book: https://github.com/search?q=repo%3Ageocompx%2Fgeocompr+precision&type=code

sy5938 commented 11 months ago

Hi Prof. @Robinlovelace .It doesn't look good.

Sorry I can't provide the original shpfile data, so I randomly sample 4 points (Uploaded as an attachment sample1.zip). My code:

library(sf)
test_point <- read_sf("sample1.gpkg")
st_set_precision(test_point,0.001)
st_coordinates(test_point)

Using the sample data, the st_set_precision() also didn't work well. Did I miss something? Perhaps read_sf directly drop the decimal? If read_sf can set an argument, that will be good.

image

image

JosiahParry commented 11 months ago

When you read in the dataset that you exported into QGIS, do you still have the same number of decimals? Because we're reading in data using gdal and i don't expect it to drop an precision.

Nowosad commented 11 months ago

The numbers are rounded when print, but they still have the same precision. See:

library(sf)

multipoint_matrix = rbind(c(1.0000005, 2), c(1, 3), c(3, 4), c(3, 2))
mp = st_multipoint(multipoint_matrix)

mpc = st_coordinates(mp)
mpc
#>             X Y L1
#> [1,] 1.000001 2  1
#> [2,] 1.000000 3  1
#> [3,] 3.000000 4  1
#> [4,] 3.000000 2  1

mpc[1,1]
#>        X 
#> 1.000001

formatC(mpc[1, 1], digits = 10)
#>             X 
#> "  1.0000005"
sy5938 commented 11 months ago

Thanks a bunch! Very clear now.

mdsumner commented 11 months ago

bit easier with

op <- options(digits = 16)
## st_coordinates(mp) etc
options(op)
sy5938 commented 11 months ago

@mdsumner Wow! Thanks for your wisdom. I shouldn't forget the most basic R setting. My bad....

Guys in this issue, I appreciate all your time! Thanks a lot!🤘