|
""" |
|
Custom HuggingFace dataset loading script for GeoParquet files. |
|
|
|
References: |
|
- https://huggingface.co/docs/datasets/v2.15.0/en/dataset_script |
|
- https://github.com/huggingface/datasets/blob/2.15.0/templates/new_dataset_script.py |
|
- https://huggingface.co/docs/datasets/v2.15.0/en/package_reference/builder_classes |
|
- https://huggingface.co/docs/datasets/v2.15.0/en/about_dataset_load |
|
- https://discuss.huggingface.co/t/how-to-tweak-a-dataset-without-a-loading-script/43533/5 |
|
""" |
|
import datasets |
|
import pyarrow as pa |
|
import pyarrow.parquet as pq |
|
|
|
|
|
_URLS = {"32VLM": " 32VLM_v01.gpq"} |
|
_MGRS_TILES = ["32VLM"] |
|
|
|
|
|
class ClayVectorEmbeddings(datasets.ArrowBasedBuilder): |
|
"""Clay Vector Embeddings in GeoParquet format.""" |
|
|
|
|
|
|
|
BUILDER_CONFIGS = [ |
|
datasets.BuilderConfig( |
|
name=name, |
|
version=datasets.Version(version="0.0.1"), |
|
description=f"Clay vector embeddings for MGRS tile {name}", |
|
) |
|
for name in _MGRS_TILES |
|
] |
|
|
|
|
|
|
|
def _info(self): |
|
return datasets.DatasetInfo( |
|
|
|
description="Clay Vector Embeddings in GeoParquet format.", |
|
|
|
features=datasets.Features( |
|
{ |
|
"source_url": datasets.Value(dtype="string"), |
|
"date": datasets.Value(dtype="date32"), |
|
"embeddings": datasets.Value("string"), |
|
"geometry": datasets.Value("binary"), |
|
|
|
} |
|
), |
|
) |
|
|
|
def _split_generators(self, dl_manager: datasets.download.DownloadManager): |
|
files = _URLS[self.config.name] |
|
downloaded_files = dl_manager.download(files) |
|
return [ |
|
datasets.SplitGenerator( |
|
name=datasets.Split.ALL, |
|
|
|
gen_kwargs={"filepaths": downloaded_files}, |
|
) |
|
] |
|
|
|
|
|
def _generate_tables(self, filepaths: list[str] = ["32VLM_v01.gpq"]): |
|
for file_idx, filepath in enumerate(filepaths): |
|
with open(filepath, mode="rb") as f: |
|
parquet_file = pq.ParquetFile(source=filepath) |
|
for batch_idx, record_batch in enumerate(parquet_file.iter_batches()): |
|
pa_table = pa.Table.from_batches([record_batch]) |
|
yield f"{file_idx_batch_idx}", pa_table |
|
|