#!/usr/bin/env python3 # -*- coding: utf-8 -*- import os from functools import partial from pathlib import Path import datasets import pandas as pd from datasets import DatasetDict, load_dataset VERSION = datasets.Version("0.0.1") AVAILABLE_DATASETS = { 'geonames': 'https://download.geonames.org/export/dump/allCountries.zip', 'alternateNames': 'https://download.geonames.org/export/dump/alternateNames.zip', } FIELDS = [ "geonameid", # integer id of record in geonames database "name", # name of geographical point (utf8) varchar(200) "asciiname", # name of geographical point in plain ascii characters, varchar(200) "alternatenames", # alternatenames, comma separated, ascii names automatically transliterated, convenience attribute from alternatename table, varchar(10000) "latitude", # latitude in decimal degrees (wgs84) "longitude", # longitude in decimal degrees (wgs84) "feature_class", # see http://www.geonames.org/export/codes.html, char(1) "feature_code", # see http://www.geonames.org/export/codes.html, varchar(10) "country_code", # ISO-3166 2-letter country code, 2 characters "cc2", # alternate country codes, comma separated, ISO-3166 2-letter country code, 200 characters "admin1_code", # fipscode (subject to change to iso code), see exceptions below, see file admin1Codes.txt for display names of this code; varchar(20) "admin2_code", # code for the second administrative division, a county in the US, see file admin2Codes.txt; varchar(80) "admin3_code", # code for third level administrative division, varchar(20) "admin4_code", # code for fourth level administrative division, varchar(20) "population", # bigint (8 byte int) "elevation", # in meters, integer "dem", # digital elevation model, srtm3 or gtopo30, average elevation of 3''x3'' (ca 90mx90m) or 30''x30'' (ca 900mx900m) area in meters, integer. srtm processed by cgiar/ciat. "timezone", # the iana timezone id (see file timeZone.txt) varchar(40) "modification_date", # date of last modification in yyyy-MM-dd format" ] class GeonamesDataset(datasets.GeneratorBasedBuilder): """GeonamesDataset dataset.""" @staticmethod def load(data_name_config: str = "geonames") -> DatasetDict: ds = load_dataset(__file__, data_name_config) return ds def _info(self): return datasets.DatasetInfo( description="", features=datasets.Features( { "geonameid": datasets.Value("string"), "name": datasets.Value("string"), "asciiname": datasets.Value("string"), "alternatenames": datasets.Value("string"), "latitude": datasets.Value("string"), "longitude": datasets.Value("string"), "feature_class": datasets.Value("string"), "feature_code": datasets.Value("string"), "country_code": datasets.Value("string"), "cc2": datasets.Value("string"), "admin1_code": datasets.Value("string"), "admin2_code": datasets.Value("string"), "admin3_code": datasets.Value("string"), "admin4_code": datasets.Value("string"), "population": datasets.Value("string"), "elevation": datasets.Value("string"), "dem": datasets.Value("string"), "timezone": datasets.Value("string"), "modification_date": datasets.Value("string"), } ), supervised_keys=None, homepage="https://download.geonames.org/export/zip/", citation="", ) def _split_generators(self, dl_manager): root_paths = { "geonames": Path(dl_manager.download_and_extract(AVAILABLE_DATASETS["geonames"]))/'allCountries.txt', "alternateNames": Path(dl_manager.download_and_extract(AVAILABLE_DATASETS["alternateNames"]))/'alternateNames.txt', } return [ datasets.SplitGenerator( name=datasets.Split.TRAIN, gen_kwargs={ "root_path": root_paths, "split": "train" }, ), ] def _generate_examples(self, root_path, split): data_file = str(root_path["geonames"]) data = pd.read_csv( data_file, sep="\t", header=None, encoding='utf-8', usecols=list(range(len(FIELDS))), names=FIELDS ) # data_file = str(root_path["alternateNames"]) # alt_data = pd.read_csv( # data_file, sep="\t", header=None, # encoding='utf-8', names=["id", "geonameid", "altname"], nrows=100 # ) # data = data.merge(alt_data, on="geonameid", how="left") for idx, sample in data.iterrows(): yield idx, dict(sample)