File size: 1,991 Bytes
a6ec964
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import requests
import geopandas as gpd

def has_associated_features(iso_code, url="https://gis.unhcr.org/arcgis/rest/services/core_v2/wrl_polbnd_adm1_a_unhcr/MapServer/0/query"):
    params = {
        "where": f"iso3 = '{iso_code}'",
        "outFields": "*",
        "outSR": "4326",
        "f": "json"
    }
    try:
        response = requests.get(url, params=params, timeout=10)
        if response.status_code == 200 and response.text.strip():
            data = response.json()
            if 'features' in data:
                return len(data['features']) > 0, data
        return False, None
    except Exception as e:
        return False, None


def fetch_gis_subdivisions(iso_code, url="https://gis.unhcr.org/arcgis/rest/services/core_v2/wrl_polbnd_adm1_a_unhcr/MapServer/0/query"):
    params = {
        "where": f"iso3 = '{iso_code}'",
        "outFields": "iso3,gis_name",
        "returnGeometry": "false",
        "f": "json"
    }
    response = requests.get(url, params=params)
    if response.status_code == 200:
        data = response.json()
        if "features" in data:
            return [f["attributes"]["gis_name"] for f in data["features"]]
    return []


def get_geometry(iso_code, gis_name=None, url="https://gis.unhcr.org/arcgis/rest/services/core_v2/wrl_polbnd_adm1_a_unhcr/MapServer/0/query"):
    if gis_name:
        params = {
            "where": f"iso3 = '{iso_code}' AND gis_name = '{gis_name}'",
            "outFields": "*",
            "outSR": "4326",
            "f": "geojson"
        }
    else:
        params = {
            "where": f"iso3 = '{iso_code}'",
            "outFields": "*",
            "outSR": "4326",
            "f": "geojson"
        }
    response = requests.get(url, params=params)
    if response.status_code == 200:
        geojson_data = response.json()
        if "features" in geojson_data and geojson_data["features"]:
            return gpd.GeoDataFrame.from_features(geojson_data["features"])
    return None