Create geonames.py
Browse files- geonames.py +114 -0
geonames.py
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
# -*- coding: utf-8 -*-
|
| 3 |
+
import os
|
| 4 |
+
from functools import partial
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
|
| 7 |
+
import datasets
|
| 8 |
+
import pandas as pd
|
| 9 |
+
from datasets import DatasetDict, load_dataset
|
| 10 |
+
|
| 11 |
+
from data.data_loader_base import DataLoaderBase
|
| 12 |
+
|
| 13 |
+
VERSION = datasets.Version("0.0.1")
|
| 14 |
+
|
| 15 |
+
AVAILABLE_DATASETS = {
|
| 16 |
+
'geonames': 'https://download.geonames.org/export/dump/allCountries.zip',
|
| 17 |
+
'alternateNames': 'https://download.geonames.org/export/dump/alternateNames.zip',
|
| 18 |
+
}
|
| 19 |
+
FIELDS = [
|
| 20 |
+
"geonameid", # integer id of record in geonames database
|
| 21 |
+
"name", # name of geographical point (utf8) varchar(200)
|
| 22 |
+
"asciiname", # name of geographical point in plain ascii characters, varchar(200)
|
| 23 |
+
"alternatenames",
|
| 24 |
+
# alternatenames, comma separated, ascii names automatically transliterated, convenience attribute from alternatename table, varchar(10000)
|
| 25 |
+
"latitude", # latitude in decimal degrees (wgs84)
|
| 26 |
+
"longitude", # longitude in decimal degrees (wgs84)
|
| 27 |
+
"feature_class", # see http://www.geonames.org/export/codes.html, char(1)
|
| 28 |
+
"feature_code", # see http://www.geonames.org/export/codes.html, varchar(10)
|
| 29 |
+
"country_code", # ISO-3166 2-letter country code, 2 characters
|
| 30 |
+
"cc2",
|
| 31 |
+
# alternate country codes, comma separated, ISO-3166 2-letter country code, 200 characters
|
| 32 |
+
"admin1_code",
|
| 33 |
+
# fipscode (subject to change to iso code), see exceptions below, see file admin1Codes.txt for display names of this code; varchar(20)
|
| 34 |
+
"admin2_code",
|
| 35 |
+
# code for the second administrative division, a county in the US, see file admin2Codes.txt; varchar(80)
|
| 36 |
+
"admin3_code", # code for third level administrative division, varchar(20)
|
| 37 |
+
"admin4_code", # code for fourth level administrative division, varchar(20)
|
| 38 |
+
"population", # bigint (8 byte int)
|
| 39 |
+
"elevation", # in meters, integer
|
| 40 |
+
"dem",
|
| 41 |
+
# 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.
|
| 42 |
+
"timezone", # the iana timezone id (see file timeZone.txt) varchar(40)
|
| 43 |
+
"modification_date", # date of last modification in yyyy-MM-dd format"
|
| 44 |
+
]
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
class GeonamesDataset(datasets.GeneratorBasedBuilder, DataLoaderBase):
|
| 48 |
+
"""GeonamesDataset dataset."""
|
| 49 |
+
|
| 50 |
+
@staticmethod
|
| 51 |
+
def load(data_name_config: str = "geonames") -> DatasetDict:
|
| 52 |
+
ds = load_dataset(__file__, data_name_config)
|
| 53 |
+
return ds
|
| 54 |
+
|
| 55 |
+
def _info(self):
|
| 56 |
+
return datasets.DatasetInfo(
|
| 57 |
+
description="",
|
| 58 |
+
features=datasets.Features(
|
| 59 |
+
{
|
| 60 |
+
"geonameid": datasets.Value("string"),
|
| 61 |
+
"name": datasets.Value("string"),
|
| 62 |
+
"asciiname": datasets.Value("string"),
|
| 63 |
+
"alternatenames": datasets.Value("string"),
|
| 64 |
+
"latitude": datasets.Value("string"),
|
| 65 |
+
"longitude": datasets.Value("string"),
|
| 66 |
+
"feature_class": datasets.Value("string"),
|
| 67 |
+
"feature_code": datasets.Value("string"),
|
| 68 |
+
"country_code": datasets.Value("string"),
|
| 69 |
+
"cc2": datasets.Value("string"),
|
| 70 |
+
"admin1_code": datasets.Value("string"),
|
| 71 |
+
"admin2_code": datasets.Value("string"),
|
| 72 |
+
"admin3_code": datasets.Value("string"),
|
| 73 |
+
"admin4_code": datasets.Value("string"),
|
| 74 |
+
"population": datasets.Value("string"),
|
| 75 |
+
"elevation": datasets.Value("string"),
|
| 76 |
+
"dem": datasets.Value("string"),
|
| 77 |
+
"timezone": datasets.Value("string"),
|
| 78 |
+
"modification_date": datasets.Value("string"),
|
| 79 |
+
}
|
| 80 |
+
),
|
| 81 |
+
supervised_keys=None,
|
| 82 |
+
homepage="https://download.geonames.org/export/zip/",
|
| 83 |
+
citation="",
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
def _split_generators(self, dl_manager):
|
| 87 |
+
root_paths = {
|
| 88 |
+
"geonames": Path(dl_manager.download_and_extract(AVAILABLE_DATASETS["geonames"]))/'allCountries.txt',
|
| 89 |
+
"alternateNames": Path(dl_manager.download_and_extract(AVAILABLE_DATASETS["alternateNames"]))/'alternateNames.txt',
|
| 90 |
+
}
|
| 91 |
+
return [
|
| 92 |
+
datasets.SplitGenerator(
|
| 93 |
+
name=datasets.Split.TRAIN,
|
| 94 |
+
gen_kwargs={
|
| 95 |
+
"root_path": root_paths, "split": "train"
|
| 96 |
+
},
|
| 97 |
+
),
|
| 98 |
+
]
|
| 99 |
+
|
| 100 |
+
def _generate_examples(self, root_path, split):
|
| 101 |
+
data_file = str(root_path["geonames"])
|
| 102 |
+
data = pd.read_csv(
|
| 103 |
+
data_file, sep="\t", header=None,
|
| 104 |
+
encoding='utf-8', usecols=list(range(len(FIELDS))), names=FIELDS
|
| 105 |
+
)
|
| 106 |
+
# data_file = str(root_path["alternateNames"])
|
| 107 |
+
# alt_data = pd.read_csv(
|
| 108 |
+
# data_file, sep="\t", header=None,
|
| 109 |
+
# encoding='utf-8', names=["id", "geonameid", "altname"], nrows=100
|
| 110 |
+
# )
|
| 111 |
+
# data = data.merge(alt_data, on="geonameid", how="left")
|
| 112 |
+
for idx, sample in data.iterrows():
|
| 113 |
+
yield idx, dict(sample)
|
| 114 |
+
|