42 upload data
Uncomment the following line to install geemap if needed.
In [1]:
Copied!
# !pip install geemap
# !pip install geemap
In [2]:
Copied!
import os
import ee
import geemap
import ipywidgets as widgets
from ipyleaflet import WidgetControl
import os
import ee
import geemap
import ipywidgets as widgets
from ipyleaflet import WidgetControl
In [3]:
Copied!
Map = geemap.Map(center=(40, -100), zoom=4, height='750px')
Map = geemap.Map(center=(40, -100), zoom=4, height='750px')
In [4]:
Copied!
# Add Earth Engine dataset
dem = ee.Image('USGS/SRTMGL1_003')
# Set visualization parameters.
vis_params = {
'min': 0,
'max': 4000,
'palette': ['006633', 'E5FFCC', '662A00', 'D8D8D8', 'F5F5F5'],
}
# Add Earth Eninge layers to Map
Map.addLayer(dem, vis_params, 'SRTM DEM', True, 0.7)
Map
# Add Earth Engine dataset
dem = ee.Image('USGS/SRTMGL1_003')
# Set visualization parameters.
vis_params = {
'min': 0,
'max': 4000,
'palette': ['006633', 'E5FFCC', '662A00', 'D8D8D8', 'F5F5F5'],
}
# Add Earth Eninge layers to Map
Map.addLayer(dem, vis_params, 'SRTM DEM', True, 0.7)
Map
Out[4]:
In [5]:
Copied!
output_widget = widgets.Output(layout={'border': '1px solid black'})
output_control = WidgetControl(widget=output_widget, position='bottomright')
Map.add_control(output_control)
output_widget = widgets.Output(layout={'border': '1px solid black'})
output_control = WidgetControl(widget=output_widget, position='bottomright')
Map.add_control(output_control)
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) Input In [5], in <cell line: 3>() 1 output_widget = widgets.Output(layout={'border': '1px solid black'}) 2 output_control = WidgetControl(widget=output_widget, position='bottomright') ----> 3 Map.add_control(output_control) AttributeError: 'Map' object has no attribute 'add_control'
In [6]:
Copied!
style = {'description_width': 'initial'}
uploader = widgets.FileUpload(
description='Upload data',
accept='.zip, .json, .geojson',
multiple=False,
button_style='primary',
style=style,
)
submit = widgets.Button(
description='Display data', button_style='success', tooltip='Click me', style=style
)
reset = widgets.Button(
description='Reset', button_style='warning', tooltip='Click me', style=style
)
style = {'description_width': 'initial'}
uploader = widgets.FileUpload(
description='Upload data',
accept='.zip, .json, .geojson',
multiple=False,
button_style='primary',
style=style,
)
submit = widgets.Button(
description='Display data', button_style='success', tooltip='Click me', style=style
)
reset = widgets.Button(
description='Reset', button_style='warning', tooltip='Click me', style=style
)
In [7]:
Copied!
with output_widget:
print('Upload shapefile or \ngeojson as a zip file')
display(uploader)
display(submit)
display(reset)
with output_widget:
print('Upload shapefile or \ngeojson as a zip file')
display(uploader)
display(submit)
display(reset)
In [8]:
Copied!
def get_vector(upload_widget, out_dir=None):
import zipfile
import glob
if out_dir is None:
out_dir = os.path.join(os.path.expanduser('~'), 'Downloads')
if not os.path.exists(out_dir):
os.makedirs(out_dir)
vector = None
try:
[uploaded_file] = upload_widget.value
file = upload_widget.value[uploaded_file]
name = file['metadata']['name']
content = file['content']
out_file = os.path.join(out_dir, name)
with open(out_file, "wb") as fp:
fp.write(content)
if name.endswith('.zip'):
with zipfile.ZipFile(out_file, "r") as zip_ref:
extract_dir = os.path.join(
out_dir, name[:-4] + "_" + geemap.random_string(3)
)
zip_ref.extractall(extract_dir)
files = glob.glob(extract_dir + '/*.shp')
if len(files) > 0:
shp = files[0]
vector = geemap.shp_to_ee(shp)
else:
files = glob.glob(extract_dir + '/*.geojson')
if len(files) > 0:
geojson = files[0]
vector = geemap.geojson_to_ee(geojson)
else:
vector = geemap.geojson_to_ee(out_file)
except Exception as e:
print(e)
return vector
def get_vector(upload_widget, out_dir=None):
import zipfile
import glob
if out_dir is None:
out_dir = os.path.join(os.path.expanduser('~'), 'Downloads')
if not os.path.exists(out_dir):
os.makedirs(out_dir)
vector = None
try:
[uploaded_file] = upload_widget.value
file = upload_widget.value[uploaded_file]
name = file['metadata']['name']
content = file['content']
out_file = os.path.join(out_dir, name)
with open(out_file, "wb") as fp:
fp.write(content)
if name.endswith('.zip'):
with zipfile.ZipFile(out_file, "r") as zip_ref:
extract_dir = os.path.join(
out_dir, name[:-4] + "_" + geemap.random_string(3)
)
zip_ref.extractall(extract_dir)
files = glob.glob(extract_dir + '/*.shp')
if len(files) > 0:
shp = files[0]
vector = geemap.shp_to_ee(shp)
else:
files = glob.glob(extract_dir + '/*.geojson')
if len(files) > 0:
geojson = files[0]
vector = geemap.geojson_to_ee(geojson)
else:
vector = geemap.geojson_to_ee(out_file)
except Exception as e:
print(e)
return vector
In [9]:
Copied!
def submit_clicked(b):
if uploader._counter > 0:
Map.default_style = {'cursor': 'wait'}
try:
fc = get_vector(uploader)
layer_name = 'Layer ' + geemap.random_string(3)
Map.addLayer(fc, {}, layer_name)
Map.centerObject(fc)
uploader.value.clear()
uploader._counter = 0
except Exception as e:
print(e)
Map.default_style = {'cursor': 'pointer'}
submit.on_click(submit_clicked)
def reset_clicked(b):
Map.layers = Map.layers[:3]
output_widget.clear_output()
with output_widget:
print('Upload shapefile or \ngeojson as a zip file')
display(uploader)
display(submit)
display(reset)
uploader.value.clear()
uploader._counter = 0
reset.on_click(reset_clicked)
def submit_clicked(b):
if uploader._counter > 0:
Map.default_style = {'cursor': 'wait'}
try:
fc = get_vector(uploader)
layer_name = 'Layer ' + geemap.random_string(3)
Map.addLayer(fc, {}, layer_name)
Map.centerObject(fc)
uploader.value.clear()
uploader._counter = 0
except Exception as e:
print(e)
Map.default_style = {'cursor': 'pointer'}
submit.on_click(submit_clicked)
def reset_clicked(b):
Map.layers = Map.layers[:3]
output_widget.clear_output()
with output_widget:
print('Upload shapefile or \ngeojson as a zip file')
display(uploader)
display(submit)
display(reset)
uploader.value.clear()
uploader._counter = 0
reset.on_click(reset_clicked)
Last update:
2022-03-14