【问题标题】:Creating 2D array from spatial data从空间数据创建二维数组
【发布时间】:2020-08-31 19:56:43
【问题描述】:

我有两个文件,一个在每一行 (x,y,z) 上都有空间数据;一个在每一行都有字段值。空间数据文件中的每一行对应其他字段值文件中的行。

空间数据:

(-2 2 0.1)
(-2 1.92708 0.1)
(-2 1.85417 0.1)
...
(4.92784 1.92708 0.1)
(5 1.85417 0.1)
... 
etc

字段值数据:

4.35
8.90
5.6
44.4
3.4 
.. etc

我想创建一个二维数组。 我的 x 数据:我有 98 个点从 -2 -> 5 均匀分布 我的 y 数据:我有 49 个点从 -1.5 -> 2

我想要一个大小为 98x49 的字段值数据的二维矩阵,对应于空间位置。

问题是,我的数据没有顺序,所以我需要将空间 (x,y) 映射到它的值。然后,我需要给它正确的二维数组索引。

感谢您的帮助!

【问题讨论】:

  • 我不确定我们能否帮助将空间数据映射到字段值。我的建议是将字段数据附加到空间数据作为另一列。然后你可以随心所欲地重塑。或者,由于您似乎没有使用 z 列,您可以将其替换为字段数据,然后重新整形。

标签: python arrays python-3.x pandas numpy


【解决方案1】:

假设您有一个空间数据数组coords,其中每一行给出一个数据点的 (x,y,z) 坐标,以及点 data_vals 的另一个字段值数组,我会这样做类似:

# create empty zeros array to be filled with the data vals 
output = np.zeros((98, 49))

# evenly spaced in x and y, so determine the size of the step along each dimension
# there are 97 steps between -2 and 5 according to your question  
XstepSize = (5 - -2) / 97 
YstepSize = (2 - -1.5) / 48 

# loop for every ROW in your data
for i in range(98): 
      # the loop variable is the row    
      # index of the coordinates 
      # array (every row 
      # corresponds to a data 
      # value) 
      
      # determine the x index
      Xval = coords[i,0] 

      # since your data is evenly spaced in x and y direction
      Xidx = int((Xval - xmin) / XstepSize)
        
      # determine the y index 
      Yval = coords[i,1] 
      Yidx = int((Yval - Ymin) / YstepSize)

      # fill your output with the
      # data at the desired index 
      output[xidx, yidx] = data_vals[i]

此方法采用数据点的连续 (x,y) 坐标并将其映射到 data_vals 的二维数组中的正确索引。

【讨论】:

    猜你喜欢
    • 2016-12-27
    • 2016-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-04
    • 2022-07-09
    • 2020-04-21
    相关资源
    最近更新 更多