How to filter a GeoPandas dataframe based on latitude or longitude ?

Published: May 17, 2023

Updated: May 17, 2023

DMCA.com Protection Status

In this article, we will show you how to filter a GeoPandas dataframe based on latitude or longitude.

Using GeoPandas query()

First, we will create a GeoPandas dataframe. We will use the cities dataset from the GeoPandas library which contains information about cities from around the world.

import geopandas
import pandas as pd

df = pd.DataFrame(
    {
        "City": ["Paris", "Brasilia", "Beijing", "Moscow", "Caracas"],
        "Latitude": [-48.51, -15.78, -39.55, -55.45, 90],
        "Longitude": [-2.20, -47.91, -116.25, -37.36, 180],
    }
)

gdf = geopandas.GeoDataFrame(
    df, geometry=geopandas.points_from_xy(df.Longitude, df.Latitude), crs="EPSG:4326"
)

print(gdf.head())

Next, we can select rows in our dataframe based on their latitude or longitude values using a query. A query allows us to specify conditions for our search. For example, we can select all cities with a latitude greater than 0:

gdf.query('Latitude > 0')

returns

      City  Latitude  Longitude                    geometry
4  Caracas      90.0      180.0  POINT (180.00000 90.00000)

Note that

gdf[ gdf['Latitude'] > 0 ]

also work and returns the same output.

We can also use queries to select cities within a certain range of longitude and latitude values:

gdf.query('-40 < Latitude < 0')

returns

      City  Latitude  Longitude                    geometry
4  Caracas      90.0      180.0  POINT (180.00000 90.00000)

Now, our dataframe only contains cities within the specified range.

An example of how to use geopandas dataframe filtering

import matplotlib.pyplot as plt

world = geopandas.read_file(geopandas.datasets.get_path('naturalearth_lowres'))

world.crs

Output

Geographic 2D CRS: EPSG:4326>
Name: WGS 84
Axis Info [ellipsoidal]:
- Lat[north]: Geodetic latitude (degree)
- Lon[east]: Geodetic longitude (degree)
Area of Use:
- name: World.
- bounds: (-180.0, -90.0, 180.0, 90.0)
Datum: World Geodetic System 1984 ensemble
- Ellipsoid: WGS 84
- Prime Meridian: Greenwich

print(world)

Ouput

         pop_est      continent                      name iso_a3  gdp_md_est  \
0       889953.0        Oceania                      Fiji    FJI        5496   
1     58005463.0         Africa                  Tanzania    TZA       63177   
2       603253.0         Africa                 W. Sahara    ESH         907   
3     37589262.0  North America                    Canada    CAN     1736425   
4    328239523.0  North America  United States of America    USA    21433226   
..           ...            ...                       ...    ...         ...   
172    6944975.0         Europe                    Serbia    SRB       51475   
173     622137.0         Europe                Montenegro    MNE        5542   
174    1794248.0         Europe                    Kosovo    -99        7926   
175    1394973.0  North America       Trinidad and Tobago    TTO       24269   
176   11062113.0         Africa                  S. Sudan    SSD       11998

                                              geometry  
0    MULTIPOLYGON (((180.00000 -16.06713, 180.00000...  
1    POLYGON ((33.90371 -0.95000, 34.07262 -1.05982...  
2    POLYGON ((-8.66559 27.65643, -8.66512 27.58948...  
3    MULTIPOLYGON (((-122.84000 49.00000, -122.9742...  
4    MULTIPOLYGON (((-122.84000 49.00000, -120.0000...  
..                                                 ...  
172  POLYGON ((18.82982 45.90887, 18.82984 45.90888...  
173  POLYGON ((20.07070 42.58863, 19.80161 42.50009...  
174  POLYGON ((20.59025 41.85541, 20.52295 42.21787...  
175  POLYGON ((-61.68000 10.76000, -61.10500 10.890...  
176  POLYGON ((30.83385 3.50917, 29.95350 4.17370, ...

[177 rows x 6 columns]

Plot the data

ax = world.plot()

ax.set_title("WGS84 (lat/lon)");

How to filter a GeoPandas dataframe based on latitude or longitude ?
How to filter a GeoPandas dataframe based on latitude or longitude ?

world['longitude'] = world['geometry'].centroid.x
world['latitude'] = world['geometry'].centroid.y

print( world )

Output

         pop_est      continent                      name iso_a3  gdp_md_est  \
0       889953.0        Oceania                      Fiji    FJI        5496   
1     58005463.0         Africa                  Tanzania    TZA       63177   
2       603253.0         Africa                 W. Sahara    ESH         907   
3     37589262.0  North America                    Canada    CAN     1736425   
4    328239523.0  North America  United States of America    USA    21433226   
..           ...            ...                       ...    ...         ...   
172    6944975.0         Europe                    Serbia    SRB       51475   
173     622137.0         Europe                Montenegro    MNE        5542   
174    1794248.0         Europe                    Kosovo    -99        7926   
175    1394973.0  North America       Trinidad and Tobago    TTO       24269   
176   11062113.0         Africa                  S. Sudan    SSD       11998

                                              geometry     longitude  \
0    MULTIPOLYGON (((20037508.343 -1812498.413, 200...  1.824878e+07   
1    POLYGON ((3774143.866 -105758.362, 3792946.708...  3.869296e+06   
2    POLYGON ((-964649.018 3205725.605, -964597.245... -1.348403e+06   
3    MULTIPOLYGON (((-13674486.249 6274861.394, -13... -1.079779e+07   
4    MULTIPOLYGON (((-13674486.249 6274861.394, -13... -1.329713e+07   
..                                                 ...           ...   
172  POLYGON ((2096126.508 5765757.958, 2096127.988...  2.316658e+06   
173  POLYGON ((2234260.104 5249565.284, 2204305.520...  2.146954e+06   
174  POLYGON ((2292095.761 5139344.949, 2284604.344...  2.326059e+06   
175  POLYGON ((-6866186.192 1204901.071, -6802177.4... -6.827259e+06   
176  POLYGON ((3432408.751 390883.649, 3334408.389 ...  3.361521e+06

         latitude  
0   -1.958098e+06  
1   -7.003071e+05  
2    2.794163e+06  
3    1.044422e+07  
4    6.667416e+06  
..            ...  
172  5.505001e+06  
173  5.280332e+06  
174  5.248463e+06  
175  1.167359e+06  
176  8.153257e+05

[176 rows x 8 columns]

We will now convert geometries to a different coordinate reference system called 3857 (mercator projection).

world = world.to_crs(3857)

ax = world.plot()

How to filter a GeoPandas dataframe based on latitude or longitude ?
How to filter a GeoPandas dataframe based on latitude or longitude ?

In order to improve the Mercator projection map, we should exclude the rows that have a latitude value exceeding 80:

world = world[ world['latitude'] > -80.0 ]

print(world)

then

ax = world.plot()

plt.savefig("mercator.png", bbox_inches='tight', dpi=200)

ax.set_title("Mercator");

will generate the following figure

How to filter a GeoPandas dataframe based on latitude or longitude ?
How to filter a GeoPandas dataframe based on latitude or longitude ?

References

Links Site
query() pandas.pydata.org
centroid() geopandas.org
to_crs() geopandas.org
Image

of