gera-richarte commited on
Commit
7f0f541
1 Parent(s): a220474

add google maps url (#3)

Browse files

- add transforms module (0bc90860fd76414c2e2007d0624185096d2f31a2)
- change module name (74d1713583a52df9fd24d5d9b1e1bb91a8189faf)

Files changed (1) hide show
  1. utils.py +41 -0
utils.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ # each image has bounds but they should coincide
19
+ bounds_crs = metadata["bounds"][0]
20
+ elif subset == "sentinel_1":
21
+ crs = metadata["crs"]
22
+ bounds_crs = metadata["coordinates"][0]
23
+ assert len(bounds_crs) == 5
24
+ # bounds are a polygon with same first & last vertex
25
+ bounds_crs = (
26
+ bounds_crs[0][0],
27
+ bounds_crs[0][1],
28
+ bounds_crs[2][0],
29
+ bounds_crs[2][1],
30
+ )
31
+ else:
32
+ raise ValueError("subset not known")
33
+
34
+ bounds = pyproj.Transformer.from_crs(crs, "EPSG:4326").transform_bounds(*bounds_crs)
35
+ # dumb average for now
36
+ return np.array([bounds[0] + bounds[2], bounds[1] + bounds[3]]) / 2
37
+
38
+
39
+ def get_google_map_link(item, subset: str):
40
+ lat, lon = lat_lon_mid_pixel(item, subset)
41
+ return f"https://www.google.com/maps?ll={lat},{lon}&z=16&t=k"