【问题标题】:Planet NDVI calculation: ModuleNotFoundError: No module named 'rasterio'Planet NDVI 计算:ModuleNotFoundError: No module named 'rasterio'
【发布时间】:2017-07-31 13:30:14
【问题描述】:

我正在按照 Planet 的 documentation 对 Planet Scope 4 波段图像执行 NDVI 计算

以下代码块是我写的:

从工作目录中的原始图像中提取波段数据

import rasterio import numpy

image_file = "20170430_194027_0c82_3B_AnalyticMS"

with rasterio.open(image_file) as src:    band_red = src.read(3)

with rasterio.open(image_file) as src:    band_nir = src.read(4)

from xml.dom import minidom

xmldoc = minidom.parse("20170430_194027_0c82_3B_AnalyticMS_metadata") nodes = xmldoc.getElementsByTagName("ps:bandSpecificMetadata")

从目录中的元数据文件中提取TOA校正系数

TOA_coeffs = {} for node in nodes:    bn = node.getElementsByTagName("ps:bandNumber")[0].firstChild.data    if bn in ['1', '2', '3', '4']:
       i = int(bn)
       value = node.getElementsByTagName("ps:ReflectanceCoefficient")[0].firstChild.data
       TOA_coeffs[1] = float(value)

计算 NDVI 并保存文件

band_red = band_red * TOA_coeffs[3] band_nir = band_nir * TOA_coeffs[4]

numpy.seterr(divide = 'ignore', invalid = 'ignore')

NDVI = (band_nir.astype(float) - band_red.astype(float))/(band_nir + band_red) numpy.nanmin(NDVI), numpy.nanmax(NDVI)

kwargs = src.meta kwargs.update(dtype=rasterio.float32, 
             count = 1)

with rasterio.open('ndvi.tif', 'W', **kwargs) as dst:    dst.write_band(1, NDVI.astype(rasterio.float32))

添加符号系统并绘制颜色条

import matplotlib.pyplot as plt import matplotlib.colors as colors

class MidpointNormalize(colors.Normalize):    def __init__(self, vmin=None, vmax=None, midpoint=None, clip=False):
       self.midpoint = midpoint
       colors.Normalize.__init__(self, vmin, vmax, clip)
           def __call__(self, value, clip=None):
       x, y = [self.vmin, self.midpoint, self.vmax], [0, 0.5, 1]
       return numpy.ma.masked_array(numpy.interp(value, x, y), >numpy.isnan(value))
    min = numpy.nanmin(NDVI) min = numpy.nanmax(NDVI) mid = 0.1

fig = plt.figure(figsize= (20,10)) ax = fig.add_subplot(111)

cmap = plt.cm.RdYlGn

cax = ax.imshow(NDVI, cmap=cmap, clim=(min,max),
>norm=MidpointNormalize(midpoint=mid, vmin=min, vmax=max))

ax.axis('off') ax.set_title('NDVI_test', fontsize= 18, fontweight='bold')

cbar = fig.colorbar(cax, orientation= 'horizontal', shrink=0.65)

fig.savefig("output/NDVI_test.png", dpi=200, bbox_inches='tight',
>pad_inches=0.7)

plt.show()

绘制 NDVI 像素值分布的直方图

fig2 = plt.figure(figsize=(10,10)) ax = fig2.add_subplot(111)

plt.title("NDVI Histogram", fontsize=18, fontweight='bold') plt.xlabel("NDVI values", fontsize=14) plt.ylabel("# pixels", fontsize=14)

x = NDVI[~numpy.isnan(NDVI)] numBins = 20 ax.hist(x,numBins,color='green',alpha=0.8)

fig2.savefig("output/ndvi-histogram.png", dpi=200, bbox_inches='tight', >pad_inches=0.7)

plt.show()

唉,脚本的执行在代码的开头被缩短了:

File "C:/Users/David/Desktop/ArcGIS files/Planet Labs/2017.6_Luis_Bedin_Bolivia/planet_order_58311/20170430_194027_0c82/TOA_correction_NDVI.py", line 8, in <module>
    import rasterio
ModuleNotFoundError: No module named 'rasterio'

所以我决定安装 rasterio,应该可以解决问题:

C:\Users\David\Desktop\ArcGIS files\Planet Labs\2017.6_Luis_Bedin_Bolivia\planet_order_58311\20170430_194027_0c82>pip install rasterio
Collecting rasterio
  Using cached rasterio-0.36.0.tar.gz
Requirement already satisfied: affine in c:\users\david\anaconda3\lib\site-packages (from rasterio)
Requirement already satisfied: cligj in c:\users\david\anaconda3\lib\site-packages (from rasterio)
Requirement already satisfied: numpy in c:\users\david\anaconda3\lib\site-packages (from rasterio)
Requirement already satisfied: snuggs in c:\users\david\anaconda3\lib\site-packages (from rasterio)
Requirement already satisfied: click-plugins in c:\users\david\anaconda3\lib\site-packages (from rasterio)

我的解释是已经安装了 rasterio。如果 Python 控制台告诉我没有名为 rasterio 的模块,这怎么可能呢?控制台的输出还显示需要 Microsoft Visual C++。经过进一步研究,我发现了这个用户的solution。我试过了,但控制台也告诉我已经安装了 rasterio:

(envpythonfs) C:\Users\David\Desktop\ArcGIS files\Planet Labs\2017.6_Luis_Bedin_Bolivia\planet_order_58311\20170430_194027_0c82>conda install rasterio gdal
Fetching package metadata .............
Solving package specifications: .

# All requested packages already installed.
# packages in environment at C:\Users\David\Anaconda3\envs\envpythonfs:
#

我在 Windows 10 64 位机器上使用 Spyder 3.1.2 和 Python 3.6 创建脚本。

【问题讨论】:

  • 也许我可以在 ArcMap 中使用 python 控制台,并跳过在 gis 软件环境之外起草脚本。但是,我在编码方面没有经验,不知道从我上面发布的内容中删除什么以及保留什么。所有这些都是我从 Planet 的文档中获得的。

标签: satellite-image rasterio


【解决方案1】:

我认为pip 不是确保正确处理依赖关系的最佳方式。由于您已经在使用 anaconda,我建议:

conda install rasterio -c conda-forge/label/dev

请注意,从标记为 dev 的版本安装不是长期解决方案(请参阅https://github.com/conda-forge/rasterio-feedstock/pull/36)。

【讨论】:

  • 感谢您的帮助 jdmcbr,我遇到语法错误: conda install rasterio -c conda-forge/label/dev File "",第 1 行 conda安装 rasterio -c conda-forge/label/dev ^ SyntaxError: invalid syntax
  • 抱歉,该命令应该在 anaconda 命令提示符中输入,而不是在 python 会话中。
  • (C:\Users\David\Anaconda3) C:\Users\David&gt;conda install rasterio -c conda-forge/label/dev Fetching package metadata ............... Solving package specifications: . `` `` UnsatisfiableError: The following specifications were found to be in conflict: ` - python 3.6*` ` - rasterio -> python 3.5*` Use "conda info &lt;package&gt;" to see the dependencies for each package. `` 显然,我有python 3.6 rasterio 适用于 3.5;真可惜。人们会认为新版本仍会采用旧版本。
猜你喜欢
  • 2022-12-27
  • 2022-12-27
  • 2022-12-19
  • 2020-02-10
  • 1970-01-01
  • 1970-01-01
  • 2022-08-24
  • 2022-10-17
  • 2022-08-22
相关资源
最近更新 更多