# pip/conda install geopandas

import geopandas as gpd
import matplotlib.pyplot as plt




# Nachbar von Land X suchen
def find_neighbouring_countries(df, country_name):
    target = df[df["NAME"] == country_name]

    if len(target) == 0:
        print(f"{country_name} not found")
        return []

    # sjoin
    neighbours = []
    for idx, country in df.iterrows():
        if country["NAME"] == country_name:
            continue
        if target.geometry.iloc[0].touches(country.geometry):
            neighbours.append(country["NAME"])

    return neighbours

if __name__ == "__main__":
    world = gpd.read_file("../../data/110m_cultural.zip", layer="ne_110m_admin_0_countries")

    print(list(world.columns))

    print(world.iloc[0, -1])
    print(world.iloc[0]["NAME"])

    print()
    print(world.crs)

    # EPSG:4326  (latitude/longitude)

    # epsg:3587  Mercator

    w2 = world.to_crs("epsg:3587")
    print(w2.iloc[0, -1])

    print(world[["NAME", "CONTINENT", "POP_EST", "GDP_MD", "geometry"]].head())

    # wie groß ist jedes Land?
    world_proj_cylinder = world.to_crs("epsg:3587")  # zylinder
    world_proj_cylinder["area_sq_km"] = world_proj_cylinder.geometry.area / 1_000_000
    print(world_proj_cylinder[["NAME", "area_sq_km"]].head(10))

    cities = gpd.read_file("../../data/110m_cultural.zip",
                           layer="ne_110m_populated_places")

    print(cities[["NAME", "geometry"]].head())
    print(len(cities))

    fig, ax = plt.subplots()
    world.plot(column="CONTINENT", legend=True, ax=ax)
    cities.plot(ax=ax, color="red")
    plt.show()
    res = find_neighbouring_countries(world, "Germany")
    print(res)