使用 Python 将纬度、经度、CSV 值转换为栅格地图 [英] Lat, Lon, Values CSV to Raster Map Using Python
问题描述
如果我有一个包含 Lat、Lon 和 Value 字段的 CSV 数据集,使用 python 生成栅格地图的最佳方法是什么?栅格 Z 字段可以是此表中的任何列..
If I have a CSV Dataset with Lat, Lon, and Value fields, what is the best approach on generating a raster map using python? The raster Z field can be any column within this table..
L5 L6 L7 L8 L9 L10 L11 L12 L13 L14 LAT LON
3.571732 1.338448 0 9.96921E+36 -3.482845 -1.42944 133.229919 141.246002 67.685631 5.059844 24.335797 -95.088764
3.571732 1.338448 0 9.96921E+36 -3.420345 -1.42944 132.749512 140.641464 67.318848 5.105563 24.335107 -95.060013
3.571732 1.338448 0 9.96921E+36 -3.420345 -1.42944 132.230164 140.047211 67.318848 5.063346 24.334408 -95.031263
3.571732 1.338448 0 9.96921E+36 -3.420345 -1.42944 132.230164 139.463104 67.318848 5.063346 24.333701 -95.002512
3.509232 1.369698 0 9.96921E+36 -3.357845 -1.42944 131.702133 137.82196 66.940475 5.021552 24.332986 -94.973763
3.509232 1.369698 0 9.96921E+36 -3.357845 -1.49194 131.702133 137.26651 66.043732 5.021552 24.332263 -94.945013
3.509232 1.369698 0 9.96921E+36 -3.357845 -1.49194 131.165268 136.72081 66.043732 4.980192 24.331531 -94.916265
3.509232 1.338448 0 9.96921E+36 -3.357845 -1.49194 131.165268 136.184738 66.043732 4.980192 24.330792 -94.887516
记住,这些是numpy数组
Keep in mind, These are numpy arrays
推荐答案
根据您的经纬度坐标,有两个选项.当 Lat、Lon 坐标形成等距网格时,您可以使用第一个选项,否则您可以使用下面的第二个选项.
There are two options depending on your Lat, Lon coordinates. When the Lat, Lon coordinates form an equidistant grid you can use this first option, otherwise you can use the second option below.
第一个选项
我使用第一列中的值和第二和第三列中的 Lat、Lon 创建了下面的数组:
I create the array below with values in the first column and Lat, Lon in the 2nd and 3rd column:
import numpy as np
lat = np.arange(0, 15, 5)
lon = np.arange(0, 10, 5)
val = np.random.randint(0,10, size =len(lat)*len(lon))
xx, yy = np.meshgrid(lon, lat)
array = np.array([val, yy.ravel(), xx.ravel()]).T
print(array)
>>> array([[ 7, 0, 0],
[ 8, 0, 5],
[ 7, 5, 0],
[ 3, 5, 5],
[ 2, 10, 0],
[ 8, 10, 5]])
当你的纬度、经度坐标被整齐地排序后,你可以重新调整值以获得这样的网格数组:
When your Lat, Lon coordinates are neatly sorted you can reshape the values to get a grid array like this:
no_lon = len(np.unique(array[:,-1]))
no_lat = len(np.unique(array[:,-2]))
grid_array = array[:,0].reshape((no_lat,no_lon))[::-1]
print(grid_array)
>>> array([[2, 8],
[7, 3],
[7, 8]])
<小时>
第二个选项
second option
当你有一堆随机的 Lat、Lon 与这里创建的值协调时:
When you have a bunch of random Lat, Lon coordinates with values like the one created here:
array = np.random.randint(0,10, size =(6,3))
print(array)
>>> array([[9 6 0]
[7 8 8]
[6 0 9]
[7 7 4]
[2 4 3]
[0 2 9]])
您可以使用如下插值将其转换为网格:
you can convert this to a grid by using interpolation like this:
from scipy import interpolate
lon_list = np.arange(3, 6, 1)
lat_list = np.arange(4, 8, 1)
lon_2d, lat_2d = np.meshgrid(lon_list, lat_list)
grid_array = interpolate.griddata((array[:,-1], array[:,-2]), array[:,0],
(lon_2d, lat_2d))[::-1]
print(grid_array)
>>> [[ nan 7. 6.72]
[ 6. 5.4 5.6 ]
[ 4. 3.8 4. ]
[ 2. 2.2 2.4 ]]
请注意,如果您的网格单元不在您的点的范围内,您将获得 nan 值.
Note that you get nan values if your grid cells are not within the bounds of your points.
您可以使用 plt.imshow 将结果可视化
You can visualize the results using plt.imshow
import matplotlib.pyplot as plt
plt.imshow(grid_array)
这篇关于使用 Python 将纬度、经度、CSV 值转换为栅格地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!