python - 使用 Python 将纬度、经度、值 CSV 转换为栅格 map

标签 python numpy maps raster

如果我有一个包含纬度、经度和值字段的 CSV 数据集,使用 python 生成栅格 map 的最佳方法是什么?栅格 Z 字段可以是该表中的任何列。

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 数组

最佳答案

根据您的纬度、经度坐标,有两个选项。当纬度、经度坐标形成等距网格时,您可以使用第一个选项,否则您可以使用下面的第二个选项。

<小时/>

第一个选项

我使用第一列中的值以及第二列和第三列中的纬度、经度创建下面的数组:

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]])

当您的纬度、经度坐标整齐地排序后,您可以 reshape 值以获得如下所示的网格数组:

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]])
<小时/>

第二个选项

当您有一堆随机的纬度、经度坐标以及此处创建的值时:

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]])

您可以使用如下插值将其转换为网格:

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 值。

您可以使用 plt.imshow 可视化结果

import matplotlib.pyplot as plt
plt.imshow(grid_array)

关于python - 使用 Python 将纬度、经度、值 CSV 转换为栅格 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52165359/

相关文章:

python - 如何解码这个小端(int32)消息?

python - 将 XYZ 文件中的不规则 3d 数据插值到规则网格

php - 没有路况的谷歌地图方向请求

android - 使用 Apple( map )URL Scheme 时,非 iOS 设备会发生什么情况?

maps - Google Maps API 系列使用的域列表

python - 如何将自定义 Pytorch 模型转换为 torchscript(pth 到 pt 模型)?

即使安装了 pyzmq,Python 也无法导入 zmq

python - [AZURE]OSError : libgomp. so.1:无法打开共享对象文件:没有这样的文件或目录

python - 如何将np数组存储到psql数据库和django中

python - 为数组赋值(Python、Numpy)