Thai Province Data

repository·master·Indexed 18 days ago

https://github.com/kongvut/thai-province-data

A comprehensive dataset of Thailand's administrative divisions, including Geographies, Provinces, Districts, and Sub-districts. The project provides data via ready-to-use JSON API endpoints on GitHub Raw and exported files in CSV, JSON, SQL, XLSX, and XML formats. It includes a data pipeline for validation and export using Python scripts or Docker Compose, following a strict hierarchical relationship from Geographies down to Sub-districts.

Tokens
5.1K
Snippets
21
Records
26
Agent score
63%

What's inside thai-province-data

  1. Overview of Thai Province Data

    master
    The thai-province-data project provides structured datasets for Thailand's administrative divisions: Provinces, Districts, and Sub-districts. It includes validation scripts and supports multiple export formats including CSV, JSON, SQL, XLSX, and XML. Users can consume the data via direct JSON API calls from GitHub or by downloading exported files.
  2. Understand the Thai Province Data hierarchy

    master

    The data follows a strict hierarchical structure from the largest geographic area down to the smallest administrative division. You can navigate the data by following these relationships:

    1. Geographies: The root level.
    2. Provinces: Linked to Geographies via geography_id.
    3. Districts: Linked to Provinces via province_id.
    4. Sub-districts: Linked to Districts via district_id.

    This One-to-Many (1..*) relationship allows you to drill down from a specific geography to its constituent provinces, then to their districts, and finally to their sub-districts.

  3. Understand the nested SubDistrict structure

    master

    The SubDistrict With District And Province schema defines a bottom-up hierarchical object where a SubDistrict includes its parent district, which in turn includes its parent province.

    {
      "type": "object",
      "allOf": [
        { "$ref": "#/definitions/sub_district" },
        {
          "properties": {
            "district": {
              "allOf": [
                { "$ref": "#/definitions/district" },
                {
                  "properties": {
                    "province": { "$ref": "#/definitions/province" }
                  }
                }
              ]
            }
          }
        }
      ]
    }
  4. Understand the nested Province structure

    master

    The Province With District And SubDistrict schema defines a hierarchical object where a Province contains an array of districts, and each district contains an array of sub_districts.

    {
      "type": "object",
      "allOf": [
        { "$ref": "#/definitions/province" },
        {
          "properties": {
            "districts": {
              "type": "array",
              "items": {
                "allOf": [
                  { "$ref": "#/definitions/district" },
                  {
                    "properties": {
                      "sub_districts": {
                        "type": "array",
                        "items": { "$ref": "#/definitions/sub_district" }
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  5. Understand the data hierarchy and relationships

    master

    The data follows a strict hierarchical relationship (ERD) from largest to smallest administrative unit:

    1. Geographies (Top level)
    2. Provinces (Belong to Geographies)
    3. Districts (Belong to Provinces)
    4. Sub-districts (Belong to Districts)

    Relationship mapping:

    • GEOGRAPHIES 1 -> N PROVINCES
    • PROVINCES 1 -> N DISTRICTS
    • DISTRICTS 1 -> N SUB_DISTRICTS
    erDiagram
      GEOGRAPHIES ||--o{ PROVINCES : "1..*"
      PROVINCES   ||--o{ DISTRICTS : "1..*"
      DISTRICTS   ||--o{ SUB_DISTRICTS : "1..*"
  6. Access data via JSON API

    master

    You can fetch the latest administrative data directly from the GitHub repository using the api/latest/ directory. Available endpoints include:

    • province.json
    • district.json
    • sub_district.json
    • province_with_district_and_sub_district.json
    • sub_district_with_district_and_province.json

    Use the raw GitHub URL to perform curl requests or fetch data in your application.

    curl -s https://raw.githubusercontent.com/kongvut/thai-province-data/refs/heads/master/api/latest/province.json | jq '.[0]'
  7. Use exported data files

    master

    For integration into databases, data pipelines, or spreadsheets, you can use the files located in the formats/ directory. Supported formats include:

    • CSV: Ideal for Excel or Google Sheets.
    • SQL: Includes CREATE TABLE and INSERT statements for database seeding.
    • XLSX: Requires pandas and openpyxl for programmatic processing.
    • XML: For systems requiring XML structures.
  8. Run Data Pipeline and Automation Scripts

    master

    The repository includes several scripts for validating data and exporting it into various formats. These are located in the scripts/ directory.

    Available Scripts:

    • scripts/0_validate_data.py: Validates data against JSON schemas, checks foreign key integrity, and verifies formats.
    • scripts/1_export_file_format.py --overwrite: Exports data into CSV, JSON, SQL, XLSX, or XML formats.
    • scripts/2_export_api.py --overwrite: Builds the JSON files used for the API endpoints.
    • scripts/make.py: Runs the complete pipeline (validation through export).
  9. Run automation using Docker

    master

    If you prefer not to install Python or dependencies locally, use Docker Compose to run the validation or the full pipeline.

    Commands:

    • Build the image: docker compose build
    • Run only validation: docker compose run --rm validate
    • Run the full pipeline (validate → export formats → export API): docker compose run --rm make
    docker compose build
    docker compose run --rm make
  10. Fetch Province Data using Python

    master

    Use the requests library to fetch the province JSON array from the GitHub Raw URL.

    import requests
    
    url = "https://raw.githubusercontent.com/kongvut/thai-province-data/refs/heads/master/api/latest/province.json"
    provinces = requests.get(url).json()
    
    print(provinces[0])
    # Output example: {'id': 1, 'name_th': 'กรุงเทพมหานคร', 'name_en': 'Bangkok', 'geography_id': 2, ...}