You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何恢复OSMNX已弃用的ox.distance.get_nearest_edge函数?

问题解决思路

先排查服务器的实际OSMNX版本

你本地1.1.1能触发弃用警告,说明该版本仍保留ox.distance.get_nearest_edge函数,但服务器端报错找不到,大概率是实际安装的OSMNX版本并非1.1.1——conda安装时可能因依赖冲突、缓存问题自动升级到了更高版本(比如1.2+,该版本已彻底移除这个旧函数)。

先在服务器的JupyterLab里执行以下代码确认版本:

import osmnx as ox
print(ox.__version__)

确保服务器安装正确的1.1.1版本

如果输出不是1.1.1,用conda重新指定版本安装:

conda activate 你的环境名
conda install osmnx=1.1.1 --force-reinstall

安装完成后再次验证版本,确认无误后重启JupyterLab再运行脚本。

若仍有问题,安装更早的稳定版本

如果1.1.1版本在服务器环境下仍无法正常加载旧函数,说明可能是依赖包(geopandas/networkx等)的细微差异导致模块加载异常。此时可以安装更早的OSMNX版本,比如1.0.1——该版本完全保留get_nearest_edge函数,且弃用警告还未触发,适配旧脚本更稳妥:

conda install osmnx=1.0.1

终极方案:手动移植旧函数代码

如果不想折腾版本,可以直接从旧版OSMNX中提取get_nearest_edge的实现代码,复制到你的脚本开头。这样不管当前OSMNX版本如何,都能独立使用该函数。示例代码(基于旧版实现):

import numpy as np
import pandas as pd
from shapely.geometry import Point

def get_nearest_edge(G, point, return_dist=False):
    """
    适配旧脚本的get_nearest_edge实现
    """
    # 将点转换为坐标数组
    if isinstance(point, Point):
        point = (point.y, point.x)
    point = np.array(point)
    
    # 获取所有边的节点坐标
    nodes = G.nodes(data=True)
    coords = np.array([(nodes[u]['y'], nodes[u]['x']) for u, v in G.edges()] + 
                      [(nodes[v]['y'], nodes[v]['x']) for u, v in G.edges()])
    edges = np.array(G.edges())
    
    # 计算点到每条边的距离,找到最近的
    from scipy.spatial.distance import cdist
    dists = cdist([point], coords.reshape(-1, 2)).reshape(-1, 2).min(axis=1)
    nearest_idx = dists.argmin()
    nearest_edge = edges[nearest_idx]
    
    if return_dist:
        return nearest_edge, dists[nearest_idx]
    else:
        return nearest_edge

之后脚本里直接调用这个自定义的get_nearest_edge即可,无需依赖OSMNX的旧函数。

内容的提问来源于stack exchange,提问作者PinkyBrain

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 20:03:30