-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_json_glacier_ids.py
69 lines (50 loc) · 2.27 KB
/
create_json_glacier_ids.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
import os, json, gzip
import numpy as np
import pandas as pd
import geopandas as gpd
from oggm import utils
###### IMPORTANT HERE: DECIDE WHAT VERSION YOU WANT TO CREATE
version = '62'
if version not in ('62', '70G'): raise ValueError("Accepted RGI versions are 62 or 70G. Exit.")
if version == '62': name_column_id = 'RGIId'
elif version == '70G': name_column_id = 'rgi_id'
all_world_glaciers = []
decimal_places = 4
for rgi in np.arange(1, 20):
print(rgi)
if not isinstance(rgi, str): rgi = f"{rgi:02d}"
# get rgi region and intersect shp files
# get rgi dataset of glaciers and glaciers intersects
FILE_RGI_SHP = utils.get_rgi_region_file(rgi, version=version)
rgi_glaciers = gpd.read_file(FILE_RGI_SHP, engine='pyogrio')
for idx, row in rgi_glaciers.iterrows():
id = row[name_column_id]
geom = row['geometry']
bbox = geom.bounds
cenLon, cenLat = geom.centroid.x, geom.centroid.y
# Round the bounding box values to the desired number of decimal places
rounded_bbox = [round(coord, decimal_places) for coord in bbox]
rounded_center = [round(coord, decimal_places) for coord in (cenLon, cenLat)]
is_tile = os.path.isdir(f"tiles/RGI{version}/{id}")
#print(cenLon, cenLat)
#print(rounded_bbox)
#print(rounded_center)
#input('wait')
# Create a dictionary entry for this glacier
glacier_entry = {
'id': id,
'bbox': rounded_bbox, # Convert tuple to list for JSON serialization
#'center': rounded_center # Convert tuple to list for JSON serialization
"is_tile": is_tile
}
#if ((id == 'RGI60-06.00416') or (id == 'RGI60-06.00475') or (id == 'RGI60-11.01450')):
all_world_glaciers.append(glacier_entry)
print(f"Fetched all glacier names: {len(all_world_glaciers)}")
# Create a dictionary to hold the glacier IDs
output_json = {"glaciers": all_world_glaciers}
# Specify the path to save the JSON file
json_file_path = f'tileFolders_RGI{version}_with_bbox.json.gz'
# Write the data to a gzipped JSON file
with gzip.open(json_file_path, 'wt', encoding='utf-8') as json_file:
json.dump(output_json, json_file)
print(f"Version {version}: Gzipped JSON file {json_file_path} created successfully.")