I have a similar question here
That question is about resemble the tiff data, and I want to find a more universal way to deal with it.
My question
For example:
- A 2-d numpy array with the shape of 200x150 represent the 1 km x 1 km resolution population density data.
http://i4.tietuku.com/060fe38e9ccf6daf.png
- My target: change the space resolution => 5 km x 5 km resolution
this is an example picturefor random distributed data cluster into grid network http://i4.tietuku.com/a4c1355413f8cdbc.png
* the red point: original data
* the blue dot: grid network represent the 2-d array * the green circle: find the nearest blue dot for each red point and sum them.
* In this question, the difference is that the original data is 2-d numpy array too.
My solution
Similar with my another question here which I cluster 2-d scatter point to nearest grid point. And I appreciate the answer supported by @HYRY which improved my code a lot.
In that question, I use KD-tree algorithm to find the nearest network node of each original point data. The result shows here:
http://i4.tietuku.com/1a420c48ed7bcb1c.pngI think there must be some easier way to reshape the structured 2-d numpy array rather than random 2-d scatter point.
Add 2016-01-09
Thanks for the answer from @Praveen.
I have another method using scipy interpolate 2d function.
This is my code:
xi = np.linspace(x_map1,x_map2,pop.shape[1])
yi = np.linspace(y_map1,y_map2,pop.shape[0])
hfunc = interpolate.interp2d(xi,yi,pop)
x_grid = np.linspace(x_map1,x_map2,new_shape_x)
y_grid = np.linspace(y_map1,y_map2,new_shape_y)
new_pop = np.zeros(new_shape_x * new_shape_y)
t = 0
for i in range(0,new_shape_y,1):
for j in range(0,new_shape_y,1):
new_pop[t] = hfunc(x_grid[j],y_grid[i])
t+=1
new_pop = new_pop.reshape(new_shape_y,new_shape_x)
plt.pcolormesh(new_pop)
The result shows like:
http://i4.tietuku.com/b020db6dc2d75d70.png
- Are there some problems when I use interpolate to coarser the data?
Add 2
Is there some useful function that I can sample some data from origin array dataset by location(x,y)?