-1

I have spatial data points with x-y-coordinates. They represent the banks of a channel. Now, I need to make this channel bank line to be denser. I want to linearly interpolate the bank points by giving a minimum distance in meters that all interpolated points must have to each other. enter image description here

For example: the blue points are the original points. The red are the interpolated points. They are all at least 1 m apart from each other. The lower right two blue points do not receive an interpolation.

How can I achieve this?

EDIT

Downvoting questions seems to be SO new hobby. What can I do to make you guys happy? My efforts?

par = list()
par$bank.min.dist = 6 # meters

for(i in c(2:length(data$x[ix]))){

      # add the actual point
      banks$x[[bank]] = c(banks$x[[bank]], data$x[ix.l][i-1])
      banks$y[[bank]] = c(banks$y[[bank]], data$y[ix.l][i-1])

      # calculate distance between consecutive bank points
      cb_dist = ( ( data$x[ix][i] - data$x[ix][i-1] )^2 + ( data$y[ix][i] - data$y[ix][i-1] )^2 ) ^(1/2)

      # if distance larger than threshold distance interpolate
      if(cb_dist > par$bank.min.dist){

        # calculate number of artificial points
        nr_of_pts = ceiling(cb_dist / par$bank.min.dist)
        ap = ap + nr_of_pts

        # add artificial points
        for(ido in c(1: (nr_of_pts-1))){

          banks$x[[bank]] = c(banks$x[[bank]], data$x[ix][i-1] + ( ( (data$x[ix][i] - data$x[ix][i-1]) / nr_of_pts ) * ido ) )
          banks$y[[bank]] = c(banks$y[[bank]], data$y[ix][i-1] + ( ( (data$y[ix][i] - data$y[ix][i-1]) / nr_of_pts ) * ido ) )

        }

      }

    }

Currently I'm doing it with a loop. Which is slow but works.

The suggestion by G5W is working! However, it does not incorporate the blue dots in the orange dots. Of course I can add the blue dots later to the interpolated orange ones. But I need the order of the dots intact for later processing.

Since I think G5W is an expert in R, I don't think that there is a one-line-command to do this. Thus, I stick to loop I have. Here is a plot of the timing (I performed 10 runs to do the boxplots):enter image description here

Note: The absolute run time is decreased here, since I set the minimum distance quite high.

agoldev
  • 2,078
  • 3
  • 23
  • 38
  • Not my downvote, but I am sure that part of the problem is the lack of a [Reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). You show a nice plot but are we supposed to create a data set to mimic that? Why don't _you_ provide us with the x-y coordinates of your blue points and the value of minimum distance that you want to get to? – G5W Jan 12 '17 at 18:53

1 Answers1

1

Updated to include original points

This is basically the same as what you did, but using vectorized functions instead of the loop. See if this works better for you.

## Set up test data
x = c(1, 2.5, 5, 8, 8.8) 
y = c(6.5, 7, 4.2, 0, 0)
plot(x,y, pch=21, bg="blue")
MinD = 1

## Get the distances between successive points
DM = as.matrix(dist(data.frame(x,y)))
Distances = DM[row(DM) == col(DM) + 1]

NumPts = ceiling(Distances/MinD) + 1

InterpX = unlist(sapply(1:(length(x)-1), 
    function(i) { 
        PAll = seq(x[i], x[i+1], length.out=NumPts[i])
        PAll[-c(length(PAll))]
     } ))
InterpX = c(InterpX, x[length(x)])
InterpY = unlist(sapply(1:(length(x)-1), 
    function(i) { 
        PAll = seq(y[i], y[i+1], length.out=NumPts[i])
        PAll[-c(length(PAll))] 
    } ))
InterpX = c(InterpX, x[length(x)])

points(InterpX, InterpY , pch=16, col="orange")

Interpolated points

G5W
  • 36,531
  • 10
  • 47
  • 80
  • Awesome, thanks man. Actually, it is faster, but I'm not reproducing all my "orange" dots. I'll update my post. – agoldev Jan 13 '17 at 08:42
  • Actually, I have extra code in there specifically to remove the blue dots. I misunderstood that you wanted them in. I will comment that code out in the solution. – G5W Jan 13 '17 at 11:44
  • I see! Haven't looked at the code carefully. So, this works. I mark it as correct, since there is likely nothing shorter. Thank you! – agoldev Jan 13 '17 at 12:23