File size: 5,012 Bytes
19ab1ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b5ea5e3
19ab1ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/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)