How to Load Administrative Boundaries Data

This guide explains how to load administrative boundaries data (Counties, Constituencies, and Wards) into your system using the Django manage.py shell. The necessary geojson data and the scripts to save them are already provided in the stations/scripts directory.

Important

This guide assumes you have already set up your django project and run migrations and you are already inside the src/ folder.

Steps

  1. Open the Django Shell

  • Run the following command in your terminal to open the Django shell:

     python manage.py shell
    
  1. Load Counties Data

    Execute the following commands in the shell:

    import os
    from stations.scripts.save_counties_from_geojson import save_counties_from_geojson_file
    
    src_dir = os.getcwd()
    geojson_file = os.path.join(src_dir, "stations/scripts", "counties.geojson")
    save_counties_from_geojson_file(geojson_file)
    
  2. Load Constituencies Data

    Execute the following commands in the shell:

    import os
    from stations.scripts.save_counties_from_geojson import save_constituencies_from_geojson_file
    
    src_dir = os.getcwd()
    geojson_file = os.path.join(src_dir, "stations/scripts", "constituencies.geojson")
    save_constituencies_from_geojson_file(geojson_file)
    
  3. Load Wards Data

    Execute the following commands in the shell:

    import os
    from stations.scripts.save_counties_from_geojson import save_wards_from_geojson_file
    
    src_dir = os.getcwd()
    geojson_file = os.path.join(src_dir, "stations/scripts", "wards.geojson")
    save_wards_from_geojson_file(geojson_file)
    
    exit()
    

For all the below steps, you can run the scripts in the terminal. If you are using Docker, make sure to run the commands inside the container. You can do this by prefixing the commands with docker compose exec web.

  1. Convert polling station data (optional step)

    Caution

    The json file is in the repo already and this step may be skipped. The only reason to run this script is to regenerate the json file is to update the json dat as the csv files will be regularly updated by the community and they will not necessarily run the scripts to update the json file.

    We first convert the cleaned CSV files to GeoJSON format.

    The script parse_polling_station_data.py is used for this purpose. It reads the CSV files and generates the corresponding GeoJSON file cleaned_polling_station_data.json.

  • Run the following command in your terminal to convert the CSV files to GeoJSON format:

    python stations/scripts/parse_polling_station_data.py
    
  1. Save Polling Center and Polling Station Data

  • If already inside the Django shell, exit and run the following command:

    python stations/scripts/save_polling_stations.py
    
  1. Save Polling Center Pin Locations

    This script saves the polling center pin locations to the database. It reads the cleaned_polling_station_data.json file and saves the lat/lng data to the database.

  • Run the following command in your terminal to save the polling center pin locations:

    python stations/scripts/save_polling_center_pin_locations.py
    
  1. Initial Check for pin location errors

    This script checks for any errors e.g missing lat/lng data, pin outside ward boundaries, missing parent ward boundary etc. and save the errors to the PollingCenter model

  • Run the following command in your terminal to check for pin location errors:

    python stations/scripts/polling_center_pin_errors_parse.py
    
  1. Create Fake polling station and national results

    Since you already created a superuser, you need to first assign a polling center to the user since you did not register the user using the sign up form. This step is necessary so that you can later see election results like an ordinary user in the app instead of manually creating this in the admin panel.

    Caution

    This step is only for development environment and to visualise data. In production, you will need to create a superuser and assign them a polling center using the admin panel and ofcourse, data will come from users who will be submitting results from the polling stations through the app.

  • Run the following command in your terminal to create fake polling station results:

    python results/scripts/load_fake_results.py
    python results/scripts/create_100k_pres_results.py
    

Notes

  • Ensure the GeoJSON files are correctly formatted and contain the necessary data.

  • If any errors occur, verify the file paths and ensure the scripts are implemented correctly.

By following these steps, you will successfully load the administrative boundaries data into your system.