【问题标题】:Mapping GPS coordinates around Chicago using Basemap使用 Basemap 绘制芝加哥周围的 GPS 坐标
【发布时间】:2014-01-29 22:38:41
【问题描述】:

我正在使用 python 的 matplotlibBasemap 库。

我正在尝试为我正在进行的一个项目绘制芝加哥市周围的 GPS 点列表,但它不起作用。我查看了所有可用的示例,但尽管逐字复制和粘贴它们(然后更改 gps 点),但地图无法用绘制的点进行渲染。

以下是一些示例点,因为它们存储在我的代码中:

[(41.98302392, -87.71849159), 
(41.77351707, -87.59144826), 
(41.77508317, -87.58899995),
(41.77511247, -87.58646695), 
(41.77514645, -87.58515301), 
(41.77538531, -87.58611272), 
(41.71339537, -87.56963306), 
(41.81685612, -87.59757281), 
(41.81697313, -87.59910809), 
(41.81695808, -87.60049861), 
(41.75894604, -87.55560586)]

这是我用来渲染地图的代码(不起作用)。

# -*- coding: utf-8 -*-

from pymongo import *
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
from collections import Counter
import ast

def routes_map():
    """  

    doesn't work :(
    # map of chicago
    """ 
    all_locations = []  #<-- this is the example data above
    x = []
    y = []

    for loc in all_locations:     #creates two lists for the x and y (lat,lon) coordinates
        x.append(float(loc[0]))
        y.append(float(loc[1]))

    # llcrnrlat,llcrnrlon,urcrnrlat,urcrnrlon
    # are the lat/lon values of the lower left and upper right corners
    # of the map.
    # resolution = 'i' means use intermediate resolution coastlines.
    # lon_0, lat_0 are the central longitude and latitude of the projection.
    loc = [41.8709, -87.6331]

    # setup Lambert Conformal basemap.
    m = Basemap(llcrnrlon=-90.0378,llcrnrlat=40.6046,urcrnrlon=-85.4277,urcrnrlat=45.1394,
                projection='merc',resolution='h')
    # draw coastlines.
    m.drawcoastlines()
    m.drawstates()

    # draw a boundary around the map, fill the background.
    # this background will end up being the ocean color, since
    # the continents will be drawn on top.
    m.drawmapboundary(fill_color='white')

    x1, y1 = m(x[:100],y[:100])
    m.plot(x1,y1,marker="o",alpha=1.0)
    plt.title("City of Chicago Bus Stops")
    plt.show()

这是我运行这段代码得到的结果:

有人对我做错了什么有任何提示吗?

【问题讨论】:

  • 您的示例图跨越大约 5x5 度纬度 x 经度,而您的示例数据点都在 0.3x0.3 度范围内。因此,即使他们出现,他们也会互相纠缠。

标签: python matplotlib gps matplotlib-basemap


【解决方案1】:

您不小心将纬度值输入为x,将经度值输入为y。在您提供的示例数据中,第一列是 latitude,第二列是 longitude,而不是您的代码似乎认为的相反。

所以使用x.append(float(loc[1]))y.append(float(loc[0])) 而不是你拥有的。

【讨论】:

  • 顺便说一句,读取值的方式比使用append 语句的for 循环要干净得多,即列表理解或使用numpy。而且由于输入数据已经是浮点数,因此无需调用float() 函数(假设您未显示的其余数据也是浮点数)。
  • 这正是问题所在。我可以发誓我已经检查过了。至于我将位置转换为浮点数,那只是我对正在读取的数据进行白痴校对。而且我有 很多 的数据点要绘制,而不是我在此处提供的少数数据点。不过,我不想用一个庞大的清单压倒你们。多谢大佬。
  • 不用担心——容易出错。 (太容易了,我花了 30 多分钟弄乱你的代码才能自己抓住它!)
猜你喜欢
  • 1970-01-01
  • 2014-05-06
  • 1970-01-01
  • 2021-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多