问题
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.
- scales must remain realistic, X and Y should have same step size.
- the matrix doesn't have to catch the exact elevations picture, It will obviously need some kind of resolution reduction in order to have a constant number of pixels.
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.
- 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.
- Is there a numpy function for converting my set of points to the desired matrix? (maybe meshgrid with steps of a constant value?)
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!
回答1:
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()
回答2:
I am aware that I am not answering half of your questions but this is how I would do it:
- Create a 2D array of the desired resolution,
- The "leftmost" values correspond to the smallest values of x and so forth
- Fill the array with the elevation value of the closest match in terms of x and y values
- Smoothen the result.
来源:https://stackoverflow.com/questions/33926704/converting-coordinates-vector-to-numpy-2d-matrix