Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
add google maps url
#3
by
granamaa
- opened
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"
|