forked from gee-community/qgis-earthengine-plugin
-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.py
92 lines (64 loc) · 2.44 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# -*- coding: utf-8 -*-
"""
Utils functions GEE
"""
from qgis.core import QgsRasterLayer, QgsProject
from qgis.utils import iface
import qgis
import ee
def get_ee_image_url(image):
map_id = ee.data.getMapId({'image': image})
url = map_id['tile_fetcher'].url_format
return url
def update_ee_layer_properties(layer, image, shown, opacity):
layer.setCustomProperty('ee-layer', True)
if not (opacity is None):
layer.renderer().setOpacity(opacity)
# serialize EE code
ee_script = image.serialize()
layer.setCustomProperty('ee-script', ee_script)
def add_ee_image_layer(image, name, shown, opacity):
check_version()
url = "type=xyz&url=" + get_ee_image_url(image)
layer = QgsRasterLayer(url, name, "wms")
update_ee_layer_properties(layer, image, shown, opacity)
QgsProject.instance().addMapLayer(layer)
if not (shown is None):
QgsProject.instance().layerTreeRoot().findLayer(
layer.id()).setItemVisibilityChecked(shown)
def update_ee_image_layer(image, layer, shown=True, opacity=1.0):
check_version()
url = "type=xyz&url=" + get_ee_image_url(image)
layer.dataProvider().setDataSourceUri(url)
layer.dataProvider().reloadData()
update_ee_layer_properties(layer, image, shown, opacity)
layer.triggerRepaint()
layer.reload()
iface.mapCanvas().refresh()
item = QgsProject.instance().layerTreeRoot().findLayer(layer.id())
if not (shown is None):
item.setItemVisibilityChecked(shown)
def get_layer_by_name(name):
layers = QgsProject.instance().mapLayers().values()
for l in layers:
if l.name() == name:
return l
return None
def add_or_update_ee_image_layer(image, name, shown=True, opacity=1.0):
layer = get_layer_by_name(name)
if layer:
if not layer.customProperty('ee-layer'):
raise Exception('Layer is not an EE layer: ' + name)
update_ee_image_layer(image, layer, shown, opacity)
else:
add_ee_image_layer(image, name, shown, opacity)
def add_ee_catalog_image(name, asset_name, visParams, collection_props):
image = None
if collection_props:
raise Exception('Not supported yet')
else:
image = ee.Image(asset_name).visualize(visParams)
add_or_update_ee_image_layer(image, name)
def check_version():
# check if we have the latest version only once plugin is used, not once it is loaded
qgis.utils.plugins['ee_plugin'].check_version()