mstrimas / smoothr

Spatial Feature Smoothing in R
http://strimas.com/smoothr
GNU General Public License v3.0
96 stars 5 forks source link

smooth 3D linestrings? #9

Closed danstrobridge-Weston closed 3 years ago

danstrobridge-Weston commented 3 years ago

Hi there, I've used your package on a few things and absolutely love it. I've now got some simulated particle tracks from a 3D GW model that i'd like to smooth a little for presentation. I've tried zoo::rollmean on the Z coordinate within a dplyr group_by on lineid and this works ok. However, I'd prefer more of a smoothed spline through the vertices rather than a rolling mean since a few outliers are causing the "smoothed" line to be too far offset above/below the original line in some places. I could potentially do some stats to drop outliers first but that's going to get tedious/subjective.

I've created a z-aware sf linestring but smoother throws:
Error in smoother(m, wrap = all(m[1, ] == m[nrow(m), ]), ...) : ncol(x) == 2 is not TRUE

I gather this means 3D linestrings aren't supported? If not, do you think you might at some point? Beggars can't be choosers but an additional bonus option might be to smooth only on z or smooth on xy and z.

Thanks!

mstrimas commented 3 years ago

That is correct, sf objects with a Z dimension aren't supported. In the dev version, sf and sfc objects will say explicitly that Input geometry has unsupported Z dimension, although I think sfg objects still say ncol(x) == 2 is not TRUE.

Seems like this could be a worthwhile feature to add. Will take a look now to see if it'll be reasonable easy.

I've never worked with 3d sf objects before, can. you recommend a good function for plotting these in R? Would be useful to have to a way to see if the smoothing is working correctly

`

mstrimas commented 3 years ago

This turned out to not be too hard. I've implemented 3D smoothing for all three smoothing algorithms and for densification. It was actually just as easy to make this work for arbitrary N-dimensional features, so if you want to smooth some 14 dimensional lines it should now work 😉

This still needs some further testing and I've yet to add unit tests for 3D smoothing, but once those are done I'll probably do another CRAN release. If you have a minute, it would be useful if you could test out these new smoothing algorithms on your data and let me know if things look good.

danstrobridge-Weston commented 3 years ago

Beautiful! It worked great and am so glad it wasn't all that much effort (apparently) for you. I will only be working in 3D for the foreseeable future :). The kmooth method with a factor of 7 worked reasonably well for my particular case.

image

image