This question is pretty much identical to this one except mine is about vector format not raster.
When I export float numbers to a shapefile from terra:writeVector
the output includes many more decimal points than the original file and the values are up to 3e-16 higher or lower than the input. I am sending the file as part of permitting processes so I cannot change the file format.
v <- vect(cbind(1:5,1:5))
crs(v) <- "+proj=longlat +datum=WGS84"
v$id <- 1:length(v)
v$name <- letters[1:length(v)]
v$values<-c(0.3, 3.1, 4.3, 5.0, 9.8)
v$values2<-c(329.7, 124.0, 64.2, 210.0, 254.1)
tmpfname<-paste0("name", ".shp")
p<-writeVector(v, tmpfname, overwrite=TRUE)
#this doesn't work to save the output as an object `p`
v<-vect(tmpfname)
sprintf("%.8f", v$values)
#[1] "329.70000000" "124.00000000" "64.20000000" "210.00000000" "254.10000000"
sprintf("%.16f", v$values)
#[1] "0.3000000000000000" "3.1000000000000001" "4.2999999999999998" "5.0000000000000000" "9.8000000000000007"
sprintf("%.8f", v$values2)
#[1] "329.70000000" "124.00000000" "64.20000000" "210.00000000" "254.10000000"
sprintf("%.16f", v$values2)
#[1] "329.6999999999999886" "124.0000000000000000" "64.2000000000000028" "210.0000000000000000"
#[5] "254.0999999999999943"
When I load the files into QGIS (which I expect the user will do), the numbers look like this:
I tried implementing a solution with FLT8
as proposed for the raster format but couldn’t find anything that worked. I’m pretty sure that FLT4
or FLT2
would be sufficient for my numbers.
1