使用OSMNX时未绘制街道数据

发布于 2025-02-06 17:53:25 字数 2100 浏览 2 评论 0原文

我正在尝试使用OSMNX绘制各个城市的街道地图。如教程中所示,我已经能够成功地绘制德国柏林,但任何其他城市似乎都空白(只出现背景色)。我已经检查了我的代码是否特定于位置,并且无法找到可能导致错误的任何东西。我尝试了2个美国,瓦伦西亚和柏林的城市,也尝试了唯一正确的绘制柏林的城市。

import osmnx as ox
import networkx as nx
from PIL import Image, ImageOps, ImageColor, ImageFont, ImageDraw
from matplotlib.lines import Line2D
import matplotlib.cm as cm
import matplotlib.colors as colors
import pandas as pd

place = ['Valencia, Spain']
G = ox.graph_from_place(place, retain_all = True, simplify = True, network_type = 'all')

u = []
v = []
key = []
data = []

for uu, vv, kkey, ddata in G.edges(keys = True, data = True):
    u.append(uu)
    v.append(vv)
    key.append(kkey)
    data.append(ddata)

roadColors = []
roadWidths = []

for item in data:
    if 'highway' in item.keys():
        if item['length'] <= 100:
            linewidth = 0.1
            color = '#454545'
            
        elif item['length'] > 100 and item['length'] <= 200:
            linewidth = 0.15
            color = '#676767'
            
        elif item['length'] > 200 and item['length'] <= 400:
            linewidth = 0.25
            color = '#a6a6a6'
            
        elif item['length'] > 400 and item['length'] <= 800:
            linewidth = 0.35
            color = '#bdbdbd'
            
        elif item['length'] > 800:
            linewidth = 0.45
            color = '#d5d5d5'
            
        else:
            linewidth = 0.1
            color = '#a6a6a6'
            
        if 'primary' in item['highway']:
            linewidth = 0.5
            color = '#ffff'

    roadColors.append(color)
    roadWidths.append(linewidth)

latitude = 39.4699
longitude = 0.3763

north = latitude + 0.15
south = latitude - 0.15
east = longitude + 0.15
west = longitude - 0.15

bgcolor = '#061529'

fig, ax = ox.plot_graph(G, node_size = 0, bbox = (north, south, east, west),
                       dpi = 300, bgcolor = bgcolor, save = False,
                       edge_color = roadColors, edge_linewidth = roadWidths,
                       edge_alpha = 1)

fig.tight_layout(pad = 0)

I am trying to plot street maps of various cities using OSMnx. I have been able to successfully plot Berlin, Germany as shown in a tutorial, but any other city appears blank (only the background color appears). I have examined my code for anything that is location specific and am not able to find anything that may be causing an error. I have tried 2 US cities, Valencia, and Berlin and the only one that plots correctly is Berlin.

import osmnx as ox
import networkx as nx
from PIL import Image, ImageOps, ImageColor, ImageFont, ImageDraw
from matplotlib.lines import Line2D
import matplotlib.cm as cm
import matplotlib.colors as colors
import pandas as pd

place = ['Valencia, Spain']
G = ox.graph_from_place(place, retain_all = True, simplify = True, network_type = 'all')

u = []
v = []
key = []
data = []

for uu, vv, kkey, ddata in G.edges(keys = True, data = True):
    u.append(uu)
    v.append(vv)
    key.append(kkey)
    data.append(ddata)

roadColors = []
roadWidths = []

for item in data:
    if 'highway' in item.keys():
        if item['length'] <= 100:
            linewidth = 0.1
            color = '#454545'
            
        elif item['length'] > 100 and item['length'] <= 200:
            linewidth = 0.15
            color = '#676767'
            
        elif item['length'] > 200 and item['length'] <= 400:
            linewidth = 0.25
            color = '#a6a6a6'
            
        elif item['length'] > 400 and item['length'] <= 800:
            linewidth = 0.35
            color = '#bdbdbd'
            
        elif item['length'] > 800:
            linewidth = 0.45
            color = '#d5d5d5'
            
        else:
            linewidth = 0.1
            color = '#a6a6a6'
            
        if 'primary' in item['highway']:
            linewidth = 0.5
            color = '#ffff'

    roadColors.append(color)
    roadWidths.append(linewidth)

latitude = 39.4699
longitude = 0.3763

north = latitude + 0.15
south = latitude - 0.15
east = longitude + 0.15
west = longitude - 0.15

bgcolor = '#061529'

fig, ax = ox.plot_graph(G, node_size = 0, bbox = (north, south, east, west),
                       dpi = 300, bgcolor = bgcolor, save = False,
                       edge_color = roadColors, edge_linewidth = roadWidths,
                       edge_alpha = 1)

fig.tight_layout(pad = 0)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小…楫夜泊 2025-02-13 17:53:25

对于Valencia,您写的:

longitude = 0.3763

最好使用通过格林威治。

longitude = -0.3763

在描述Prime子午线以西的位置时,

For Valencia you wrote:

longitude = 0.3763

Better to use

longitude = -0.3763

when describing a location West of the prime meridian running through Greenwich.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文