-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDownload_Remote_Feature.py
More file actions
180 lines (162 loc) · 6.44 KB
/
Copy pathDownload_Remote_Feature.py
File metadata and controls
180 lines (162 loc) · 6.44 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import ee
ee.Authenticate()
import requests
def download_BH(lat_min, lat_max, lon_min, lon_max, output_file):
ee.Initialize()
# 创建感兴趣区域 (ROI)
roi = ee.Geometry.Rectangle([lon_min, lat_min, lon_max, lat_max])
# 加载 建筑高度building height 图像集合
built = (
ee.ImageCollection("JRC/GHSL/P2023A/GHS_BUILT_H")
.filterBounds(roi)
.median()
)
url = built.clip(roi).getDownloadURL({
'scale': 100, # 分辨率 (米)
'region': roi.coordinates().getInfo(), # ROI 区域
'format': 'GeoTIFF' # 文件格式
})
# 下载数据到本地
# output_file = 'average_building_height_2018.tif'
response = requests.get(url, stream=True)
if response.status_code == 200:
with open(output_file, 'wb') as file:
for chunk in response.iter_content(chunk_size=1024):
file.write(chunk)
print(f"数据已下载到本地:{output_file}")
else:
print(f"下载失败,状态码:{response.status_code}")
def download_BV(lat_min, lat_max, lon_min, lon_max, output_file):
ee.Initialize()
# 创建感兴趣区域 (ROI)
roi = ee.Geometry.Rectangle([lon_min, lat_min, lon_max, lat_max])
# 加载 Sentinel-2 图像集合
built = (
ee.ImageCollection("JRC/GHSL/P2023A/GHS_BUILT_V")
.filterDate('2020-01-01', '2020-12-31') # 时间范围
.filterBounds(roi)
.median()
)
# 检查波段名称
print("波段名称:", built.bandNames().getInfo())
# 指定波段下载
band_name = "built_volume_total" # 替换为实际波段名称
url = built.select(band_name).clip(roi).getDownloadURL({
'scale': 100, # 分辨率 (米)
'region': roi.coordinates().getInfo(), # ROI 区域
'format': 'GeoTIFF' # 文件格式
})
response = requests.get(url, stream=True)
if response.status_code == 200:
with open(output_file, 'wb') as file:
for chunk in response.iter_content(chunk_size=1024):
file.write(chunk)
print(f"波段 {band_name} 数据已下载到本地:{output_file}")
else:
print(f"下载失败,状态码:{response.status_code}")
def download_BS(lat_min, lat_max, lon_min, lon_max, output_file):
ee.Initialize()
# 创建感兴趣区域 (ROI)
roi = ee.Geometry.Rectangle([lon_min, lat_min, lon_max, lat_max])
# 加载 Sentinel-2 图像集合
built = (
ee.ImageCollection("JRC/GHSL/P2023A/GHS_BUILT_S_10m")
.filterBounds(roi)
.median()
)
# 检查波段名称
print("波段名称:", built.bandNames().getInfo())
# 指定波段下载
band_name = "built_surface" # 替换为实际波段名称
url = built.select(band_name).clip(roi).getDownloadURL({
'scale': 100, # 分辨率 (米)
'region': roi.coordinates().getInfo(), # ROI 区域
'format': 'GeoTIFF' # 文件格式
})
# 下载数据到本地
response = requests.get(url, stream=True)
if response.status_code == 200:
with open(output_file, 'wb') as file:
for chunk in response.iter_content(chunk_size=1024):
file.write(chunk)
print(f"波段 {band_name} 数据已下载到本地:{output_file}")
else:
print(f"下载失败,状态码:{response.status_code}")
def download_S2_og(lat_min, lat_max, lon_min, lon_max, output_file):
ee.Initialize()
roi = ee.Geometry.Rectangle([lon_min, lat_min, lon_max, lat_max])
# 加载 Sentinel-2 图像集合,筛选云量小于 5%,并计算中位数
sentinel2 = (
ee.ImageCollection('COPERNICUS/S2_SR_HARMONIZED')
.filterDate('2022-01-01', '2022-12-01') # 时间范围
.filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 5)) # 云量过滤
.filterBounds(roi) # 感兴趣区域
.median()
)
print("波段名称:", sentinel2.bandNames().getInfo())
# 提取原始波段
bands_to_include = ['B11', 'B9', 'B8', 'B7', 'B6', 'B5', 'B4', 'B3', 'B1'] # 常用波段
# 将原始波段与计算的特征合并
features = sentinel2.select(bands_to_include)
print("合并后的波段:", features.bandNames().getInfo())
url = features.clip(roi).getDownloadURL({
'scale': 100, # 分辨率 (米)
'region': roi.coordinates().getInfo(), # ROI 区域
'format': 'GeoTIFF' # 文件格式
})
# 下载数据到本地
response = requests.get(url, stream=True)
if response.status_code == 200:
with open(output_file, 'wb') as file:
for chunk in response.iter_content(chunk_size=1024):
file.write(chunk)
print(f"数据已下载到本地:{output_file}")
else:
print(f"下载失败,状态码:{response.status_code}")
def download_pop(lat_min, lat_max, lon_min, lon_max, output_file):
ee.Initialize()
# 创建感兴趣区域 (ROI)
roi = ee.Geometry.Rectangle([lon_min, lat_min, lon_max, lat_max])
# 加载 建筑高度 图像集合
built = (
ee.ImageCollection("JRC/GHSL/P2023A/GHS_POP")
.filterDate('2020-01-01', '2020-12-31') # 时间范围
.filterBounds(roi)
.median()
)
url = built.clip(roi).getDownloadURL({
'scale': 100, # 分辨率 (米)
'region': roi.coordinates().getInfo(), # ROI 区域
'format': 'GeoTIFF' # 文件格式
})
# 下载数据到本地
response = requests.get(url, stream=True)
if response.status_code == 200:
with open(output_file, 'wb') as file:
for chunk in response.iter_content(chunk_size=1024):
file.write(chunk)
print(f"数据已下载到本地:{output_file}")
else:
print(f"下载失败,状态码:{response.status_code}")
lon_min, lon_max = -2.990, -1.660
lat_min, lat_max = 53.035, 54.000
output_file = 'your_Manchester_2020_pop.tif'
download_pop(lat_min, lat_max, lon_min, lon_max, output_file)
output_file = 'your_Manchester_2022_BV.tif'
download_BV(lat_min, lat_max, lon_min, lon_max, output_file)
output_file = 'your_Manchester_S2_2022_10bands.tif'
download_S2_og(lat_min, lat_max, lon_min, lon_max, output_file)
output_file = 'your_Manchester_BH_2022.tif'
download_BH(lat_min, lat_max, lon_min, lon_max, output_file)
'''
# ROI indice for London
lat_min = 51.137
lat_max = 52.335
lon_min = -0.8241
lon_max = 0.9728
#ROI indice for Barcelona
lat_min = 41.250
lat_max = 41.780
lon_min = 1.790
lon_max = 2.540
'''