- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我在使用带有纬度和经度数据的 Python basemap 模块绘制 RGB 图像时遇到问题。现在,我可以绘制我想要的图,但问题是它有多慢,因为它能够比 RGB 数据更快地绘制单 channel 数据,而且一般来说,自己绘制 RGB 图像也是如此快速地。由于我有纬度/经度数据,这就是事情变得复杂的地方。我已经检查了这个问题的解决方案:
How to plot an irregular spaced RGB image using python and basemap?
这就是我到达现在位置的方式。它本质上归结为以下问题。在 basemap 中使用 pcolormesh
方法时,要绘制 RGB 数据,您必须定义一个 colorTuple 参数,它将逐点映射 RGB 数据。由于数组大小约为 2000x1000,因此这需要一段时间才能完成。下面是我正在谈论的片段(完整的工作代码在下方):
if one_channel:
m.pcolormesh(lons, lats, img[:,:,0], latlon=True)
else:
# This is the part that is slow, but I don't know how to
# accurately plot the data otherwise.
mesh_rgb = img[:, :-1, :]
colorTuple = mesh_rgb.reshape((mesh_rgb.shape[0] * mesh_rgb.shape[1]), 3)
# What you put in for the image doesn't matter because of the color mapping
m.pcolormesh(lons, lats, img[:,:,0], latlon=True,color=colorTuple)
当只绘制一个 channel 时,它可以在大约 10 秒左右的时间内绘制出 map 。绘制 RGB 数据时,可能需要 3-4 分钟。鉴于只有 3 倍多的数据,我觉得一定有更好的方法,尤其是当您绘制矩形图像时,绘制 RGB 数据的速度与单 channel 数据一样快。
所以,我的问题是:是否有任何方法可以加快此计算速度,可以使用其他绘图模块(例如 Bokeh)或以任何方式更改颜色映射?我尝试使用 imshow
仔细选择 map 边界,但由于它只是将图像拉伸(stretch)到 map 的整个范围,这对于准确绘制数据来说还不够好。
下面是我的代码的一个精简版本,它适用于具有正确模块的示例:
from pyhdf.SD import SD,SDC
import numpy as np
from scipy.interpolate import interp1d
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
def get_hdf_attr(infile,dataset,attr):
f = SD(infile,SDC.READ)
data = f.select(dataset)
index = data.attr(attr).index()
attr_out = data.attr(index).get()
f.end()
return attr_out
def get_hdf_dataset(infile,dataset):
f = SD(infile,SDC.READ)
data = f.select(dataset)[:]
f.end()
return data
class make_rgb:
def __init__(self,file_name):
sds_250 = get_hdf_dataset(file_name, 'EV_250_Aggr1km_RefSB')
scales_250 = get_hdf_attr(file_name, 'EV_250_Aggr1km_RefSB', 'reflectance_scales')
offsets_250 = get_hdf_attr(file_name, 'EV_250_Aggr1km_RefSB', 'reflectance_offsets')
sds_500 = get_hdf_dataset(file_name, 'EV_500_Aggr1km_RefSB')
scales_500 = get_hdf_attr(file_name, 'EV_500_Aggr1km_RefSB', 'reflectance_scales')
offsets_500 = get_hdf_attr(file_name, 'EV_500_Aggr1km_RefSB', 'reflectance_offsets')
data_shape = sds_250.shape
along_track = data_shape[1]
cross_track = data_shape[2]
rgb = np.zeros((along_track, cross_track, 3))
rgb[:, :, 0] = (sds_250[0, :, :] - offsets_250[0]) * scales_250[0]
rgb[:, :, 1] = (sds_500[1, :, :] - offsets_500[1]) * scales_500[1]
rgb[:, :, 2] = (sds_500[0, :, :] - offsets_500[0]) * scales_500[0]
rgb[rgb > 1] = 1.0
rgb[rgb < 0] = 0.0
lin = np.array([0, 30, 60, 120, 190, 255]) / 255.0
nonlin = np.array([0, 110, 160, 210, 240, 255]) / 255.0
scale = interp1d(lin, nonlin, kind='quadratic')
self.img = scale(rgb)
def plot_image(self):
fig = plt.figure(figsize=(10, 10))
ax = fig.add_subplot(111)
ax.set_yticks([])
ax.set_xticks([])
plt.imshow(self.img, interpolation='nearest')
plt.show()
def plot_geo(self,geo_file,one_channel=False):
fig = plt.figure(figsize=(10, 10))
ax = fig.add_subplot(111)
lats = get_hdf_dataset(geo_file, 0)
lons = get_hdf_dataset(geo_file, 1)
lat_0 = np.mean(lats)
lat_range = [np.min(lats), np.max(lats)]
lon_0 = np.mean(lons)
lon_range = [np.min(lons), np.max(lons)]
map_kwargs = dict(projection='cass', resolution='l',
llcrnrlat=lat_range[0], urcrnrlat=lat_range[1],
llcrnrlon=lon_range[0], urcrnrlon=lon_range[1],
lat_0=lat_0, lon_0=lon_0)
m = Basemap(**map_kwargs)
if one_channel:
m.pcolormesh(lons, lats, self.img[:,:,0], latlon=True)
else:
# This is the part that is slow, but I don't know how to
# accurately plot the data otherwise.
mesh_rgb = self.img[:, :-1, :]
colorTuple = mesh_rgb.reshape((mesh_rgb.shape[0] * mesh_rgb.shape[1]), 3)
m.pcolormesh(lons, lats, self.img[:,:,0], latlon=True,color=colorTuple)
m.drawcoastlines()
m.drawcountries()
plt.show()
if __name__ == '__main__':
# https://ladsweb.nascom.nasa.gov/archive/allData/6/MOD021KM/2015/183/
data_file = 'MOD021KM.A2015183.1005.006.2015183195350.hdf'
# https://ladsweb.nascom.nasa.gov/archive/allData/6/MOD03/2015/183/
geo_file = 'MOD03.A2015183.1005.006.2015183192656.hdf'
# Very Fast
make_rgb(data_file).plot_image()
# Also Fast, takes about 10 seconds
make_rgb(data_file).plot_geo(geo_file,one_channel=True)
# Much slower, takes several minutes
make_rgb(data_file).plot_geo(geo_file)
最佳答案
我通过将 1.0 添加到 colorTuple 的每个部分的值以将其转换为 RGBA 数组来解决这个问题。我检查了 pcolormesh
函数,发现它调用颜色转换器将 RGB 转换为 RGBA 数组 4 次不同的时间,每次大约需要 50 秒。如果你给它一个 RGBA 数组作为开始,它会绕过它并在合理的时间范围内生成绘图。添加的附加代码行如下所示:
if one_channel:
m.pcolormesh(lons, lats, img[:,:,0], latlon=True)
else:
mesh_rgb = img[:, :-1, :]
colorTuple = mesh_rgb.reshape((mesh_rgb.shape[0] * mesh_rgb.shape[1]), 3)
# ADDED THIS LINE
colorTuple = np.insert(colorTuple,3,1.0,axis=1)
# What you put in for the image doesn't matter because of the color mapping
m.pcolormesh(lons, lats, img[:,:,0], latlon=True,color=colorTuple)
关于python - 如何使用 Python basemap 更快地绘制地理定位的 RGB 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41389335/
我正在创建一个显示世界地图的图形,上面有一些数据(这与这里无关)。现在我希望能够使用平移/缩放按钮或缩放到矩形按钮来放大它,然后在完成放大后将图形保存到图片文件中。问题是轴注释(和 lng-/lat-
我在 Python 2.7 上使用 basemap ,但想使用 Python 3,因此,转向 cartopy。如果您能给我一些如何将我的代码从 basemap 更改为 cartopy 的建议,那就太棒
我在安装 Matplotlib basemap 工具包时真的很难受,谁能帮我解决这个问题... 我已按照以下说明操作: basemap-1.x.x $cd geos-3.3.3 basemap-1.x
我已经安装 basemap 使用 conda install basemap 我可以使用导入 import mpl_toolkits.basemap as bm 但是打电话 bm.Basemap(pr
我想计算地球表面两点之间的距离(以米为单位) 我尝试过同时使用 basemap 和 cartopy,但两者的结果不同。 basemap : import mpl_toolkits.basemap.py
我是 python 和 matplotlib(以及 stackoverflow)的新手。你能告诉我如何用这个椭圆函数扩展我的 basemap 类吗?来自 regeirk 的原始帖子“在 matplot
from mpl_toolkits.basemap import Basemap 给予 ImportError: No module named 'mpl_toolkits.basemap' 我用
我想在开放图层中使用 wmts map 服务。 wmts层应该是基础层,它应该只显示wmts层,没有别的! 开放层的问题是我只能看到 osm 层而根本看不到 wmts 层。 还是应该使用 getCap
我们可以使用 MapBox API 来显示 bing basemap 或 ESRI basemap (例如 ESRI 街道或 ESRI Topo 等)吗?我正在浏览 MapBox API,但似乎没有找
如何在 basemap 库中添加 basemap 。在默认的 BasemapGallery 类中,我们只有 5 个 basemap 图层。但是我们如何向该 BasemapGallery 添加另一个/自
是否可以指定 ESRI basemap 图库 (esri/dijit/BasemapGallery) 使用哪些 ArcGIS basemap ? 将 showArcGISBasemaps 设置为 tr
我使用的是 Mac OS X 10.6.8。我使用 http://www.python.org/ 中的二进制安装程序安装了 Python 2.6。 .自 2011 年 3 月以来,我一直将它与 Sci
简而言之,我需要由传单图层控件控制的图层组,一次两到三个。在此 JSFiddle ,在更改 basemap 时,水力叠加层需要始终位于各种 basemap 之上。 如果您运行并使用右上角的图层控件,您
我对颜色条的文本位置有疑问。我正在使用 matplotlib Basemap 绘制一些图,我使用 colorbar()功能。 现在我需要把我的颜色条放在我的图的左边。所以我使用 location='l
我想在 word 文档中添加一个 basemap 。 在官员包的文档中有一个使用 plot_instr 函数的示例: anyplot <- plot_instr(code = { barplot(
我正在尝试熟悉 matplotlib 和 Basemap。首先,我尝试生成一个与我有数据的特定网格相匹配的格陵兰图像。 下面令人毛骨悚然的细节描述了我的问题:我无法创建大小与所需投影/区域相匹配的图像
我看到一些奇怪的 basemap 行为。它画了一个大圆,里面有一个缺口。 以下是从温哥华到伦敦绘制大圆的代码: from mpl_toolkits.basemap import Basemap imp
map = Basemap(resolution ='c') map.drawcoastlines() map.drawcountries() map.drawmeridian
我正在将代码中的默认 basemap 更改为 mapbox.streets。但它不会再从 baseLayerPicker 小部件更新 baselayer。 var vi
installing basemap 中有一些非常有用的链接对于 python,但是在尝试测试安装是否有效时,我似乎在安装结束时遇到了一个问题。我正在 Mac 上安装。 我看到的错误信息是: Fi
我是一名优秀的程序员,十分优秀!