在地图上放置坐标 - Python [英] Placing coordinates on a map - Python

查看:39
本文介绍了在地图上放置坐标 - Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在地图图像上绘制一些坐标(纬度和经度).我正在绘制的数据来自列表列表(纬度和经度分别是第二和第三元素).我已经阅读了一些有关matplotlib和'extent'的文章,但是我仍然无法将坐标与地图对齐(也就是说,我无法使其按比例缩放").

I am trying to plot some coordinates (latitudes and longitudes) on top of an image of a map. The data I am plotting comes from a list of lists (the lats and lons are the 2nd and 3rd elements respectively). I have done some reading about matplotlib and 'extent', but I am still having trouble with aligning the coordinates with the map (that is, I can't get it 'to scale' so to speak).

def mapping_data(atlas_data):
    i = 0
    x = []
    y = []
    while i < len(atlas_data):
        x.append(atlas_data[i][1])
        y.append(atlas_data[i][2])
        i = i + 1

    image = plt.imread('map.png')
    mapped = plt.imshow(image)
    plt.imshow(mpimg.imread('map.png'))
    plt.scatter(x, y)
    plt.show()

按原样运行代码时,我会得到im1(地图正常,但全部指向一个点而不是地图上).当我添加所需的地图范围时,如下所示:

When I run my code as is, I get im1 (map normal, but points all in one spot and not on the map). When I add the desired extent of the map as below:

(plt.imshow(mpimg.imread('map.png'), extent = (149.105, 149.130, -35.29, -35.27))

...我得到了im2(点数与以前一样,但是地图图像上下颠倒了)?

... I get im2 (points as before, but map image flipped upside down)??

有人可以帮我解决问题吗?我错过了一步吗?我真的很感激任何在对齐方面的帮助!

Can someone please help me with what is going wrong? Have I missed a step? I would really appreciate any help in aligning things!

以下是我用于参考的数据示例:

Here's an example of the data I'm using for reference:

[['Kookaburra',
  -35.27667,
  149.1097,
  'Australian National Botaniacl Gardens, Canberra',
  '2000-08-14',
  'Aves',
  'Coraciiformes',
  'Alcedinidae',
  'Dacelo',
  'Dacelo novaeguineae',
  'False'],
 ['White-faced heron',
  -35.272244105599064,
  149.12580246473127,
  'Sullivans Creek--Turner Parkland',
  '2016-08-09',
  'Aves',
  'Ciconiiformes',
  'Ardeidae',
  'Egretta',
  'Egretta novaehollandiae',
  'False'],
 ['Australian King-parrot',
  -35.274386,
  149.112636,
  'CSIRO (Black Mountain)',
  '2014-10-20',
  'Aves',
  'Psittaciformes',
  'Psittacidae',
  'Alisterus',
  'Alisterus scapularis',
  'False'],
 ['Eastern Spinebill',
  -35.27719917903922,
  149.10937031732462,
  'Australian National Botanic Gardens',
  '2000-09-08',
  'Aves',
  'Passeriformes',
  'Meliphagidae',
  'Acanthorhynchus',
  'Acanthorhynchus tenuirostris',
  'False'],
 ['Crimson Rosella',
  -35.2780499,
  149.11015749999999,
  'Australian National Botanic Gardens',
  '2003-08-08',
  'Aves',
  'Psittaciformes',
  'Psittacidae',
  'Platycercus',
  'Platycercus elegans',
  'False'],
 ['Australian Raven',
  -35.27856893080605,
  149.10974594347084,
  'Australian National Botanic Gardens',
  '2018-03-18',
  'Aves',
  'Passeriformes',
  'Corvidae',
  'Corvus',
  'Corvus coronoides',
  'False'],
 ['Australian King-parrot',
  -35.2780499,
  149.11015749999999,
  'Australian National Botanic Gardens',
  '2012-07-24',
  'Aves',
  'Psittaciformes',
  'Psittacidae',
  'Alisterus',
  'Alisterus scapularis',
  'False']]

推荐答案

您快到那里了!x 和 y 需要交换.我还整理了一下您的功能.

You nearly got there! The x and y need to be swapped. I also cleaned up your function a bit.

以下应能工作:

import matplotlib.pyplot as plt
import matplotlib.image as mpimg

fig, ax = plt.subplots()

atlas_data = [['Kookaburra',
  -35.27667,
  149.1097,
  'Australian National Botaniacl Gardens, Canberra',
  '2000-08-14',
  'Aves',
  'Coraciiformes',
  'Alcedinidae',
  'Dacelo',
  'Dacelo novaeguineae',
  'False'],
 ['White-faced heron',
  -35.272244105599064,
  149.12580246473127,
  'Sullivans Creek--Turner Parkland',
  '2016-08-09',
  'Aves',
  'Ciconiiformes',
  'Ardeidae',
  'Egretta',
  'Egretta novaehollandiae',
  'False'],
 ['Australian King-parrot',
  -35.274386,
  149.112636,
  'CSIRO (Black Mountain)',
  '2014-10-20',
  'Aves',
  'Psittaciformes',
  'Psittacidae',
  'Alisterus',
  'Alisterus scapularis',
  'False'],
 ['Eastern Spinebill',
  -35.27719917903922,
  149.10937031732462,
  'Australian National Botanic Gardens',
  '2000-09-08',
  'Aves',
  'Passeriformes',
  'Meliphagidae',
  'Acanthorhynchus',
  'Acanthorhynchus tenuirostris',
  'False'],
 ['Crimson Rosella',
  -35.2780499,
  149.11015749999999,
  'Australian National Botanic Gardens',
  '2003-08-08',
  'Aves',
  'Psittaciformes',
  'Psittacidae',
  'Platycercus',
  'Platycercus elegans',
  'False'],
 ['Australian Raven',
  -35.27856893080605,
  149.10974594347084,
  'Australian National Botanic Gardens',
  '2018-03-18',
  'Aves',
  'Passeriformes',
  'Corvidae',
  'Corvus',
  'Corvus coronoides',
  'False'],
 ['Australian King-parrot',
  -35.2780499,
  149.11015749999999,
  'Australian National Botanic Gardens',
  '2012-07-24',
  'Aves',
  'Psittaciformes',
  'Psittacidae',
  'Alisterus',
  'Alisterus scapularis',
  'False']]

def mapping_data(atlas_data):
    x, y = [], []
    for i in range(len(atlas_data)):
        x.append(atlas_data[i][1])
        y.append(atlas_data[i][2])

    return x, y

y, x = mapping_data(atlas_data)

ax.scatter(x, y, edgecolors='red', linewidths=2, zorder=2)
ax.imshow(mpimg.imread('https://preview.ibb.co/jSD99o/map.png'), extent=(149.105, 149.130, -35.29, -35.27), zorder=1)

plt.show()

仅供参考,我创建了一个链接,指向该地区谷歌地图的截图,因此实际位置是估计的,坐标不会完全匹配.

FYI, I created a link to a screenshotted image from google maps in that area, so the actual location is estimated and the coordinates will not match exactly.

这篇关于在地图上放置坐标 - Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆