简体   繁体   English

使用python查找网格上的纬度点的索引

[英]find indices of lat lon point on a grid using python

I am new to python, and I can't figure out how to find the minimum distance from a given lat/lon point (which is not given from the grid, but selected by me) to a find the closest indices of a lat/lon point on a grid. 我是python的新手,我不知道如何找到从给定纬​​度/经度点(不是从网格中给出,而是由我选择)到找到纬度/经度的最接近索引的最小距离网格上的lon点。

Basically , I am reading in an ncfile that contains 2D coordinates: 基本上,我正在读取包含2D坐标的ncfile:

coords = 'coords.nc'
fh = Dataset(coords,mode='r')
lons = fh.variables['latitudes'][:,:]
lats = fh.variables['longitudes'][:,:]
fh.close()

>>> lons.shape
(94, 83)
>>> lats.shape
(94, 83)

I want to find the indices in the above grid for the nearest lat lon to the below values: 我想在上面的网格中找到与以下值最近的纬度的索引:

sel_lat=71.60556
sel_lon=-161.458611

I tried to make lat/lon pairs in order to use the scipy.spatial.distance function, but I still am having problems because I did not set up the input arrays to the format it wants, but I don't understand how to do that: 我尝试制作纬度/经度对以便使用scipy.spatial.distance函数,但是仍然遇到问题,因为我没有将输入数组设置为所需的格式,但是我不知道该怎么做那:

latLon_pairsGrid = np.vstack(([lats.T],[lons.T])).T

>>> latLon_pairsGrid.shape
(94, 83, 2)

distance.cdist([sel_lat,sel_lon],latLon_pairsGrid,'euclidean')

Any help or hints would be appreciated 任何帮助或提示,将不胜感激

I think I found an answer, but it is a workaround that avoids calculating distance between the chosen lat/lon and the lat/lons on the grid. 我想我找到了答案,但这是一种变通方法,可以避免计算所选纬度/经度和网格上纬度/经度之间的距离。 This doesn't seem completely accurate because I am never calculating distances, just the closest difference between lat/lon values themselves. 这似乎并不完全准确,因为我从不计算距离,而只是计算纬度/经度值本身之间最接近的差。

I used the answer to the question find (i,j) location of closest (long,lat) values in a 2D array 我使用了问题的答案来查找2D数组中最接近的(长,纬)值的(i,j)位置

a = abs(lats-sel_lat)+abs(lons-sel_lon)
i,j = np.unravel_index(a.argmin(),a.shape)

Using those returned indices i,j, I can then find on the grid the coordinates that correspond most closely to my selected lat, lon value: 然后,使用这些返回的索引i,j,我可以在网格上找到与所选的经度和经度值最接近的坐标:

>>> lats[i,j]
71.490295
>>> lons[i,j]
-161.65045

Checkout the pyresample package. 检出pyresample包装。 It provides spatial nearest neighbour search using a fast kdtree approach: 它使用快速kdtree方法提供空间最近邻居搜索:

import pyresample
import numpy as np

# Define lat-lon grid
lon = np.linspace(30, 40, 100)
lat = np.linspace(10, 20, 100)
lon_grid, lat_grid = np.meshgrid(lon, lat)
grid = pyresample.geometry.GridDefinition(lats=lat_grid, lons=lon_grid)

# Generate some random data on the grid
data_grid = np.random.rand(lon_grid.shape[0], lon_grid.shape[1])

# Define some sample points
my_lons = np.array([34.5, 36.5, 38.5])
my_lats = np.array([12.0, 14.0, 16.0])
swath = pyresample.geometry.SwathDefinition(lons=my_lons, lats=my_lats)

# Determine nearest (w.r.t. great circle distance) neighbour in the grid.
_, _, index_array, distance_array = pyresample.kd_tree.get_neighbour_info(
    source_geo_def=grid, target_geo_def=swath, radius_of_influence=50000,
    neighbours=1)

# get_neighbour_info() returns indices in the flattened lat/lon grid. Compute
# the 2D grid indices:
index_array_2d = np.unravel_index(index_array, grid.shape)

print "Indices of nearest neighbours:", index_array_2d
print "Longitude of nearest neighbours:", lon_grid[index_array_2d]
print "Latitude of nearest neighbours:", lat_grid[index_array_2d]
print "Great Circle Distance:", distance_array

There is also a shorthand method for directly obtaining the data values at the nearest grid points: 还有一种简便的方法可以直接获取最近的网格点处的数据值:

data_swath = pyresample.kd_tree.resample_nearest(
    source_geo_def=grid, target_geo_def=swath, data=data_grid,
    radius_of_influence=50000)
print "Data at nearest grid points:", data_swath

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM