GeoPandas – Fixing Warnings by Correctly Using EPSG Codes

coordinate systemepsggeopandaspython

Recently I've found out that GeoPandas has a world map available:

import geopandas as gpd
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))

Looks beautiful:

world.plot()

enter image description here

world.head(5)

enter image description here

If I try calculating the area of each country by naively doing:

world.area

I get this warning:

/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:1:
UserWarning: Geometry is in a geographic CRS. Results from 'area' are
likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to
a projected CRS before this operation.

Ok, let's use the to_crs() function. To find out what EPSG code I need to use, I do:

world.crs.to_epsg()

4326 is returned.

If I do, then:

world=world.assign(calculated_area1 = world.to_crs(4326).area)

I still get the warning:

/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:1:
UserWarning: Geometry is in a geographic CRS. Results from 'area' are
likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to
a projected CRS before this operation.

However, if I deliberately use the wrong EPSG code 4328, I don't get a warning.

world=world.assign(calculated_area2 = world.to_crs(4328).area)

The result is:

world[['name','calculated_area1','calculated_area2']]

enter image description here

Ie none of the columns contain the area of countries in square meters or kilometers.

Why do I get a warning when using the right EPSG code while not getting warnings when using a wrong one?

If I understand this, I hope to be able to calculate the area of countries.

Best Answer

You get that warning because EPSG:4326 (WGS84) is geographic CRS and its unit is degree. The key message here is "Geometry is in a geographic CRS". You don't get a warning when using EPSG:4328 because it is not geographic, but it is geocentric and its unit is meter.

When you use area, length or distance, GeoPandas checks CRS before calculation and warns if the planar operation is done in a geographic CRS.

Why checking?
One of the packages that Geopandas depends on is Shapely, where geometric calculations are made. Shapely uses 2D Cartesian coordinates (x, y) and calculations are performed in 2D plane. So Shapely doesn't care if the coordinates are in degree or meter. They are just numbers to Shapely. Therefore, in the table, the unit of calculated_area1 is square degree (no meaning), and the other is square meter.