-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgee_data_pull.py
166 lines (127 loc) · 4.08 KB
/
gee_data_pull.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# %%
import time
from pathlib import Path
import ee
import geemap
# %%
# Config
# Note: CRS must be EPSG:4326
# geemap and ee assume it and do not read the crs from the file
aoi_path = Path("aoi/greece_aoi.geojson")
gdrive_dirname = "map_dataset_tests"
# %%
# Connect to Google Earth Engine
ee.Authenticate()
ee.Initialize()
# %%
# Load the aoi
aoi_ee = geemap.geojson_to_ee(aoi_path.as_posix())
# Display aoi to double check
map = geemap.Map()
map.centerObject(aoi_ee, 7)
map.addLayer(
aoi_ee.style(**{"color": "red", "width": 2, "fillColor": "00000000"}),
{},
"AOI",
)
map
# %%
# Load landcover data
# https://developers.google.com/earth-engine/datasets/catalog/COPERNICUS_Landcover_100m_Proba-V-C3_Global
landcover_100m_raw = ee.Image(
"COPERNICUS/Landcover/100m/Proba-V-C3/Global/2019"
).select("discrete_classification")
# # Make an image where any holes are filled with the nearest value
# landcover_100m_filled = landcover_100m_raw.focal_mode(radius=2.5)
# Clean up the landcover data
landcover_100m = (
landcover_100m_raw
# .unmask(landcover_100m_filled)
.clip(aoi_ee)
)
# %%
# Load 30m DEM
# https://developers.google.com/earth-engine/datasets/catalog/COPERNICUS_DEM_GLO30
dem_30m_ic = ee.ImageCollection("COPERNICUS/DEM/GLO30").select("DEM")
# Mosaic the DEM and fix the projection
# Note, the GLO30 dataset is a composite that requires reprojection after
# mosaicked
dem_proj = dem_30m_ic.first().projection()
dem_30m = dem_30m_ic.mosaic().setDefaultProjection(dem_proj)
# Fill no data values with 0 and clip to the aoi
dem_30m = dem_30m.unmask(0).clip(aoi_ee)
# Generate a hillshade
hillshade = ee.Terrain.hillshade(dem_30m)
# %%
# Identify land vs not land
# Not land is ocean (200) or other permanent water bodies (80)
# For now just exclude ocean because other water bodies mixes marshes and lakes
landcover_100m_binary = landcover_100m.neq(200) # .And(landcover_100m.neq(80))
# Polygonize the landcover 100m binary
landcover_100m_binary_vector = landcover_100m_binary.reduceToVectors(
geometry=aoi_ee,
geometryType="polygon",
scale=100,
maxPixels=1e13,
reducer=ee.Reducer.countEvery(),
)
# %%
# Display all layers
map = geemap.Map()
map.centerObject(aoi_ee, 7)
# map.addLayer(dem_30m, {'min': 0, 'max': 3000}, 'DEM 30m')
map.addLayer(hillshade, {}, "Hillshade")
# Add landcover data with opacity 50%
map.addLayer(landcover_100m, {}, "Landcover")
map.addLayer(landcover_100m_binary, {}, "Landcover (binary)")
map.addLayer(landcover_100m_binary_vector, {}, "Landcover (vector)")
# Display AOI as an empty box
map.addLayer(
aoi_ee.style(**{"color": "red", "width": 2, "fillColor": "00000000"}),
{},
"AOI",
)
map
# %%
# Define a function to export a vector to Google Drive
def export_vector(vector, filename, wait=True):
if gdrive_dirname is None or gdrive_dirname == "":
print("No drive folder specified, skipping export")
return
# Export the vector data to a geojson on Google Drive
task = ee.batch.Export.table.toDrive(
collection=vector,
description=filename,
folder=gdrive_dirname,
fileNamePrefix=filename,
fileFormat="GeoJSON",
)
task.start()
if wait:
wait_for_tasks([task])
return task
# Define a function to wait for tasks to complete
def wait_for_tasks(task_ids: list[str]):
# Wait for the task to complete
print(f"Exporting {len(task_ids)} item(s) to Google Drive")
all_status = ee.data.getTaskStatus(task_ids)
running_states = ["READY", "RUNNING"]
while any(task["state"] in running_states for task in all_status):
print("Checking task status")
all_status = ee.data.getTaskStatus(task_ids)
remaining_tasks = [
s for s in all_status if s["state"] in running_states
]
print(f"Remaining tasks: {len(remaining_tasks)}")
time.sleep(20)
print("All tasks completed")
# %%
# Export the landcover 100m binary vector
task = export_vector(
landcover_100m_binary_vector, "landcover_100m_binary_vector"
)
# %%
# Get a list of all tasks
all_tasks = ee.batch.Task.list()
all_tasks
# %%