Skip to content
Advertisement

Python: Convert map in kilometres to degrees

I have a pandas Dataframe with a few million rows, each with an X and Y attribute with their location in kilometres according to the WGS 1984 World Mercator projection (created using ArcGIS).

What is the easiest way to project these points back to degrees, without leaving the Python/pandas environment?

Advertisement

Answer

Many years later, this is how I would do this. Keeping everything in GeoPandas to minimise the possibility of footguns.

Some imports:

import pandas as pd
import geopandas as gpd
from shapely.geometry import Point

Create a dataframe (note the values must be in metres!)

df = pd.DataFrame({"X": [50e3, 900e3], "Y": [20e3, 900e3]})

Create geometries from the X/Y coordinates

df["geometry"] = df.apply(lambda row: Point(row.X, row.Y), axis=1)

Convert to a GeoDataFrame, setting the current CRS. In this case EPSG:3857, the projection from the question.

gdf = gpd.GeoDataFrame(df, crs=3857)

Project it to the standard WGS84 CRS in degrees (EPSG:4326).

gdf = gdf.to_crs(4326)

And then (optionally), extract the X/Y coordinates in degrees back into standard columns:

gdf["X_deg"] = gdf.geometry.apply(lambda p: p.x)
gdf["Y_deg"] = gdf.geometry.apply(lambda p: p.y)
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement