如何在 Django 中使用 OSMnx?

How to use OSMnx with Django?

我想创建一个应用程序来计算地图上给定点之间的路线。我正在使用 Conda 来管理我的环境。在我的 venv 中,我安装了 Django 和 OSMnx。添加 OSMnx 服务器后不会 运行。堆栈跟踪很长并以此结尾:

    with fiona._loading.add_gdal_dll_directories():
AttributeError: module 'fiona' has no attribute '_loading'

在我的 Django 项目中,我有一个名为 planner 的应用程序,视图使用的是 OSMnx(不确定这段代码是否应该放在那里),它看起来像这样:

from django.shortcuts import render
from django.http import HttpResponse, JsonResponse
import osmnx as ox
ox.config(use_cache=True, log_console=True)
# Create your views here.

def index(request):
    
    # As for now it is ok to hardcode place and network type
    warsaw_streets = ox.graph_from_place("Warsaw", network_type="drive")
    return JsonResponse({})

我是 Django 的新手,我不知道我是否使用了错误的外部库或者这个错误的目的是什么...

您的 OSMnx 库可能有错误。

你可以试试:

  1. 安装较旧的稳定版本 OSMnx
  2. 使用不同的 python 库

我解决了!我第一次创建 conda env 并通过 'conda install Django' 然后 osmnx 'conda install osmnx' 安装 Django 但它没有用。我尝试了其他方式:

  1. conda config --prepend channels conda-forge
  2. conda create --prefix ./venv --strict-channel-priority osmnx
  3. 激活创建的 venv
  4. pip 安装 Django

这个解决方案对我有用。