我正在使用SF库函数st_linestring将矩阵转换为线串。当我在矩阵上使用此函数时,会将值四舍五入为7位数字。无论有多少位,我都需要最多6个小数位的有效位。例如,数字109.319272会四舍五入为109.319300。
这是两个矩阵:
> testFt
[,1] [,2] [,3]
[1,] 353367.5 1228429 0.000000
[2,] 353363.2 1228428 4.332678
[3,] 353359.3 1228433 10.712694
[4,] 353356.0 1228438 16.029563
[5,] 353358.1 1228451 29.364214
[6,] 353366.3 1228469 49.616436
[7,] 353383.1 1228461 68.395384
[8,] 353382.9 1228443 86.336387
[9,] 353374.4 1228429 102.443063
[10,] 353367.5 1228429 109.319272
> st_coordinates(helperTest)
X Y M L1
[1,] 353367.5 1228429 0.000000 1
[2,] 353363.2 1228428 4.332678 1
[3,] 353359.3 1228433 10.712690 1
[4,] 353356.0 1228438 16.029560 1
[5,] 353358.1 1228451 29.364210 1
[6,] 353366.3 1228469 49.616440 1
[7,] 353383.1 1228461 68.395380 1
[8,] 353382.9 1228443 86.336390 1
[9,] 353374.4 1228429 102.443100 1
[10,] 353367.5 1228429 109.319300 1
第一个矩阵具有正确的精度。第二个矩阵是通过使用st_linestring将第一个矩阵转换为一个线串而得到的。然后,我将线串转换为sf对象,以使用st_coordinates轻松显示矩阵,但是,如果仅打印出线串,则可以看到第三列中的值已四舍五入。有没有办法设置st_linestring的精度,以便我的值像第一个矩阵一样具有6个小数的有效位?
答案 0 :(得分:1)
对于一些测试数据,我无法复制。
制作3x10矩阵:
> testFt = matrix(runif(30),ncol=3)
> testFt
[,1] [,2] [,3]
[1,] 0.89427444 0.9614064 0.03305242
[2,] 0.64785749 0.2379120 0.76285818
[3,] 0.18872965 0.3535477 0.19718624
[4,] 0.21516340 0.4436407 0.36707556
[5,] 0.85731714 0.4872524 0.46147387
[6,] 0.98970232 0.6678123 0.76021971
[7,] 0.16932569 0.1547750 0.16983325
[8,] 0.18255042 0.7313963 0.60732292
[9,] 0.03580905 0.4369531 0.32653902
[10,] 0.21047936 0.4164918 0.40285367
转换为LINESTRING:
> testFtPts = st_linestring(testFt, dim="XYM")
> testFtPts
LINESTRING M (0.8942744 0.9614064 0.03305242, 0.6478575 0.237912 0.7628582, 0.1887296 0.3535477 0.1971862, 0.2151634 0.4436407 0.3670756, 0.8573171 0.4872524 0.4614739, 0.9897023 0.6678123 0.7602197, 0.1693257 0.154775 0.1698332, 0.1825504 0.7313963 0.6073229, 0.03580905 0.4369531 0.326539, 0.2104794 0.4164918 0.4028537)
任何明显的精度损失都归因于显示截断而不是值的实际舍入。如果我转换为坐标:
> testFtPtsCoord = st_coordinates(testFtPts)
> testFtPtsCoord
X Y M L1
[1,] 0.89427444 0.9614064 0.03305242 1
[2,] 0.64785749 0.2379120 0.76285818 1
[3,] 0.18872965 0.3535477 0.19718624 1
[4,] 0.21516340 0.4436407 0.36707556 1
[5,] 0.85731714 0.4872524 0.46147387 1
[6,] 0.98970232 0.6678123 0.76021971 1
[7,] 0.16932569 0.1547750 0.16983325 1
[8,] 0.18255042 0.7313963 0.60732292 1
[9,] 0.03580905 0.4369531 0.32653902 1
[10,] 0.21047936 0.4164918 0.40285367 1
这些值都相同:
> all(testFt == testFtPtsCoord[,1:3])
[1] TRUE
我什至尝试使用与您的数据相同的粗略范围内的数字进行尝试:
> set.seed(99)
> testFt = cbind(runif(10,353300,353400),runif(10,1228400, 1228500), runif(10, 0, 100))
> testFt
[,1] [,2] [,3]
[1,] 353358.5 1228455 22.7903474
[2,] 353311.4 1228451 8.0524150
[3,] 353368.4 1228419 82.1618422
[4,] 353399.3 1228464 59.1113977
[5,] 353353.5 1228469 77.3389019
[6,] 353396.7 1228464 35.0085977
[7,] 353367.1 1228436 0.6061323
[8,] 353329.5 1228410 81.4506223
[9,] 353335.8 1228410 0.1179229
[10,] 353317.5 1228418 20.0690183
> testFtPts = st_linestring(testFt, dim="XYM")
> testFtPtsCoord = st_coordinates(testFtPts)
> all(testFt == testFtPtsCoord[,1:3])
[1] TRUE
>
假设这是您从矩阵到线再到矩阵的方式,那么我无法复制任何精度损失。