-1

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?

carderne
  • 199
  • 4
  • How is a location defined in a unit of distance (km)? – roganjosh Aug 17 '16 at 13:54
  • In a distance based projection the locations are defined as distances from the origin (0 degrees N, 0 degrees E). Much the same as is done with degrees. – carderne Aug 17 '16 at 14:40
  • I completely misread that line. I suppose you have seen [pyproj](https://github.com/jswhit/pyproj) and it didn't have the functionality you need? – roganjosh Aug 17 '16 at 15:04
  • Thanks that looks perfect! Somehow everything that came up when I searched was how to project to display on a plot (e.g. matplotlib) but this looks like it does exactly what I want. If you add is an answer I'll mark it as solved. – carderne Aug 17 '16 at 15:11

2 Answers2

1

There is already a python module that can do these kind of transformations for you called pyproj. I will agree it is actually not the simplest module to find via google. Some examples of its use can be seen here

Community
  • 1
  • 1
roganjosh
  • 12,594
  • 4
  • 29
  • 46
0

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)
carderne
  • 199
  • 4