【问题标题】:How to check if a coordinate pair (lat,lon) exists in a coordinate grid?如何检查坐标网格中是否存在坐标对(纬度,经度)?
【发布时间】:2019-07-31 15:49:58
【问题描述】:

我有一种算法,可以在满足某些条件时使用地理坐标计算形状。该算法输出一个纬度列表和一个经度列表,如下所示。所以 lat[0] 和 lon[0] 将代表一个坐标对。我想创建一个经纬度地图形状的布尔数组,如果算法输出中存在相应的坐标点,则索引将为真。我有来自 netcdf 文件的原始纬度和经度信息,但是如何制作一个可以与算法输出进行比较的二维坐标点数组,然后使用匹配的索引来制作这个布尔数组?

我尝试将纬度和经度组合成一个数组。 len(lat) = 81, len(lon) = 480 我需要一个 (81,480) 数组。我想我必须使用一个 numpy where 函数来确定坐标对匹配的位置。

lat_alg = [-47.25 -47.25 -47.25 -48.   -48.   -48.   -48.   -48.   -48.   -48.
 -48.   -48.75 -48.75 -48.75 -48.75 -48.75 -48.75 -49.5  -49.5  -49.5
 -49.5  -50.25 -50.25 -50.25]
lon_alg = [225.75 226.5  227.25 226.5  227.25 228.   228.75 229.5  230.25 231.
 231.75 228.   228.75 229.5  230.25 231.   231.75 229.5  230.25 231.
 231.75 230.25 231.   231.75]

我创建的布尔数组是... ar_tracker = np.zeros((len(lat),len(lon)))

我希望在坐标匹配的地方输出为 1。

【问题讨论】:

  • 请指出其中一个答案是否适合您!如果其中一个是您接受的答案,那么您可以单击“已接受”复选框(位于赞成/反对投票标志的正下方,单击时变为绿色)。所以每个人都知道,不需要更多的答案,这个任务已经结束了。

标签: python numpy logic geography


【解决方案1】:

由于您有真实值,因此您无法使用== 进行检查。所以我们必须使用受不等式限制的窗口。结果,我们获得了所选窗口内元素的索引(本例中为 11 和 12)

# 1. build numpy arrays
lat = np.array([-47.25, -47.25, -47.25, -48.,   -48.,   -48.,   -48.,   -48.,   -48.,   -48., -48.,   -48.75, -48.75, -48.75, -48.75, -48.75, -48.75, -49.5,  -49.5,  -49.5, -49.5,  -50.25, -50.25, -50.25])
lon= np.array([225.75, 226.5,  227.25, 226.5,  227.25, 228.,   228.75, 229.5,  230.25, 231., 231.75, 228.,   228.75, 229.5,  230.25, 231.,   231.75, 229.5,  230.25, 231., 231.75, 230.25, 231., 231.75])


# 2. pick the values in the desired window for each data series, set to zeros the others
La = np.where( (lat> -49.0) & (lat<-48), lat, 0*lat)
Lo = np.where( (lon>226) & (lon<229), lon, 0*lon)

#3. give the indices where both series are non-zero
ind = np.argwhere(np.abs(Lo*La)>0.0001)
ind
array([[11],
       [12]], dtype=int64)

或者如果您更喜欢带有布尔值的数组:

(np.abs(Lo*La)>0.0001).astype(int)
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0])

【讨论】:

    【解决方案2】:

    试试这个:

    import numpy as np
    
    lat_alg = np.array([-47.25, -47.25, -47.25, -48., -48., -48., -48., -48., -48.,
                        -48., -48., -48.75, -48.75, -48.75, -48.75, -48.75, -48.75,
                        -49.5, -49.5, -49.5, -49.5, -50.25, -50.25, -50.25])
    lon_alg = np.array([225.75, 226.5, 227.25, 226.5, 227.25, 228., 228.75, 229.5,
                        230.25, 231., 231.75, 228., 228.75, 229.5, 230.25, 231.,
                        231.75, 229.5, 230.25, 231., 231.75, 230.25, 231., 231.75])
    
    desired_position = np.array([-47.5, 228])
    tolerance = 1
    
    lat = np.abs(lat_alg - desired_position[0]) <= tolerance
    lon = np.abs(lon_alg - desired_position[1]) <= tolerance 
    
    desired_area = np.outer(lat, lon).astype(int)
    

    如果您想比较精确匹配而不是坐标windows,请尝试使用numpy.isclose 以避免浮点差异:

    import numpy as np
    
    lat_alg = np.array([-47.25, -47.25, -47.25, -48., -48., -48., -48., -48., -48.,
                        -48., -48., -48.75, -48.75, -48.75, -48.75, -48.75, -48.75,
                        -49.5, -49.5, -49.5, -49.5, -50.25, -50.25, -50.25])
    lon_alg = np.array([225.75, 226.5, 227.25, 226.5, 227.25, 228., 228.75, 229.5,
                        230.25, 231., 231.75, 228., 228.75, 229.5, 230.25, 231.,
                        231.75, 229.5, 230.25, 231., 231.75, 230.25, 231., 231.75])
    
    desired_position = np.array([-47.5, 228])
    
    lat = np.isclose(lat_alg - desired_position[0], 0)
    lon = np.isclose(lon_alg - desired_position[1], 0)
    
    exact_matches = np.outer(lat, lon).astype(int)
    

    desired_area 和 exact_matches 都是形状为 (len(lat), len(lon)) 的二维数组。

    【讨论】:

      【解决方案3】:

      为了完全确定该对是否存在,我建议您构建一个元组列表,其中每个元组包含一对 (lat,lon)。例如:

      def Mesh(X,Y):
          A=[]
          for x,y in zip(X,Y):
              A.append((x,y))
      
          return A
      
      Coord=Mesh(lat_alg,lon_alg)
      

      然后,如果你知道你的网格分辨率,你可以很容易地检查一对如下:

      coord=(-49.5,230.25)
      
      if coord in Coord:
          print('True')
      else:
          print('False')
      

      【讨论】:

        猜你喜欢
        • 2015-02-18
        • 2012-07-15
        • 1970-01-01
        • 2018-08-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-07
        相关资源
        最近更新 更多