【问题标题】:Workaround for Google Earth Engine Python API and no support for `ee.mapclient` in Python 3Google Earth Engine Python API 的解决方法,不支持 Python 3 中的 `ee.mapclient`
【发布时间】:2018-04-08 04:34:41
【问题描述】:

所以我使用了 Google 地球引擎并处理了他们存储库中的一些示例代码。我正在使用 Python 3.6。看起来谷歌将不再通过他们的ee.mapclient() 支持 Python 3 中的映射功能。我想知道是否有人找到了合适的解决方法?让我概述一下问题。

我尝试加载ee.mapclient 来绘制地图。

import ee
import ee.mapclient
ee.Initialize()

但是我得到了一个错误:

ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-13-6d4860410653> in <module>()
      1 import ee
----> 2 import ee.mapclient
      3 ee.Initialize()

/media/krishnab/lakshmi/anaconda3/envs/pMining/lib/python3.6/site-packages/ee/mapclient.py in <module>()
     29 
     30 import collections
---> 31 import cStringIO
     32 import functools
     33 import math

ModuleNotFoundError: No module named 'cStringIO'

cStringIO 问题很容易解决,如下所示: python 3.4.0 email package install: ImportError: No module named 'cStringIO'

所以我去 Google Earth Engine Repo 上发布了一个问题,但发现了一个预先存在的问题:

https://github.com/google/earthengine-api/issues/16

在问题中,开发人员承认了问题,但表示由于底层 Tk 包的限制,他们不会修复它。

以下是该问题的引述:

我们并没有积极维护 mapclient 对象,因为它 依赖于 Tk,一个图形用户界面工具包,它的行为 在不同的机器上不同。 您能描述一下您需要 mapclient 的用例吗?我们可能是 能够提出替代方法。

谷歌开发人员提出提交解决方法,但到目前为止尚未发布任何解决方法。

因此,我想知道是否有人在 Python3.6 中找到了解决此问题的合适解决方法?

作为一个真正的代码示例,我可以提供以下来自 Google 示例 repo 的代码:

import datetime
import ee
import ee.mapclient

ee.Initialize()
ee.mapclient.centerMap(-95.738, 18.453, 9)

# Filter the LE7 collection to a single date.
collection = (ee.ImageCollection('LE7_L1T')
              .filterDate(datetime.datetime(2002, 11, 8),
                          datetime.datetime(2002, 11, 9)))
image = collection.mosaic().select('B3', 'B2', 'B1')

# Display the image normally.
ee.mapclient.addToMap(image, {'gain': '1.6, 1.4, 1.1'}, 'Land')

# Add and stretch the water.  Once where the elevation is masked,
# and again where the elevation is zero.
elev = ee.Image('srtm90_v4')
mask1 = elev.mask().eq(0).And(image.mask())
mask2 = elev.eq(0).And(image.mask())
ee.mapclient.addToMap(
    image.mask(mask1), {'gain': 6.0, 'bias': -200}, 'Water: Masked')
ee.mapclient.addToMap(
    image.mask(mask2), {'gain': 6.0, 'bias': -200}, 'Water: Elev 0')

【问题讨论】:

    标签: python python-3.x gis google-earth-engine


    【解决方案1】:

    不确定在 Python 3 中 import ee.mapclient 是否仍然存在问题,但这里有一个使用 folium package 的解决方法,它可以具有与 ee.mapclient 类似的方法。但是,这将在您的网络浏览器中呈现结果,而不是作为弹出窗口(除非使用 Jupyter 笔记本,否则您只需调用 folium 地图对象即可呈现)。

    import ee
    import folium
    import datetime
    import webbrowser
    
    ee.Initialize()
    
    class eeMapHack(object):
        def __init__(self,center=[0, 0],zoom=3):
            self._map = folium.Map(location=center,zoom_start=zoom)
            return
    
        def addToMap(self,img,vizParams,name):
             map_id = ee.Image(img.visualize(**vizParams)).getMapId()
             tile_url_template = "https://earthengine.googleapis.com/map/{mapid}/{{z}}/{{x}}/{{y}}?token={token}"
             mapurl = tile_url_template.format(**map_id)
             folium.WmsTileLayer(mapurl,name=name).add_to(self._map)
    
             return
    
        def addLayerControl(self):
             self._map.add_child(folium.map.LayerControl())
             return
    
    
    # initialize map object
    eeMap = eeMapHack(center=[18.453,-95.738],zoom=9)
    
    # Filter the LE7 collection to a single date.
    collection = (ee.ImageCollection('LE7_L1T')
              .filterDate(datetime.datetime(2002, 11, 8),
                          datetime.datetime(2002, 11, 9)))
    image = collection.mosaic().select('B3', 'B2', 'B1')
    eeMap.addToMap(image, {'gain': '1.6, 1.4, 1.1'}, 'Land')
    
    # Add and stretch the water.  Once where the elevation is masked,
    # and again where the elevation is zero.
    elev = ee.Image('srtm90_v4')
    mask1 = elev.mask().eq(0).And(image.mask())
    mask2 = elev.eq(0).And(image.mask())
    
    eeMap.addToMap(image.mask(mask1), {'gain': 6.0, 'bias': -200}, 'Water: Masked')
    eeMap.addToMap(image.mask(mask2), {'gain': 6.0, 'bias': -200}, 'Water: Elev 0')
    
    # add layer control to map
    eeMap.addLayerControl()
    
    outHtml = 'map.html' # temporary file path, change if needed
    eeMap._map.save(outHtml)
    
    webbrowser.open('file://'+outHtml) 
    

    您应该在浏览器中获得一个交互式网络地图,其中包含如下所示的分析结果:

    【讨论】:

      猜你喜欢
      • 2020-10-09
      • 2014-08-05
      • 1970-01-01
      • 2015-12-07
      • 1970-01-01
      • 1970-01-01
      • 2016-01-22
      • 2019-03-29
      • 2018-04-07
      相关资源
      最近更新 更多