【问题标题】:Geemap package - PCA AnalysisGeemap 包 - PCA 分析
【发布时间】:2021-08-20 15:16:00
【问题描述】:

我正在使用 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, 0, 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"],
      'min': 0,
      'max': 3000,
      'gamma':1.5
    }
    Map.addLayer(sentImage, 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,
        'scale': scale,
        '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, scale, 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.53033
                            ],
                            [
                              -51.770479,
                              -21.745825
                            ],
                            [
                              -52.035679,
                              -21.745825
                            ]
                          ]
                        ]
                      },
                      "geodesic": {
                        "constantValue": false
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }), 'scale': 30, 'maxPixels': 1000000000},).  Must be a ComputedObject. 

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

非常感谢,祝你好运。

安娜

【问题讨论】:

    标签: python google-app-engine geospatial geemap


    【解决方案1】:

    我认为问题可能在于尝试将字符串与数字连接起来。这为我解决了这个问题:

    def getNewBandNames(prefix):
       seq = ee.List.sequence(1, bandNames.length())
       return seq.map(lambda b: ee.String(prefix).cat(ee.Number(b).int().format()))
    

    最好的

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-24
      • 1970-01-01
      • 2015-07-29
      • 2018-01-18
      • 2017-09-30
      • 1970-01-01
      相关资源
      最近更新 更多