|
|
""" |
|
|
Fetch ISO country code mappings from GeoNames. |
|
|
|
|
|
This script fetches comprehensive country data from GeoNames countryInfo.txt |
|
|
and saves it as a CSV file for use in data preprocessing pipelines. |
|
|
""" |
|
|
|
|
|
import io |
|
|
from pathlib import Path |
|
|
|
|
|
import httpx |
|
|
import pandas as pd |
|
|
|
|
|
|
|
|
def fetch_country_mappings(save_raw=True): |
|
|
""" |
|
|
Fetch country code mappings from GeoNames. |
|
|
|
|
|
Args: |
|
|
save_raw: Whether to save raw data file to data/input |
|
|
|
|
|
Returns: |
|
|
pd.DataFrame: DataFrame with country information from GeoNames |
|
|
""" |
|
|
|
|
|
geonames_url = "https://download.geonames.org/export/dump/countryInfo.txt" |
|
|
|
|
|
with httpx.Client() as client: |
|
|
response = client.get(geonames_url) |
|
|
response.raise_for_status() |
|
|
content = response.text |
|
|
|
|
|
|
|
|
if save_raw: |
|
|
input_dir = Path("../data/input") |
|
|
input_dir.mkdir(parents=True, exist_ok=True) |
|
|
|
|
|
raw_path = input_dir / "geonames_countryInfo.txt" |
|
|
with open(raw_path, "w", encoding="utf-8") as f: |
|
|
f.write(content) |
|
|
|
|
|
|
|
|
lines = content.split("\n") |
|
|
header_line = [line for line in lines if line.startswith("#")][-1] |
|
|
column_names = header_line[1:].split("\t") |
|
|
|
|
|
|
|
|
|
|
|
df = pd.read_csv( |
|
|
io.StringIO(content), |
|
|
sep="\t", |
|
|
comment="#", |
|
|
header=None, |
|
|
keep_default_na=False, |
|
|
na_values=[""], |
|
|
names=column_names, |
|
|
) |
|
|
|
|
|
|
|
|
df = df.rename( |
|
|
columns={"ISO": "iso_alpha_2", "ISO3": "iso_alpha_3", "Country": "country_name"} |
|
|
) |
|
|
|
|
|
return df |
|
|
|
|
|
|
|
|
def create_country_dataframe(geonames_df): |
|
|
""" |
|
|
Create a cleaned DataFrame with country codes and names. |
|
|
|
|
|
Args: |
|
|
geonames_df: DataFrame from GeoNames with all country information |
|
|
|
|
|
Returns: |
|
|
pd.DataFrame: DataFrame with columns [iso_alpha_2, iso_alpha_3, country_name] |
|
|
""" |
|
|
|
|
|
df = geonames_df[["iso_alpha_2", "iso_alpha_3", "country_name"]].copy() |
|
|
|
|
|
|
|
|
df = df.sort_values("country_name").reset_index(drop=True) |
|
|
|
|
|
return df |
|
|
|
|
|
|
|
|
def save_country_codes(output_path="../data/intermediate/iso_country_codes.csv"): |
|
|
""" |
|
|
Fetch country codes from GeoNames and save to CSV. |
|
|
|
|
|
Args: |
|
|
output_path: Path to save the CSV file |
|
|
""" |
|
|
|
|
|
geonames_df = fetch_country_mappings() |
|
|
|
|
|
|
|
|
df = create_country_dataframe(geonames_df) |
|
|
|
|
|
|
|
|
output_file = Path(output_path) |
|
|
output_file.parent.mkdir(parents=True, exist_ok=True) |
|
|
|
|
|
|
|
|
df.to_csv(output_file, index=False) |
|
|
|
|
|
return df |
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
|
|
|
df = save_country_codes() |
|
|
|