kd tree python_Python scipy.spatial 模块,KDTree() 实例源码 - 编程字典

def get_closest_station(latitude, longitude, minumum_recent_data=20140000,

match_max=100):

'''Query function to find the nearest weather station to a particular

set of coordinates. Optionally allows for a recent date by which the

station is required to be still active at.

Parameters

----------

latitude : float

Latitude to search for nearby weather stations at, [degrees]

longitude : float

Longitude to search for nearby weather stations at, [degrees]

minumum_recent_data : int, optional

Date that the weather station is required to have more recent

weather data than; format YYYYMMDD; set this to 0 to not restrict data

by date.

match_max : int, optional

The maximum number of results in the KDTree to search for before

applying the filtering criteria; an internal parameter which is

increased automatically if the default value is insufficient [-]

Returns

-------

station : IntegratedSurfaceDatabaseStation

Instance of IntegratedSurfaceDatabaseStation which was nearest

to the requested coordinates and with sufficiently recent data

available [-]

Notes

-----

Searching for 100 stations is a reasonable choice as it takes, ~70

microseconds vs 50 microsecond to find only 1 station. The search does get

slower as more points are requested. Bad data is returned from a KDTree

search if more points are requested than are available.

Examples

--------

>>> get_closest_station(51.02532675, -114.049868485806, 20150000)

'''

# Both station strings may be important

# Searching for 100 stations is fine, 70 microseconds vs 50 microsecond for 1

# but there's little point for more points, it gets slower.

# bad data is returned if k > station_count

distances, indexes = kd_tree.query([latitude, longitude], k=min(match_max, station_count))

#

for i in indexes:

latlon = _latlongs[i]

enddate = stations[i].END

# Iterate for all indexes until one is found whose date is current

if enddate > minumum_recent_data:

return stations[i]

if match_max < station_count:

return get_closest_station(latitude, longitude, minumum_recent_data=minumum_recent_data, match_max=match_max*10)

raise Exception('Could not find a station with more recent data than '

'specified near the specified coordinates.')

# This should be agressively cached

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值