dropbop commited on
Commit
6ee20cf
·
verified ·
1 Parent(s): c501c19

Create utils.py

Browse files
Files changed (1) hide show
  1. utils.py +48 -0
utils.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+
3
+ import numpy as np
4
+ import pyproj
5
+
6
+
7
+ def get_metadata(item):
8
+ metadata = item["metadata"]
9
+ if isinstance(metadata, str):
10
+ metadata = json.loads(metadata)
11
+ return metadata
12
+
13
+
14
+ def lat_lon_mid_pixel(item, subset: str):
15
+ metadata = get_metadata(item)
16
+ if subset == "satellogic":
17
+ crs = metadata["crs"][0]
18
+ bounds_crs = metadata["bounds"]
19
+ elif subset == "neon":
20
+ crs = metadata["epsg"]
21
+ bounds_crs = metadata["bounds"]
22
+ elif subset == "sentinel_1":
23
+ geometry = metadata
24
+ elif subset == "sentinel_2":
25
+ geometry = metadata["tileGeometry"][0]
26
+ else:
27
+ raise ValueError("subset not known")
28
+
29
+ if subset.startswith("sentinel_"):
30
+ crs = geometry["crs"]
31
+ bounds_crs = geometry["coordinates"][0]
32
+ assert len(bounds_crs) == 5
33
+ bounds_crs = (
34
+ bounds_crs[0][0],
35
+ bounds_crs[0][1],
36
+ bounds_crs[2][0],
37
+ bounds_crs[2][1],
38
+ )
39
+
40
+
41
+ bounds = pyproj.Transformer.from_crs(crs, "EPSG:4326").transform_bounds(*bounds_crs)
42
+ # dumb average for now
43
+ return np.array([bounds[0] + bounds[2], bounds[1] + bounds[3]]) / 2
44
+
45
+
46
+ def get_google_map_link(item, subset: str):
47
+ lat, lon = lat_lon_mid_pixel(item, subset)
48
+ return f"https://www.google.com/maps?ll={lat},{lon}&z=16&t=k"