Search code examples
pythongeopandas

define correct values for clip in geopandas


let us suppose we have following code :

import pandas as pd
import geopandas
import matplotlib.pyplot as plt
from geodatasets import get_path
df = pd.DataFrame(
    {
        "City": ["Tbilisi", "Batumi", "Kutaisi", "Rustavi"],
        "Country": ["Georgia", "Georgia", "Georgia", "Georgia"],
        "Latitude": [41.7225, 41.6458, 42.2500, 41.5472],
        "Longitude": [44.7925, 41.6417,42.7000, 45.0111]
    }
)
gdf = geopandas.GeoDataFrame(
    df, geometry=geopandas.points_from_xy(df.Longitude, df.Latitude), crs="EPSG:4326"
)
world = geopandas.read_file(get_path("naturalearth.land"))
# print(world.)
# We restrict to Georgia
ax = world.clip([-90, -55, -25, 15]).plot(color="white", edgecolor="black")
# We can now plot our ``GeoDataFrame``.
gdf.plot(ax=ax, color="red")

plt.show()

i have taken exact coordinates from given website :

https://simplemaps.com/data/ge-cities

but result what i am getting is this : enter image description here

it is because we have following clip :

ax = world.clip([-90, -55, -25, 15]).plot(color="white", edgecolor="black")

based on figure got, could you please tell me what values for clip i should take? i tried following [0,40,20,40], but it returns error of empty rectangle or something like this, please give me correct direction


Solution

  • Your clip coordinates for the world land map are completely different from the coordinates from the cities...

    I guessed some coordinates based on the cities and then the result looks better...

    Changed script:

    import pandas as pd
    import geopandas
    import matplotlib.pyplot as plt
    from geodatasets import get_path
    
    df = pd.DataFrame(
        {
            "City": ["Tbilisi", "Batumi", "Kutaisi", "Rustavi"],
            "Country": ["Georgia", "Georgia", "Georgia", "Georgia"],
            "Latitude": [41.7225, 41.6458, 42.2500, 41.5472],
            "Longitude": [44.7925, 41.6417, 42.7000, 45.0111],
        }
    )
    gdf = geopandas.GeoDataFrame(
        df, geometry=geopandas.points_from_xy(df.Longitude, df.Latitude), crs="EPSG:4326"
    )
    
    world = geopandas.read_file(get_path("naturalearth.land"))
    # print(world.)
    # We restrict to Georgia
    ax = world.clip([40, 40, 50, 45.0]).plot(color="white", edgecolor="black")
    # We can now plot our ``GeoDataFrame``.
    gdf.plot(ax=ax, color="red")
    
    plt.show()
    

    Result:

    enter image description here