Planet NDVI计算:ModuleNotFoundError:没有名为'rasterio'的模块

时间:2017-07-31 13:30:15

标签: satellite-image rasterio

我正按照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创建脚本。

1 个答案:

答案 0 :(得分:0)

我认为pip不是确保依赖关系得到妥善处理的最佳方法。既然你已经在使用anaconda,我会建议:

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

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

相关问题