File size: 1,176 Bytes
84e78bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

import requests
import geopandas as gpd

def download_github_geojson(github_user, repository, file_path, token):
    """
    Load GeoJSON data from a GitHub repository.
    
    Args:
        github_user (str): GitHub username.
        repository (str): GitHub repository name.
        file_path (str): Path of the GeoJSON file in the repository.

    Returns:
        pd.DataFrame: The loaded GeoJSON data.
    """
    
    # headers with  personal access token
    headers = {
    "Authorization": f"token {token}"
    }

    # Create a URL to the raw GeoJSON file in the repository
    raw_url = f"https://raw.githubusercontent.com/{github_user}/{repository}/{file_path}"

    print(f"Debug: raw_url = {raw_url}")  # Debugging line

    # Make a GET request to the URL
    response = requests.get(raw_url, headers=headers)

    if response.status_code == 200:
        # Parse the GeoJSON data
        geojson_data = gpd.read_file(response.text)
        print("File loaded succesfully.")
        print(geojson_data.head())
        return geojson_data
    
    else:
        print(f"Failed to retrieve GeoJSON data. Status code: {response.status_code}")
        return None