简体   繁体   English

将地图坐标转换为纬度/经度

[英]converting map coordinates to lat/long

Having received a very helpful and useful solution to my problem of contouring irregular data, Contours with map overlay on irregular grid in python , here I still have the problem of combining the contours with the basemap. 对于我的轮廓不规则数据问题, Contours在python中将轮廓覆盖在不规则网格上 ,这已经收到了非常有用和有用的解决方案,在这里我仍然遇到了将轮廓与底图相结合的问题。 For this I know I have to do: x,y = m(lon,lat) . 为此,我知道我必须做: x,y = m(lon,lat) None of the SO posts I have seen corresponds to my needs. 我见过的SO职位均不符合我的需求。 My problem is where do I get (from my various parameters available) the x,y that the lon, lat use in the above formula? 我的问题是(从可用的各种参数中)我可以从上面的公式中得到lon, latx,y吗? Here is my map object: 这是我的地图对象:

m = Basemap(projection = 'merc',llcrnrlon = 21, llcrnrlat = -18, urcrnrlon = 34, urcrnrlat = -8, resolution='h')

and here is the data for contours and the gridding: 这是轮廓和网格的数据:

data = pd.read_csv('meansr.txt', delim_whitespace=True)
numcols, numrows = 300, 300
xi = np.linspace(data.Lon.min(), data.Lon.max(), numcols)
yi = np.linspace(data.Lat.min(), data.Lat.max(), numrows)
xi, yi = np.meshgrid(xi, yi)

x, y, z = data.Lon.values, data.Lat.values, data.Z.values
zi = griddata(x, y, z, xi, yi)

Here are the plotting commands 这是绘图命令

plt.figure()
plt.contourf(xi, yi, zi)
plt.scatter(data.Lon, data.Lat, c=data.Z, s=100,
vmin=zi.min(), vmax=zi.max())
plt.colorbar()
plt.show()

This gives me two plots, a basemap and contours side by side, which I think is due to the difference in the two coordinates. 这给了我两个图,一个底图和一个并排的轮廓,我认为这是由于两个坐标的不同。 Pointing inside the contours indicates the correct latitudes and longitudes, while on the map the x, y displayed are very large, order of 2000 and above. 指向轮廓内部表示正确的纬度和经度,而在地图上显示的x, y非常大,约为2000或更高。 Any help please. 请帮忙。

Changed my reply a bit, so that what i typed in reply to your changed version of your problem: https://stackoverflow.com/a/20950887/2393569 works with the same names/... as in your code you gave here: 更改了我的答复,以便我键入的内容来答复问题的更改版本: https : //stackoverflow.com/a/20950887/2393569可以使用相同的名称/ ...与在此处输入的代码相同:

data = pd.read_csv('meansr.txt', delim_whitespace=True) 
numcols, numrows = 30, 30 
xi = np.linspace(data.Lon.min(), data.Lon.max(), numrows)
yi = np.linspace(data.Lat.min(), data.Lat.max(), numcols)
xi, yi = np.meshgrid(xi, yi)
x, y, z = data.Lon, data.Lat, data.Z

# NOTE: from here on it changes
points = np.vstack((x,y)).T 
values = z
wanted = (xi, yi)
zi = griddata(points, values, wanted) # for more info on griddata: http://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.griddata.html

# from here its the same code again as you had
plt.figure()
plt.contourf(xi, yi, zi)
plt.scatter(data.Lon, data.Lat, c=data.Z, s=100, vmin=zi.min(), vmax=zi.max())
plt.colorbar()
plt.show()

so the problem you actually had was not fully understanding scipy.interpolate.griddata , and not really a problem related to matplotlib. 因此,您实际上遇到的问题不是完全了解scipy.interpolate.griddata ,也不是与matplotlib有关的真正问题。

So the correct use is 所以正确的用法是

scipy.interpolate.griddata(points, values, xi, method='linear', fill_value=nan)
# 'points' has to contain what you named  x and y
# 'values' has to contain what you named z
# 'xi' has to contain what you named xi,yi

Does the plotting still give you trouble after you change this? 更改此设置后,绘图是否还会给您带来麻烦?

Note: i assume your imports are: 注意:我假设您的进口是:

import numpy as np
from scipy.interpolate import griddata
import pylab as plt
# i guess the pd.read_csv is something pandas (heard of it, but never used it before, but since the only thing you do is read_csv i guess that wont cause problems)

It is best to give your imports too, if you ask a question here, since i just found out that from matplotlib.mlab import griddata exists too, and with a similar syntax to what you used, in Contours with map overlay on irregular grid in python you use the griddata from scipy, but the answer, on which you seem to have based your follow-up question (this one) contains the matplotlib.mlab import of griddata (which works differently). 如果我在这里问一个问题,最好也进行导入,因为我刚刚发现from matplotlib.mlab import griddata存在,并且语法与您使用的语法类似,在等高线中的地图覆盖在不规则网格中在python中,您使用了来自scipy的griddata,但是您似乎在其后续问题(此问题)所基于的答案中包含了matplotlib.mlab导入的griddata(其工作原理不同)。 Does this solve your confusion? 这能解决您的困惑吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM