| 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 |
|
|