MarcSkovMadsen commited on
Commit
64df5de
1 Parent(s): 2d50870
Files changed (9) hide show
  1. .gitignore +106 -0
  2. Dockerfile +4 -4
  3. LICENSE +21 -0
  4. README.md +22 -2
  5. app.py +196 -38
  6. download_datasets.py +7 -0
  7. requirements.txt +3 -1
  8. requirements_dev.txt +7 -0
  9. utils.py +77 -17
.gitignore ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ .Python
11
+ build/
12
+ develop-eggs/
13
+ dist/
14
+ downloads/
15
+ eggs/
16
+ .eggs/
17
+ lib/
18
+ lib64/
19
+ parts/
20
+ sdist/
21
+ var/
22
+ wheels/
23
+ *.egg-info/
24
+ .installed.cfg
25
+ *.egg
26
+ MANIFEST
27
+
28
+ # PyInstaller
29
+ # Usually these files are written by a python script from a template
30
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
31
+ *.manifest
32
+ *.spec
33
+
34
+ # Installer logs
35
+ pip-log.txt
36
+ pip-delete-this-directory.txt
37
+
38
+ # Unit test / coverage reports
39
+ htmlcov/
40
+ .tox/
41
+ .coverage
42
+ .coverage.*
43
+ .cache
44
+ nosetests.xml
45
+ coverage.xml
46
+ *.cover
47
+ .hypothesis/
48
+ .pytest_cache/
49
+
50
+ # Translations
51
+ *.mo
52
+ *.pot
53
+
54
+ # Django stuff:
55
+ *.log
56
+ local_settings.py
57
+ db.sqlite3
58
+
59
+ # Flask stuff:
60
+ instance/
61
+ .webassets-cache
62
+
63
+ # Scrapy stuff:
64
+ .scrapy
65
+
66
+ # Sphinx documentation
67
+ docs/_build/
68
+
69
+ # PyBuilder
70
+ target/
71
+
72
+ # Jupyter Notebook
73
+ .ipynb_checkpoints
74
+
75
+ # pyenv
76
+ .python-version
77
+
78
+ # celery beat schedule file
79
+ celerybeat-schedule
80
+
81
+ # SageMath parsed files
82
+ *.sage.py
83
+
84
+ # Environments
85
+ .env
86
+ .venv
87
+ env/
88
+ venv/
89
+ ENV/
90
+ env.bak/
91
+ venv.bak/
92
+
93
+ # Spyder project settings
94
+ .spyderproject
95
+ .spyproject
96
+
97
+ # Rope project settings
98
+ .ropeproject
99
+
100
+ # mkdocs documentation
101
+ /site
102
+
103
+ # mypy
104
+ .mypy_cache/
105
+
106
+ data/
Dockerfile CHANGED
@@ -3,14 +3,14 @@ FROM python:3.11
3
  WORKDIR /code
4
 
5
  COPY ./requirements.txt /code/requirements.txt
6
- RUN python3 -m pip install --no-cache-dir --upgrade pip
7
- RUN python3 -m pip install --no-cache-dir --upgrade -r /code/requirements.txt
 
8
 
9
  COPY . .
10
 
11
- CMD ["panel", "serve", "/code/app.py", "--address", "0.0.0.0", "--port", "7860", "--allow-websocket-origin", "*", "--num-procs", "2", "--index", "app"]
12
 
13
- RUN mkdir data
14
  RUN chmod 777 data
15
 
16
  ENV NUMBA_CACHE_DIR=/code/.cache
 
3
  WORKDIR /code
4
 
5
  COPY ./requirements.txt /code/requirements.txt
6
+ RUN python3 -m pip install uv
7
+ RUN python3 -m uv pip install -r /code/requirements.txt
8
+ RUN python3 download_datasets.py
9
 
10
  COPY . .
11
 
12
+ CMD ["panel", "serve", "/code/app.py", "--address", "0.0.0.0", "--port", "7860", "--allow-websocket-origin", "*", "--num-procs", "4", "--index", "app"]
13
 
 
14
  RUN chmod 777 data
15
 
16
  ENV NUMBA_CACHE_DIR=/code/.cache
LICENSE ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Marc Skov Madsen
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
README.md CHANGED
@@ -10,8 +10,28 @@ license: mit
10
 
11
  See [Major Tom Explorer](https://awesome-panel.org/resources/major_tom_explorer/) by [awesome-panel.org](https://awesome-panel.org) for more info.
12
 
13
- ## Serve
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
  ```python
16
  panel serve app.py --autoreload
17
- ```
 
10
 
11
  See [Major Tom Explorer](https://awesome-panel.org/resources/major_tom_explorer/) by [awesome-panel.org](https://awesome-panel.org) for more info.
12
 
13
+ ## Install
14
+
15
+ ```python
16
+ git clone https://huggingface.co/spaces/MarcSkovMadsen/major-tom-explorer
17
+ ```
18
+
19
+ Create and activate your virtual environment
20
+
21
+ ```bash
22
+ python -m venv .venv
23
+ source .venv/bin/activate # linux
24
+ ```
25
+
26
+ Install the dependencies
27
+
28
+ ```bash
29
+ pip install uv
30
+ uv pip install -r requirements_dev.txt
31
+ ```
32
+
33
+ ## Serve App
34
 
35
  ```python
36
  panel serve app.py --autoreload
37
+ ```
app.py CHANGED
@@ -1,54 +1,212 @@
1
- import numpy as np
2
- import holoviews as hv
3
  import dask.dataframe as dd
4
-
5
- from holoviews import opts
6
- from holoviews.operation.datashader import aggregate
7
  import panel as pn
8
- from utils import get_meta_data
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
- pn.extension()
11
 
12
- hv.extension('bokeh')
 
13
 
14
- renderer = hv.renderer('bokeh')
 
 
15
 
16
- opts.defaults(
17
- opts.Curve(xaxis=None, yaxis=None, show_grid=False, show_frame=False,
18
- color='orangered', framewise=True, width=100),
19
- opts.Image(width=800, height=400, shared_axes=False, logz=True, colorbar=True,
20
- xaxis=None, yaxis=None, axiswise=True, bgcolor='black'),
21
- opts.HLine(color='white', line_width=1),
22
- opts.Layout(shared_axes=False),
23
- opts.VLine(color='white', line_width=1))
24
 
25
- pandas_df = pn.cache(get_meta_data)()
26
- df = dd.from_pandas(pandas_df).persist()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
- points = hv.Points(df, kdims=['centre_easting', 'centre_northing'], vdims=[])
 
 
 
 
 
 
 
 
 
29
 
30
- agg = aggregate(points, link_inputs=True, x_sampling=0.0001, y_sampling=0.0001)
 
 
31
 
32
- agg.opts(cmap="kr_r")
 
 
 
 
 
 
 
 
 
33
 
34
- pointerx = hv.streams.PointerX(x=np.mean(points.range('centre_easting')), source=points)
35
- pointery = hv.streams.PointerY(y=np.mean(points.range('centre_northing')), source=points)
36
- box = hv.streams.BoundsXY(source=points, bounds=(0,0,0,0))
37
- vline = hv.DynamicMap(lambda x: hv.VLine(x), streams=[pointerx])
38
- hline = hv.DynamicMap(lambda y: hv.HLine(y), streams=[pointery])
39
 
40
- tiles = hv.Tiles('https://tile.openstreetmap.org/{Z}/{X}/{Y}.png', name="OSM").opts(width=600, height=550)
41
- hvobj = tiles * agg * hline * vline
42
 
43
- @pn.depends(pointerx.param.x, pointery.param.y)
44
- def table(x,y):
45
- return f"Easting: {x}, Northing: {y}"
46
 
47
- @pn.depends(box.param.bounds)
48
- def bbox(value):
49
- return str(value)
50
 
51
- pn.pane.HoloViews(hvobj).servable()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
 
53
- pn.panel(table).servable()
54
- pn.panel(bbox).servable()
 
 
 
 
 
 
 
 
1
  import dask.dataframe as dd
2
+ import holoviews as hv
3
+ import numpy as np
 
4
  import panel as pn
5
+ import param
6
+ from holoviews.operation.datashader import dynspread, rasterize
7
+
8
+ from utils import (
9
+ DATASETS,
10
+ DATASHADER_LOGO,
11
+ DATASHADER_URL,
12
+ DESCRIPTION,
13
+ MAJOR_TOM_LOGO,
14
+ MAJOR_TOM_PICTURE,
15
+ MAJOR_TOM_REF_URL,
16
+ PANEL_LOGO,
17
+ PANEL_URL,
18
+ get_closest_rows,
19
+ get_image,
20
+ get_meta_data,
21
+ )
22
+
23
+
24
+ class DatasetInput(pn.viewable.Viewer):
25
+ value = param.Selector(objects=DATASETS, allow_None=False, label="Dataset")
26
+
27
+ data = param.DataFrame(allow_None=False)
28
+
29
+ def __panel__(self):
30
+ return pn.widgets.RadioButtonGroup.from_param(
31
+ self.param.value, button_style="outline"
32
+ )
33
+
34
+ @pn.depends("value", watch=True, on_init=True)
35
+ def _update_data(self):
36
+ self.data = pn.cache(get_meta_data)(dataset=self.value)
37
+
38
+
39
+ class MapInput(pn.viewable.Viewer):
40
+ data = param.DataFrame(allow_refs=True, allow_None=False)
41
+
42
+ data_in_view = param.DataFrame(allow_None=False)
43
+ data_selected = param.DataFrame(allow_None=False)
44
+
45
+ _plot = param.Parameter(allow_None=False)
46
+ _pointer_x = param.Parameter(allow_None=False)
47
+ _pointer_y = param.Parameter(allow_None=False)
48
+ _range_xy = param.Parameter(allow_None=False)
49
+ _tap = param.Parameter(allow_None=False)
50
+
51
+ updating = param.Boolean()
52
+
53
+ def __panel__(self):
54
+ return pn.pane.HoloViews(
55
+ self._plot, height=550, width=800, loading=self.param.updating
56
+ )
57
+
58
+ @param.depends("data", watch=True, on_init=True)
59
+ def _handle_data_dask_change(self):
60
+ with self.param.update(updating=True):
61
+ data_dask = dd.from_pandas(self.data).persist()
62
+ points = hv.Points(
63
+ data_dask, kdims=["centre_easting", "centre_northing"], vdims=[]
64
+ )
65
+
66
+ mean_easting = np.mean(points.range("centre_easting"))
67
+ mean_northing = np.mean(points.range("centre_northing"))
68
+ rangexy = hv.streams.RangeXY(source=points)
69
+ tap = hv.streams.Tap(source=points, x=mean_easting, y=mean_northing)
70
+
71
+ agg = rasterize(
72
+ points, link_inputs=True, x_sampling=0.0001, y_sampling=0.0001
73
+ )
74
+ dyn = dynspread(agg)
75
+ dyn.opts(cmap="kr_r", colorbar=True)
76
+
77
+ pointerx = hv.streams.PointerX(x=mean_easting, source=points)
78
+ pointery = hv.streams.PointerY(y=mean_northing, source=points)
79
+ vline = hv.DynamicMap(lambda x: hv.VLine(x), streams=[pointerx])
80
+ hline = hv.DynamicMap(lambda y: hv.HLine(y), streams=[pointery])
81
+ tiles = hv.Tiles(
82
+ "https://tile.openstreetmap.org/{Z}/{X}/{Y}.png", name="OSM"
83
+ ).opts(width=600, height=550, xlabel="Longitude", ylabel="Latitude")
84
+
85
+ self.param.update(
86
+ _plot=tiles * agg * dyn * hline * vline,
87
+ _pointer_x=pointerx,
88
+ _pointer_y=pointery,
89
+ _range_xy=rangexy,
90
+ _tap=tap,
91
+ )
92
+
93
+ update_viewed = pn.bind(
94
+ self._update_data_in_view,
95
+ rangexy.param.x_range,
96
+ rangexy.param.y_range,
97
+ watch=True,
98
+ )
99
+ update_viewed()
100
+
101
+ update_selected = pn.bind(
102
+ self._update_data_selected, tap.param.x, tap.param.y, watch=True
103
+ )
104
+ update_selected()
105
+
106
+ def _update_data_in_view(self, x_range, y_range):
107
+ if not x_range or not y_range:
108
+ self.data_in_view = self.data.head(0)
109
+ return
110
+
111
+ data = self.data
112
+ data = data[
113
+ (data.centre_easting.between(*x_range))
114
+ & (data.centre_northing.between(*y_range))
115
+ ]
116
+ self.data_in_view = data.head(10).reset_index(drop=True)
117
+
118
+ def _update_data_selected(self, tap_x, tap_y):
119
+ self.data_selected = get_closest_rows(self.data, tap_x, tap_y)
120
 
 
121
 
122
+ class ImageInput(pn.viewable.Viewer):
123
+ data = param.DataFrame(allow_refs=True, allow_None=False)
124
 
125
+ updating = param.Boolean()
126
+ image = param.Parameter()
127
+ plot = param.Parameter()
128
 
129
+ _timestamp = param.Selector(label="Timestamp", objects=[None])
 
 
 
 
 
 
 
130
 
131
+ def __panel__(self):
132
+ return pn.Column(
133
+ pn.widgets.RadioButtonGroup.from_param(
134
+ self.param._timestamp, button_style="outline"
135
+ ),
136
+ pn.Tabs(
137
+ pn.pane.HoloViews(
138
+ hv.DynamicMap(pn.bind(lambda plot: plot, self.param.plot)),
139
+ loading=self.param.updating,
140
+ height=800,
141
+ width=800,
142
+ name="Interactive Image",
143
+ ),
144
+ pn.pane.Image(
145
+ self.param.image,
146
+ name="Static Image",
147
+ loading=self.param.updating,
148
+ width=800,
149
+ ),
150
+ dynamic=True,
151
+ ),
152
+ )
153
 
154
+ @pn.depends("data", watch=True, on_init=True)
155
+ def _update_timestamp(self):
156
+ if self.data.empty:
157
+ default_value = None
158
+ options = [None]
159
+ print("empty options")
160
+ else:
161
+ options = sorted(self.data["timestamp"].unique())
162
+ default_value = options[0]
163
+ print("options", options)
164
 
165
+ self.param._timestamp.objects = options
166
+ if not self._timestamp in options:
167
+ self._timestamp = default_value
168
 
169
+ @pn.depends("_timestamp", watch=True, on_init=True)
170
+ def tap_image(self):
171
+ if self.data.empty or not self._timestamp:
172
+ self.image = hv.RGB(np.array([]))
173
+ else:
174
+ with self.param.update(updating=True):
175
+ row = self.data[self.data.timestamp == self._timestamp].iloc[0]
176
+ self.image = image = pn.cache(get_image)(row)
177
+ image_array = np.array(image)
178
+ self.plot = hv.RGB(image_array).opts(xaxis=None, yaxis=None)
179
 
 
 
 
 
 
180
 
181
+ pn.extension("tabulator", design="fast")
 
182
 
183
+ dataset = DatasetInput()
184
+ app = MapInput(data=dataset.param.data)
185
+ image_input = ImageInput(data=app.param.data_selected)
186
 
 
 
 
187
 
188
+ main = pn.Column(
189
+ pn.FlexBox(
190
+ pn.Column(dataset, app),
191
+ image_input,
192
+ ),
193
+ )
194
+ sidebar = pn.Column(
195
+ pn.pane.Image(
196
+ MAJOR_TOM_LOGO, link_url=MAJOR_TOM_REF_URL, sizing_mode="stretch_width"
197
+ ),
198
+ pn.pane.Image(
199
+ MAJOR_TOM_PICTURE, link_url=MAJOR_TOM_REF_URL, sizing_mode="stretch_width"
200
+ ),
201
+ DESCRIPTION,
202
+ pn.pane.Image(PANEL_LOGO, link_url=PANEL_URL, width=200, margin=(10, 20)),
203
+ pn.pane.Image(DATASHADER_LOGO, link_url=DATASHADER_URL, width=200, margin=(10, 20)),
204
+ )
205
 
206
+ pn.template.FastListTemplate(
207
+ title="Major TOM Explorer",
208
+ main=[main],
209
+ sidebar=[sidebar],
210
+ main_layout=None,
211
+ accent="#003247", # "#A01346"
212
+ ).servable()
download_datasets.py ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ from utils import DATASETS, get_meta_data
2
+
3
+ for dataset in DATASETS:
4
+ print(f"downloading {dataset}")
5
+ get_meta_data(dataset=dataset)
6
+
7
+ print("finished downloading datasets")
requirements.txt CHANGED
@@ -1,8 +1,10 @@
 
1
  dask[dataframe]
2
  datashader
3
  fsspec
4
  holoviews
5
  hvplot
6
  pandas
7
- pyarrow
8
  panel
 
 
 
1
+ aiohttp
2
  dask[dataframe]
3
  datashader
4
  fsspec
5
  holoviews
6
  hvplot
7
  pandas
 
8
  panel
9
+ pyarrow
10
+ requests
requirements_dev.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ -r requirements.txt
2
+
3
+ black
4
+ isort
5
+ mypy
6
+ pylint
7
+ watchfiles
utils.py CHANGED
@@ -1,46 +1,106 @@
1
- import pandas as pd
2
  from pathlib import Path
3
- from fsspec.parquet import open_parquet_file
4
- import pyarrow.parquet as pq
 
5
  import pandas as pd
6
- from io import BytesIO
 
 
7
  from PIL import Image
8
- import holoviews as hv
9
 
 
 
 
 
 
 
 
 
 
10
  REPOSITORY = "Major-TOM"
11
- DATASETS = ['Core-S2L2A', 'Core-S2L1C']
 
 
 
 
 
 
 
 
 
 
 
12
 
13
- DATA_PATH = Path(__file__).parent/"data"
14
 
 
15
 
16
- def _meta_data_url(dataset='Core-S2L2A', repository=REPOSITORY):
17
- return f'https://huggingface.co/datasets/{repository}/{dataset}/resolve/main/metadata.parquet'
18
 
19
- def _meta_data_path(dataset='Core-S2L2A', repository=REPOSITORY):
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  DATA_PATH.mkdir(parents=True, exist_ok=True)
21
- return DATA_PATH/f"{dataset}_metadata.parquet"
 
22
 
23
- def get_meta_data(dataset='Core-S2L2A', repository=REPOSITORY):
24
  path = _meta_data_path(dataset=dataset)
25
  if not path.exists():
26
  data = pd.read_parquet(_meta_data_url(dataset=dataset))
27
  data.to_parquet(path)
28
  data = pd.read_parquet(path)
29
-
30
- data["centre_easting"], data["centre_northing"] = hv.util.transform.lon_lat_to_easting_northing(data["centre_lon"], data["centre_lat"])
 
 
 
 
31
 
32
  return data
33
 
 
34
  def get_image(row):
35
  parquet_url = row["parquet_url"]
36
  parquet_row = row["parquet_row"]
37
  print(parquet_url)
38
  print(parquet_row)
39
- with open_parquet_file(parquet_url,columns = ["thumbnail"]) as f:
40
  with pq.ParquetFile(f) as pf:
41
- first_row_group = pf.read_row_group(parquet_row, columns=['thumbnail'])
42
 
43
- stream = BytesIO(first_row_group['thumbnail'][0].as_py())
44
  image = Image.open(stream)
45
  return image
46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from io import BytesIO
2
  from pathlib import Path
3
+
4
+ import holoviews as hv
5
+ import numpy as np
6
  import pandas as pd
7
+ import pyarrow.parquet as pq
8
+ from fsspec.parquet import open_parquet_file
9
+ from holoviews import opts
10
  from PIL import Image
 
11
 
12
+ MAJOR_TOM_LOGO = "https://cdn-uploads.huggingface.co/production/uploads/6304c06eeb6d777a838eab63/BJKsLwX0GG4W3-gdf40TJ.png"
13
+ MAJOR_TOM_PICTURE = (
14
+ "https://upload.wikimedia.org/wikipedia/en/6/6d/Major_tom_space_oddity_video.JPG"
15
+ )
16
+ MAJOR_TOM_REF_URL = "https://huggingface.co/Major-TOM"
17
+ PANEL_LOGO = "https://panel.holoviz.org/_static/logo_horizontal_light_theme.png"
18
+ PANEL_URL = "https://panel.holoviz.org"
19
+ DATASHADER_LOGO = "https://datashader.org/_static/logo_horizontal.svg"
20
+ DATASHADER_URL = "https://datashader.org/"
21
  REPOSITORY = "Major-TOM"
22
+ DATASETS = ["Core-S2L2A", "Core-S2L1C"]
23
+
24
+ DATA_PATH = Path(__file__).parent / "data"
25
+
26
+ DESCRIPTION = f"""\
27
+ ## Dataset Explorer
28
+
29
+ This app provides a way of exploring samples present in the [MajorTOM-Core]({MAJOR_TOM_REF_URL}) dataset. It contains nearly every piece of Earth captured by ESA [Sentinel-2](https://sentinels.copernicus.eu/web/sentinel/missions/sentinel-2) satellite.
30
+
31
+ ## Instructions
32
+
33
+ To find a sample, navigate on the map to a place of interest. Click the map to find a dataset sample at the location you clicked.
34
 
35
+ ## Powered by
36
 
37
+ """
38
 
39
+ hv.extension("bokeh")
 
40
 
41
+ opts.defaults(
42
+ # opts.Curve(xaxis=None, yaxis=None, show_grid=False, show_frame=False,
43
+ # color='orangered', framewise=True, width=100),
44
+ opts.HLine(color="gray", line_width=1),
45
+ # opts.Layout(shared_axes=False),
46
+ opts.VLine(color="gray", line_width=1),
47
+ )
48
+
49
+
50
+ def _meta_data_url(dataset="Core-S2L2A", repository=REPOSITORY):
51
+ return f"https://huggingface.co/datasets/{repository}/{dataset}/resolve/main/metadata.parquet"
52
+
53
+
54
+ def _meta_data_path(dataset="Core-S2L2A", repository=REPOSITORY):
55
  DATA_PATH.mkdir(parents=True, exist_ok=True)
56
+ return DATA_PATH / f"{dataset}_metadata.parquet"
57
+
58
 
59
+ def get_meta_data(dataset="Core-S2L2A", repository=REPOSITORY):
60
  path = _meta_data_path(dataset=dataset)
61
  if not path.exists():
62
  data = pd.read_parquet(_meta_data_url(dataset=dataset))
63
  data.to_parquet(path)
64
  data = pd.read_parquet(path)
65
+
66
+ data["centre_easting"], data["centre_northing"] = (
67
+ hv.util.transform.lon_lat_to_easting_northing(
68
+ data["centre_lon"], data["centre_lat"]
69
+ )
70
+ )
71
 
72
  return data
73
 
74
+
75
  def get_image(row):
76
  parquet_url = row["parquet_url"]
77
  parquet_row = row["parquet_row"]
78
  print(parquet_url)
79
  print(parquet_row)
80
+ with open_parquet_file(parquet_url, columns=["thumbnail"]) as f:
81
  with pq.ParquetFile(f) as pf:
82
+ first_row_group = pf.read_row_group(parquet_row, columns=["thumbnail"])
83
 
84
+ stream = BytesIO(first_row_group["thumbnail"][0].as_py())
85
  image = Image.open(stream)
86
  return image
87
 
88
+
89
+ def euclidean_distance(x, y, target_x, target_y):
90
+ return np.sqrt((x - target_x) ** 2 + (y - target_y) ** 2)
91
+
92
+
93
+ def get_closest_row(df, target_easting, target_northing):
94
+ distance = euclidean_distance(
95
+ df["centre_easting"], df["centre_northing"], target_easting, target_northing
96
+ )
97
+ closest_row = df.loc[distance.idxmin()]
98
+ return closest_row
99
+
100
+
101
+ def get_closest_rows(df, target_easting, target_northing):
102
+ distance = euclidean_distance(
103
+ df["centre_easting"], df["centre_northing"], target_easting, target_northing
104
+ )
105
+ closest_rows = df[distance == distance.min()]
106
+ return closest_rows