Tom
Tom

Reputation: 464

Converting coordinates vector to numpy 2D matrix

I have a set of 3D coordinates points: [lat,long,elevation] ([X,Y,Z]), derived from LIDAR data. The points are not sorted and the steps size between the points is more or less random.

My goal is to build a function that converts this set of points to a 2D numpy matrix of a constant number of pixels where each (X,Y) cell hold the Z value, then plot it as elevations heatmap.

The solution I was thinking of is to build a bucket for each pixel, iterate over the points and put each in a bucket according to it's (X,Y) values. At last create a matrix where each sell holds the mean of the Z values in the corresponding bucket.

  1. Since I don't have lots of experience in this field I would love to hear some tips and specially if there are better ways to address this task.

  2. Is there a numpy function for converting my set of points to the desired matrix? (maybe meshgrid with steps of a constant value?)

  3. If I build very sparse matrix, where the step size is

    min[min{Xi,Xj} , min{Yk,Yl}] for all i,j,k,l

    is there a way to "reduce" the resolution and convert it to a matrix with the required size?

Thanks!

Upvotes: 3

Views: 3540

Answers (2)

yevgeniy
yevgeniy

Reputation: 908

You don't need to reinvent the bicycle.

from matplotlib.mlab import griddata
import numpy as np

#-- Your coordinates
x = np.random.random(100)
y = np.random.random(100)
z = np.random.random(100)*10
#--

#-- Your new grid
xsteps=200    # resolution in x
ysteps=200    # resolution in y
xi = linspace(min(x), max(x), xsteps)
yi = linspace(min(y), max(y), ysteps)
Z = griddata(x, y, z, xi, yi)  # interpolates between points in your data
#--

plt.pcolormesh(xi, yi, Z, cmap=plt.cm.hot)   # plot your elevation map :D
plt.show()

Upvotes: 4

steffen
steffen

Reputation: 8958

I am aware that I am not answering half of your questions but this is how I would do it:

  1. Create a 2D array of the desired resolution,
  2. The "leftmost" values correspond to the smallest values of x and so forth
  3. Fill the array with the elevation value of the closest match in terms of x and y values
  4. Smoothen the result.

Upvotes: 0

Related Questions