微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Geemap 包 - PCA 分析

如何解决Geemap 包 - PCA 分析

我使用的是geemap包(它适应了gee for python的gee功能) 首先,我创建了一个图像集:

#Select and Filter Sentinel 2 L2A Image

     sentimages = ee.ImageCollection('copERNICUS/S2') \
    .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE',10)) \
    .filterDate("2020-01-02","2020-01-30") \
    .filterBounds(geometry)
    
    sentmosaic = sentimages.mosaic()
    sentimage = sentmosaic.clip(geometry)
    
    print("Sentinel 2 Scene",sentimage)

此外,我正在执行 PCA 分析:

    Map = geemap.Map()
    
    # PCA Code
    def getPrincipalComponents(centered,scale,region):
      # Collapse the bands of the image into a 1D array per pixel.
      arrays = centered.toArray()
      print('PCA applying on',centered)
      # Compute the covariance of the bands within the region.
      covar = arrays.reduceRegion({
        'reducer': ee.Reducer.centeredCovariance(),'geometry': geometry,'scale': scale,'maxPixels': 1e9
      })
    
      # Get the 'array' covariance result and cast to an array.
      # This represents the band-to-band covariance within the region.
      covararray = ee.Array(covar.get('array'))
    
      # Perform an eigen analysis and slice apart the values and vectors.
      eigens = covararray.eigen()
    
      # This is a P-length vector of Eigenvalues.
      eigenValues = eigens.slice(1,1)
      # This is a PxP matrix with eigenvectors in rows.
      eigenVectors = eigens.slice(1,1)
    
      # Convert the array image to 2D arrays for matrix computations.
      arrayImage = arrays.toArray(1)
    
      # Left multiply the image array by the matrix of eigenvectors.
      principalComponents = ee.Image(eigenVectors).matrixMultiply(arrayImage)
    
      # Turn the square roots of the Eigenvalues into a P-band image.
      sdImage = ee.Image(eigenValues.sqrt()) \
        .arrayProject([0]).arrayFlatten([getNewBandNames('sd')])
    
      # Turn the PCs into a P-band image,normalized by SD.
      return principalComponents \
        .arrayProject([0]) \
        .arrayFlatten([getNewBandNames('pc')]) \
        .divide(sdImage)
    
    # Sentinel-2 True Color Map
    TrueColor = {
      'bands': ["B4","B3","B2"],'min': 0,'max': 3000,'gamma':1.5
    }
    Map.addLayer(sentimage,TrueColor,"Sentinel 2 True Color")
    
    # Sentinel-2 False Color Map
    FalseColor = {
      'bands': ["B11","B12","B8"],FalseColor,"Sentinel 2 False Color")
    
    # Sentinel - 2 PCA
    # display the input imagery and the region in which to do the PCA.
    sentbands = ['B2','B3','B4','B8','B11','B12']
    region = sentimage.geometry()
    image =  sentimage.select(sentbands)
    
    # Set some information about the input to be used later.
    scale = 30
    bandNames = image.bandNames()
    
    # Mean center the data to enable a faster covariance reducer
    # and an SD stretch of the principal components.
    meanDict = image.reduceRegion({
        'reducer': ee.Reducer.mean(),'geometry': roi,'maxPixels': 1000000000
    })
    means = ee.Image.constant(meanDict.values(bandNames))
    centered = image.subtract(means)
    
    
    
    # This helper function returns a list of new band names.
    def getNewBandNames(prefix):
        seq = ee.List.sequence(1,bandNames.length())
    
    def func_rci(b):
    return ee.String(prefix).cat(ee.Number(b).int())
    
    return seq.map(func_rci)
    
    pcImage = getPrincipalComponents(centered,region)
    
    # Plot each PC as a new layer
    Map.addLayer(pcImage,{'bands': ['pc4','pc5','pc3'],'min': -2,'max': 2},'Sentinel 2 - PCA')

但是,我收到以下错误

EEException: Invalid argument for ee.Reducer(): ({'reducer': <ee.Reducer object at 0x00000261415B0F48>,'geometry': ee.Geometry({
      "functionInvocationValue": {
        "functionName": "Feature.geometry","arguments": {
          "feature": {
            "functionInvocationValue": {
              "functionName": "Feature","arguments": {
                "geometry": {
                  "functionInvocationValue": {
                    "functionName": "GeometryConstructors.polygon","arguments": {
                      "coordinates": {
                        "constantValue": [
                          [
                            [
                              -52.035679,-21.745825
                            ],[
                              -52.035679,-21.53033
                            ],[
                              -51.770479,-21.745825
                            ]
                          ]
                        ]
                      },"geodesic": {
                        "constantValue": false
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }),'scale': 30,'maxPixels': 1000000000},).  Must be a Computedobject. 

我已经尝试更改特征的几何类型,但没有成功。 任何人有任何想法?我也已经尝试减少图像收集的时间范围,但效果不佳。

非常感谢,祝好。

安娜

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。